-
Notifications
You must be signed in to change notification settings - Fork 0
/
Day.java
191 lines (169 loc) · 3.4 KB
/
Day.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
import java.util.*;
/*
* A day, each day contains a list of tasks
*/
public class Day implements Cloneable {
private ArrayList<String> tasks;
private Calendar today;
public Day(Calendar c) {
today = Calendar.getInstance();
today.set(c.get(Calendar.YEAR), c.get(Calendar.MONTH), c.get(Calendar.DAY_OF_MONTH));
tasks = new ArrayList<>();
}
/*
* Setters & Getters
*/
public Calendar getToday() {
today.set(Calendar.HOUR_OF_DAY, 0);
today.set(Calendar.MINUTE, 0);
today.set(Calendar.SECOND, 0);
today.set(Calendar.MILLISECOND, 0);
return today;
}
public int getDay() {
return today.get(Calendar.DAY_OF_MONTH);
}
public void setDay(int day) {
today.set(Calendar.DAY_OF_MONTH, day);
}
public String getMonth() {
int i = today.get(Calendar.MONTH);
String s = null;
switch (i) {
case 0:
s = "January";
break;
case 1:
s = "February";
break;
case 2:
s = "March";
break;
case 3:
s = "April";
break;
case 4:
s = "May";
break;
case 5:
s = "June";
break;
case 6:
s = "July";
break;
case 7:
s = "August";
break;
case 8:
s = "September";
break;
case 9:
s = "October";
break;
case 10:
s = "November";
break;
case 11:
s = "December";
break;
default:
s = "invalid month";
}
return s;
}
public int getMonthInt() {
return today.get(Calendar.MONTH);
}
public void setMonth(int month) {
today.set(Calendar.MONTH, month);
}
public int getYear() {
return today.get(Calendar.YEAR);
}
public void setYear(int year) {
today.set(Calendar.YEAR, year);
}
public Iterator<String> getTasks() {
return new Iterator<String>() {
public boolean hasNext() {
return current < tasks.size();
}
public String next() {
return tasks.get(current++);
}
public void remove() {
throw new UnsupportedOperationException();
}
private int current = 0;
};
}
public String getDayOfWeek() {
int i = today.get(Calendar.DAY_OF_WEEK);
String s = null;
switch (i) {
case 1:
s = "Sunday";
break;
case 2:
s = "Monday";
break;
case 3:
s = "Tuesday";
break;
case 4:
s = "Wednesday";
break;
case 5:
s = "Thursday";
break;
case 6:
s = "Friday";
break;
case 7:
s = "Saturday";
break;
default:
s = "invalid day of the week";
}
return s;
}
/*
* Add, Remove, Edit from tasks arrayList
*/
public void addTask(String aTask) {
tasks.add(aTask);
}
public void removeTask(String taskSelected) {
tasks.remove(taskSelected);
}
public void resetTasks() {
tasks = new ArrayList<>();
}
public void editTask(String old, String updated) {
// traverse arrayList of tasks and find the one that match the selected task
// replace old task with updated task
Iterator<String> iterString = getTasks();
int index = 0;
while (iterString.hasNext()) {
String temp = iterString.next();
if (temp.equals(old))
tasks.set(index, updated);
index++;
}
}
/*
* @Override clone() method
*/
protected Object clone() throws CloneNotSupportedException {
Day cloned = (Day) super.clone();
cloned.tasks = new ArrayList<>();
Iterator<String> iterString = getTasks();
while (iterString.hasNext()) {
String temp = iterString.next();
cloned.tasks.add(temp);
}
cloned.today = Calendar.getInstance();
cloned.today.set(today.get(Calendar.YEAR), today.get(Calendar.MONTH), today.get(Calendar.DAY_OF_MONTH));
return cloned;
}
}