Question

The code is attached below has the Account class that was designed to model a bank account.

IN JAVA


The code is attached below has the Account class that was designed to model a bank account. An account has the properties account number, balance, annual interest rate, and date created, and methods to deposit and withdraw funds. I need creat program using the 2 java programs.

Create two subclasses for checking and savings accounts. A checking account has an overdraft limit, but a savings account cannot be overdrawn.

I need to write a test program that creates objects of Account, SavingsAccount, and CheckingAccount and invokes their toString() methods.

Account.java

class Account {
  private int id;
  private double balance;
  private static double annualInterestRate;
  private java.util.Date dateCreated;

  public Account() {
    dateCreated = new java.util.Date();
  }

  public Account(int newId, double newBalance) {
    id = newId;
    balance = newBalance;
    dateCreated = new java.util.Date();
  }

  public int getId() {
    return this.id;
  }

  public double getBalance() {
    return balance;
  }

  public static double getAnnualInterestRate() {
    return annualInterestRate;
  }

  public void setId(int newId) {
    id = newId;
  }

  public void setBalance(double newBalance) {
    balance = newBalance;
  }

  public static void setAnnualInterestRate(double newAnnualInterestRate) {
    annualInterestRate = newAnnualInterestRate;
  }

  public double getMonthlyInterest() {
    return balance * (annualInterestRate / 1200);
  }

  public java.util.Date getDateCreated() {
    return dateCreated;
  }

  public void withdraw(double amount) {
    balance -= amount;
  }

  public void deposit(double amount) {
    balance += amount;
  }
}


TestBankAccount.java

public class TestBankAccount {
  public static void main (String[] args) {
    Account account = new Account(1122, 20000);
    Account.setAnnualInterestRate(4.5);
    
    account.withdraw(2500);
    account.deposit(3000);
    System.out.println("Balance is " + account.getBalance());
    System.out.println("Monthly interest is " +
      account.getMonthlyInterest());
    System.out.println("This account was created at " +
      account.getDateCreated());
  }
}
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Account.java: ------------------------------------------------------------------------------------------------------------------

public class Account {
    private int id;
    private double balance;
    private static double annualInterestRate;
    private java.util.Date dateCreated;

    public Account() {
        dateCreated = new java.util.Date();
    }

    public Account(int newId, double newBalance) {
        id = newId;
        balance = newBalance;
        dateCreated = new java.util.Date();
    }

    public int getId() {
        return this.id;
    }

    public double getBalance() {
        return balance;
    }

    public static double getAnnualInterestRate() {
        return annualInterestRate;
    }

    public void setId(int newId) {
        id = newId;
    }

    public void setBalance(double newBalance) {
        balance = newBalance;
    }

    public static void setAnnualInterestRate(double newAnnualInterestRate) {
        annualInterestRate = newAnnualInterestRate;
    }

    public double getMonthlyInterest() {
        return balance * (annualInterestRate / 1200);
    }

    public java.util.Date getDateCreated() {
        return dateCreated;
    }

    public void withdraw(double amount) {
        balance -= amount;
    }

    public void deposit(double amount) {
        balance += amount;
    }

    @Override
    public String toString(){
        return "Balance is " + this.getBalance() + "\nMonthly interest is " + this.getMonthlyInterest() + "\nThis account was created at " + this.getDateCreated();
    }
}


TestBankAccount.java-------------------------------------------------------------------------------------------------------

public class TestBankAccount {
    public static void main (String[] args) {
        Account account = new Account(1122, 20000);
        Account.setAnnualInterestRate(4.5);

        account.withdraw(2500);
        account.deposit(3000);
        System.out.println(account.toString());
    }
}

SavingsAccount.java: ------------------------------------------------------------------------------------------------------

public class SavingsAccount extends Account{

    //Constructor's calling the respective super class' constructors.
    public SavingsAccount(){
        super();
    }

    public SavingsAccount(int newId, double newBalance){
        super(newId, newBalance);
    }

