-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAccount.java
More file actions
67 lines (51 loc) · 1.58 KB
/
Account.java
File metadata and controls
67 lines (51 loc) · 1.58 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
import java.io.Serializable;
import java.util.*;
public abstract class Account implements Serializable {
protected String accountNumber;
protected double balance;
protected List<Transaction> transactions;
public Account() {
this.accountNumber = UUID.randomUUID().toString();
this.balance = 0.0;
this.transactions = new ArrayList<>();
}
public String getAccountNumber() {
return accountNumber;
}
public boolean deposit(double amount) {
if (amount <= 0) {
System.out.println("Invalid deposit amount.");
return false;
}
balance += amount;
transactions.add(new Transaction(TransactionType.DEPOSIT, amount));
System.out.println("Deposit successful.");
return true;
}
public boolean withdraw(double amount) {
if (amount <= 0) {
System.out.println("Invalid withdrawal amount.");
return false;
}
if (amount > balance) {
System.out.println("Insufficient balance.");
return false;
}
balance -= amount;
transactions.add(new Transaction(TransactionType.WITHDRAW, amount));
System.out.println("Withdrawal successful.");
return true;
}
public double getBalance() {
return balance;
}
public void printStatement() {
if (transactions.isEmpty()) {
System.out.println("No transactions available.");
return;
}
for (Transaction t : transactions) {
System.out.println(t);
}
}
}