175k views
4 votes
Charge a $5 penalty if an attempt is made to withdraw more money than available in the account.Here the account string is "S" or "C". "S" is for savings account. "C" is for checking account. For the deposit or withdraw, it indicates which account is affected. For a transfer it indicates the account from which the money is taken; the money is automatically transferred to the other account.

User Nvcken
by
5.6k points

1 Answer

3 votes

Answer:

C++ code is explained below

Step-by-step explanation:

class Account {

double balance;

double add(double sum) {

balance += sum;

return sum;

}

double withdraw(double sum) {

if (sum > balance) {

balance -= 5;

return -5; // this will come in handy in Prob. 6

} else {

balance -= sum;

return balance; // Notice: always >= 0 (never < 0)

}

}

double inquire() { return balance; }

Account() { balance = 0; }

Account(double sum) { balance = sum; }

double interest (double rate) {

return rate * balance;

}

}

_______________________________

class Bank {

Account checking;

Account savings;

void deposit(double amount, String account) {

if (account.equals("C")) checking.add(amount);

else // my default

savings.add(amount);

}

void withdraw(double amount, String account) {

if (account.equals("C")) checking.withdraw(amount);

else // my default

savings.withdraw(amount);

}

void transfer (double amount, String account) {

if (account.equals("C"))

if (checking.withdraw(amount) >= 0)

savings.add(amount);

else checking.add(5); // somewhat fault-tolerant

else // default

if (savings.withdraw(amount) >= 0)

checking.add(amount);

else savings.add(5); // no penalty for transfers

}

void printBalances() {

System.out.println(

"Checking: " + checking.inquire() +

"\\Savings: " + savings.inquire()

);

}

}

User Glessard
by
6.2k points