-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathData.java
More file actions
160 lines (140 loc) · 4.05 KB
/
Data.java
File metadata and controls
160 lines (140 loc) · 4.05 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
import java.text.*;
import java.util.*;
/*
* The model
* Stores a list of all the days with tasks to-do
*/
public class Data {
ArrayList<Day> days;
private Day currentDay;
private Calendar cal;
private ToDoListFrame toDoFrame;
private CalendarFrame calendarFrame;
public Data() {
cal = Calendar.getInstance(); // calendar obj with current time and date
currentDay = new Day(cal);
days = new ArrayList<>();
calendarFrame = new CalendarFrame(this);
toDoFrame = new ToDoListFrame(this);
}
/*
* Setters & Getters
*/
public Day getCurrentDay() {
return currentDay;
}
public void setCurrentDay(int year, int month, int day) {
currentDay.setYear(year);
currentDay.setMonth(month);
currentDay.setDay(day);
// reset currentday task array
currentDay.resetTasks();
Iterator<Day> iterDay = getDays();
Day temp = null;
while (iterDay.hasNext()) {
temp = iterDay.next();
if (currentDay.getToday().equals(temp.getToday())) {
// add selected days tasks to currentday tasks with iterstring
Iterator<String> iterString = temp.getTasks();
while (iterString.hasNext())
currentDay.addTask(iterString.next());
}
}
}
public Iterator<Day> getDays() {
return new Iterator<Day>() {
public boolean hasNext() {
return current < days.size();
}
public Day next() {
return days.get(current++);
}
public void remove() {
throw new UnsupportedOperationException();
}
private int current = 0;
};
}
public ToDoListFrame getToDoFrame() {
return toDoFrame;
}
// Add a currentDay to days arrayList
public void addDay() {
try {
if (currentDay instanceof Cloneable) {
Day newDay = new Day(Calendar.getInstance());
newDay = (Day) currentDay.clone();
days.add(newDay);
}
} catch (CloneNotSupportedException e) {
e.printStackTrace();
}
}
/*
* Add, Edit, Delete task of currentDay
*/
public void addTask(String taskToBeAdded) {
// checks if the current day already exists in the days arrayList
// if yes, add new task to day that already exists
// if not, add a new day (with currentDay's date) to day arrayList
Iterator<Day> iterDay = getDays();
Day temp = null;
while (iterDay.hasNext()) {
temp = iterDay.next();
if ((currentDay.getToday().getTime().equals(temp.getToday().getTime()))) {
temp.addTask(taskToBeAdded);
return;
}
}
currentDay.addTask(taskToBeAdded);
addDay(); // add a new day into days arrayList
}
public void editTask(String old, String updated) {
// find day with a date that matches currentDay's date
// edit that day's task
Iterator<Day> iterDay = getDays();
Day temp = null;
while (iterDay.hasNext()) {
temp = iterDay.next();
if (currentDay.getToday().equals(temp.getToday()))
temp.editTask(old, updated);
}
}
public void deleteTask(String taskSelected) {
// find day with a date that matches currentDay's date
// remove it from days arrayList
Iterator<Day> iterDay = getDays();
Day temp = null;
while (iterDay.hasNext()) {
temp = iterDay.next();
if (currentDay.getToday().equals(temp.getToday()))
temp.removeTask(taskSelected);
}
}
/*
* Export all task in a specific month to a text file
* Text file name in format MONTH_YEAR.txt
*/
public void export() {
// generate the a filename in the format of month_year.txt as a String
String fileName = currentDay.getMonth() + "_" + currentDay.getYear() + ".txt";
String content = "";
Iterator<Day> iterDay = getDays();
Day temp = null;
// iterate thru arrayList of days, check if a day has month that matches month
// of currentDay
// if T, iterate thru tasks arrayList of that day and append each task to content
while (iterDay.hasNext()) {
temp = iterDay.next();
if(temp.getYear() == currentDay.getYear()) {
if (temp.getMonth().equals(currentDay.getMonth())) {
Iterator<String> iterString = temp.getTasks();
while (iterString.hasNext())
content += "-" + iterString.next() + "\r\n";
}
}
}
WriteToFile writer = new WriteToFile(fileName, content);
System.out.printf("File %s is written\n", fileName);
}
}