-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathRecurringTask.java
More file actions
111 lines (99 loc) · 5.01 KB
/
RecurringTask.java
File metadata and controls
111 lines (99 loc) · 5.01 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
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import javax.management.InvalidAttributeValueException;
public class RecurringTask extends Task {
public RecurringTask(String name, int date, Runtime runtime, int endDate, int frequency)
throws IllegalArgumentException, InvalidAttributeValueException {
super(name, date, runtime);
// check that the frequency is a 1 or a 7
// 1 is daily, 7 is weekly
if (frequency != 1 && frequency != 7)
throw new IllegalArgumentException("Frequency must be a 1 or a 7"); // this may be better handled in main,
// but
// for purposes of this project we don't
// really have time to discuss that.
this.frequency = frequency;
typeVal = Type.RECURRING;
this.endDate = endDate;
tasks = new ArrayList<TransientTask>();
instantiateTasks();
}
private void instantiateTasks() throws InvalidAttributeValueException {
String s = Integer.toString(date);
String e = Integer.toString(endDate);
int startYear = Integer.parseInt(s.substring(0, 4));
int startMonth = Integer.parseInt(s.substring(4, 6));
int startDay = Integer.parseInt(s.substring(6, 8));
int endYear = Integer.parseInt(e.substring(0, 4));
int endMonth = Integer.parseInt(e.substring(4, 6));
int endDay = Integer.parseInt(e.substring(6, 8));
//create LocalDate object for start and end date
LocalDate start = LocalDate.of(startYear, startMonth, startDay);
LocalDate end = LocalDate.of(endYear, endMonth, endDay);
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMdd");
/**
* if frequency is 1, increment start by one day until start > end, create a transient task
* with the new date and same runtime, add this new task to the list of tasks
*/
if(frequency == 1) {
while(start.isBefore(end) || start.isEqual(end)) {
start = start.plusDays(1);
String newDate = start.format(formatter);
TransientTask t = new TransientTask(name, Integer.parseInt(newDate), runtime);
tasks.add(t);
}
}
/**
* if frequency is 7, increment start by one week until start > end, create a transient task
* with the new date and same runtime, add this new task to the list of tasks
*/
else if(frequency == 7) {
while(start.isBefore(end) || start.isEqual(end)) {
start = start.plusWeeks(1);
String newDate = start.format(formatter);
TransientTask t = new TransientTask(name, Integer.parseInt(newDate), runtime);
tasks.add(t);
}
}
else {
throw new InvalidAttributeValueException();
}
}
// private void instantiateTasks() {
// String dateString = String.valueOf(date);
// int years = Integer.parseInt(dateString.substring(0, 4));
// int months = Integer.parseInt(dateString.substring(4, 6));
// int days = Integer.parseInt(dateString.substring(6, 8));
// String endDateString = String.valueOf(endDate);
// int endYears = Integer.parseInt(endDateString.substring(0, 4));
// int endMonths = Integer.parseInt(endDateString.substring(4, 6));
// int endDays = Integer.parseInt(endDateString.substring(6, 8));
// switch (frequency) {
// case 1:
// for (int i = years; i < endYears; i++) {
// for (int j = months; j < endMonths; j++) {
// for (int k = days; k < endDays; k++) {
// String newDateString = String.valueOf(i) + String.valueOf(j) + String.valueOf(k);
// int newDate = Integer.parseInt(newDateString);
// TransientTask newTask = new TransientTask(name, newDate, runtime);
// tasks.add(newTask);
// }
// }
// }
// break;
// case 7:
// for (int i = years; i < endYears; i++) {
// for (int j = months; j < endMonths; j++) {
// for (int k = days; k < endDays; k += 7) {
// String newDateString = String.valueOf(i) + String.valueOf(j) + String.valueOf(k);
// int newDate = Integer.parseInt(newDateString);
// TransientTask newTask = new TransientTask(name, newDate, runtime);
// tasks.add(newTask);
// }
// }
// }
// break;
// }
// }
}