-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathPSS.java
More file actions
325 lines (307 loc) · 15.2 KB
/
PSS.java
File metadata and controls
325 lines (307 loc) · 15.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
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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.Scanner;
import java.util.ArrayList;
public class PSS {
public static void main(String[] args) throws Exception {
Scanner input = new Scanner(System.in);
Schedule taskSchedule = new Schedule();
while (true) {
System.out.println();
System.out.println(
"Please select an option below: \n 1. Create a task \n 2. View a task \n 3. Delete a task \n 4. Edit a task"
+ "\n 5. Write the schedule to a file \n 6. Read the Schedule from a file \n 7. View schedule for 1 day \n 8. View schedule for 1 week"
+ "\n 9. View schedule for 1 month \n 10. Write schedule for 1 day \n 11. Write schedule for 1 week \n 12. Write schedule for 1 month \n 0. Exit");
int option = input.nextInt();
switch (option) {
//create a task
case 1: {
System.out.println("Create Transient or Recurring task?");
System.out.println("Press 1 for Transient");
System.out.println("Press 2 for Recurring");
int choice = input.nextInt();
Runtime runtime = new Runtime();
input.nextLine();
if(choice==1){
System.out.println("What is the name of the event?");
String name = input.nextLine();
System.out.println("When does the event start?");
runtime.startTime = input.nextDouble();
runtime.startTime = getNear15Minute((int)runtime.startTime*60) / 60;
System.out.println("How long is the event?");
runtime.duration = input.nextDouble();
runtime.duration = getNear15Minute((int)runtime.duration*60) / 60;
System.out.println("What is the date of the event? (YYYYMMDD)");
int date = input.nextInt();
Task newTask = new TransientTask(name, date, runtime);
input.nextLine();
System.out.println("Does this have an associated anti-task? (y/n)");
String choice2 = input.nextLine();
if(choice2.equals("y")){
newTask.setAntiTask(true);
}
else{
newTask.setAntiTask(true);
}
taskSchedule.checkConflicts(newTask);
taskSchedule.addTask(newTask);
}
else if(choice==2){
System.out.println("What is the name of the event?");
String name = input.nextLine();
System.out.println("When does the event start?");
runtime.startTime = input.nextDouble();
System.out.println("How long is the event?");
runtime.duration = input.nextDouble();
System.out.println("What is the date of the event? (YYYYMMDD)");
int date = input.nextInt();
System.out.println("What is the end date of the event? (YYYYMMDD)");
int endDate = input.nextInt();
System.out.println("Frequency? (1 for daily, 7 for weekly)");
int recFreq = input.nextInt();
Task newTask = new RecurringTask(name, date, runtime, endDate, recFreq);
taskSchedule.checkConflicts(newTask);
taskSchedule.addTask(newTask);
}
// for testing
//Runtime testRuntime = new Runtime();
//testRuntime.startTime = 20;
//testRuntime.duration = 60;
// RecurringTask testTask1 = new RecurringTask(recTask, recStartDate, testRuntime, recEndDate, recFreq);
// TransientTask testTask2 = new TransientTask(transTask, transDate, testRuntime);
//RecurringTask testTask1 = new RecurringTask("testTask1", 19900107, testRuntime, 19900130, 7);
//TransientTask testTask2 = new TransientTask("testTask2", 19900204, testRuntime);
//taskSchedule.addTask(testTask1);
//taskSchedule.addTask(testTask2);
ArrayList<Task> tempArray = new ArrayList<Task>();
for(Task task : taskSchedule.taskList){
tempArray.add(null);
}
taskSchedule.mergeSort(taskSchedule.taskList, tempArray, 0, taskSchedule.taskList.size());
break;
}
//View a task
case 2: {
System.out.println("Enter task to view:");
input.nextLine();
String taskName = input.nextLine();
int loc = taskSchedule.findTask(taskName);
if (loc == -1) {
System.out.println("Error: task not found");
break;
}
taskSchedule.viewTask(taskSchedule.getTask(loc));
break;
}
//Delete a task
case 3: {
System.out.println("Enter task to delete:");
input.nextLine();
String taskName = input.nextLine();
int loc = taskSchedule.findTask(taskName);
if (loc == -1) {
System.out.println("Error: task not found");
break;
}
taskSchedule.deleteTask(taskSchedule.getTask(loc));
ArrayList<Task> tempArray = new ArrayList<Task>();
for(Task task : taskSchedule.taskList){
tempArray.add(null);
}
taskSchedule.mergeSort(taskSchedule.taskList, tempArray, 0, taskSchedule.taskList.size());
break;
}
//Edit a task
case 4: {
System.out.println("Enter task to edit:");
input.nextLine();
String taskName = input.nextLine();
int loc = taskSchedule.findTask(taskName);
if (loc == -1) {
System.out.println("Error: task not found");
break;
}
while (true) {
String newName = taskSchedule.getTask(loc).getName();
int newDate = taskSchedule.getTask(loc).getDate();
double newStartTime = taskSchedule.getTask(loc).getStartTime();
double newDuration = taskSchedule.getTask(loc).getDuration();
boolean antiTaskStatus = taskSchedule.getTask(loc).hasAntiTask();
int newEndDate = taskSchedule.getTask(loc).getEndDate();
System.out.println(
"Edit values: 1) Name 2) Date 3) Start time \n 4) Duration 5) Antitask Status 6) End date \n 7) Complete Edit");
int key = input.nextInt();
input.nextLine();
switch (key) {
case 1:
System.out.println("New Name:");
newName = input.nextLine();
break;
case 2:
System.out.println("New Start Date:");
newDate = input.nextInt();
ArrayList<Task> tempArray = new ArrayList<Task>();
tempArray.ensureCapacity(taskSchedule.taskList.size());
for(Task task : taskSchedule.taskList){
tempArray.add(null);
}
break;
case 3:
System.out.println("New Start Time:");
newStartTime = input.nextInt();
break;
case 4:
System.out.println("New Duration: ");
newDuration = input.nextInt();
break;
case 5:
System.out.println("Toggle antitask status (1 = True, 2 = False):");
int antiTaskVal = input.nextInt();
antiTaskStatus = false;
if (antiTaskVal == 1)
antiTaskStatus = true;
if (antiTaskVal == 2)
antiTaskStatus = false;
taskSchedule.getTask(loc).setAntiTask(antiTaskStatus);
break;
case 6:
System.out.println("New End Date: ");
newEndDate = input.nextInt();
break;
case 7:
Runtime newRuntime = new Runtime();
newRuntime.startTime = newStartTime;
newRuntime.duration = newDuration;
taskSchedule.editTask(taskSchedule.getTask(loc), newName, newDate, newRuntime, antiTaskStatus, newEndDate);
break;
default:
break;
}
}
}
//Write the schedule to a file
case 5: {
System.out.println("Enter the name of the new .json file");
input.nextLine();
taskSchedule.writeSchedule(input.nextLine());
break;
}
//Read the Schedule from a file
case 6: {
System.out.println("Enter the name of the .json file to upload");
input.nextLine();
taskSchedule.readSchedule(input.nextLine());
ArrayList<Task> tempArray = new ArrayList<Task>();
for(Task task : taskSchedule.taskList){
tempArray.add(null);
}
taskSchedule.mergeSort(taskSchedule.taskList, tempArray, 0, taskSchedule.taskList.size());
break;
}
//View schedule for 1 day
case 7: {
System.out.println("Enter the start date: ");
int startDate = input.nextInt();
String s = Integer.toString(startDate);
int endDate = durationHelper(s, 1);
taskSchedule.viewScheduleDuration(startDate, endDate);
break;
}
//View schedule for 1 week
case 8: {
System.out.println("Enter the start date: ");
int startDate = input.nextInt();
String s = Integer.toString(startDate);
int endDate = durationHelper(s, 2);
taskSchedule.viewScheduleDuration(startDate, endDate);
break;
}
//View schedule for 1 month
case 9: {
System.out.println("Enter the start date: ");
int startDate = input.nextInt();
String s = Integer.toString(startDate);
int endDate = durationHelper(s, 2);
taskSchedule.viewScheduleDuration(startDate, endDate);
break;
}
//Write schedule for 1 day
case 10: {
System.out.println("Enter the name of the new .json file: ");
String filename = input.next();
System.out.println("Enter the start date: ");
int startDate = input.nextInt();
String s = Integer.toString(startDate);
int endDate = durationHelper(s, 1);
taskSchedule.writeScheduleDuration(filename, startDate, endDate);
break;
}
//Write schedule for 1 week
case 11: {
System.out.println("Enter the name of the new .json file: ");
String filename = input.next();
System.out.println("Enter the start date: ");
int startDate = input.nextInt();
String s = Integer.toString(startDate);
int endDate = durationHelper(s, 2);
taskSchedule.writeScheduleDuration(filename, startDate, endDate);
break;
}
//Write schedule for 1 month
case 12: {
System.out.println("Enter the name of the new .json file: ");
String filename = input.next();
System.out.println("Enter the start date: ");
int startDate = input.nextInt();
String s = Integer.toString(startDate);
int endDate = durationHelper(s, 3);
taskSchedule.writeScheduleDuration(filename, startDate, endDate);
break;
}
case 0: {
input.close();
System.exit(0);
}
default:
break;
}
}
}
//return the proper end date
private static int durationHelper(String dateString, int increment) {
int year = Integer.parseInt(dateString.substring(0, 4));
int month = Integer.parseInt(dateString.substring(4, 6));
int day = Integer.parseInt(dateString.substring(6, 8));
LocalDate start = LocalDate.of(year, month, day);
LocalDate end = start;
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMdd");
//day
if(increment == 1) {
end = start.plusDays(1);
}
//month
else if(increment == 2) {
end = start.plusWeeks(1);
}
//year
else if(increment == 3) {
end = start.plusMonths(1);
}
else {
throw new IllegalArgumentException();
}
String temp = end.format(formatter);
return Integer.parseInt(temp);
}
}
//round to the nearest 15 minutes
public static double getNear15Minute(int minutes){
int mod = minutes%15;
double res = 0 ;
if((mod) >=8){
res = minutes+(15 - mod);
}else{
res = minutes-mod;
}
return res;
}