HELP PLEASE I FOR SOME REASON I CANT REALLY FIGURE OUT WHERE TO START THANK YOU
ID: 3772201 • Letter: H
Question
HELP PLEASE I FOR SOME REASON I CANT REALLY FIGURE OUT WHERE TO START THANK YOU IN ADVANCE.
Inheritance and Polymorphism
Based on the UML diagram from Lab #6, implement the three classes, Account, Checking, and Savings. Then implement a main program that reads commands from bank2.txt and carries out the commands using objects of these classes.
New Commands
Checking account initialBalance minimumBalance monthlyFee Savings account initialBalance minimumBalance annualInterestRate Deposit account amount
Withdraw account amount
Balance account
Close
Report
In the above commands, account is an integer between 1 and 9. All other arguments are doubles.
The close command performs the end of month functions for every existing account. The only output it produces is an “End of month processing complete” message. Note that Savings accounts have an annual interest rate, but at the end of the month only the monthly interest rate should be added. The annual interest rate is specified as a percentage, so a 7.5% interest rate is indicated as 7.5 in the Savings command.
The Report command lists all existing accounts and their balances, like – Checking account #1 has a balance of $402.25
Savings account #2 has a balance of $53576.65
Tasks
Modify the Account class.
Implement and test your new subclasses.
Implement a main program which reads commands from a file and carries them out.
Test your program thoroughly. The instructor will use a different bank2.txt file to evaluate
your program.
Submit the working classes and main program via the BlackBoard assignment.
Absolute Requirements
The Account class must be an abstract class with two pure virtual functions, closeMonth
and accountString.
The Savings and Checking classes must be subclasses of Account
The bank must handle at least 10 accounts simultaneously with account numbers 1-9
Do not duplicate the balance or ID fields in the subclasses
The balance, withdrawal and deposit manipulation must use the methods in the base
class Account.
Each action must display appropriate messages indicating that the action was
completed.
The closeMonth and accountString functions must be called polymorphically. This
means, they are called for each account without checking to see what kind of account it
is being called on.
An implication of the last point is that there will be a single array of Account objects,
some of which are Checking accounts and some of which are Savings accounts.
UML
Account
Id: int
Balance: double
+Account()
+Account(newID: int, initialBalance: double)
+setId(newID: int): void
+getId() : int
+setBalance(newBalance: double) : void
+getBalance() : double
+withdraw(amount: double) : void
+deposit(amount: double) : void
+virtual closeMonth() : void
+virtual accountString() : string
-----------------------------------------
Checking
minBalance : double
monthlyFee : double
+closeMonth (): void
+accountString () : string
-----------------------------------
Savings
minBalance : double
AnnPerRate : double
+closeMonth () : void
+accountString () : string
HEADER.H
#ifndef Header_h
#define Header_h
#include <iostream>
#include <string>
using namespace std;
class Account
{
public:
Account();
Account(int newId, double initialBalance);
void setId(int newId);
int getId() { return id; };
void setBalance(double newBalance);
double getBalance() { return balance; };
void withdraw(double amount);
void deposit(double amount);
private:
int id;
double balance;
};
class Checking
{
private:
double minBalance();
double monthlyFee();
public:
void closeMonth();
string accountString();
};
class Savings
{
private:
double minBalance();
double APR();
public:
void closeMonth();
string acountString();
};
#endif
HEADER.CPP
#include <stdio.h>
#include "Header.h"
#include <string>
using namespace std;
Account::Account()
{
balance = 0;
}
Account::Account(int newId, double initialBalance)
{
id = newId;
balance = initialBalance;
}
void Account::setId(int newId)
{
id = newId;
}
void Account::setBalance(double newBalance)
{
balance = newBalance;
}
void Account::withdraw(double amount)
{
balance = balance - amount;
}
void Account::deposit(double amount)
{
balance = balance + amount;
}
MAIN.CPP
#include <iostream>
#include <cmath>
#include <string>
#include <iomanip>
#include <fstream>
#include "Header.h"
using namespace std;
int main(){
fstream infile("bank2.txt");
infile.open("bank2.txt");
if(!infile)
{
cout<<"Error opening file ";
}
return 0;
}
BANK2.TXT
Explanation / Answer
Header.h(modified)
#pragma once
#ifndef Header_h
#define Header_h
#include <iostream>
#include <string>
using namespace std;
class Account
{
public:
Account();
virtual ~Account();
Account(int newId, double initialBalance);
void setId(int newId);
int getId() { return id; };
void setBalance(double newBalance);
double getBalance() { return balance; };
void withdraw(double amount);
void deposit(double amount);
virtual void closeMonth() = 0;
virtual string accountString() = 0;
private:
int id;
double balance;
};
class Checking:public Account
{
private:
double minBalance;
double monthlyFee;
public:
Checking();
Checking(double mBalance, double mFee,int id,double initialBalance);
void closeMonth();
string accountString();
};
class Savings:public Account
{
private:
double minBalance;
double APR;
public:
Savings();
Savings(double mBalance, double apr, int id, double initialBalance);
void closeMonth();
string accountString();
};
#endif
Header.cpp
#include "HEADER.h"
#include <stdio.h>
#include <string>
using namespace std;
Account::Account()
{
balance = 0;
}
Account::Account(int newId, double initialBalance)
{
id = newId;
balance = initialBalance;
}
void Account::setId(int newId)
{
id = newId;
}
void Account::setBalance(double newBalance)
{
balance = newBalance;
}
void Account::withdraw(double amount)
{
balance = balance - amount;
}
void Account::deposit(double amount)
{
balance = balance + amount;
}
Checking::Checking(double mBalance, double mFee, int id, double initialBalance) :Account(id, initialBalance), minBalance(mBalance), monthlyFee(mFee)
{
}
void Checking::closeMonth()
{
double balance = getBalance();
if (balance <= minBalance)
{
setBalance(balance - monthlyFee);
}
cout << "End of month processing complete" << endl;
}
string Checking::accountString()
{
string s = "Checking Account #" + to_string(getId()) + "has a balance of $" + to_string(getBalance());
return s;
}
Savings::Savings(double mBalance, double apr, int id, double initialBalance) :Account(id, initialBalance), minBalance(mBalance), APR(apr)
{
}
void Savings::closeMonth()
{
double balance = getBalance();
if (balance>minBalance)
{
setBalance(balance+(balance*APR/100));
}
cout << "End of month processing complete" << endl;
}
string Savings::accountString()
{
string s = "Savings Account #" + to_string(getId()) + "has a balance of $" + to_string(getBalance());
return s;
}
Main.cpp
#include <iostream>
#include <cmath>
#include <string>
#include <iomanip>
#include <fstream>
#include "Header.h"
#include <sstream>
using namespace std;
int main(){
Account* acc[10];
fstream infile("bank2.txt");
infile.open("bank2.txt");
if (!infile)
{
cout << "Error opening file ";
}
else
{
string command;
while (getline(infile, command))
{
stringstream ssin(command);
string first;
ssin >> first;
if (command == "Savings")
{
string sid;
string ibal;
string minbal;
string sapr;
ssin >> sid >> ibal >> minbal >> sapr;
int id = stoi(sid);
double initialBalance = stod(ibal);
double minBalance = stod(minbal);
double apr = stod(sapr);
acc[id - 1] = new Savings(minBalance, apr, id, initialBalance);
}
else if (first == "Checking")
{
string sid;
string ibal;
string minbal;
string mfee;
ssin >> sid>>ibal>>minbal>>mfee;
int id = stoi(sid);
double initialBalance = stod(ibal);
double minBalance = stod(minbal);
double monthlyFees = stod(mfee);
acc[id - 1] = new Checking(minBalance, monthlyFees, id, initialBalance);
}
else if (first == "Deposit")
{
string sid;
string samt;
ssin >> sid;
ssin >> samt;
int id = stoi(sid);
double amount = stod(samt);
acc[id - 1]->deposit(amount);
}
else if (first == "Withdraw")
{
string sid;
string samt;
ssin >> sid;
ssin >> samt;
int id = stoi(sid);
double amount = stoi(samt);
acc[id - 1]->withdraw(amount);
}
else if (first == "Balance")
{
string id;
ssin >> id;
int ids = stoi(id);
cout<<acc[ids - 1]->getBalance()<<endl;
}
else if (first == "Close")
{
for (int i = 0; i < 10; ++i)
{
if (acc[i] != NULL)
{
acc[i]->closeMonth();
}
}
}
else if (first == "Report")
{
for (int i = 0; i < 10; ++i)
{
if (acc[i] != NULL)
{
string s = acc[i]->accountString();
cout << s;
}
}
}
}
}
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.