Need help please!.. C++ program Design a bank account class named Account that h
ID: 3804077 • Letter: N
Question
Need help please!.. C++ program
Design a bank account class named Account that has the following private member variables:
accountNumber of type int
ownerName of type string
balance of type double
transactionHistory of type pointer to Transaction structure (structure is defined below)
numberTransactions of type int
totalNetDeposits of type static double.The totalNetDeposits is the cumulative sum of all deposits (at account creation and at deposits) minus the cumulative sum of all withdrawals.
numberAccounts of type static int
and the following public member functions:
Account(number, name, amount, month, day, year) is the constructor that initializes the account’s member variables with number, name and amount. amount is the amount deposited when the account is created. The “account creation” transaction is recorded in the transaction history by calling recordTransaction. numberTransactions is set to 1, and amount is added to totalNetDeposits. numberAccounts is incremented.
withdraw(amount, month, day, year) function to withdraw a specified amount from the account. The function should first check if there is sufficient balance in the account. If the balance is sufficient, withdrawal is processed. Otherwise the withdrawal is not made. If the withdrawal is made, the withdrawal amount is deducted from balance and from totalNetDeposits. The transaction is recorded in the transaction history. numberTransactions is incremented.
deposit(amount, month, day, year) function to deposit a specified amount of money to the account. The function should first check if the deposit amount is positive. If it is positive, deposit is processed. Otherwise the deposit is not made. If the deposit is made, the amount is added to balance and to totalNetDeposits, and the transaction is recorded in the transaction history. numberTransactions is incremented.
getAccountNumber(): An accessor function that returns the account number. This function is later called by the displayAccountInfo() function.
getName(): An accessor function that returns the name on the account.
getBalance(): An accessor function that returns the account balance. This function is later called by the displayAccountInfo() function.
printTransactions: prints the history of transactions: date (month, year, day), amount and type for each transaction
getTotalNetDeposit(): A static member function that returns totalNetDeposits.
Demonstrate the class in a program.
2. Transaction structure
Transaction structure has the following members:
transactionMonth of type Month
transactionDay of type int
transactionYear of type int
transactionAmount of type double
transactionType of type Operation
Operation is of type enum, and the possible values are CREATION, DEPOSIT, WITHDRAWAL.
3. Record a Transaction in history
To record a transaction, you should implement recordTransaction, a private member function, which takes as argument a Transaction to be recorded.
The transaction history is implemented as an array of Transaction structures. Initially, the history is empty and no array is allocated. The first transaction is recorded in the history (account creation is the first transaction) by dynamically allocating an array of size 1, and populating with the transaction to be recorded.
/* This function takes as argument a Transaction structure and adds it to the transaction history. The transaction history
is implemented as an array of Transaction structures. A new array is dynamically allocated, of size equal to (size of old array)+1, to hold the added transaction. The values of the old array are copied into the new array,and the transaction to be added is copied into the last element of the new array.The old array is released through delete. The address returned from dynamic array allocation is assigned to transactionHistory.
*/
void Account::recordTransaction(Transaction transact)
{
// Function body
}
4. Additional Requirements
a) What to turn in
Your code should be structured into the following files:
Account.h which contains the class definition and the function prototypes of all the member functions
Account.cpp which contains the function description of all the member functions
Main.cpp file which contains the main function.
b) Outline of main
Define a global array of pointers to Account, of size NUM_ACCOUNT (set NUM_ACCOUNT to 5).
Loop on displaying the following menu of choices:
1. Create account (user will be prompted for account number, name, amount, month, day and year)
2. Deposit to account (user will be prompted for account number, amount, month, day and year)
3. Withdraw from account (user will be prompted for account number, amount, month, day and year)
4. Print transaction history (user will be prompted for account number)
5. Display information for all accounts (including the transaction history)
6. Display totalNetDeposits
7. Exit the program.
Explanation / Answer
Here is the code for the question. Sample output is also attached. Please dont forget to rate the answer if it helped. Thank you very much.
Account.h
#include <iostream>
using namespace std;
//enum for months starting from 1
enum Month
{
JANUARY=1,FEBRUARY,MARCH,APRIL,MAY,JUNE,JULY,AUGUST,SEPTEMBER,OCTOBER,NOVEMBER,DECEMBER
};
enum Operation
{
CREATION,DEPOSIT,WITHDRAWAL
};
struct Transaction
{
Month transactionMonth;
int transactionDay;
int transactionYear;
double transactionAmount;
Operation transactionType;
Transaction(){}
Transaction(Operation type,Month month,int day,int year,double amount)
{
transactionMonth=month;
transactionDay=day;
transactionYear=year;
transactionType=type;
transactionAmount=amount;
}
};
class Account
{
private:
int accountNumber;
string ownerName;
double balance;
Transaction *transactionHistory;
int numberTransactions;
static double totalNetDeposits;
static int numberAccounts;
void recordTransaction(Operation op,Month m,int d,int y,double amount);//records a transaction
public:
//constructor
Account(int number,string name,double amount,Month month,int day, int year);
bool withdraw(double amount,Month month,int day,int year); //function for withdrawal
bool deposit(double amount,Month month ,int day, int year);//function to deposit
int getAccountNumber();//return the accoutn number
string getName();//return the ownerName
double getBalance();//return the balance amount
void printTransactions();//prints transaction history
void display(); //display full details of account including trasactoin history
static double getTotalNetDeposits();//return the totalNetDeposit value
};
Account.cpp
#include "Account.h"
double Account::totalNetDeposits=0;
int Account::numberAccounts=0;
string MonthName(int num)
{
switch(num)
{
case JANUARY: return "Jan";
break;
case FEBRUARY: return "Feb";
break;
case MARCH: return "Mar";
break;
case APRIL: return "Apr";
break;
case MAY: return "May";
break;
case JUNE: return "Jun";
break;
case JULY: return "Jul";
break;
case AUGUST: return "Aug";
break;
case SEPTEMBER: return "Sep";
break;
case OCTOBER: return "Oct";
break;
case NOVEMBER: return "Nov";
break;
case DECEMBER: return "Dec";
break;
default:
return "";
}
}
string OperationName(int num)
{
switch(num)
{
case CREATION:return "Creation";
case DEPOSIT: return "Deposit";
case WITHDRAWAL: return "Withdraw";
default: return "";
}
}
void Account::recordTransaction(Operation type,Month month,int day,int year,double amount)//records a transaction
{
Transaction t(type,month,day,year,amount);
Transaction *temp=new Transaction[numberTransactions+1]; //allocate a bigger array
for(int i=0;i<numberTransactions;i++)
temp[i]=transactionHistory[i];
temp[numberTransactions]=t; //store in last location
numberTransactions++; //increment number of transaction
delete []transactionHistory; //deallocate old array
transactionHistory=temp; //save the newly allocated
}
//constructor
Account::Account(int number,string name,double amount,Month month,int day, int year)
{
accountNumber=number;
ownerName=name;
balance=amount;
numberTransactions=0;
recordTransaction(CREATION,month,day,year,amount);
totalNetDeposits+=amount;
numberAccounts++;
}
bool Account::withdraw(double amount,Month month,int day,int year) //function for withdrawal
{
if(amount<=balance)//check if enough balance
{
balance-=amount;
totalNetDeposits-=amount;
recordTransaction(WITHDRAWAL,month,day,year,amount);
return true;
}
else //not enough balance
{
return false;
}
}
bool Account::deposit(double amount,Month month ,int day, int year)//function to deposit
{
if(amount>0) //check if positive
{
balance+=amount;
totalNetDeposits+=amount;
recordTransaction(DEPOSIT,month,day,year,amount);
return true;
}
else //not a +ve number
{
return false;
}
}
int Account::getAccountNumber()//return the accoutn number
{
return accountNumber;
}
string Account::getName()//return the ownerName
{
return ownerName;
}
double Account::getBalance()//return the balance amount
{
return balance;
}
void Account::printTransactions()//prints transaction history
{
cout<<"Transaction history for Account "<<accountNumber<<endl;
cout<<"__________________________________________________"<<endl;
cout<<"Date Amount Type"<<endl;
cout<<"__________________________________________________"<<endl;
Transaction t;
for(int i=0;i<numberTransactions;i++)
{
t=transactionHistory[i];
cout<<t.transactionDay<<"-"<<MonthName(t.transactionMonth)<<"-"<<t.transactionYear<<" "<<t.transactionAmount<<" "<<OperationName(t.transactionType)<<endl;
}
}
void Account::display()
{
cout<<"Account Number: "<<getAccountNumber()<<endl;
cout<<"Name: "<<getName()<<endl;
cout<<"Balance: "<<getBalance()<<endl;
printTransactions();
}
double Account::getTotalNetDeposits()//return the totalNetDeposit value
{
return Account::totalNetDeposits;
}
Main.cpp
#include <iostream>
#include "Account.h"
using namespace std;
const int NUM_ACCOUNT=5;
Account *accounts[NUM_ACCOUNT];
int NUM_RECORDS=0; //the current number of records in the array
//returns the record for the given accNum
Account* getAccount(int accNum)
{
for(int i=0;i<NUM_RECORDS;i++)
{
if(accounts[i]->getAccountNumber()==accNum) //found a matching record
return accounts[i];
}
return NULL; //if record not found , return NULL
}
int main()
{
int choice;
int accNum,amount,day,year;
string name;
int month;
Account *acc;
do
{
//display menu
cout<<"1. Create Account"<<endl;
cout<<"2. Deposit Amount"<<endl;
cout<<"3. Withdraw Amount"<<endl;
cout<<"4. Print transaction history"<<endl;
cout<<"5. Display all accounts"<<endl;
cout<<"6. Display totalNetDeposits"<<endl;
cout<<"7. Exit"<<endl;
cout<<"Enter your choice: ";
cin>>choice;
switch(choice)
{
case 1:
if(NUM_RECORDS<NUM_ACCOUNT) //if enough space is available for new record
{
cout<<"Enter details for new account"<<endl;
cout<<"Account Number: " ;
cin>>accNum;
cout<<"Name: " ;
cin>>name;
cout<<"Amount: " ;
cin>>amount;
cout<<"Day: " ;
cin>>day;
cout<<"Month(1-12): " ;
cin>>month;
cout<<"Year: ";
cin>>year;
accounts[NUM_RECORDS]=new Account(accNum,name,amount,Month(month),day,year);
NUM_RECORDS++;
}
else
{
cout<<"Array already full!"<<endl;
}
break;
case 2:
cout<<"Enter details for depositing amount"<<endl;
cout<<"Account Number: ";
cin>>accNum;
acc=getAccount(accNum); //get record for given account number;
if(acc==NULL)// if not present
{
cout<<"No such account!"<<endl;
break;
}
cout<<"Amount: " ;
cin>>amount;
cout<<"Day: " ;
cin>>day;
cout<<"Month(1-12): " ;
cin>>month;
cout<<"Year: " ;
cin>>year;
if(acc->deposit(amount,Month(month),day,year))
{
cout<<"Amount "<<amount<<" deposited to account "<<acc->getAccountNumber()<<endl;
}
else
{
cout<<"Cannot deposit amount "<<amount<<" to account "<<acc->getAccountNumber()<<endl;
}
break;
case 3:
cout<<"Enter details for withdrawing amount"<<endl;
cout<<"Account Number: " ;
cin>>accNum;
acc=getAccount(accNum); //get record for given account number;
if(acc==NULL)// if not present
{
cout<<"No such account!"<<endl;
break;
}
cout<<"Amount: " ;
cin>>amount;
cout<<"Day: " ;
cin>>day;
cout<<"Month(1-12): " ;
cin>>month;
cout<<"Year: " ;
cin>>year;
if(acc->withdraw(amount,Month(month),day,year))
{
cout<<"Amount "<<amount<<" withdrawn from account "<<acc->getAccountNumber()<<endl;
}
else
{
cout<<"Cannot withdraw amount "<<amount<<" from account "<<acc->getAccountNumber()<<endl;
}
break;
case 4:
cout<<"Account Number: " ;
cin>>accNum;
acc=getAccount(accNum); //get record for given account number;
if(acc==NULL)// if not present
{
cout<<"No such account!"<<endl;
break;
}
else
{
acc->printTransactions();
}
break;
case 5:
cout<<"Details of all records"<<endl;
cout<<"_____________________________________"<<endl;
for(int i=0;i<NUM_RECORDS;i++)
accounts[i]->display();
cout<<"_____________________________________"<<endl;
break;
case 6:
cout<<"Total Net Deposits = "<<Account::getTotalNetDeposits()<<endl;
break;
case 7:
break;
default:
cout<<"Invalid menu choice!"<<endl;
}
cout<<"**********************************************"<<endl;
}while(choice!=7);
}
Output
1. Create Account
2. Deposit Amount
3. Withdraw Amount
4. Print transaction history
5. Display all accounts
6. Display totalNetDeposits
7. Exit
Enter your choice: 1
Enter details for new account
Account Number: 1
Name: Alice
Amount: 1000
Day: 23
Month(1-12): 1
Year: 2017
**********************************************
1. Create Account
2. Deposit Amount
3. Withdraw Amount
4. Print transaction history
5. Display all accounts
6. Display totalNetDeposits
7. Exit
Enter your choice: 2
Enter details for depositing amount
Account Number: 2
No such account!
**********************************************
1. Create Account
2. Deposit Amount
3. Withdraw Amount
4. Print transaction history
5. Display all accounts
6. Display totalNetDeposits
7. Exit
Enter your choice: 2
Enter details for depositing amount
Account Number: 1
Amount: 500
Day: 5
Month(1-12): 2
Year: 2017
Amount 500 deposited to account 1
**********************************************
1. Create Account
2. Deposit Amount
3. Withdraw Amount
4. Print transaction history
5. Display all accounts
6. Display totalNetDeposits
7. Exit
Enter your choice: 5
Details of all records
_____________________________________
Account Number: 1
Name: Alice
Balance: 1500
Transaction history for Account 1
__________________________________________________
Date Amount Type
__________________________________________________
23-Jan-2017 1000 Creation
5-Feb-2017 500 Deposit
_____________________________________
**********************************************
1. Create Account
2. Deposit Amount
3. Withdraw Amount
4. Print transaction history
5. Display all accounts
6. Display totalNetDeposits
7. Exit
Enter your choice: 6
Total Net Deposits = 1500
**********************************************
1. Create Account
2. Deposit Amount
3. Withdraw Amount
4. Print transaction history
5. Display all accounts
6. Display totalNetDeposits
7. Exit
Enter your choice: 1
Enter details for new account
Account Number: 2
Name: John
Amount: 400
Day: 10
Month(1-12): 2
Year: 2017
**********************************************
1. Create Account
2. Deposit Amount
3. Withdraw Amount
4. Print transaction history
5. Display all accounts
6. Display totalNetDeposits
7. Exit
Enter your choice: 1
Enter details for new account
Account Number: 3
Name: Henry
Amount: 500
Day: 15
Month(1-12): 2
Year: 2017
**********************************************
1. Create Account
2. Deposit Amount
3. Withdraw Amount
4. Print transaction history
5. Display all accounts
6. Display totalNetDeposits
7. Exit
Enter your choice: 1
Enter details for new account
Account Number: 4
Name: Angel
Amount: 600
Day: 20
Month(1-12): 2
Year: 2017
**********************************************
1. Create Account
2. Deposit Amount
3. Withdraw Amount
4. Print transaction history
5. Display all accounts
6. Display totalNetDeposits
7. Exit
Enter your choice: 5
Details of all records
_____________________________________
Account Number: 1
Name: Alice
Balance: 1500
Transaction history for Account 1
__________________________________________________
Date Amount Type
__________________________________________________
23-Jan-2017 1000 Creation
5-Feb-2017 500 Deposit
Account Number: 2
Name: John
Balance: 400
Transaction history for Account 2
__________________________________________________
Date Amount Type
__________________________________________________
10-Feb-2017 400 Creation
Account Number: 3
Name: Henry
Balance: 500
Transaction history for Account 3
__________________________________________________
Date Amount Type
__________________________________________________
15-Feb-2017 500 Creation
Account Number: 4
Name: Angel
Balance: 600
Transaction history for Account 4
__________________________________________________
Date Amount Type
__________________________________________________
20-Feb-2017 600 Creation
_____________________________________
**********************************************
1. Create Account
2. Deposit Amount
3. Withdraw Amount
4. Print transaction history
5. Display all accounts
6. Display totalNetDeposits
7. Exit
Enter your choice: 1
Enter details for new account
Account Number: 5
Name: Peter
Amount: 800
Day: 25
Month(1-12): 2
Year: 2017
**********************************************
1. Create Account
2. Deposit Amount
3. Withdraw Amount
4. Print transaction history
5. Display all accounts
6. Display totalNetDeposits
7. Exit
Enter your choice: 1
Array already full!
**********************************************
1. Create Account
2. Deposit Amount
3. Withdraw Amount
4. Print transaction history
5. Display all accounts
6. Display totalNetDeposits
7. Exit
Enter your choice: 3
Enter details for withdrawing amount
Account Number: 2
Amount: 700
Day: 1
Month(1-12): 3
Year: 2017
Cannot withdraw amount 700 from account 2
**********************************************
1. Create Account
2. Deposit Amount
3. Withdraw Amount
4. Print transaction history
5. Display all accounts
6. Display totalNetDeposits
7. Exit
Enter your choice: 4
Account Number: 2
Transaction history for Account 2
__________________________________________________
Date Amount Type
__________________________________________________
10-Feb-2017 400 Creation
**********************************************
1. Create Account
2. Deposit Amount
3. Withdraw Amount
4. Print transaction history
5. Display all accounts
6. Display totalNetDeposits
7. Exit
Enter your choice: 3
Enter details for withdrawing amount
Account Number: 2
Amount: 200
Day: 1
Month(1-12): 3
Year: 2017
Amount 200 withdrawn from account 2
**********************************************
1. Create Account
2. Deposit Amount
3. Withdraw Amount
4. Print transaction history
5. Display all accounts
6. Display totalNetDeposits
7. Exit
Enter your choice: 5
Details of all records
_____________________________________
Account Number: 1
Name: Alice
Balance: 1500
Transaction history for Account 1
__________________________________________________
Date Amount Type
__________________________________________________
23-Jan-2017 1000 Creation
5-Feb-2017 500 Deposit
Account Number: 2
Name: John
Balance: 200
Transaction history for Account 2
__________________________________________________
Date Amount Type
__________________________________________________
10-Feb-2017 400 Creation
1-Mar-2017 200 Withdraw
Account Number: 3
Name: Henry
Balance: 500
Transaction history for Account 3
__________________________________________________
Date Amount Type
__________________________________________________
15-Feb-2017 500 Creation
Account Number: 4
Name: Angel
Balance: 600
Transaction history for Account 4
__________________________________________________
Date Amount Type
__________________________________________________
20-Feb-2017 600 Creation
Account Number: 5
Name: Peter
Balance: 800
Transaction history for Account 5
__________________________________________________
Date Amount Type
__________________________________________________
25-Feb-2017 800 Creation
_____________________________________
**********************************************
1. Create Account
2. Deposit Amount
3. Withdraw Amount
4. Print transaction history
5. Display all accounts
6. Display totalNetDeposits
7. Exit
Enter your choice: 6
Total Net Deposits = 3600
**********************************************
1. Create Account
2. Deposit Amount
3. Withdraw Amount
4. Print transaction history
5. Display all accounts
6. Display totalNetDeposits
7. Exit
Enter your choice: 7