/
TransactionProcessor.java
218 lines (187 loc) · 5.12 KB
/
TransactionProcessor.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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
//Name: Max Chen
//Student ID: 111316366
//Stony Brook University
//CSE 114
//Fall 2017
//Final Project
package finalProject;
//Driver class for the final project
import java.util.*;
import java.io.*;
public class TransactionProcessor
{
private static String getCardType (long number)
{
// Return a String indicating whether 'number' belongs to a
// CreditCard, RewardsCard, or a PrepaidCard (or null if it's none
// of the three)
String result;
int firstTwo = Integer.parseInt(("" + number).substring(0,2));
switch(firstTwo)
{
case 84:
case 85: result = "CreditCard"; break;
case 86:
case 87: result = "RewardsCard"; break;
case 88:
case 89: result = "PrepaidCard"; break;
default: result = null; // invalid card number
}
return result;
}
public static BankCard convertToCard(String data){
Scanner datain = new Scanner(data);
int expiration;
double limit;
double balance;
long cardnumber;
String name;
String type = null;
//while(datain.hasNextLong()){
cardnumber = datain.nextLong();
if(getCardType(cardnumber) == (null)){
datain.nextLine();
}
//else{
type = getCardType(cardnumber);
//testingSystem.out.print(type);
//}
if(type.equals("CreditCard")){
CreditCard card;
name = datain.next();
name = checkForUnderscore(name);
//testing System.out.print(name);
expiration = datain.nextInt();
if(datain.hasNextLine()){
limit = Double.parseDouble(datain.nextLine());
card = new CreditCard(name, cardnumber, expiration, limit);
}
else{
card = new CreditCard(name, cardnumber, expiration);
}
datain.close();
return card;
}
else if(type.equals("RewardsCard")){
RewardsCard card;
name = datain.next();
name = checkForUnderscore(name);
expiration = datain.nextInt();
if(datain.hasNextLine()){
limit = Double.parseDouble(datain.nextLine());
card = new RewardsCard(name, cardnumber, expiration, limit);
}
else{
card = new RewardsCard(name, cardnumber, expiration);
}
datain.close();
return card;
}
else {
PrepaidCard card;
name = datain.next();
name = checkForUnderscore(name);
if(datain.hasNextLine()){
balance = Double.parseDouble(datain.next());
card = new PrepaidCard(name, cardnumber, balance);
}
else{
card = new PrepaidCard(name, cardnumber);
}
datain.close();
return card;
}
}
public static CardList loadCardData(String fName){
File f = new File(fName);
CardList lis = new CardList();
try{
Scanner scan = new Scanner(f);
while(scan.hasNextLine()){
lis.add(convertToCard(scan.nextLine()));
}
scan.close();
return lis;
}
catch(IOException IOE){
return null;
}
}
public static void processTransactions(String filename, CardList c){
File f = new File(filename);
int index = 0;
try{
Scanner scan = new Scanner(f);
while(scan.hasNextLine()){
String[] arr = scan.nextLine().split(" ");
long cardnumber = Long.parseLong(arr[0]);
for(int i = 0; i < c.size(); i++){
if(cardnumber == c.get(i).number()){
index = i;
}
}
if(arr[1].equals("redeem")){
int numpoints = Integer.parseInt(arr[2]);
RewardsCard rc = (RewardsCard) c.get(index);
//try {
rc.redeemPoints(numpoints);
//}
//catch(RedeemPointsException RPE) {
// System.out.println(RPE.getMessage());
//}
}
else if(arr[1].equals("top-up")){
double numadd = Double.parseDouble(arr[2]);
PrepaidCard pc = (PrepaidCard) c.get(index);
pc.addFunds(numadd);
}
else if(arr[1].equals("advance")) {
double add = Double.parseDouble(arr[2]);
CreditCard cred = (CreditCard) c.get(index);
cred.getCashAdvance(add);
}
else{
Transaction trans = new Transaction(arr[1], arr[3], Double.parseDouble(arr[2]));
trans.setMerchant(checkForUnderscore(trans.merchant()));
c.get(index).addTransaction(trans);
}
}
scan.close();
}
catch(IOException IOE){
}
}
public static String checkForUnderscore(String name) {
if(name.indexOf('_') != -1) {
while(name.indexOf('_')!=-1) {
int underscoreIndex = name.indexOf('_');
String part1 = name.substring(0, underscoreIndex);
String part2 = name.substring(underscoreIndex + 1);
name = part1 + " " + part2;
}
return name;
}
else {
return name;
}
}
public static void main(String[] args){
System.out.println("Enter card data file name: ");
Scanner userin = new Scanner(System.in);
String fil = userin.next();
//File f = new File(fil);
try{
CardList cl = loadCardData(fil);
System.out.println("Enter transaction data file name: ");
String transfil = userin.next();
processTransactions(transfil, cl);
for(int i = 0; i < cl.size(); i++){
cl.get(i).printStatement();
}
userin.close();
}
catch(Exception ex){
System.out.println("LoadCardData failed.");
}
}
}