Write a SavingsAccount class for this Java program.
Write a SavingsAccount class for this Java program. The SavingsAccount must be a subclass of BankAccount. Do not change the code.
class Main
{
public static void main(String args[])
{
BankAccount a1 = new BankAccount(510, "Elvis");
SavingsAccount a2 = new SavingsAccount(500, "Madonna");
SavingsAccount a3 = new SavingsAccount(100, "Craig");
BankAccount account3;
SavingsAccount.setRate(0.025); // all SavingsAccounts will earn 2.5% interest
a2.compoundInterest();
a3.compoundInterest();
System.out.println("After accruing interest, here are accounts 1 and 2:");
outputInOrder(a1, a2);
account3 = a3; // Testing a polymorphic reference:
// SavingsAccount is a sublclass of BankAccount
System.out.println("And here's account 3:");
System.out.println(account3);
account3.deposit(10);
System.out.println("And account 3 after depositing $10:");
System.out.println(account3);
}
private static void outputInOrder(Comparable o1, Comparable o2)
{
if(o1.compareTo(o2) > 0)
{
System.out.println(o2); // The println method calls an Object's
System.out.println(o1); // toString() method to output it.
}
else
{
System.out.println(o1);
System.out.println(o2);
}
}
}
class BankAccount extends Record implements Comparable<BankAccount>
{
protected double balance;
private String ownerName;
public BankAccount(double bal, String owner)
{
balance = bal;
ownerName = owner;
}
public void deposit(double dep)
{
balance += dep;
}
@Override
public String toString()
{
return ownerName + " has $" + balance + " in account number " + getId();
}
@Override
public int compareTo(BankAccount other)
{
if (balance > other.balance)
return 1;
else if (balance < other.balance)
return -1;
else
return 0;
}
}
abstract class Record
{
private static int nextNumber = 1;
private int recordNumber;
public Record()
{
recordNumber = nextNumber;
nextNumber++;
}
public int getId()
{
return recordNumber;
}
}
/* The completed program should produce the following output:
After accruing interest, here are accounts 1 and 2:
Elvis has $510.0 in account number 1
Madonna has $512.5 in account number 2 earning 2.5% interest
And here's account 3:
Craig has $102.5 in account number 3 earning 2.5% interest
And account 3 after depositing $10:
Craig has $112.5 in account number 3 earning 2.5% interest
*/
Trending now
This is a popular solution!
Step by step
Solved in 2 steps