-
Notifications
You must be signed in to change notification settings - Fork 0
/
GradebookAdd.java
146 lines (115 loc) · 6.87 KB
/
GradebookAdd.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
import com.beust.jcommander.JCommander;
import com.beust.jcommander.Parameter;
import com.beust.jcommander.ParameterException;
import com.beust.jcommander.Parameters;
import java.util.ArrayList;
import java.util.List;
public class GradebookAdd {
@Parameters(commandDescription = "Add a new assignment")
private static class AddAssignmentCommand {
@Parameter(names = "-AN", description = "Assignment name", required = true)
private List<String> assignmentName = new ArrayList<String>();
@Parameter(names = "-P", description = "Points assignment is out of", required = true)
private List<String> points = new ArrayList<String>();
@Parameter(names = "-W", description = "Assignment weight", required = true)
private List<String> weight = new ArrayList<String>();
}
@Parameters(commandDescription = "Delete an existing assignment")
private static class DeleteAssignmentCommand {
@Parameter(names = "-AN", description = "Assignment name", required = true)
private List<String> assignmentName = new ArrayList<String>();
}
@Parameters(commandDescription = "Add a new student")
private static class AddStudentCommand {
@Parameter(names = "-FN", description = "Student first name", required = true)
private List<String> studentFirstName = new ArrayList<String>();
@Parameter(names = "-LN", description = "Student last name", required = true)
private List<String> studentLastName = new ArrayList<String>();
}
@Parameters(commandDescription = "Delete an existing student")
private static class DeleteStudentCommand {
@Parameter(names = "-FN", description = "Student first name", required = true)
private List<String> studentFirstName = new ArrayList<String>();
@Parameter(names = "-LN", description = "Student last name", required = true)
private List<String> studentLastName = new ArrayList<String>();
}
@Parameters(commandDescription = "Add a new grade or overwrite an existing grade")
private static class AddGradeCommand {
@Parameter(names = "-FN", description = "Student first name", required = true)
private List<String> studentFirstName = new ArrayList<String>();
@Parameter(names = "-LN", description = "Student last name", required = true)
private List<String> studentLastName = new ArrayList<String>();
@Parameter(names = "-AN", description = "Assignment name", required = true)
private List<String> assignmentName = new ArrayList<String>();
@Parameter(names = "-G", description = "Number of points student received", required = true)
private List<String> grade = new ArrayList<String>();
}
@Parameter(names = "-N", description = "Gradebook filename", validateWith = CLIUtils.GradebookFileNameValidator.class, required = true)
private String gradebook;
@Parameter(names = "-K", description = "Encryption/Decryption key", validateWith = CLIUtils.KeyValidator.class, required = true)
private String key;
public static void main(String[] args) {
int exitCode = 0;
Gradebook gbook = null;
try {
GradebookAdd app = new GradebookAdd();
AddAssignmentCommand aa = new AddAssignmentCommand();
DeleteAssignmentCommand da = new DeleteAssignmentCommand();
AddStudentCommand as = new AddStudentCommand();
DeleteStudentCommand ds = new DeleteStudentCommand();
AddGradeCommand ag = new AddGradeCommand();
JCommander cmd = JCommander.newBuilder().addObject(app).addCommand("-AA", aa).addCommand("-DA", da)
.addCommand("-AS", as).addCommand("-DS", ds).addCommand("-AG", ag).build();
cmd.parse(args);
if (!(args[0].equals("-N") && args[2].equals("-K"))) {
throw new ParameterException("Wrong flag order");
}
if (cmd.getParsedCommand() == null) {
throw new ParameterException("No action specified");
}
gbook = Gradebook.load_and_decrypt(app.gradebook, app.key);
if (cmd.getParsedCommand().equals("-AA")) {
new CLIUtils.AssignmentNameValidator().validate("-AN", CLIUtils.getLast(aa.assignmentName));
new CLIUtils.NonNegativeIntegerValidator().validate("-P", CLIUtils.getLast(aa.points));
new CLIUtils.AssignmentWeightValidator().validate("-W", CLIUtils.getLast(aa.weight));
gbook.addAssignment(CLIUtils.getLast(aa.assignmentName), Integer.parseInt(CLIUtils.getLast(aa.points)),
Double.parseDouble(CLIUtils.getLast(aa.weight)));
}
if (cmd.getParsedCommand().equals("-DA")) {
new CLIUtils.AssignmentNameValidator().validate("-AN", CLIUtils.getLast(da.assignmentName));
gbook.deleteAssignment(CLIUtils.getLast(da.assignmentName));
}
if (cmd.getParsedCommand().equals("-AS")) {
new CLIUtils.StudentNameValidator().validate("-FN", CLIUtils.getLast(as.studentFirstName));
new CLIUtils.StudentNameValidator().validate("-LN", CLIUtils.getLast(as.studentLastName));
gbook.addStudent(CLIUtils.getLast(as.studentFirstName), CLIUtils.getLast(as.studentLastName));
}
if (cmd.getParsedCommand().equals("-DS")) {
new CLIUtils.StudentNameValidator().validate("-FN", CLIUtils.getLast(ds.studentFirstName));
new CLIUtils.StudentNameValidator().validate("-LN", CLIUtils.getLast(ds.studentLastName));
gbook.deleteStudent(CLIUtils.getLast(ds.studentFirstName), CLIUtils.getLast(ds.studentLastName));
}
if (cmd.getParsedCommand().equals("-AG")) {
new CLIUtils.StudentNameValidator().validate("-FN", CLIUtils.getLast(ag.studentFirstName));
new CLIUtils.StudentNameValidator().validate("-LN", CLIUtils.getLast(ag.studentLastName));
new CLIUtils.AssignmentNameValidator().validate("-AN", CLIUtils.getLast(ag.assignmentName));
new CLIUtils.NonNegativeIntegerValidator().validate("-G", CLIUtils.getLast(ag.grade));
gbook.addGrade(CLIUtils.getLast(ag.studentFirstName), CLIUtils.getLast(ag.studentLastName),
CLIUtils.getLast(ag.assignmentName), Integer.parseInt(CLIUtils.getLast(ag.grade)));
}
} catch (ParameterException | Gradebook.AppError appError) {
System.out.println("invalid");
exitCode = 255;
} catch (Gradebook.BackendError backendError) {
backendError.printStackTrace();
}
if (gbook != null) {
try {
gbook.save_and_encrypt();
} catch (Gradebook.BackendError backendError) {
backendError.printStackTrace();
}
}
System.exit(exitCode);
}
}