asked 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.

asked
User Nvcken
by
7.5k 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()

);

}

}

answered
User Glessard
by
7.8k points

No related questions found

Welcome to Qamnty — a place to ask, share, and grow together. Join our community and get real answers from real people.