    //Overriding the withdraw function such that the withdrawal amount cannot be more than that of the bank balance.
    @Override
    public void withdraw(double amount) {
        if(amount < getBalance())
            super.withdraw(amount);
        else{
            System.out.println("ERROR: The withdrawal amount exceeds account balance.");
            System.exit(1);
        }
    }

    //Defining a more meaningful toString function.
    @Override
    public String toString(){
        return super.toString() + "\nThis is a Savings Account\n";
    }
}

TestSavingsAccount.java: -------------------------------------------------------------------------------------------------

public class TestSavingsAccount {
    public static void main (String[] args) {
        SavingsAccount saccount = new SavingsAccount(1122, 20000);
        SavingsAccount.setAnnualInterestRate(4.5);

        saccount.withdraw(2500);
        saccount.deposit(3000);
        System.out.println(saccount.toString());
    }
}

CheckingAccount.java: -----------------------------------------------------------------------------------------------------

public class CheckingAccount extends Account{

    private double overdraftLimit;  //Variable to hold the overdraftLimit for the account.


    //Constructor's calling the respective super class' constructors.
    public CheckingAccount(double overdraftLimit){
        super();
        this.overdraftLimit = overdraftLimit;
    }

    public CheckingAccount(int newId, double newBalance, double overdraftLimit){
        super(newId, newBalance);
        this.overdraftLimit = overdraftLimit;
    }

    public double getOverdraftLimit() {
        return overdraftLimit;
    }

    //Overriding the withdraw function such that the withdrawal amount cannot be more than that of the bank balance + overdraftLimit.
    @Override
    public void withdraw(double amount){
        if(amount < this.getOverdraftLimit() + this.getBalance()){
            super.withdraw(amount);
        }
        else {
            System.out.println("ERROR: The amount exceeds the overdraft limit.");
            System.exit(1);
        }
    }

    //Defining a more meaningful toString function.
    @Override
    public String toString(){
        return super.toString() + "\nThis is a Checking Account with an overdraft limit of: " + getOverdraftLimit();
    }
}

TestCheckingAccount.java: ------------------------------------------------------------------------------------------------

