-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBankAccountTest.java
46 lines (39 loc) · 1.94 KB
/
BankAccountTest.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
import java.util.ArrayList;
public class BankAccountTest {
public static void testWithdraw(int numberOfTests) {
BankAccount account = new BankAccount("Bob", "43234522");
ArrayList<Double> balancesAfterSuccessfulWithdrawals = new ArrayList<>();
ArrayList<Double> balancesAfterFailedWithdrawals = new ArrayList<>();
for (int i = 0; i < numberOfTests; i++) {
int amountToWithdraw = (int) (Math.random() * 5000); // Range [0, 5000[
double amountWithdrawn = account.withdraw(amountToWithdraw);
double balance = account.getBalance();
if (amountWithdrawn == 0) {
balancesAfterFailedWithdrawals.add(balance);
} else {
balancesAfterSuccessfulWithdrawals.add(balance);
}
}
// Print the contents of both lists
System.out.println("balancesAfterSuccessfulWithdrawals: " + balancesAfterSuccessfulWithdrawals);
System.out.println("balancesAfterFailedWithdrawals: " + balancesAfterFailedWithdrawals);
// Check for any negative balances
checkForNegativeValues(balancesAfterSuccessfulWithdrawals, "balancesAfterSuccessfulWithdrawals");
checkForNegativeValues(balancesAfterFailedWithdrawals, "balancesAfterFailedWithdrawals");
}
// Helper method to check for negative values in a list
private static void checkForNegativeValues(ArrayList<Double> balances, String listName) {
boolean negativeFound = false;
for (double balance : balances) {
if (balance < 0) {
negativeFound = true;
System.out.println("Warning: Negative balance detected in " + listName + ": " + balance);
}
}
if (negativeFound) {
System.out.println("Warning: Negatives found in the " + listName + " array");
} else {
System.out.println("No Negatives found in the " + listName + " array");
}
}
}