Repository navigation
Expand file tree
/
Copy pathBankingException.java
More file actions
59 lines (50 loc) · 1.75 KB
/
Copy pathBankingException.java
File metadata and controls
59 lines (50 loc) · 1.75 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
public class BankAccount {
private final String accountNo;
private final String holderName;
protected double balance;
public BankAccount(String accountNo, String holderName, double openingBalance) throws BankingException {
if (accountNo == null || accountNo.isBlank()) {
throw new BankingException("Account number cannot be empty");
}
if (holderName == null || holderName.isBlank()) {
throw new BankingException("Holder name cannot be empty");
}
if (openingBalance < 0) {
throw new BankingException("Opening balance cannot be negative");
}
this.accountNo = accountNo;
this.holderName = holderName;
this.balance = openingBalance;
}
public String getAccountNo() {
return accountNo;
}
public String getHolderName() {
return holderName;
}
public double getBalance() {
return balance;
}
public void deposit(double amount) throws BankingException {
if (amount <= 0) {
throw new BankingException("Deposit amount must be positive");
}
balance += amount; // ✅ CORRECT
}
public void withdraw(double amount) throws BankingException {
if (amount <= 0) {
throw new BankingException("Withdraw amount must be positive");
}
if (amount > balance) {
throw new BankingException("Insufficient balance");
}
balance -= amount;
}
public String getAccountType() {
return "GENRIC";
}
public String summary() {
return String.format("[%s] AccNo=%s, Holder=%s, Balance=%.2f", getAccountType(), accountNo, holderName,
balance);
}
}