Question

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 Account object, should have an overdraft limit variable. Ensure that you have overridden methods of the Account class as necessary in both derived classes.

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

• The Bank class requires a method for opening accounts, and to deposit and withdraw from accounts specified by their account number. Account numbers are generated sequentially starting with 101.

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. • Be sure to test what you have done after each step.
• Use BankDemo.java to test your code.

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

package com.bank;
public class BankDemo
{
public static void main(String[] args)
{
Bank tinyBank = new Bank(10); // 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.printAccouns();
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.printAccouns();
}
}


----------
package com.bank;

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.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());
}

public void addInterest() {
}
}
---------------
/**
*
*/
package com.bank;

/**
* @author
*
*/
public class CheckingAccount extends Account {

int overDraftLimit;

/**
* @return the overDraftLimit
*/
public int getOverDraftLimit() {
return overDraftLimit;
}

/**
* @param overDraftLimit the overDraftLimit to set
*/
public void setOverDraftLimit(int overDraftLimit) {
this.overDraftLimit = overDraftLimit;
}

public CheckingAccount(int accNum, int b, int od) {
super(accNum);
super.deposit(b);
overDraftLimit = od;
// TODO Auto-generated constructor stub
}

}

---------
/**
*
*/
package com.bank;

/**
* @author
*
*/
public class SavingsAccount extends Account {

double interest;

/**
* @return the interest
*/
public double getInterest() {
return interest;
}

/**
* @param interest the interest to set
*/
public void setInterest(double interest) {
this.interest = interest;
}

public SavingsAccount(int accNum, int balance,double interest) {
super(accNum);
super.deposit(balance);
this.interest = interest;
// TODO Auto-generated constructor stub
}

}

----------
/**
*
*/
package com.bank;

/**
* @author
*
*/
public class Bank {

int accountNum = 100;
Account[] accounts = null;
int count = 0;

public Bank(int size) {
accounts = new Account[size];
}

public void openCheckingAccount(int i, int j) {
// TODO Auto-generated method stub
accounts[count++] = new CheckingAccount(accountNum++, i, j);
}

public void openSavingsAccount(int i, double d) {
// TODO Auto-generated method stub
accounts[count++] = new SavingsAccount(accountNum++, i, d);

}

public void deposit(int i, int j) {
// TODO Auto-generated method stub

for (int k = 0; k < count; k++) {
if (accounts[k].getAccountNumber() == i) {
accounts[k].deposit(j);
}
}

}

public void withdraw(int i, int j) {
// TODO Auto-generated method stub
for (int k = 0; k < count; k++) {
if (accounts[k].getAccountNumber() == i) {
if (accounts[k].getBalance() > j)
accounts[k].withdraw(j);
else
System.out.println("No sufficient balance");
}
}
}

public void printAccouns() {
// TODO Auto-generated method stub
for (int k = 0; k < count; k++) {

accounts[k].print();
;

}
}

public void addInterest(int i) {
// TODO Auto-generated method stub

for (int k = 0; k < count; k++) {
if (accounts[k].getAccountNumber() == i && accounts[k] instanceof SavingsAccount) {
double bal = accounts[k].getBalance();
double intRate = ((SavingsAccount) accounts[k]).getInterest();

double intrest = bal + ((bal * intRate) / 100);
accounts[k].deposit(intrest);
}
}

}

}

-------
Output
Acc 100: balance = 100.0
Acc 101: balance = 200.0
Acc 102: balance = 300.0
Acc 103: balance = 400.0
Acc 104: balance = 400.0
Acc 105: balance = 400.0

tinyBank.deposit(112,500)

tinyBank.withdraw(101,500)
No sufficient balance

tinyBank.withdraw(101,110)

tinyBank.addInterest(101)

tinyBank.addInterest(102)

tinyBank.withdraw(102,500)
No sufficient balance

tinyBank.withdraw(102,200)

Acc 100: balance = 100.0
Acc 101: balance = 180.04500000000002
Acc 102: balance = 100.0
Acc 103: balance = 400.0
Acc 104: balance = 400.0
Acc 105: balance = 400.0

Add a comment
Know the answer?
Add Answer to:
Bank Accounts Look at the Account class Account.java and write a main method in a different...
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
  • 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...

  • You are to write a banking application in c# that keeps track of bank accounts. It will consist of three classes, Account, Bank and BankTest. The Account class is used to represent a savings account i...

    You are to write a banking application in c# that keeps track of bank accounts. It will consist of three classes, Account, Bank and BankTest. The Account class is used to represent a savings account in a bank. The class must have the following instance variables: a String called accountID                       a String called accountName a two-dimensional integer array called deposits (each row represents deposits made in a week). At this point it should not be given any initial value. Each...

  • In C++ Create an inheritance hierarchy that a bank might use to represent customers’ bank accounts. All customers at this bank can deposit (i.e., credit) money into their accounts and withdraw (i.e.,...

    In C++ Create an inheritance hierarchy that a bank might use to represent customers’ bank accounts. All customers at this bank can deposit (i.e., credit) money into their accounts and withdraw (i.e., debit) money from their accounts. More specific types of accounts also exist. Savings accounts, for instance, earn interest on the money they hold. Checking accounts, on the other hand, charge a fee per transaction (i.e., credit or debit). Create an inheritance hierarchy containing base class Account and derived...

  • In this, you will create a bank account management system according to the following specifications: BankAccount...

    In this, you will create a bank account management system according to the following specifications: BankAccount Abstract Class contains the following constructors and functions: BankAccount(name, balance): a constructor that creates a new account with a name and starting balance. getBalance(): a function that returns the balance of a specific account. deposit(amount): abstract function to be implemented in both Checking and SavingAccount classes. withdraw(amount): abstract function to be implemented in both Checking and SavingAccount classes. messageTo Client (message): used to print...

  • 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,...

  • In C++ Write a program that contains a BankAccount class. The BankAccount class should store the...

    In C++ Write a program that contains a BankAccount class. The BankAccount class should store the following attributes: account name account balance Make sure you use the correct access modifiers for the variables. Write get/set methods for all attributes. Write a constructor that takes two parameters. Write a default constructor. Write a method called Deposit(double) that adds the passed in amount to the balance. Write a method called Withdraw(double) that subtracts the passed in amount from the balance. Do not...

  • 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...

  • TASK 1 Create a new class called CheckingAccount that extends BankAccount. It should contain a static...

    TASK 1 Create a new class called CheckingAccount that extends BankAccount. It should contain a static constant FEE that represents the cost of clearing onecheck. Set it equal to 15 cents. Write a constructor that takes a name and an initial amount as parameters. Itshould call the constructor for the superclass. It should initializeaccountNumber to be the current value in accountNumber concatenatedwith -10 (All checking accounts at this bank are identified by the extension -10). There can be only one...

  • 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...

  • c++ please    Define the class bankAccount to implement the basic properties of a bank account....

    c++ please    Define the class bankAccount to implement the basic properties of a bank account. An object of this class should store the following data: Account holder’s name (string), account number (int), account type (string, checking/saving), balance (double), and interest rate (double). (Store interest rate as a decimal number.) Add appropriate member func- tions to manipulate an object. Use a static member in the class to automatically assign account numbers. Also declare an array of 10 compo- nents of...

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