-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAccount.java
More file actions
144 lines (122 loc) · 5.15 KB
/
Account.java
File metadata and controls
144 lines (122 loc) · 5.15 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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
package models;
import java.io.*;
import java.util.ArrayList;
import java.util.List;
public abstract class Account {
protected int accountNumber;
protected String name;
protected String email;
protected String uniId;
protected int pin;
protected double balance;
protected String cardType;
protected double withdrawLimit;
protected List<Transaction> transactions;
protected String status; // PENDING or ACTIVE
public Account(int accountNumber, String name, String email, String uniId,
int pin, String cardType, double withdrawLimit) {
this.accountNumber = accountNumber;
this.name = name;
this.email = email;
this.uniId = uniId;
this.pin = pin;
this.cardType = cardType;
this.withdrawLimit = withdrawLimit;
this.balance = 0.0;
this.transactions = new ArrayList<>();
this.status = "PENDING";
}
// ---------------- GETTERS & SETTERS ----------------
public int getAccountNumber() { return accountNumber; }
public String getName() { return name; }
public String getEmail() { return email; }
public String getCardType() { return cardType; }
public String getStatus() { return status; }
public double getBalance() { return balance; }
public void setStatus(String s) { status = s; }
public void setBalance(double b) { this.balance = b; }
// ---------------- AUTHENTICATION ----------------
public boolean validatePin(int inputPin) {
return this.pin == inputPin;
}
// ---------------- BANKING OPERATIONS ----------------
// 1. Method Overloading (Polymorphism)
public void deposit(double amount) {
deposit(amount, "DEPOSIT");
}
public void deposit(double amount, String customNote) {
if (amount > 0) {
balance += amount;
addTransaction(customNote, amount);
appendTransactionToFile(new Transaction(customNote, amount));
}
}
// 2. Custom Exception Handling
public void withdraw(double amount) throws exceptions.InsufficientFundsException {
if (amount > balance) {
throw new exceptions.InsufficientFundsException("Error: Insufficient funds. Balance is " + balance);
} else if (amount > withdrawLimit) {
throw new exceptions.InsufficientFundsException("Error: Exceeds " + cardType + " limit of " + withdrawLimit);
} else {
balance -= amount;
addTransaction("WITHDRAW", amount);
appendTransactionToFile(new Transaction("WITHDRAW", amount));
}
}
public void transfer(Account target, double amount) throws exceptions.InsufficientFundsException {
// Logic to prevent self-transfer
if (this.accountNumber == target.accountNumber) {
throw new exceptions.InsufficientFundsException("Error: Cannot transfer money to yourself.");
}
// Logic to check funds
if (amount > balance) {
throw new exceptions.InsufficientFundsException("Transfer failed: Insufficient funds.");
} else {
this.balance -= amount;
target.balance += amount;
this.addTransaction("TRANSFER OUT to " + target.name, amount);
target.addTransaction("TRANSFER IN from " + this.name, amount);
this.appendTransactionToFile(new Transaction("TRANSFER OUT to " + target.name, amount));
target.appendTransactionToFile(new Transaction("TRANSFER IN from " + this.name, amount));
}
}
public void checkBalance() {
System.out.println("Account: " + accountNumber + " | Type: " + cardType);
System.out.println("Current Balance: " + balance);
}
protected void addTransaction(String type, double amount) {
transactions.add(new Transaction(type, amount));
}
public void printHistory() {
List<Transaction> list = readTransactionFile();
for (Transaction t : list) System.out.println(t);
}
// ---------------- PERSISTENCE HELPERS ----------------
public String toCSVLine() {
return accountNumber + "|" + name + "|" + email + "|" + uniId + "|" +
pin + "|" + cardType + "|" + balance + "|" + status;
}
public void appendTransactionToFile(Transaction t) {
String fileName = "transactions_" + accountNumber + ".txt";
try (FileWriter fw = new FileWriter(fileName, true);
BufferedWriter bw = new BufferedWriter(fw)) {
bw.write(t.toString());
bw.newLine();
} catch (IOException e) {
System.out.println("Error saving transaction file: " + e.getMessage());
}
}
public List<Transaction> readTransactionFile() {
List<Transaction> list = new ArrayList<>();
String fileName = "transactions_" + accountNumber + ".txt";
File f = new File(fileName);
if (!f.exists()) return list;
try (BufferedReader br = new BufferedReader(new FileReader(fileName))) {
String line;
while ((line = br.readLine()) != null) {
list.add(new Transaction(line));
}
} catch (IOException e) { }
return list;
}
}