-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBankAccount.java
More file actions
61 lines (52 loc) · 1.9 KB
/
BankAccount.java
File metadata and controls
61 lines (52 loc) · 1.9 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
public class BankAccount {
private String accountNumber;
private double balance;
public BankAccount(String accountNumber, double initialBalance) {
if (!verify(accountNumber)) {
throw new BankException("Invalid account number format or check digits.");
}
if (initialBalance < 0) {
throw new BankException("Initial balance must be greater than or equal to zero.");
}
this.accountNumber = accountNumber;
this.balance = initialBalance;
}
public void deposit(double amount) {
if (amount < 0) {
throw new BankException("Deposit amount cannot be negative.");
}
balance += amount;
}
public void withdraw(double amount) {
if (amount < 0) {
throw new BankException("Withdrawal amount cannot be negative.");
}
if (balance < amount) {
throw new BankException("Insufficient funds :(");
}
balance -= amount;
}
public double getBalance() {
return balance;
}
private boolean verify(String accountNumber) {
if (accountNumber.length() != 8) {
return false;
}
String modifiedAccountNumber = accountNumber.substring(4) + accountNumber.substring(0, 4);
StringBuilder numericAccountNumber = new StringBuilder();
for (char character : modifiedAccountNumber.toCharArray()) {
if (Character.isLetter(character)) {
numericAccountNumber.append((int) character - 55);
} else {
numericAccountNumber.append(character);
}
}
long accountNumberValue = Long.parseLong(numericAccountNumber.toString());
return accountNumberValue % 11 == 1;
}
@Override
public String toString() {
return String.format("BankAccount[accountNumber=%s, balance=$%.2f]", accountNumber, balance);
}
}