-
Notifications
You must be signed in to change notification settings - Fork 42
Expand file tree
/
Copy pathTransactionProcessor.java
More file actions
125 lines (105 loc) · 4.25 KB
/
TransactionProcessor.java
File metadata and controls
125 lines (105 loc) · 4.25 KB
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
package Banking;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.List;
import java.util.Objects;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
public class TransactionProcessor implements Runnable {
private final BankAccount account;
private final String fileName;
private final List<BankAccount> allAccounts;
private final Lock logLock = new ReentrantLock();
private static int transactionCounter = 0;
private static final Lock counterLock = new ReentrantLock();
public TransactionProcessor(BankAccount account, String fileName, List<BankAccount> allAccounts) {
this.account = account;
this.fileName = fileName;
this.allAccounts = allAccounts;
}
@Override
public void run() {
logWithLock("Starting processing for account " + account.getId());
try (InputStreamReader is = new InputStreamReader(
Objects.requireNonNull(getClass().getClassLoader().getResourceAsStream(fileName)));
BufferedReader reader = new BufferedReader(is)) {
String line;
while ((line = reader.readLine()) != null) {
processTransactionLine(line.trim());
}
} catch (Exception e) {
logWithLock("Error processing file " + fileName + ": " + e.getMessage());
e.printStackTrace();
} finally {
logWithLock("Completed processing for account " + account.getId());
}
}
private void processTransactionLine(String line) {
if (line.isEmpty()) return;
String[] parts = line.split("\\s+");
String transactionType = parts[0];
try {
switch (transactionType) {
case "Deposit":
int depositAmount = Integer.parseInt(parts[1]);
account.deposit(depositAmount);
logTransaction(transactionType, depositAmount);
break;
case "Withdraw":
int withdrawAmount = Integer.parseInt(parts[1]);
account.withdraw(withdrawAmount);
logTransaction(transactionType, withdrawAmount);
break;
case "Transfer":
int targetIndex = Integer.parseInt(parts[1]) - 1;
int transferAmount = Integer.parseInt(parts[2]);
BankAccount target = allAccounts.get(targetIndex);
account.transfer(target, transferAmount);
logTransfer(transferAmount, target);
break;
default:
logWithLock("Unknown transaction type: " + transactionType);
}
incrementTransactionCounter();
} catch (NumberFormatException e) {
logWithLock("Invalid number format in transaction: " + line);
} catch (IndexOutOfBoundsException e) {
logWithLock("Invalid target account in transfer: " + line);
} catch (Exception e) {
logWithLock("Error processing transaction: " + line + " - " + e.getMessage());
}
}
private void logTransaction(String type, int amount) {
logWithLock(String.format("Processed %s of %d for account %d. New balance: %d",
type, amount, account.getId(), account.getBalance()));
}
private void logTransfer(int amount, BankAccount target) {
logWithLock(String.format("Transfer %d from account %d to %d. Balances: %d -> %d",
amount, account.getId(), target.getId(),
account.getBalance(), target.getBalance()));
}
private void logWithLock(String message) {
logLock.lock();
try {
System.out.println("[Thread-" + Thread.currentThread().getId() + "] " + message);
} finally {
logLock.unlock();
}
}
private void incrementTransactionCounter() {
counterLock.lock();
try {
transactionCounter++;
} finally {
counterLock.unlock();
}
}
public static int getTransactionCount() {
counterLock.lock();
try {
return transactionCounter;
} finally {
counterLock.unlock();
}
}
}