Question

Look at the Account class below and write a main method in a different class to briefly experiment with some instances of the Account class.

Look at the Account class below and write a main method in a different class to briefly experiment with some instances of the Account class.


Using the Accountclass as a base class, write two derived classes called SavingsAccountand CurrentAccount.ASavingsAccountobject, in addition to the attributes of an Account object, should have an interest variable and a method which adds interest to the account. ACurrentAccount object, in addition to the instance variables of an Account object, should have an overdraft limit variable. Ensure that you have overridden methods of the Accountclass as necessary in both derived classes.

Now create a Bankclass, an object of which contains an array of Account objects. Accounts in the array could be instances of the Account class, the SavingsAccountclass, or the CurrentAccount class. Create some test accounts (some of each type).

Write an update method in the bankclass. It iterates through each account, updating it in the following ways: Savings accounts get interest added (via the method you already wrote); CurrentAccounts get a letter sent if they are in overdraft.

The Bank class requires methods for opening and closing accounts, and for paying a dividend into each account.

Hints:

  • Note that the balance of an account may only be modified through the deposit(double) and withdraw(double) methods.

  • The Account class should not need to be modified at all.

-----------------------------------------------------------------------


Account.java :

/**

A class for bank accounts.


This class provides the basic functionality of accounts.

It allows deposits and withdrawals but not overdraft

limits or interest rates.

@author Stuart Reynolds ... 1999

*/

public class Account

{

private double bal;//The current balance

private int accnum;//The account number



public Account(int a)

{

bal=0.0;

accnum=a;

}


public void deposit(double sum)

{

if (sum>0)

bal+=sum;

else

System.err.println("Account.deposit(...):"

+"cannot deposit negativeamount.");

}


public void withdraw(double sum)

{

if (sum>0)

bal-=sum;

else

System.err.println("Account.withdraw(...): "

+"cannot withdraw negativeamount.");&nbsõbpx.øi5/p>

}


public double getBalance()

{

return bal;

}


public double getAccountNumber()

{

return accnum;

}


public String toString()

{

return "Acc "+ accnum + ": " + "balance = " + bal;

}


public final void print()

{

//Don't override this,

//override the toStringmethod

System.out.println(toString() );

}


}

0 0
Add a comment Improve this question Transcribed image text
✔ Recommended Answer
Answer #1

// Account.java

public class Account {
   private double bal; // The current balance
   private int accnum; // The account number
  

   public Account(int accNo,double bal) {
       this.bal = bal;
       accnum = accNo;

   }

   public void deposit(double sum) {
       if (sum > 0)
           bal += sum;
       else
           System.out.println("Account.deposit(...): "
                   + "cannot deposit negative amount.");
   }

   public void withdraw(double sum) {
       if (sum > 0)
           bal -= sum;
       else
           System.out.println("Account.withdraw(...): "
                   + "cannot withdraw negative amount.");
   }

   public double getBalance() {
       return bal;
   }

   public int getAccountNumber() {
       return accnum;
   }

   public String toString() {
       return "Acc " + accnum + ": " + "balance = " + bal;
   }

   public final void print() {
       // Don't override this,
       // override the toString method
       System.out.println(toString());
   }

}

===================================

// SavingsAccount.java

public class SavingsAccount extends Account {
// Declaring instance variables
   private double annualInterest;

   // Parameterized constructor
   public SavingsAccount(int accNO, double balance, double rate) {
       super(accNO, balance);
       this.annualInterest = rate;
   }

   public double getAnnualInterest() {
       return annualInterest;
   }

   public void setAnnualInterest(double annualInterest) {
       this.annualInterest = annualInterest;
   }

   @Override
   public void deposit(double amt) {

       super.deposit(amt);

   }

   @Override
   public void withdraw(double amt) {
       if (getBalance() > amt)
           super.withdraw(amt);
   }

   public double monthlyInterestRate() {
       return annualInterest / 12;
   }

   public double getMonthlyInterest() {
       return getBalance() * (monthlyInterestRate() / 100);
   }

   public void addInterest() {
       super.deposit(getMonthlyInterest());

   }


   @Override
   public String toString() {
       return super.toString() + " Annual Interest: " + annualInterest;
   }

}

===================================

// CheckingAccount.java

public class CheckingAccount extends Account {
   private double creditLimt;

   // Parameterized constructor
   public CheckingAccount(int accNO, double balance, double creditLimit) {
       super(accNO, balance);
       this.creditLimt = creditLimit;
   }

