In: Computer Science
• 1. Write a java program using methods to find the information about a account holder at bank.
c) For every transaction make sure make use of his account balance.
// File: BankAccountTest2.java class BankAccount { private String accountNum; // the account number private double balance; // the amount on deposit public BankAccount(String acctNum, double initialBalance) { accountNum = acctNum; balance = initialBalance; } public void deposit(double amount) // note "mutator" method { double newBalance = balance + amount; balance = newBalance; // modifies instance var } public void withdraw(double amount) // note "mutator" method { double newBalance = balance - amount; balance = newBalance; // modifies instance var } public String getAccount() // note "accessor" method { return accountNum; // returns value of instance var } public double getBalance() // note "accessor" method { return balance; // returns value of instance var } public void transferFundsA(BankAccount destination, double amount) { destination.balance = destination.balance + amount; // note explicit use of this to reference instance variables of the // object for which the method was called this.balance = this.balance - amount; } public void transferFundsB(BankAccount destination, double amount) { destination.deposit(amount); // deposit to "destination" account this.withdraw(amount); // withdraw from this account } } //******************** end of BankAccount class definition ******************** /** A class to test the BankAccount2 class */ public class BankAccountTest2 { public static void main(String[] args) { BankAccount first = new BankAccount("1111111", 20000); BankAccount second = new BankAccount("2222222", 30000); System.out.printf("Account #%s has initial balance of $%.2f%n", first.getAccount(), first.getBalance()); System.out.printf("Account #%s has initial balance of $%.2f%n", second.getAccount(), second.getBalance()); first.transferFundsA(second, 5000); System.out.println("\nAfter \"first.transferFunds(second, 5000)\" ..."); System.out.printf("Account #%s has new balance of $%.2f%n", first.getAccount(), first.getBalance()); System.out.printf("Account #%s has new balance of $%.2f%n", second.getAccount(), second.getBalance()); second.transferFundsB(first, 10000); System.out.println("\nAfter \"second.transferFunds(first,10000)\" ..."); System.out.printf("Account #%s has new balance of $%.2f%n", first.getAccount(), first.getBalance()); System.out.printf("Account #%s has new balance of $%.2f%n", second.getAccount(), second.getBalance()); } } /**************************** OR ***********************************/ public class AccountTesting implements Runnable { private Account acct = new Account(); public static void main(String[] args) { AccountTesting r = new AccountTesting(); Thread one = new Thread(r); Thread two = new Thread(r); one.setName("Ranjeet"); two.setName("Reema"); one.start(); two.start(); } @Override public void run() { for (int x = 0; x < 5; x++) { makeWithdrawal(10); if (acct.getBalance() < 0) { System.out.println("account is overdrawn!"); } } } private void makeWithdrawal(int amt) { if (acct.getBalance() >= amt) { System.out.println(Thread.currentThread().getName() + " is going to withdraw"); try { Thread.sleep(100); } catch (InterruptedException ex) { } acct.withdraw(amt); System.out.println(Thread.currentThread().getName() + " completes the withdrawal"); } else { System.out.println("Not enough in account for " + Thread.currentThread().getName() + " to withdraw " + acct.getBalance()); } } } class Account { private int balance = 50; public int getBalance() { return balance; } public void withdraw(int amount) { balance = balance - amount; } }