Create a bank account class called Account using Java with methods deposit & wit
ID: 3773259 • Letter: C
Question
Create a bank account class called Account using Java with methods deposit & withdraw. The deposit method should accept attribute amount & update balance to the sum of amount & balance. Similarly, the withdraw method should accept the attribute amount & update the balance ‘balance – amount’ if balance > = amount or print an error otherwise. Below is the stub.
public class Account {
private double balance = 0;
public Account(double balance) {
this.balance = balance;
}
public void deposit(double amount) {
//Implementation here
}
public void withdraw(double amount) {
//Implementation here
}
}
Create 2 thread classes (1 for withdrawal and 1 for deposit). The stub is below:
public class WithdrawThread implements Runnable {
private Account account;
private double amount;
public WithdrawThread(Account account, double amount) {
//Set the account & balance
}
public void run() {
//make a withdrawal
}
}//end WithdrawThread class
public class DepositThread implements Runnable {
private Account account;
private double amount;
public DepositThread(Account account, double amount) {
//Set the account & balance
}
public void run() {
//make a deposit
}}
Create a main class below and run it. Provide output.
public class InternetBankingSystem {
public static void main(String [] args ) {
Account accountObject = new Account(100);
new Thread(new DepositThread(accountObject,30)).start();
new Thread(new DepositThread(accountObject,20)).start();
new Thread(new DepositThread(accountObject,10)).start();
new Thread(new WithdrawThread(accountObject,30)).start();
new Thread(new WithdrawThread(accountObject,50)).start();
new Thread(new WithdrawThread(accountObject,20)).start();
} // end main()
}
Now, update the account class using synchronization of methods (See chapter 5), to ensure that the balance is locked during the withdrawal & deposit methods. Run the main class again and provide output.
Explanation / Answer
Note: for the purpose of logging i printing balance after every withdraw and deposit for the i placed getBalance() in the Accounts class
public class Account {
private double balance = 0;
public Account(double balance) {
this.balance = balance;
}
public synchronized void deposit(double amount) {
// Implementation here
balance += amount;
}
/**
* @return the balance
*/
public double getBalance() {
return balance;
}
public synchronized void withdraw(double amount) {
// Implementation here
if (balance >= amount) {
balance -= amount;
} else {
System.out.println("Error !");
}
}
}
public class WithdrawThread implements Runnable {
private Account account;
private double amount;
public WithdrawThread(Account account, double amount) {
// Set the account & balance
this.account=account;
this.amount=amount;
}
public void run() {
// make a withdrawal
account.withdraw(amount);
System.out.println("After WithDraw:"+account.getBalance());
}
}// end WithdrawThread class
public class DepositThread implements Runnable {
private Account account;
private double amount;
public DepositThread(Account account, double amount) {
// Set the account & balance
this.account=account;
this.amount=amount;
}
public void run() {
// make a deposit
account.deposit(amount);
System.out.println("After Deposit:"+account.getBalance());
}
}
public class InternetBankingSystem {
public static void main(String[] args) {
Account accountObject = new Account(100);
new Thread(new DepositThread(accountObject, 30)).start();
new Thread(new DepositThread(accountObject, 20)).start();
new Thread(new DepositThread(accountObject, 10)).start();
new Thread(new WithdrawThread(accountObject, 30)).start();
new Thread(new WithdrawThread(accountObject, 50)).start();
new Thread(new WithdrawThread(accountObject, 20)).start();
} // end main()
}
OUTPUT:
After Deposit:130.0
After Deposit:150.0
After Deposit:160.0
After WithDraw:130.0
After WithDraw:60.0
After WithDraw:60.0