-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAccountReport
More file actions
183 lines (165 loc) · 4.2 KB
/
AccountReport
File metadata and controls
183 lines (165 loc) · 4.2 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
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
package uwstout.cs144.projects.project2.account;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.*;
import java.text.NumberFormat;
/**
* This class will read in account information and write an account report.
*
* @author FlowersC
* @version 2016-11-20
*/
public class AccountReport {
// the amounts formated in currency
// the negative amounts displayed in ()'s and no negative sign
// use NumberFormat's currency formatter since it does it for you
/**
* This is an account object.
*/
private Account account;
/**
* this constructor sets the account object to null.
*/
public AccountReport() {
account = null;
}
/**
* This getter gets the account.
*
* @return the account
*/
public Account getAccount() {
return account;
}
/**
* This method reads an account with a Scanner.
*
* @param input
* the Scanner that the user uses
*/
public void readAccount(Scanner input) {
Account burg;
int theId;
boolean theAccount;
int theTransId;
String theDate;
double theAmount;
try {
if (input.hasNextInt()) {
theId = input.nextInt();
if (input.hasNextBoolean()) {
theAccount = input.nextBoolean();
burg = new Account(theId, theAccount);
try {
while (input.hasNext()) {
if (input.hasNextInt()) {
theTransId = input.nextInt();
if (input.hasNext()) {
theDate = input.next();
if (input.hasNextDouble()) {
theAmount = input.nextDouble();
burg.addTransaction(theTransId,
theDate, theAmount);
account = burg;
}
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* This method reads a file given by the user.
*
* @param fileName
* the fileName the user is trying to read
*/
public void readAccount(String fileName) {
// open the file
File file = new File(fileName);
try {
Scanner read = new Scanner(file);
// loop over all the data
if (read.hasNext()) {
readAccount(read);
}
// close the file
read.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
/**
* This method prints the report by using a PrintWriter given by the user.
*
* @param writer
* the PrintWriter that the user creates to use this
*/
public void printReport(PrintWriter writer) {
// add account id
// add either savings or checking
// add the fee if it is more than zero
NumberFormat defaultFormat = NumberFormat.getCurrencyInstance();
if (account != null) {
writer.println(account.getAccountId());
if (account.isChecking()) {
writer.println("Checking");
} else {
writer.println("Savings");
}
for (int i = 0; i < account.getTransactionCount(); i++) {
Transaction burg = account.getTransaction(i);
writer.println(burg.getDate() + " " +
defaultFormat.format(burg.getAmount()) + " \n");
}
writer.println("Balance: "
+ defaultFormat.format(account.getBalance()));
if (account.getFee() > 0) {
writer.println("Fee: "
+ defaultFormat.format(account.getFee()));
}
}
}
/**
* This method reads a fileName from the user and prints to a new file.
*
* @param fileName
* the file the user is trying to read
*/
public void printReport(String fileName) {
File file = new File(fileName);
NumberFormat defaultFormat = NumberFormat.getCurrencyInstance();
readAccount("" + file);
try {
PrintWriter p = new PrintWriter(fileName);
if (account != null) {
p.println(account.getAccountId());
if (account.isChecking()) {
p.println("Checking");
} else {
p.println("Savings");
}
for (int i = 0; i < account.getTransactionCount(); i++) {
Transaction burg = account.getTransaction(i);
p.println(burg.getDate() + " " +
defaultFormat.format(burg.getAmount()) + " \n");
}
p.println("Balance: "
+ defaultFormat.format(account.getBalance()));
if (account.getFee() > 0) {
p.println("Fee: " + defaultFormat.format(account.getFee()));
}
p.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}