JAVA Write another class called Bank containing a main method in order to fully
ID: 3597528 • Letter: J
Question
JAVA
Write another class called Bank containing a main method in order to fully test your account classes. Create two objects for each account class (Total 6). Using the objects call various methods defined within these classes. This needs to be continued to the following:
public class Account
{
protected int accountNumber;
protected double balance;
// Constructor
public Account(int a){
balance = 0.0;
accountNumber = a;
}
public void deposit(double amount){
if (amount > 0)
balance += amount;
else
System.err.println("Account.deposit(...): "
+"cannot deposit negative amount.");
}
public void withdraw(double amount){
if (amount > 0)
balance -= amount;
else
System.err.println("Account.withdraw(...): "
+"cannot withdraw negative amount.");
}
public double getbalance(){
return balance;
}
public double getAccountNumber(){
return accountNumber;
}
public void display(){
System.out.println("Account " + accountNumber + " : " + "Balance = " + balance);
}
}
class SavingsAccount extends Account
{
private double interest;
//assuming interest to be in % i.e. 50%,58.5%
public SavingsAccount(int a,int interest)
{
super(a);
this.interest=interest;
}
public void addInterest()
{
balance+=balance*(interest/100);
}
public void withdraw(double amount){
if (amount>= 0 && balance>=amount)
balance -= amount;
else if (amount<0)
System.err.println("Account.withdraw(...): "
+"cannot withdraw negative amount.");
else
System.err.println("Account.withdraw(...): "
+"cannot withdraw more than balance.");
}
}
class CurrentAccount extends Account
{
private double overdraftLimit;
public CurrentAccount(int a,int overdraftLimit)
{
super(a);
this.overdraftLimit=overdraftLimit;
}
public void withdraw(double amount){
if (amount> 0 && (balance+overdraftLimit)>=amount)
balance -= amount;
else if(amount<0)
System.err.println("Account.withdraw(...): "
+"cannot withdraw negative amount.");
else
System.err.println("Account.withdraw(...): "
+"cannot withdraw more than balance+overdraft limit");
}
}
Explanation / Answer
Below is the Bank.java class
public class Bank
{
public static void main(String [] args)
{
//First account object
Account ac1 = new Account(123);
System.out.println("The general account 1 account number is: "+ ac1.getAccountNumber());
ac1.deposit(23);
System.out.println("The money in general account 1 is: "+ ac1.getbalance());
ac1.withdraw(11);
System.out.println("The money in general account 1 after withdrawing is: "+ ac1.getbalance());
ac1.display();
//Second account object
Account ac2 = new Account(456);
System.out.println("The general account 1 account number is: "+ ac2.getAccountNumber());
ac2.deposit(23);
System.out.println("The money in general account 1 is: "+ ac2.getbalance());
ac2.withdraw(11);
System.out.println("The money in general account 1 after withdrawing is: "+ ac2.getbalance());
ac2.display();
//First savings account object
SavingsAccount sa1 = new SavingsAccount(789,56);
sa1.deposit(500);
sa1.addInterest();
System.out.println("The money in savings account 1 after adding interest is:" + sa1.getbalance());
sa1.withdraw(100);
System.out.println("The money in savings account 1 after withdrawing is:" + sa1.getbalance());
sa1.display();
//Second savings account object
SavingsAccount sa2 = new SavingsAccount(101,45);
sa2.deposit(300);
sa2.addInterest();
System.out.println("The money in savings account 2 after adding interest is:" + sa2.getbalance());
sa2.withdraw(100);
System.out.println("The money in savings account 2 after withdrawing is:" + sa2.getbalance());
sa2.display();
//First current account object
CurrentAccount ca1 = new CurrentAccount(112,200);
ca1.deposit(1000);
System.out.println("The money in current account 1 is:" + ca1.getbalance());
ca1.withdraw(200);
System.out.println("The money in current account 1 after withdrawing is:" + ca1.getbalance());
ca1.display();
//First current account object
CurrentAccount ca2 = new CurrentAccount(121,100);
ca2.deposit(1000);
System.out.println("The money in current account 2 is:" + ca2.getbalance());
ca2.withdraw(300);
System.out.println("The money in current account 2 after withdrawing is:" + ca2.getbalance());
ca2.display();
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.