forked from serenapascual/cs151-project
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLoader.java
More file actions
82 lines (76 loc) · 1.84 KB
/
Loader.java
File metadata and controls
82 lines (76 loc) · 1.84 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
import java.io.*;
import java.util.ArrayList;
import java.util.Scanner;
/**
* This class saves the events to events.txt when program terminates, and also
* loads the events from the text file.
*
* @author Himanshu Mehta, Serena Pascual and Cherie Sew
*
*/
public class Loader {
private static ArrayList<Event> eventList = new ArrayList<Event>();
public static EventModel model = new EventModel();
public Loader(final EventModel model) {
this.model = model;
}
public Loader() {
}
/**
* Loads the events from the text file and then add them to the calendar.
*/
public static void loadEvent() {
Scanner s = null;
try {
s = new Scanner(new File("input.txt"));
} catch (FileNotFoundException e) {
System.out.println("This is your first run! The file was not found");
e.printStackTrace();
}
ArrayList<String> list = new ArrayList<String>();
while (s.hasNextLine()) {
list.add(s.nextLine());
}
s.close();
for (String values : list) {
model.populateEvents(values);
}
}
/**
* This method saves events to the events text file
* @param event is an event which is saved to the text file
*
*/
public static void saveEvent(ArrayList<Event> event) {
for (int i = 0; i < event.size(); i++) {
eventList.add(event.get(i));
}
File f = new File("events.txt");
if (!f.exists()) {
try {
f.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
FileWriter fw = null;
try {
fw = new FileWriter(f.getAbsoluteFile());
} catch (IOException e) {
e.printStackTrace();
}
BufferedWriter bw = new BufferedWriter(fw);
for (Event s : eventList) {
try {
bw.write(s.toString() + System.getProperty("line.separator"));
} catch (IOException e) {
e.printStackTrace();
}
}
try {
bw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}