Write a SavingsAccount class for this Java program.

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question

Write a SavingsAccount class for this Java program. The SavingsAccount must be a subclass of BankAccount. Do not change the code.

 
 
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

*/

Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 2 steps

Blurred answer
Knowledge Booster
Development strategies
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education