I am trying to make a banking program that reads commands from a text file and
processes them.
These are the commands:
Create account amount
Deposit account amount
Withdraw account amount
Balance account
In all of the above commands, account is an integer and amount is a double.
I need the program to do all of the following:
1. Valid account numbers are 1-9. This requires the program to have an array of references
to Account objects.
2. If the first word on any line contains a command other than the four listed above, an
error message should be displayed and that line ignored.
3. The create command creates a new account object with the given account number and
initial balance. If an account already exists with that number an error message should be
displayed and the command ignored.
4. The Deposit and Withdraw commands perform the indicated operation on an existing
account. If no account with that number has been created, an error message is
displayed and the command ignored.
5. The Balance command displays the balance of the requested account. No change to the
account occurs. If no account with that number has been created, an error message is
displayed and the command ignored.
I am having trouble figuring out how to create new Account objects and create an array of pointers to the Account objects.
Here is the code I have so far:
.cpp file
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
|
#include "Account.h"
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
using namespace std;
const int bankNum = 100;
struct accountType
{
string command;
int accNum;
double dollars;
};
int getLineNum()
{
int number_of_lines = 0;
string line;
ifstream myfile("bank.txt");
while (getline(myfile, line))
++number_of_lines;
return number_of_lines;
}
void accountStrings(ifstream& infile, accountType accountInfo[], int LineNum)
{
string strLine = "";
int j = 0;
for (j = 0; j < LineNum;)
{
while (getline(infile, strLine))
{
istringstream ss(strLine);
ss >> accountInfo[j].command
>> accountInfo[j].accNum
>> accountInfo[j].dollars;
j++;
}
}
}
void accountAssess(accountType accountInfo[], int LineNum)
{
int j = 0;
string create = "Create";
string deposit = "Deposit";
string withdraw = "Withdraw";
string balance = "Balance";
for (j = 0; j < LineNum; j++)
{
if (accountInfo[j].accNum <= 9 & accountInfo[j].accNum != 0)
{
if (accountInfo[j].command == create)
{
cout << "create" << endl;
}
else if (accountInfo[j].command == deposit)
{
cout << "deposit" << endl;
}
else if (accountInfo[j].command == withdraw)
{
cout << "withdraw" << endl;
}
else if (accountInfo[j].command == balance)
{
cout << "balance" << endl;
}
else
{
cout << "ERROR: Bad Command" << endl;
}
}
else
{
cout << "ERROR: Invalid Account Number" << endl;
}
}
}
int main()
{
string strline;
accountType accountInfo[bankNum];
ifstream inFile("bank.txt");
int LineNum = getLineNum();
int accounts[8];
accountStrings(inFile, accountInfo, LineNum);
accountAssess(accountInfo, LineNum);
return 0;
}
|
Account.h file
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
|
#ifndef ACCOUNT1_DEFINED
#define ACCOUNT1_DEFINED 1
class Account
{
private:
double balance;
int id;
public:
Account()
{
}
Account(int newID, double initialBalance)
{
balance = initialBalance;
id = newID;
}
void setId(int newID)
{
id = newID;
}
int getId()
{
return id;
}
void setBalance(double newBalance)
{
balance = newBalance;
}
double getBalance()
{
return balance;
}
void withdraw(double amount)
{
balance -= amount;
}
void deposit(double amount)
{
balance += amount;
}
};
#endif#pragma once
|
Here are the contents of the txt file:
Create 1 1000.01
Create 2 2000.02
Create 3 3000.03
Deposit 1 11.11
Deposit 2 22.22
Withdraw 4 5000.00
Create 4 4000.04
Withdraw 1 0.10
Balance 2
Withdraw 2 0.20
Deposit 3 33.33
Withdraw 4 0.40
Bad Command 65
Balance 1
Balance 2
Balance 3
Balance 4