Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Java Monthly Balance The application begins by displaying the starting balance f

ID: 3825586 • Letter: J

Question

Java Monthly Balance

   The application begins by displaying the starting balance for a checking account.

   The application prompts the user to enter the amount for a withdrawal or deposit.

   When the user finishes entering deposits and withdrawals, the application displays the fees for the month followed by the final balances for the month

Specifications

   Create interfaces named Depositable, Withdrawable, and Balanceable that specify the methods that can be used to work with accounts. The Depositable interface should include this method:

void deposit(double amount)

The Withdrawable interface should include this method:

void withdraw(double amount)

And the Balanceable interface should include these methods:

double getBalance()

void setBalance(double amount)

   Create a class named Account that implements all three of these interfaces. In addition, it should supply a method like the following method that returns a balance that has been formatted as currency:

String getBalanceFormatted()

   Create a class named CheckingAccount that inherits the Account class. This class should include an instance variable for the monthly fee and these methods:

void subtractMonthlyFee()

void setMonthlyFee(double monthlyFee)

double getMonthlyFee()

String getMonthlyFeeFormatted()

   By default, the monthly fee for a checking account should be $1.

   Create a class named Transactions that contains the following static methods for depositing and withdrawing funds from either type of account:

public static void deposit(Depositable account, double amount) {

   account.deposit(amount);

}

public static void withdraw(Withdrawable account, double amount) {

   account.withdraw(amount);

}

   Create a class named AccountApp that prompts the user for a transaction, posts the transaction, and displays the information shown in the console output. Create the necessary objects for each transaction, and post the transaction using the appropriate method of the Transactions class.

   Use the Console class presented in chapter 8 or a variation of it to get entries from the user.

   This application should not allow the user to withdraw more than the current account balance.

   This application should not allow the user to deposit more than $10,000 per transaction.

Explanation / Answer

Code for Java Monthly Balance :

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;
}

}

CheckingAccount.java:

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);
}
}

SavingsAccount .java:

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);
}
}

Transactions.java:

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());
}
}

}

AccountApp.java:

public class AccountApp
{

public static void main(String[] args)
{
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());
}

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote