-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUser.java
More file actions
54 lines (48 loc) · 1.68 KB
/
User.java
File metadata and controls
54 lines (48 loc) · 1.68 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
import java.util.ArrayList;
public class User extends Person {
int age;
double balance;
ArrayList<TransactionLog> transactionLogs;
public User(String name, int age, String username, String password, double balance) {
super(name, username, password); // Call to the superclass constructor
this.age = age;
this.balance = balance;
this.transactionLogs = new ArrayList<>();
}
public void deposit(double amount) {
if (amount <= 0) {
System.out.println("Deposit amount must be positive.");
return;
}
balance += amount;
transactionLogs.add(new TransactionLog(username, "Deposit", amount, balance));
System.out.println("Deposited " + amount + " successfully.");
}
public void withdraw(double amount) {
if (amount <= 0) {
System.out.println("Withdrawal amount must be positive.");
return;
}
if (balance >= amount) {
balance -= amount;
transactionLogs.add(new TransactionLog(username, "Withdraw", amount, balance));
System.out.println("Withdrawn " + amount + " successfully.");
} else {
System.out.println("Insufficient balance.");
}
}
public void viewTransactionLogs() {
if (transactionLogs.isEmpty()) {
System.out.println("No transaction logs available.");
} else {
for (TransactionLog log : transactionLogs) {
System.out.println(log);
}
}
}
@Override
public void displayDetails() {
super.displayDetails();
System.out.println("Age: " + age + ", Balance: " + balance);
}
}