I have the a project to c reate interfaces named Depositable, Withdrawable, and
ID: 3676883 • Letter: I
Question
I have the a project to 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:
public void deposit(double amount)
The Withdrawable interface should include this method:
public void withdraw(double amount)
And the Balanceable interface should include these methods:
public double getBalance()
public void setBalance(double amount)
· Create a class named Account that implements all three of these interfaces. This class should include an instance variable for the balance.
· Create a class named CheckingAccount that inherits the Account class. This class should include an instance variable for the monthly fee that’s initialized to the value that’s passed to the constructor. This class should also include methods that subtract the monthly fee from the account balance and return the monthly fee.
· Create a class named SavingsAccount that inherits the Account class. This class should include instance variables for the monthly interest rate and the monthly interest payment. The monthly interest rate should be initialized to the value that’s passed to the constructor. The monthly interest payment should be calculated by a method that applies the payment to the account balance. This class should also include a method that returns the monthly interest payment.
· Create a class named Transactions that contains two static methods for depositing and withdrawing funds from either type of account:
public class Transactions
{
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 Validator class or a variation of it to validate the user’s entries. This validation code should not allow the user to withdraw more than the current account balance. Create a Java Bank Account Management program with NetBeans that satisfies the specifications above.
I tried, but I got a lot of errors, that is why I am seeking your help
Again, thank you for hard work
Explanation / Answer
Account.java
import java.text.NumberFormat;
public class Account implements Depositable, Withdrawable, Balanceable {
public static double amount;
@Override
public void setBalance(double amount) {
Account.amount = amount;
}
@Override
public void deposit(double amount) {
Account.amount += amount;
}
@Override
public void withdraw(double amount) {
Account.amount -= amount;
}
public static String getBalanceFormatted() {
NumberFormat currency = NumberFormat.getCurrencyInstance();
return currency.format(amount);
}
}
CheckingAccount.java
import java.text.NumberFormat;
public class CheckingAccount extends Account {
private double monthlyFee = 1.0;
public void substractMonthlyFee() {
CheckingAccount.amount -= monthlyFee;
}
public void setMonthlyFee(double monthlyFee) {
this.monthlyFee = monthlyFee;
}
public double getMonthlyFee() {
return monthlyFee;
}
public String getMonthlyFeeFormated() {
NumberFormat currency = NumberFormat.getCurrencyInstance();
return currency.format(this.getMonthlyFee());
}
}
Transactions.java
public class Transactions {
public static void deposit (Depositable account, double amount) {
account.deposit(amount);
}
public static void withdraw (Withdrawable account, double amount) {
account.withdraw(amount);
}
}
Balenceable.java
public interface Balanceable {
void setBalance (double amount);
}
Depositable.java
public interface Depositable {
void deposit (double amount);
}
Withdrawable.java
public interface Withdrawable {
void withdraw (double amount);
}
AccountApp.java
public class AccountApp {
public static void main(String[] vars) {
CheckingAccount cAccount = new CheckingAccount();
cAccount.setBalance(1000);
Console.displayLine ("Welcome to the Account Calculator ");
Console.displayLine ("Starting Balance");
Console.displayLine("Checking: " + cAccount.getBalanceFormatted());
Console.displayLine (" Enter the Transactions for the month ");
boolean cont = true;
while(cont) {
String trans = Console.getString(" Withdrawal or deposit? (w/d): ");
Transactions transaction = new Transactions();
if (trans.equalsIgnoreCase("w")) {
while (true) {
double amount = Console.getDouble("Amount: ");
if (amount > cAccount.amount) {
Console.displayLine("Error! Cannot withdraw that amount. Insufficient Funds. Try again.");
} else {
transaction.withdraw(cAccount, amount);
break;
}
}
} else if (trans.equalsIgnoreCase("d")) {
while (true) {
double amount = Console.getDouble("Amount: ");
if (amount > 10000) {
Console.displayLine("Error! Cannot deposit that amount. Try again.");
} else {
transaction.deposit(cAccount, amount);
break;
}
}
}
String choice = Console.getString(" Continue? (y/n): ");
if (choice.equalsIgnoreCase("n")) {
cont = false;
} else if (choice.equalsIgnoreCase("y")) {
continue;
}
}
Console.displayLine("Monthly Fees");
Console.displayLine("Checking fee: " + cAccount.getMonthlyFeeFormated());
cAccount.substractMonthlyFee();
Console.displayLine("Final Balance");
Console.displayLine("Checking: " + cAccount.getBalanceFormatted());
}
}
Console.java
import java.util.Scanner;
public class Console {
private static Scanner sc = new Scanner (System.in);
public static void displayLine() {
System.out.println();
}
public static void displayLine (String s) {
System.out.println(s);
}
public static String getString (String prompt) {
System.out.print(prompt);
String s = sc.nextLine();
return s;
}
public static int getInt (String prompt) {
int i = 0;
while (true) {
System.out.print(prompt);
try {
i = Integer.parseInt(sc.nextLine());
break;
} catch (NumberFormatException e) {
System.out.println("Error! Invalid integer. Try again.");
}
}
return i;
}
public static double getDouble (String prompt) {
double d = 0;
while (true) {
System.out.print(prompt);
try {
d = Double.parseDouble(sc.nextLine());
break;
} catch (NumberFormatException e) {
System.out.println("Error! Invalid decimal. Try again.");
}
}
return d;
}
}