public class TestCheckingAccount {
    public static void main (String[] args) {
        CheckingAccount saccount = new CheckingAccount(1122, 20000, 10000);
        SavingsAccount.setAnnualInterestRate(4.5);

        saccount.withdraw(25000);
        saccount.deposit(3000);
        System.out.println(saccount.toString());
    }
}
Add a comment
Know the answer?
Add Answer to:
The code is attached below has the Account class that was designed to model a bank account.
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Unable to find out why am I getting: Account.java:85: error: reached end of file while parsing...

    Unable to find out why am I getting: Account.java:85: error: reached end of file while parsing import java.util.*; class Account{ private int id; private double balance; private double annualInterestRate; private Date dateCreated; public Account() { id = 0; balance = 0; annualInterestRate = 0; dateCreated = new Date(); } public Account(int id1, double balance1) { id =id1; balance = balance1; dateCreated = new Date(); } public void setId(int id1) { id=id1; } public void setBalance(double balance1) { balance = balance1;...

  • Hello, I'm very new to programming and I'm running into a problem with the system.out.println function...

    Hello, I'm very new to programming and I'm running into a problem with the system.out.println function in the TestAccount Class pasted below. Account Class: import java.util.Date; public class Account {    private int accountId;    private double accountBalance, annualInterestRate;    private Date dateCreated;    public Account(int id, double balance) { id = accountId;        balance = accountBalance; dateCreated = new Date();    }    public Account() { dateCreated = new Date();    }    public int getId() { return...

  • 9.7 (The Account class) Design a class named Account that contains: • A private int data...

    9.7 (The Account class) Design a class named Account that contains: • A private int data field named id for the account (default o). • A private double data field named balance for the account (default o). • A private double data field named annualInterestRate that stores the current interest rate (default o). Assume that all accounts have the same interest rate. • A private Date data field named dateCreated that stores the date when the account was created. •...

  • How do you write a test class that tests each of the setters and constructors to ensure that the appropriate exception is raised?(Note: sed. Recall that the setter method should be called in the constructor and the edits should not be in the setters, not

     /** * Write a description of class LoanTest here. * */public class Loan {  private double annualInterestRate;  private int numberOfYears;  private double loanAmount;  private java.util.Date loanDate;  /** Default constructor */  public Loan() {    this(2.5, 1, 1000);  }  /** Construct a loan with specified annual interest rate,      number of years and loan amount     */  public Loan(double annualInterestRate, int numberOfYears,double loanAmount){    if(loanAmount <= 0)    {        throw new IllegalArgumentException("loan must be greater than 0");       } ...

  • (Enable the Account class comparable & cloneable) @ Create ComparableAccount class inheritanc...

    (Enable the Account class comparable & cloneable) @ Create ComparableAccount class inheritance from the Account class, and implemented the comparable and cloneable interfaces. Override the compareTo method to compare the balance of two accounts. Print the Account ID, balance and dataCreated information from the toString() method. Implements the cloneable interface and override the clone method to perform a deep copy on the dateCreated field. Write a driver program to create one array contains 5 ComparableAcccount objects (account #: 1001-1005, initial...

  • Consider the following Account class and main function: class Account: acct_num=1000 #Constructor for Account class (default...

    Consider the following Account class and main function: class Account: acct_num=1000 #Constructor for Account class (default values for balance #and annual interest rate are 100 and e, respectively). #INITIALIZER METHOD HEADER GOES HERE #MISSING CODE def getId(self): return self._id def getBalance(self): #MISSING CODE def getAnnualInterestRate(self): return self.___annualInterestRate def setBalance(self, balance): self. balance - balance def setAnnualInterestRate(self,rate): #MISSING CODE def getMonthlyInterestRate(self): return self. annualInterestRate/12 def getMonthlyInterest (self): #MISSING CODE def withdraw(self, amount): #MISSING CODE det getAnnualInterestRate(self): return self. annualInterestRate def setBalance(self,...

  • Can the folllowing be done in Java, can code for all classes and code for the...

    Can the folllowing be done in Java, can code for all classes and code for the interface be shown please. Modify the GeometricObject class to implement the Comparable interface and define a static max method in the GeometriObject class for finding the larger of two GeometricObject objects. Write a test program that uses the max method to find the larger of two circles, the larger of two rectangles. The GeometricObject class is provided below: public abstract class GeometricObject { private...

  • (The Account class) Design a class named Account that contains: A private int data field named...

    (The Account class) Design a class named Account that contains: A private int data field named id for the account (default 0). A private double data field named balance for the account (default 0). A private double data field named annualInterestRate that stores the current interest rate (default 0). Assume all accounts have the same interest rate. A private Date data field named dateCreated that stores the date when the account was created. A no-arg constructor that creates a default...

  • The software I use is Eclipse, please show how to write it, thanks. GeometricObject class public...

    The software I use is Eclipse, please show how to write it, thanks. GeometricObject class public abstract class GeometricObject { private String color = "white"; private boolean filled; private java.util.Date dateCreated; protected GeometricObject() { dateCreated = new java.util.Date(); } protected GeometricObject(String color, boolean filled) { dateCreated = new java.util.Date(); this.color = color; this.filled = filled; } public String getColor() { return color; } public void setColor(String color) { this.color = color; } public boolean isFilled() { return filled; } public...

  • Use the Java codes below (Checking Account and Checking Account Demo), and work on these problems....

    Use the Java codes below (Checking Account and Checking Account Demo), and work on these problems. You have to do both of your java codes based on the two provided Java codes. Create one method for depositing and one for withdrawing. The deposit method should have one parameter to indicate the amount to be deposited. You must make sure the amount to be deposited is positive. The withdraw method should have one parameter to indicate the amount to be withdrawn...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT