Repository navigation
Expand file tree
/
Copy pathAccessAccount.java
More file actions
133 lines (128 loc) · 5.12 KB
/
Copy pathAccessAccount.java
File metadata and controls
133 lines (128 loc) · 5.12 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
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.Scanner;
/**
* The AccessAccount class opens an existing account (by name and password) so money can be
* deposited or withdrawn. It extends Account and overrides deposit() and withdraw() so every
* transaction is printed and logged in balanceSheet.txt. accountInfo.txt is never changed.
*/
public class AccessAccount extends Account {
/**
* Constructor: builds an AccessAccount from data already saved in the files.
* @param name The name of the account holder.
* @param accountNumber The account number.
* @param balance The current balance from balanceSheet.txt.
*/
public AccessAccount(String name, int accountNumber, double balance) {
super(name, accountNumber, balance);
}
/**
* Overrides Account's type.
* @return "Access".
*/
@Override
public String getAccountType() {
return "Access";
}
/**
* Overrides Account's deposit: uses Account's rules, then logs the deposit and confirms it.
* @param amount The amount to deposit.
* @return true if the deposit went through, false otherwise.
*/
@Override
public boolean deposit(double amount) {
if (!super.deposit(amount)) {
return false;
}
recordTransaction("deposit", amount);
System.out.println("The amount " + amount + " has been deposited to the account " + accountNumber);
return true;
}
/**
* Overrides Account's withdraw: uses Account's rules, then logs the withdrawal and confirms it.
* @param amount The amount to withdraw.
* @return true if the withdrawal went through, false otherwise.
*/
@Override
public boolean withdraw(double amount) {
if (!super.withdraw(amount)) {
return false;
}
recordTransaction("withdraw", amount);
System.out.println("WITHDRAW");
System.out.println(today());
System.out.println("You have withdrawn " + amount + ". Now you have " + balance + " in your account");
return true;
}
/**
* Finds the saved account line for a name and password in accountInfo.txt.
* @param name The account holder's name (any capitalization).
* @param password The account's password.
* @return The saved {name, password, accountID, dateOfBirth}, or null if no account matches.
* @throws IOException If an I/O error occurs while reading the file.
*/
private static String[] findAccount(String name, String password) throws IOException {
File file = new File(ACCOUNT_FILE);
if (!file.exists()) {
return null;
}
try (BufferedReader reader = new BufferedReader(new FileReader(file))) {
reader.readLine(); // skip header
String line;
while ((line = reader.readLine()) != null) {
String[] parts = line.split("\t");
if (parts.length < 4) continue;
if (parts[0].equalsIgnoreCase(name) && parts[1].equals(password)) {
return parts;
}
}
}
return null;
}
/**
* Finds an account's current balance: the resulting balance on its last line in balanceSheet.txt.
* @param accountNumber The account number to look for.
* @return The current balance, or 0.0 if the account has no transactions.
* @throws IOException If an I/O error occurs while reading the file.
*/
private static double findBalance(int accountNumber) throws IOException {
File file = new File(BALANCE_FILE);
if (!file.exists()) {
return 0.0;
}
double balance = 0.0;
try (BufferedReader reader = new BufferedReader(new FileReader(file))) {
reader.readLine(); // skip header
String line;
while ((line = reader.readLine()) != null) {
String[] parts = line.split("\t");
if (parts.length < 6) continue;
if (Integer.parseInt(parts[1]) == accountNumber) {
balance = Double.parseDouble(parts[5]); // keep the latest one
}
}
}
return balance;
}
/**
* Opens an existing account by prompting for the name and password.
* @param in The Scanner object for reading user input.
* @return The AccessAccount, or null if the name or password is wrong.
* @throws IOException If an I/O error occurs while reading the files.
*/
public static AccessAccount login(Scanner in) throws IOException {
System.out.print("Enter name: ");
String name = in.nextLine().trim();
System.out.print("Enter password: ");
String password = in.nextLine().trim();
String[] saved = findAccount(name, password);
if (saved == null) {
System.out.println("Name or password is incorrect.");
return null;
}
int accountNumber = Integer.parseInt(saved[2]);
return new AccessAccount(saved[0], accountNumber, findBalance(accountNumber)); // use the saved name
}
}