   // Implementing the deposit method
   @Override
   public void deposit(double amt) {
       super.deposit(amt);
   }

   // Implementing the withdraw method
   @Override
   public void withdraw(double amt) {
       if (getBalance() >= amt)
           super.withdraw(amt);
   }


   @Override
   public String toString() {
       return super.toString() + " Credit Limt: " + creditLimt;
   }

}

=====================================

// Bank.java

public class Bank {
   private int limit;
   private Account accounts[] = null;
   private int cnt;
   private static int NEXTACCNO = 101;

   public Bank(int limit) {
       this.limit = limit;
       accounts = new Account[limit];
       this.cnt = 0;
   }

   public void openSavingsAccount(double bal, double rate) {
       if (cnt != limit) {
           accounts[cnt] = new SavingsAccount(NEXTACCNO, bal, rate);
           NEXTACCNO++;
           cnt++;
       }
   }

   public void openCheckingAccount(double bal, double creditLimit) {
       if (cnt != limit) {
           accounts[cnt] = new CheckingAccount(NEXTACCNO, bal, creditLimit);
           NEXTACCNO++;
           cnt++;
       }

   }


   public void printAccounts() {
       for (int i = 0; i < cnt; i++) {
           System.out.println(accounts[i]);
       }

   }


  
   public void deposit(int accNo,double amt)
   {
       int indx=-1;
       for(int i=0;i        {
           if(accounts[i].getAccountNumber()==accNo)
           {
               indx=i;
           }
       }
       if(indx!=-1)
       {
           accounts[indx].deposit(amt);
       }
   }
   public void withdraw(int accNo,double amt)
   {
       int indx=-1;
       for(int i=0;i        {
           if(accounts[i].getAccountNumber()==accNo)
           {
               indx=i;
           }
       }
       if(indx!=-1)
       {
           accounts[indx].withdraw(amt);
       }
   }
  
   public void addInterest(int accNo)
   {
       int indx=-1;
       for(int i=0;i        {
           if(accounts[i].getAccountNumber()==accNo)
           {
               indx=i;
           }
       }
       if(indx!=-1 && (accounts[indx] instanceof SavingsAccount))
       {
           ((SavingsAccount)(accounts[indx])).addInterest();
       }  
   }
}

====================================

// BankDemo.java

public class BankDemo
{
public static void main(String[] args)
{
Bank tinyBank = new Bank(3); // create Bank that can hold 3 accounts
tinyBank.openCheckingAccount(100,20); // balance=100 credit limit = 20
tinyBank.openSavingsAccount(200, 0.05); // balance=200 interest=5%
tinyBank.openCheckingAccount(300,30); // balance=300 credit limit = 30
tinyBank.openSavingsAccount(400,0.05); // balance=400 interest=5%);
tinyBank.openCheckingAccount(400,40); // balance=400 credit limit = 40
tinyBank.openSavingsAccount(400,0.05); // balance=400 interest=5%);

tinyBank.printAccounts();
  
System.out.println("\ntinyBank.deposit(112,500)");
tinyBank.deposit(112,500);

System.out.println("\ntinyBank.withdraw(101,500)");
tinyBank.withdraw(101,500);
System.out.println("\ntinyBank.withdraw(101,110)");
tinyBank.withdraw(101,110);
System.out.println("\ntinyBank.addInterest(101)");
tinyBank.addInterest(101);

System.out.println("\ntinyBank.addInterest(102)");
tinyBank.addInterest(102);
System.out.println("\ntinyBank.withdraw(102,500)");
tinyBank.withdraw(102,500);
System.out.println("\ntinyBank.withdraw(102,200)");
tinyBank.withdraw(102,200);

System.out.println();
tinyBank.printAccounts();

}
}

=====================================

Output:

Acc 101: balance = 100.0 Credit Limt: 20.0
Acc 102: balance = 200.0 Annual Interest: 0.05
Acc 103: balance = 300.0 Credit Limt: 30.0

tinyBank.deposit(112,500)

tinyBank.withdraw(101,500)

tinyBank.withdraw(101,110)

tinyBank.addInterest(101)

tinyBank.addInterest(102)

tinyBank.withdraw(102,500)

tinyBank.withdraw(102,200)

Acc 101: balance = 100.0 Credit Limt: 20.0
Acc 102: balance = 0.008333333333325754 Annual Interest: 0.05
Acc 103: balance = 300.0 Credit Limt: 30.0

Add a comment
Know the answer?
Add Answer to:
Look at the Account class below and write a main method in a different class to briefly experiment with some instances of the Account class.
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Similar Homework Help Questions
  • Bank Accounts Look at the Account class Account.java and write a main method in a different...

    Bank Accounts Look at the Account class Account.java and write a main method in a different class to briefly experiment with some instances of the Account class. • Using the Account class as a base class, write two derived classes called SavingsAccount and CheckingAccount. A SavingsAccount object, in addition to the attributes of an Account object, should have an interest variable and a method which adds interest to the account. A CheckingAccount object, in addition to the attributes of an...

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

    IN JAVAThe 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,...

  • Write a Java class called BankAccount (Parts of the code is given below), which has two...

    Write a Java class called BankAccount (Parts of the code is given below), which has two fields name (String) and balance (double), two constructors and five methods getName(), getBalance(), deposit (double amount), withdraw(double amount) and toString(). The first constructor should initialize name to null and balance to 0. The second constructor initializes name and balance to the parameters passed. deposit method deposits the amount to the account causing the current balance to increase, withdraw method withdraws the amount causing the...

  • Write a Java class called BankAccount (Parts of the code is given below), which has two...

    Write a Java class called BankAccount (Parts of the code is given below), which has two fields name (String) and balance (double), two constructors and five methods getName(), getBalance(), deposit (double amount), withdraw(double amount) and toString(). The first constructor should initialize name to null and balance to 0. The second constructor initializes name and balance to the parameters passed. deposit method deposits the amount to the account causing the current balance to increase, withdraw method withdraws the amount causing the...

  • ATM Revisited In Java, design a subclass of the Account class called GoldAccount. It is still...

    ATM Revisited In Java, design a subclass of the Account class called GoldAccount. It is still an Account class, however it has an additional field and some additional functionality. But it will still retain all the behavior of the Account class. Write a class called GoldAccount. This class will have an additional private field called bonusPoints. This field will require no get or set methods. But it will need to be initialized to 100 in the constructor. Thereafter, after a...

  • The current code I have is the following: package uml; public class uml {        public...

    The current code I have is the following: package uml; public class uml {        public static void main(String[] args) {              // TODO Auto-generated method stub        } } class Account { private String accountID; public Account(String accountID) { this.accountID = accountID; } public String getAccountID() { return accountID; } public void setAccountID(String accountID) { this.accountID = accountID; } @Override public String toString() { return "Account [accountID=" + accountID + "]"; } } class SuppliesAccount extends Account { private...

  • Task 3: Main Program Create a main program class that: Creates three or more Nurse instances...

    Task 3: Main Program Create a main program class that: Creates three or more Nurse instances assigned to different shifts. Creates three or more Doctor instances. Creates three or more Patient instances with pre-determined names and manually assigned physicians chosen from the pool of Doctor instances previously created. Generates another 20 Patient instances using randomly generated names and randomly assigns them physicians chosen from the pool of Doctor instances previously created. Prints the toString() values for all employees. Prints the...

  • Java Code the following interfaces - An interface called Accountable with void withdraw(double) and double getBalance()...

    Java Code the following interfaces - An interface called Accountable with void withdraw(double) and double getBalance() methods, and an interface called AccountReceivable with a void deposit(double) method. Save these two interfaces in separate Java files. Then code a class, BusinessAccount, that implements the two interfaces defined above. Define and code the necessary instance variables and constructors. Override toString() so it will return the amount of a business account. Apply the particular operations, such as withdraw and deposit, required in implementing...

  • According to the following information, define Account, CheckingAccount, and TestPart1 classes. 1. Account and CheckingAccount classes...

    According to the following information, define Account, CheckingAccount, and TestPart1 classes. 1. Account and CheckingAccount classes Account - number: int - openDate: String - name: String - balance: double + Account(int, String, String, double) + deposit(double): void + withdraw (double): boolean + transferTo(Account, double): int + toString(): String CheckingAccount + CheckingAccount(int, String, String, double) + transferTo(Account, double): int Given the above UML diagam, define Account and CheckingAccount classes as follow: Account (8 points) • public Account(int nu, String op, String...

  • Please help JAVA Part II: Programming Module (80 marks): Write a banking program that simulates the operation of your l...

    Please help JAVA Part II: Programming Module (80 marks): Write a banking program that simulates the operation of your local bank. You should declare the following collection of classes: 1) An abstract class Customer: each customer has: firstName(String), lastName (String), age (integer), customerNumber (integer) - The Class customer has a class variable lastCustomerNumber that is initialized to 9999. It is used to initialize the customerNumber. Hence, the first customer number is set to 9999 and each time a new customer...

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