-
Notifications
You must be signed in to change notification settings - Fork 0
/
Depositor.java
70 lines (59 loc) · 2.1 KB
/
Depositor.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
/**
* Name: Jose Tapizquent
* Course: CNT 4714 Spring 2021
* Assignment Title: Project 2 - Synchronized, Cooperating Threads Under Locking
* Due Date: February 14, 2021
*
* Depositor.java
*/
import java.util.Random;
public class Depositor implements Runnable {
/**
* An actor that performs deposits with the help of the [accountManager]
*
* @param accountManager The account manager that allows access to the account
* @param threadNumber The number of the current thread. Only for displaying
* and identification purposes
*/
public Depositor(AccountManager accountManager, int threadNumber) {
this.accountManager = accountManager;
this.name = "Dep" + (threadNumber + 1);
}
private static Random randomNumberGenerator = new Random();
private AccountManager accountManager;
private String name;
@Override
public void run() {
while (true) {
makeDeposit();
}
}
/**
* Performs a deposit of a random amount between $1-250 and prints the
* transaction along with the account's current balance
*/
public void makeDeposit() {
int amountToDeposit = randomNumberGenerator.nextInt(250) + 1;
int currentBalance = accountManager.deposit(amountToDeposit);
printDepositAndCurrentBalance(amountToDeposit, currentBalance);
try {
Thread.sleep(randomNumberGenerator.nextInt(3000));
} catch (InterruptedException e) {
e.printStackTrace();
}
}
public String getName() {
return name;
}
/**
* Prints transaction entry along with account's currentBalance
*
* @param depositAmount The amount deposited
* @param currentBalance The currentBalance of the account returned by deposit
* function
*/
private void printDepositAndCurrentBalance(int depositAmount, int currentBalance) {
System.out.print("Thread " + name + " deposited $" + depositAmount + "\t\t\t\t\t [+] Current balance: "
+ currentBalance + "\n");
}
}