This repository has been archived by the owner on Jul 31, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Account.java
188 lines (172 loc) · 7.53 KB
/
Account.java
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
package ATMjavafx;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Scanner;
/*
1. Load from text file and Create new account object, add them to array list
2. Save all object from list and save them to text file
3. Access & Edit user's information
4. Record statistics
*/
public class Account {
//this array will contain Account objects
public static ArrayList<Account> accountList = new ArrayList<>();
static File accountsFile = new File("userData.txt"),
statisticFile = new File("statistic.txt");
public String userName;
private String userPassword, userRealName, userGender;
private long userBalance;
private int userAge;
private long
withdrawalTotal, withdrawalTimes, withdrawalAverage, withdrawalMax, withdrawalMin,
depositTotal, depositTimes, depositAverage, depositMax, depositMin ;
public Account(){}
public Account(String userName,String userPassword,long userBalance, String userRealName,int userAge,String userGender,
long withdrawalTotal, long withdrawalTimes, long withdrawalAverage, long withdrawalMax, long withdrawalMin,
long depositTotal, long depositTimes, long depositAverage, long depositMax, long depositMin)
{
this.userName = userName;
this.userPassword = userPassword;
this.userBalance = userBalance;
this.userRealName = userRealName;
this.userAge = userAge;
this.userGender = userGender;
this.depositTotal = depositTotal;
this.depositTimes = depositTimes;
this.depositAverage = depositAverage;
this.depositMax = depositMax;
this.depositMin = depositMin;
this.withdrawalTotal = withdrawalTotal;
this.withdrawalTimes = withdrawalTimes;
this.withdrawalAverage = withdrawalAverage;
this.withdrawalMax = withdrawalMax;
this.withdrawalMin = withdrawalMin;
}
public void changeUserInfo(String newUserName, String newPassword, String newUserRealName,String newGender, int newAge){
this.userName = newUserName;
this.userPassword = newPassword;
this.userRealName = newUserRealName;
this.userGender = newGender;
this.userAge = newAge;
}
public void deleteThisUser(){accountList.remove(this);}
public void deposit(long moneyToDeposit){
this.userBalance += moneyToDeposit;
updateStatisticsDeposit(moneyToDeposit);
}
public void withdraw(long moneyToWithdraw) {
this.userBalance -= moneyToWithdraw;
updateStatisticsWithdraw(moneyToWithdraw);
}
public String getUserPassword(){return this.userPassword;}
public String getUserRealName(){return this.userRealName;}
public String getUserGender(){return this.userGender;}
public int getUserAge(){return this.userAge;}
public long getUserBalance(){return this.userBalance;}
public static void loadUserData(){
/*
this method loads the user data from a text file, each line will be recorded as an attributes of an object
after certain number of line (9 lines), one user is created and added to accountList(ArrayList)
the loop continues untill it reaches the last blank line
*/
String currentUserName, currentUserPassword, currentUserRealName, currentUserGender,
withdrawString, depositString;
int currentUserAge;
long currentUserBalance;
try {
Scanner input = new Scanner(accountsFile);
while (input.hasNextLine()) {
currentUserName = input.next();
currentUserPassword = input.next();
currentUserBalance = Long.parseLong(input.next()) ;
currentUserRealName = input.next().replace("_", " ");
currentUserAge = Integer.parseInt(input.next());
currentUserGender = input.next();
depositString = input.next();
withdrawString = input.next();
String userEnd = input.next();
String[] depositStringParts = depositString.split("-");
String[] withdrawStringParts = withdrawString.split("-");
long[][] depositAndWithdraw = new long[2][5];
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 5; j++) {
depositAndWithdraw[0][j] = Long.parseLong(depositStringParts[j]);
depositAndWithdraw[1][j] = Long.parseLong(withdrawStringParts[j]);
}
}
// creat users
if (currentUserName.length() > 1){ // in case it reaches the last blank line
try {
accountList.add(new Account(currentUserName, currentUserPassword,
currentUserBalance,currentUserRealName, currentUserAge, currentUserGender,
depositAndWithdraw[0][0], depositAndWithdraw[0][1], depositAndWithdraw[0][4], depositAndWithdraw[0][2], depositAndWithdraw[0][3],
depositAndWithdraw[1][0], depositAndWithdraw[1][1], depositAndWithdraw[1][4], depositAndWithdraw[1][2], depositAndWithdraw[1][3]
));
} catch (Exception e) { System.err.println("Can not create accounts");}
}
}
} catch (FileNotFoundException | NumberFormatException e) {}
}
public static void saveUserData(){
/*
this method wil get all objects from array and save it to the same text file as it loads
by overwriting the current data in this text file.
it writes these data in the same order as it loads(each account has 9 lines)
*/
try {
FileWriter writer = new FileWriter(accountsFile, false);
for(Account checkingAccount : accountList) {
writer.write(checkingAccount.userName +System.lineSeparator());
writer.write(checkingAccount.userPassword + System.lineSeparator());
writer.write(checkingAccount.userBalance + System.lineSeparator());
writer.write(checkingAccount.userRealName.replace(" ", "_") + System.lineSeparator());
writer.write(checkingAccount.userAge + System.lineSeparator());
writer.write(checkingAccount.userGender + System.lineSeparator());
writer.write(
checkingAccount.depositTotal+"-"+
checkingAccount.depositTimes+"-"+
checkingAccount.depositMax+"-"+
checkingAccount.depositMin+"-"+
checkingAccount.depositAverage
+System.lineSeparator()
);
writer.write(
checkingAccount.withdrawalTotal+"-"+
checkingAccount.withdrawalTimes+"-"+
checkingAccount.withdrawalMax+"-"+
checkingAccount.withdrawalMin+"-"+
checkingAccount.withdrawalAverage
+System.lineSeparator()
);
writer.write("------------------" + System.lineSeparator());
}
writer.close();
} catch (Exception e) {
System.err.println("can not save the file.");
}
}
public static void printCurrentUser(){
System.out.println("Current accounts:");
for (Account currentAccount : accountList) {
System.out.println(" "+currentAccount.userName);
}
}
//statistic functions
public void updateStatisticsDeposit(long amount){
this.depositTotal += amount;
this.depositTimes++;
this.depositAverage = depositTotal / depositTimes;
if(amount > this.depositMax){depositMax = amount;}
if(amount < this.depositMin){depositMin = amount;}
}
public void updateStatisticsWithdraw(long amount){
this.withdrawalTotal += amount;
this.withdrawalTimes++;
this.withdrawalAverage = withdrawalTotal / withdrawalTimes;
if(amount > this.withdrawalMax){withdrawalMax = amount;}
if(amount < this.withdrawalMin){withdrawalMin = amount;}
}
}