-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.java
More file actions
153 lines (151 loc) · 5.02 KB
/
Main.java
File metadata and controls
153 lines (151 loc) · 5.02 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
import java.util.StringTokenizer;
class Main {
// Only the main routine. Do everything based on the input file
public static void main(String[] args) {
CMD reader = new CMD("input.txt",false);
//load inputfile into string array
reader.readTextFile();
//create list to store members
ArrayList list = new ArrayList();
//create Hashmap to have O(1) search
HashIndex map = new HashIndex();
//parse commands and execute
Parse(reader,list,map);
//write the member list to txt
CMD jsonreader = new CMD("input.json",true);
ParseJson(jsonreader,list, map);
reader.write(list, "output.txt");
reader.closeLog();
jsonreader.closeLog();
}
public static void Parse(CMD cmd,ArrayList list,HashIndex map){
String cd;//command
String name,id,vip;
boolean status;
if(cmd.size()==0) {
//log: failed to parse an empty text file.
cmd.log("Failed to parse an empty text file.");
return;
}
for(int i=0;i<cmd.size();i++){
//if(cmd.getLine(i)==null) break;
String line = cmd.getLine(i);//works up to here
StringTokenizer s = new StringTokenizer(line);
cd = s.nextToken();
if(cd.equals("ADD")) {
id = s.nextToken();
name = s.nextToken() + " " + s.nextToken();
if(Verify(id,map)){
//log: failure to add, ID already exists
System.out.println("fail " +name);
cmd.log("Failure to add, "+name+" already exists");
}
else{
Member mem = new Member(name,id);//create member object
list.Add(mem);//add user
cmd.log(name+" added to Member Database");//log memeber added
map.put(id,list.size()-1);//add id/index pair to map
}
}
else if(cd.equals("MOD")){
id = s.nextToken();
vip = s.nextToken();
if(vip.equals("true")) status = true;
else status = false;
if(Verify(id,map)){
list.Mod(id,status);
}
else{
//log: failure to modify, member doesn't exist
cmd.log("Failure to modify, member doesn't exist.");
}
}
else if(cd.equals("DEL")){
id = s.nextToken();
if(Verify(id,map)){
list.Rem(map.get(id));
map.remove(id);
}
else{
//log: falure to remove, member doesn't exist
cmd.log("Failed to remove member ID" + id + " doesn't exist.");
}
}
}
//cmd.closeLog();
}
public static void ParseJson(CMD cmd,ArrayList list, HashIndex map){
String cd = cmd.getJsonInput();
while(true) {
if(cd!=null){
String ary[] = cd.split(",");
String type="";
int arySize = 0;
for(String s:ary){
if(s.indexOf("type")!=-1){
type = s.substring(s.indexOf(":")+1);
}
}
type = type.replaceAll("\"","");
execute(type,ary,cmd,list,map);
cd = cmd.getJsonInput();
}
else break;
}
//cmd.closeLog();
}
public static void execute(String cmdType,String cmdAry[],CMD cmd,ArrayList list, HashIndex map){
if(cmdType.equals("ADD")){
String id = cmdAry[1].substring(cmdAry[1].indexOf(":")+1);
String name = cmdAry[2].substring(cmdAry[2].indexOf(":")+1) + " " + cmdAry[3].substring(cmdAry[3].indexOf(":")+1);
String major = cmdAry[4].substring(cmdAry[4].indexOf(":")+1);
id = id.replaceAll("\"","");
name = name.replaceAll("\"","");
major = major.replaceAll("\"","");
if(Verify(id,map)){
//log: failure to add, ID already exists
cmd.log("Failure to add, "+name+" already exists");
}
else{
Member mem = new Member(name,id);//create member object
mem.setMajor(major);
list.Add(mem);//add user
cmd.log(name+" added to Member Database");//log memeber added
map.put(id,list.size()-1);//add id/index pair to map
}
}
else if(cmdType.equals("MOD")){
boolean vip;
String id = cmdAry[1].substring(cmdAry[1].indexOf(":")+1);
String status = cmdAry[2].substring(cmdAry[2].indexOf(":")+1);
if(status.equals("true")) vip = true;
else vip = false;
id = id.replaceAll("\"","");
status = status.replaceAll("\"","");
if(Verify(id,map)){
list.Mod(id,vip);
cmd.log("Member with ID "+id+"has been modified");
}
else{
//log: failure to modify, member doesn't exist
cmd.log("Failure to modify, member doesn't exist.");
}
}
else if(cmdType.equals("DEL")){
String id = cmdAry[1].substring(cmdAry[1].indexOf(":")+1);
id = id.replaceAll("\"","");
if(Verify(id,map)){
list.Rem(map.get(id));
map.remove(id);
cmd.log(id+" was removed from database");
}
else{
//log: falure to remove, member doesn't exist
cmd.log("Failed to remove member, ID " + id + " doesn't exist.");
}
}
}
public static boolean Verify(String str, HashIndex map){
return map.containsKey(str);
}
}