- Engineering
- Computer Science
- here is the code before in c include ltiostreamgt include...
Question: here is the code before in c include ltiostreamgt include...
Question details
Here is the code before in C++:
#include <iostream>
#include <ctime>
#include <iomanip>
using namespace std;
static time_t now = time(0);
static tm *ltm = localtime(&now);
class BankAccount {
protected:
double balance, interestRate;
struct tm lastInterestDate;
public:
BankAccount() {
balance = 0;
interestRate = 0;
lastInterestDate.tm_mon = 0;
lastInterestDate.tm_year = 0;
lastInterestDate.tm_mday = 1;
lastInterestDate.tm_sec = 0;
lastInterestDate.tm_min = 0;
lastInterestDate.tm_hour = 0;
}
BankAccount(double b, double r, struct tm lid = *ltm) {
balance = b;
interestRate = r;
lastInterestDate = lid;
}
void withdraw(double deduction)
{
if (balance < deduction)
cout <<
"Insufficient amount.." << endl;
else
balance -=
deduction;
}
void setBalance(double b) {
balance = b;
}
void increaseBalanceBy(double amount) {
balance += amount;
}
void setInterestRate(double r) {
if (r < 0 || r > 1) {
cout <<
"invalid rate!" << endl;
return;
}
interestRate = r;
}
void deposit(double addition)
{
balance += addition;
}
void addInterest() {
time_t t = time(NULL);
struct tm now;
localtime_s(&now,
&t);
int today = (now.tm_year + 1990) *
10000 + (now.tm_mon + 1) * 100 + (now.tm_mday);
cout << "Today: " <<
today << endl;
cout << "last interest date:
" << lastInterestDate.tm_mday << endl;
if ((today -
lastInterestDate.tm_mday) > 30)
{
balance =
balance + balance * interestRate;
lastInterestDate.tm_mday = today;
}
}
void info() {
std::ios cout_state(NULL);
cout_state.copyfmt(cout);
cout << "Balance: $" <<
setw(6) << setfill('0') << fixed <<
setprecision(1) << balance << endl;
char buffer[80];
strftime(buffer, 80, "%D",
&lastInterestDate);
cout << "Interest rate: %"
<< interestRate << endl;
cout << "Last interest paid
on " << buffer << endl << endl;
}
double getBalance() {
return balance;
}
double getInterestRate() {
return interestRate;
}
struct tm getLastInterestDate() {
return lastInterestDate;
}
};
class walletAccount :public BankAccount
{
private:
double maxWalletCapacity = 100;
public:
walletAccount() :BankAccount()
{
}
walletAccount(double bal, double rate)
:BankAccount(bal, rate)
{
}
double getMaxWalletCapacity()
{
return maxWalletCapacity;
}
void deposit(double addition)
{
if ((balance + addition) <=
maxWalletCapacity)
deposit(addition);
else
cout <<
"Balance would exceed the wallet capacity" << endl;
}
void setBalance(double bal)
{
if (bal <=
maxWalletCapacity)
balance =
bal;
else
cout <<
"Balance would exceed the wallet capacity" << endl;
}
void printInfo()
{
info();
cout << " Max wallet
capacity: $" << maxWalletCapacity << endl;
}
};
class SavingAccount :public BankAccount
{
private:
bool lock = false;
void accountLockMessage()
{
if (lock)
cout <<
"Account is locked" << endl;
else
cout <<
"Account is open" << endl;
}
public:
SavingAccount(double bal, double rate, bool lock)
:BankAccount(bal, rate)
{
this->lock = lock;
}
void lockAccount()
{
lock = true;
accountLockMessage();
}
void removeLock()
{
lock = false;
accountLockMessage();
}
void setBalance(double bal)
{
if (lock == false)
balance =
bal;
else
cout <<
"access is denied due to account being locked." <<
endl;
}
void setInterestRate(double rate)
{
if (lock == false)
interestRate =
rate;
else
cout <<
"access is denied due to account being locked." <<
endl;
}
void addInterest()
{
if (lock == false)
BankAccount::addInterest();
else
cout <<
"access is denied due to account being locked." <<
endl;;
}
void deposit(double addition)
{
if (lock == false)
BankAccount::deposit(addition);
else
cout <<
"access is denied due to account being locked." <<
endl;;
}
void withdraw(double deduction)
{
if (lock == false)
BankAccount::withdraw(deduction);
else
cout <<
"access is denied due to account being locked." <<
endl;;
}
void printInfo()
{
info();
cout << lock <<
endl;
}
};
int main()
{
SavingAccount saving(1000, 0.02, true);
saving.printInfo();
saving.withdraw(200);
saving.removeLock();
saving.withdraw(300);
saving.withdraw(1000);
saving.lockAccount();
saving.deposit(2000);
saving.printInfo();
walletAccount wallet(70.0, 0.02);
wallet.printInfo();
wallet.deposit(50);
wallet.printInfo();
}
Solution by an expert tutor
