Operation Solution public interface Depositable { public void deposit(double amo
ID: 3653998 • Letter: O
Question
OperationExplanation / Answer
public interface Depositable { public void deposit(double amount); } public interface Withdrawable { public void withdraw(double amount); } public interface Balanceable { public void setBalance(double amount); public double getBalance(); } public class Account implements Depositable, Withdrawable, Balanceable { private double balance; @Override public void setBalance(double amount) { balance = amount; } @Override public double getBalance() { return balance; } @Override public void withdraw(double amount) { if(balance >= amount) balance -= amount; else throw new IllegalArgumentException("Insufficient funds to withdraw $ " + amount); } @Override public void deposit(double amount) { balance += amount; } } public class CheckingAccount extends Account { private double monthlyFee; public CheckingAccount(double fee) { monthlyFee = fee; } public double getMonthlyFee() { return monthlyFee; } public void changebalance() { setBalance(getBalance() - monthlyFee); } } public class SavingsAccount extends Account { private double interestRate; private double interestPayment; public SavingsAccount(double rate) { interestRate = rate; } public double getInterestPayment() { return interestPayment; } public void addToBalance() { interestPayment = interestRate * getBalance(); setBalance(getBalance() + interestPayment); } } public class Transactions { public static void deposit(Depositable account, double amount) { if(amount < 0) System.err.println("Cannot deposit Negative amount"); else account.deposit(amount); } public static void withdraw(Withdrawable account, double amount) { try { account.withdraw(amount); } catch(Exception e) { System.err.printf("%s ",e.getMessage()); } } } public class AccountApp { public static void main(String[] args) { //Assuming Monthly interest rate = 1 % i.e, 0.01 //Assuming monthly checking Fee = $ 1 //Also Assuming Starting Balances in Checkings and //Savings Accounts is $ 1000 CheckingAccount checking = new CheckingAccount(1); SavingsAccount saving = new SavingsAccount(0.01); checking.setBalance(1000); saving.setBalance(1000); Scanner input = new Scanner(System.in); System.out.println("Welcome to the account application"); System.out.printf("%s%,.2f %s%,.2f", "Starting balances Checking: $", checking.getBalance(), "Savings: $", saving.getBalance()); System.out.println(" Enter the Transactions for the month"); while(true) { try { System.out.println("Withdrawal or Deposit ? (w/d) "); String transaction = input.next(); System.out.println("Checking or savings ? (c/s) "); String accountType = input.next(); System.out.println("Amount ? "); double amount = input.nextDouble(); if(transaction.equalsIgnoreCase("w")) { if(accountType.equalsIgnoreCase("c")) Transactions.withdraw(checking, amount); else if(accountType.equalsIgnoreCase("s")) Transactions.withdraw(saving, amount); else throw new InputMismatchException("Wrong Input for account type"); } else if(transaction.equalsIgnoreCase("d")) { if(accountType.equalsIgnoreCase("c")) Transactions.deposit(checking, amount); else if(accountType.equalsIgnoreCase("s")) Transactions.deposit(saving, amount); else throw new IllegalArgumentException("Wrong Input for account type"); } else { throw new IllegalArgumentException("Wrong Input for Transaction type"); } System.out.println("continue ? (Y/N)"); String loopCondition = input.next(); if(loopCondition.equalsIgnoreCase("n")) break; } catch(IllegalArgumentException e) { System.err.println(e.getMessage()); input.nextLine(); } catch(Exception e) { System.err.println("Wrong Input.Amount should be of type double"); input.nextLine(); } } checking.changebalance(); saving.addToBalance(); System.out.println("Monthly payments and fees"); System.out.printf("%s%,.2f %s%,.2f ","Checing fee: $", checking.getMonthlyFee(), "Savings interest payment : $", saving.getInterestPayment()); System.out.printf("%s%,.2f %s%,.2f","Final Balances " + "Checking : $", checking.getBalance(), "Savings : $", saving.getBalance()); } }Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.