Name your source code as .cpp (replace the text with with your last name and fir
ID: 3809141 • Letter: N
Question
Name your source code as .cpp (replace the text with with your last name and first name, in that order) and upload to blackboard. The goal of this assignment is to design and implement classes. We will continue the lesson from the 2/27 lecture on the banking model. Write a program that implements a class model for storing and managing the following data: Test Data: Create a customer using your name and address. Create two accounts for yourself, a Checking and a Savings. Start with a $100 Credit balance in each account. Fill in the other required data as you like (date opened, time stamp). The customer should be able to perform the following actions: Customer: Get the current balance of funds in a specific account identified by the account ID value. Get the total amount of funds across ALL accounts Add money to an account specified by the ID Retrieve money from an account specified by an ID Once you have built your Class Model, write your program to implement your class by outputting to standard output the following transactions: Get balance info for your checking account. Add $10 to both checking and savings account. Retrieve $50 from your checking account Get the total available balance you now have in both accounts.Explanation / Answer
//Replace xxx with your name and give your address instead of Address.
#include <iostream>
#include <ctime>
#include <vector>
using namespace std;
enum TransactionType{Credit,Debit};
class Transaction
{
public:
Transaction(TransactionType type, float amount)
{
ID = nextTransactionID++;
Type = type;
Amount = amount;
time_t t = time(0); // get time now
struct tm * now = localtime( & t );
TimeStamp = (now->tm_year + 1900) + '-' + (now->tm_mon + 1) + '-' + now->tm_mday;
}
TransactionType GetTransactionType()
{
return Type;
}
float GetAmount()
{
return Amount;
}
TransactionType GetType()
{
return Type;
}
static int nextTransactionID;
private:
int ID;
TransactionType Type;
string TimeStamp;
float Amount;
};
class Account
{
public:
Account(string accName, float amount)
{
ID = nextAccountID++;
Name = accName;
time_t t = time(0); // get time now
struct tm * now = localtime( & t );
DateOpened = (now->tm_year + 1900) + '-' + (now->tm_mon + 1) + '-' + now->tm_mday;
balance = 0;
Transaction transaction(Credit, amount);
DoTransaction(transaction);
}
int GetID()
{
return ID;
}
int GetBalance()
{
return balance;
}
bool DoTransaction(Transaction transaction)
{
float amount = transaction.GetAmount();
if(transaction.GetType() == Credit)
{
balance += amount;
}
else
{
if(balance < amount)
{
cout<<"balance is less than withdrawl amount. can not do this transaction";
return false;
}
else
{
balance -= amount;
}
}
Transactions.push_back(transaction);
return true;
}
static int nextAccountID;
private:
int ID;
string Name;
string DateOpened;
vector<Transaction> Transactions;
int TransactionsCount;
float balance;
};
class Customer
{
public:
Customer(string Name, string Address)
{
ID = nextAccountNum++;
this->Name = Name;
this->Address = Address;
}
bool AddAccount(Account acc)
{
Accounts.push_back(acc);
}
float GetBalance(int accountID)
{
for(int i=0;i<Accounts.size();i++)
{
if(Accounts[i].GetID() == accountID)
return Accounts[i].GetBalance();
}
return 0; //assumption: returning 0 for invalid accountID
}
float GetTotalBalance()
{
float totalBalance = 0;
for(int i=0;i<Accounts.size();i++)
{
totalBalance += Accounts[i].GetBalance();
}
return totalBalance;
}
bool AddMoney(int ID, float amount)
{
for(int i=0;i<Accounts.size();i++)
{
if(Accounts[i].GetID() == ID)
{
Transaction transaction(Credit, amount);
return Accounts[i].DoTransaction(transaction);
}
}
cout << "there is no account with the given Account ID";
return false;
}
bool WithDrawMoney(int ID, float amount)
{
for(int i=0;i<Accounts.size();i++)
{
if(Accounts[i].GetID() == ID)
{
Transaction transaction(Debit, amount);
return Accounts[i].DoTransaction(transaction);
}
}
cout << "there is no account with the given Account ID";
return false;
}
static int nextAccountNum ; //Used to generate customer ID
private:
int ID;
string Name;
string Address;
vector<Account> Accounts;
};
int Customer::nextAccountNum = 0;
int Account::nextAccountID = 0;
int Transaction::nextTransactionID = 0;
int main()
{
//Replace xxx with your name and Address with your address
Customer cust("xxx", "Address");
Account checking("checking", 100);
Account savings("savings", 100);
cust.AddAccount(checking);
cust.AddAccount(savings);
cout << "Checking account balance: $" << cust.GetBalance(checking.GetID()) << endl;
cout << "Adding $10 to checking account" << endl;
cust.AddMoney(checking.GetID(), 10);
cout << "Adding $10 to Savings account" << endl;
cust.AddMoney(savings.GetID(), 10);
cout << "WithDrawing $50 from checking account" << endl;
cust.WithDrawMoney(checking.GetID(), 50);
cout << "Customer Total balance: $" << cust.GetTotalBalance() << endl ;
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.