Question
(hello i have already asked this qustion but this time i just need my code revised. ps please no hand written answers. thank you!!) (java)
(hello i have already asked this qustion but this time i just need my code revised. ps please no hand written answers. thank you!!) (java)
Bank holds an array of Accounts. Each Account has an associated Client. Each Client can have more than one Account.
An Account class should have 1. a constructor public Account(Client client, int accountNumber, int balance)
2. methods to allow for basic banking operations that are thread-safe, make sure to Log each transaction getBalance() withdraw(int amount) deposit(int amount)
3. method to display account information public String toString()
A Client class should have 1. a constructor public Client(String name) 2. methods to access, modify and display the name
For the purpose of this lab we will simulate the fact that a bank client can have multiple accounts.
A Bank class should have 1. a constructor public Bank(int accountsCount, int clientsCount) Use a random number generator to assign an account ID within the range from 0 to accountsCount.
Use a random number generator to assign a client ID within the range from 0 to clientsCount. Use the name ("Person " + client ID) as the name of the client.
Use a random number generator to assign initial balance from 0 to a 100
2. a method to get a random account public Account getRandomAccount()
3. a method to print the summary of all accounts public void printSummary()
Create a multi-threaded program that emulates the banking operation
1. Create a Bank with 5 accounts and 2 clients 2. Within the main program create three threads of execution each simulating a concurrently operating bank branch or ATM having access to account information. Within each thread have a loop of 10 following operation a. Get a random account b. sleep for random time from 0 to 100 milliseconds c. check account balance and withdraw random (10-200) amount if balance > 0 d. sleep for random time from 0 to 100 milliseconds e. check account balance f. sleep for random time from 0 to 100 milliseconds g. deposit random (10-200) amount
//code
import java.util.ArrayList;
public class Bank {
private Array
public Bank(int accountsnumber);//number of accounts {
//accounts = new ArrayList
@Override public void run() { //for (int a = 1; a
for(int a = 0; a
} }
public Account get.Account() {
return accounts.get((int) (calculation.random() * accounts.limit()));
}
public void printSummary() {
for (Account a : accounts)
System.out.println(a.toString()); /* }//declaration of mutithreading
}class Multithread extends Thread
{
public void account()
{
//public Account(Client); client; int accountNumber;
int balance;
try
{
// Display the thread that is running
System.out.println ("Thread in multithreading " + Thread.currentThread().getId() +
" is running");
}
catch (Exception e)
{ getBalance(); withdraw(int amount); deposit(int amount); // Throwing an exception System.out.println ("Exception is found"); } } } */
.println(client.getName()
+ " "
+ check account balance and withdraw random (10-200) amount if balance > 0"
+ number);
}
System.out.println(client.getName() + " withdrawal amount: "
+ balance + " from account " + number);
} catch (Exception e) {
e.printStackTrace();
}
}
public synchronized void deposit(Client client, int bal) {
try {
if (bal > 0) { public static void main(String args[]){ System.out.println(client.getName() + " "
+ "trying to deposit on account " + number);
try {
Thread.sleep(100);//sleep for random time from 0 to 100 milliseconds
} catch (Exception e) {
e.printStackTrace();
}
balance = balance + bal;
System.out.println(client.getName() + " "
+ "checking out the balance deposit on account " + number);
} else {
System.out.println(client.getName() + " "
+ " deposit random(10-200)amount on account "
+ number);
}
System.out.println(client.getName() + " deposited " + balance
+ " on account " + number);
} catch (Exception e) {
e.printStackTrace();
} }
public class Main extends Thread implements Runnable {
private static final int CLIENT_NUMBER = 2;
private static final int BANK_ACCOUNTS = 5;
private static final int ACCOUNTS_COUNT = 2;
private static final int RANDOM_MIN = 10;
private static final int RANDOM_MAX = 10;
private static final int RANDOM_MIN_AMOUNT = 10;
private static final int RANDOM_MAX_AMOUNT = 200;
private static Bank bank = new Bank(ACCOUNTS_NUMBER);
private Client client;
public Main(Client c) {
client = c;
}
private int randomAmount() {
return (new Random()).nextInt((RANDOM_MAX_AMOUNT - RANDOM_MIN_AMOUNT) + 1)
+ RAND_MIN_AMOUNT;
}
private int randSleepTime() { //sleep for random time from 0 to 100 milliseconds
return (new Random()).nextInt((RANDOM_MAX - RANDOM_MIN) + 1) + RANDOM_MIN;
}
public static void main(String[] args) {
for (int a = 0; a < CLIENT_NUMBER; a++) {
Main threads1 = new Main(new Client("Person " + i));
threads1.start(); //thread starts
}
}
private void clientprogress() {
try {
Thread.sleep(randomSleepTime());
} catch (InterruptedException ex) {
client.getAccount(Main.class.getName());
}
}
//overriding
public void run() {
for (int a= 0; a < CLIENT_ITERATIONS; a++) {
Account acc = bank.getRandomAccount();
clientWork();
account.withdraw(client, randomAmount());
clientWork();
account.deposit(client, randomAmount());
}
System.out.println("#Account balance: " + Account.getBalance());
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started