Question: Java Homework question: The assignment says I need to make applyInterest an abstract method and then to define find interest in checkingAccount and savingAccount. When
Java Homework question:
The assignment says I need to make applyInterest an abstract method and then to define find interest in checkingAccount and savingAccount. When I call acctDatabase.applyInterest(); in my main class the method is underlined and suggest that I create a .applyInterest() method in BankAccount class which I have it is just abstract like the assignment said to do. Not sure what I am doing wrong to get the applyInterest() method to work?
______________________________
public class BankMain {
/** * @param args the command line arguments */ public static void main(String[] args) { BankDatabase acctDatabase = new BankDatabase();
acctDatabase.createCheckingAccount("Alin Parker", "123-45-6789", 20000.0f);
acctDatabase.createSavingAccount("Mary Jones", "987-65-4321", 15000.0f);
acctDatabase.createSavingAccount("John Smith", "123-45-6789", 12000.0f); acctDatabase.print(); acctDatabase.applyInterest(); acctDatabase.print(); } }
__________________________________________________________
public class BankDatabase {
private Customer customer; BankAccount[] accounts; int numAccounts;
BankDatabase() { accounts = new BankAccount[50]; numAccounts = 0; } void createCheckingAccount(String customerName, String socialSecurity, float deposit) { String[] splitStr = customerName.split("\\s+"); String firstName = splitStr[0]; String lastName = splitStr[1]; accounts[numAccounts] = new CheckingAccount(firstName, lastName, socialSecurity, deposit); numAccounts++; } void createSavingAccount(String customerName, String socialSecurity, float deposit) { String[] splitStr = customerName.split("\\s+"); String firstName = splitStr[0]; String lastName = splitStr[1]; accounts[numAccounts] = new CheckingAccount(firstName, lastName, socialSecurity, deposit); numAccounts++; } void print() { for(int i = 0; i < numAccounts; i++) { System.out.println(accounts[i].customer.firstName + " " + accounts[i].customer.lastName + " " + accounts[i].accountNumber + " " + accounts[i].balance); } } public void applyIntrest() { for(int i = 0; i < numAccounts; i++) { accounts[i].applyInterest(); } } }
_________________________________________________
public abstract class BankAccount { Customer customer; protected int accountNumber = ThreadLocalRandom.current().nextInt(1000000000, Integer.MAX_VALUE); protected float balance; BankAccount(String firstName, String lastName, String socialSecurity, float balance) { customer = new Customer(firstName, lastName, socialSecurity ); this.accountNumber = accountNumber; this.balance = balance; } public void deposit(float amount) { balance = this.balance + amount; System.out.println(customer.firstName + " " + customer.lastName + " Deposited: " + amount + ". Current balance " + balance); } public void withdraw(float amount) { if(amount <= this.balance) { balance = this.balance - amount; System.out.println(customer.firstName + " " + customer.lastName + " withdrew " + amount + ". Current balance " + balance); } else { System.out.println("Unable to withdraw " + amount + " for " + customer.firstName + " " + customer.lastName + " " + " due to insufficient funds"); } } public void checkBalance() { System.out.println("Check Balance: " + balance); } @Override public String toString() { return "Succesful account created for " + customer.firstName + accountNumber + customer.lastName + accountNumber; }
public abstract void applyInterest(); }
______________________________________________________
public class CheckingAccount extends BankAccount{ public CheckingAccount(String firstName, String lastName, String socialSecurity, float balance) { super(firstName, lastName, socialSecurity, balance); System.out.println("Succesful account created for " + firstName + " " + lastName+ " Account Number: " + accountNumber); System.out.println(firstName + " " + lastName+ " Balance " + balance); } /** * */ @Override public void applyInterest() { float excess = balance - 10000; if(excess > 0) { float interest = (float) (excess * .02); balance = balance + interest; } else { System.out.println("Must have greater then 10000.00f in checking account to apply interest"); } } }
________________________________________________________________
public class SavingsAccount extends BankAccount{ public SavingsAccount(String firstName, String lastName, String socialSecurity, float balance) { super(firstName, lastName, socialSecurity, balance); System.out.println("Succesful account created for " + firstName + " " + lastName+ " Account Number: " + accountNumber); System.out.println(firstName + " " + lastName+ " Balance " + balance); } /** * */ @Override public void applyInterest() { float interest = (float) (balance * .05); balance = balance + interest; } }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
