(Java) Bank account access simulation: The bank account should have withdraw and
ID: 3717665 • Letter: #
Question
(Java) Bank account access simulation:
The bank account should have withdraw and deposit methods (subtract and add 1 from the balance respectively) You should have many (in the thousands) threads representing account users who, upon invocation, sleep for a random number of seconds, and then withdraw or deposit.
There should be an associated GUI that allows the user to start the simulation and it should have a representation of the current amount in the bank account, continually updating as the simulation proceeds.
Take note of how the synchronized keyword and the Thread.sleep() and Thread.join() methods are being used, you will need them.
Explanation / Answer
//Java code for the simulation
package com.bank;
import java.util.Scanner;
import java.util.concurrent.ConcurrentHashMap;
public class BankSimulation {
// Concurrent HashMap for having user account mapped with user balance
public static ConcurrentHashMap<String, Integer> bankAccounts = new ConcurrentHashMap<String, Integer>();
public static void main(String args[]) {
// You can use a while loop for simulating multiple transactions at a
// time.
// while (true) {
Thread newUserThread = new Thread() {
public void run() {
newTransaction();
}
};
newUserThread.start();
try {
newUserThread.join(10000);
} catch (InterruptedException e) {
e.printStackTrace();
}
// }
}
// method to open account
public static boolean openAccount(String userId, int money) {
synchronized (userId) {
if (bankAccounts.containsKey(userId)) {
System.out.println(
"Hey " + userId + ", " + " your account b is already opened with us. Account Balance : "
+ " Rs " + bankAccounts.get(userId));
return false;
} else {
bankAccounts.put(userId, money);
System.out
.println("Hey " + userId + ", " + " you have created a new account with us. Congratulations.");
}
return true;
}
}
// method to view balance
public static boolean viewBalance(String userId) {
synchronized (userId) {
if (bankAccounts.containsKey(userId)) {
System.out.println(
"Hey " + userId + ", " + " your account balance is : " + " Rs " + bankAccounts.get(userId));
return true;
} else {
System.out.println("Hey " + userId + ", " + " you dont have an account with us."
+ "Please open a new account with us.");
}
return false;
}
}
// method to add money
public static void addMoney(String userId) {
synchronized (userId) {
int money = bankAccounts.get(userId);
money++;
bankAccounts.put(userId, money);
System.out.println(
"Hey " + userId + ", " + " your account balance is credited with Rs 1. Your new account balance is "
+ " Rs " + bankAccounts.get(userId));
}
}
// method to subtract money
public static void subtractMoney(String userId) {
synchronized (userId) {
if (bankAccounts.get(userId) > 0) {
int money = bankAccounts.get(userId);
money--;
bankAccounts.put(userId, money);
System.out.println("Hey " + userId + ", "
+ " your account balance is debited with Rs 1.Your new account balance is " + " Rs "
+ bankAccounts.get(userId));
} else {
System.out.println("Hey " + userId + ", " + " you dont have enough balance for this transaction."
+ "Please credit in you account.");
}
}
}
// new transaction starts
public static void newTransaction() {
System.out.println("Welcome to Ultimate Bank");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("To open an account press 0,or press 1 to view your balance.");
Scanner inputScanner = new Scanner(System.in);
int value = inputScanner.nextInt();
String name = null;
if (value == 0) {
System.out.println("Please enter userName:");
inputScanner.nextLine();
name = inputScanner.nextLine();
System.out.println("Please enter amount : ");
int amount = inputScanner.nextInt();
openAccount(name, amount);
System.out.println("Congratulations You have opened a new account with us : ");
viewBalance(name);
} else {
System.out.println("Please enter userName: to view balance.");
name = inputScanner.nextLine();
viewBalance(name);
}
System.out.println("To credit account press 0,or press 1 for debit.");
int option = inputScanner.nextInt();
if (option == 0) {
addMoney(name);
} else {
subtractMoney(name);
}
System.out.println("Your Transaction is Complete. Thank You!");
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.