In this lab assignment, we will work with polymorphism. Starting with create a c
ID: 3697379 • Letter: I
Question
In this lab assignment, we will work with polymorphism. Starting with create a class Bank,this will be the base classin this project. The private data of this class will be vector of bank accounts. Each bank account will contain the account number (random 4digit integer), account type (saving or checking), and balance of the account. The base class and Account can be declared as suggested in the follows:
enum account_type {saving, checking}
class Account {
public:
Account ()
~Account()
private:
int account_number
account_type type
double balance }
class Bank {
public:
Bank()
~Bank()
// additional functions as needed
virtual void deposit() = 0
virtual void withdraw() = 0
private: vector bank_accounts
}
Create 2 other derived classes from the base class called Savingand Checking. Redefine the deposit and withdraw functions for each class as follow:
For Savingclass
when call deposit function, a new amount (can be zero) will be added to the current balance along with the interest from the original balance
when call withdraw function, check if the amount is valid (cannot exceed the current balance)
For Checkingclass
when call deposit function, a new amount (cannot be zero) will be added to the current balance a transaction fee will be subtracted from the balance.
when call withdraw function, check if the amount is valid (cannot exceed the current balance minus the transaction fee) withdraw the amount and subtract the transaction fee from the balance.
When making the bank accounts, the account numbers will be the random 4digit integers the initial balances will be the random double value between $0 and $100.00 the account type will be either saving or checking.
The interest rate will be 0.01 for all saving accounts. The transaction rate will be $0.50 for all checking account. Start the program by making 5 random accounts. The following is the sample inputs and outputs
Select the following options 1. Add new account
2. Deposit to an account
3. Withdraw from an account
4. Print all accounts
5. Quit
4
Account number Type Balance
1123 Saving 23.45
5432 Checking 54.65
7421 Checking 87.34
2376 Saving 65.76
5341 Checking 65.34
Select the following options
1. Add new account
2. Deposit to an account
3. Withdraw from an account
4. Print all accounts
5. Quit
1
Enter account number: 1111
Enter account type: Saving
Enter account balance: 123
Account number Type Balance
1123 Saving 23.45
5432 Checking 54.65
7421 Checking 87.34
2376 Saving 65.76
5341 Checking 65.34
1111 Saving 123.00
Select the following options
1. Add new account
2. Deposit to an account
3. Withdraw from an account
4. Print all accounts
5. Quit
2
Enter account number: 2376
Enter amount: 50
Deposit $50.00 to Saving account 2376, the new balance is $116.41
Select the following options
1. Add new account
2. Deposit to an account
3. Withdraw from an account
4. Print all accounts
5. Quit
3
Enter account number: 7421
Enter amount: 50
Withdraw $50 from Checking account 7421, the new balance is $36.84
Select the following options
1. Add new account
2. Deposit to an account
3. Withdraw from an account
4. Print all accounts
5. Quit
5
Program terminated
please use c++ for this question
please answer correctly
Explanation / Answer
#include<iostream>
#include<vector>
#include<string>
#include<memory>
#include<iomanip>
#include<time.h>
using namespace std;
enum account_type{saving,checking};
class Account {
public:
Account();
~Account();
private:
int account_number;
account_type type;
double balance;
};
class Bank {
public:
Bank();
~Bank();
void createAccount();
void printAccounts();
void initialAccounts();
int guessAccounts(int n);
int searchAccounts(int n);
//additional functions as needed
virtual void withdraw(double amount,int index) {}
virtual void deposit(double amount, int index) {}
private:
vector<Account> bank_accounts;
};
//drived class Saving from the base class Bank
class Saving : public Bank
{
void deposit(double amount,int index)override;
void withdraw(double amount,int index)override;
};
//drived class Checking from the base class Bank
class Checking : public Bank
{
void deposit(double amount,int index)override;
void withdraw(double amount,int index)override;
};
//main function
int main()
{
int ch;
auto bank = std::make_unique<Bank>();
bank->initialAccounts();
bank->printAccounts();
Saving s;
Checking c;
Bank *save = &s;
Bank *check = &c;
while (input != 5) {
cout << " Select the following options: ";
cout << "1. Add new account ";
cout << "2. Deposit to an account ";
cout << "3. Withdraw from an account ";
cout << "4. Print all accounts ";
cout << "5. Quit ";
cin >>ch;
switch (ch)
{
case 1:
cout <<" ";
bank->createAccount();
break;
case 2:
int acc_num, dep, index, res;
cout << " Enter account number: " <<endl;
cin >> acc_num;
index = bank->searchAccounts(acc_num);
res = bank->guessAccounts(index);
cout << index;
if (res == 1)
{
cout << " Enter amount: " << endl;
cin >> dep;
save->deposit(dep,index);
}
if (res==2)
{
cout << " Enter amount: " << endl;
cin >> dep;
check->deposit(dep, index);
}
break;
case 3:
int acc_num, dep, index, res;
cout << " Enter account number:" << endl;
cin >> acc_num;
index = bank->searchAccounts(acc_num);
result= bank->guessAccounts(index);
cout << index;
if (result == 1)
{
cout << " Enter amount: " << endl;
cin >> dep;
save->withdraw(dep, index);
}
if (res == 2)
{
cout << " Enter amount: " << endl;
cin >> dep;
check->withdraw(dep, index);
}
break;
case 4:
bank->printAccounts();
break;
case 5:
break;
}
}
return 0;
}
Account::Account() :balance(NULL), account_number(NULL){}
Account::~Account() {}
Bank::Bank() : bank_accounts(NULL){}
Bank::~Bank() {}
void Bank::initialAccounts()
{
srand(time(NULL));
Account acc;
for (int i = 0; i < 5; i++)
{
acc.account_number = rand() % 9999 + 1000;
acc.balance = rand() % 100 + 0;
int r = rand() % 2;
if (r == 0)
acc.type = saving;
if (r == 1)
acc.type = checking;
bank_accounts.push_back(acc);
}
}
void Bank::createAccount()
{
Account acc;
string acc_type;
int acc_num;
double bal;
cout << " Enter account number: " << endl;
cin >> acc_num;
cout << " Enter account type: " << endl;
cin >> acctype;
cout << " Enter account balance: " << endl;
cin >> bal;
if ((acc_type == "Saving") || (acctype == "saving"))
{
acc.type = saving;
cout << " no";
}
if ((acc_type == "Checking") || (acc_type == "checking"))
acc.type = checking;
acc.account_number = acc_num;
acc.balance = bal;
bank_accounts.push_back(acc);
}
void Bank::printAccounts()
{
int size;
size = bank_accounts.size() -1;
cout << "Account Number Type Balance" << endl;
for (int i = 0; i <= size; i++)
{
cout << setw(5) <<bank_accounts[i].account_number;
if (bank_accounts[i].type == 0)
cout << setw(7) << "" << setw(10) << "saving";
if (bank_accounts[i].type == 1)
cout << setw(7) << "" << setw(10) << "checking";
cout << setw(5) << "" << setw(10) << bank_accounts[i].balance << endl;
}
}
int Bank::guessAccounts(int n)
{
if (bank_accounts[n].type == 0)
return 1;
if (bank_accounts[n].type == 1)
return 2;
return 0;
}
int Bank::searchAccounts(int n)
{
int size;
size = bank_accounts.size()-1;
for (int i = 0; i < size; i++)
{
if (bank_accounts[i].account_number == n)
return i;
}
return -1;
}
void Saving::deposit(double amount,int index)
{
double i = bank_accounts[index].balance;
bank_accounts[index].balance = i + (.01*i) + amount;
}
void Saving::withdraw(double amount,int index)
{
if (bank_accounts[index].balance < amount)
{
cout << "Value too large!" << endl;
return;
}
else
{
double i = bank_accounts[index].balance;
bank_accounts[index].balance = i - amount;
}
}
void Checking::deposit(double amount,int index)
{
int i = bank_accounts[index].balance;
bank_accounts[index].balance = i + amount - .5;
}
void Checking::withdraw(double amount,int index)
{
if (bank_accounts[index].balance < amount)
{
cout << "Value too large!" << endl;
return;
}
else
{
double i = bank_accounts[index].balance;
bank_accounts[index].balance = i - amount - .5;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.