-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPlaceModel.java
More file actions
96 lines (84 loc) · 1.92 KB
/
PlaceModel.java
File metadata and controls
96 lines (84 loc) · 1.92 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
package server;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.LinkedList;
import java.util.List;
public class PlaceModel {
private List<Place> places;
public static String filepath = ".\\places.txt";
public PlaceModel()
{
places = new LinkedList<Place>();
loadPlaces();
}
private synchronized void loadPlaces(){
try {
File file = new File(filepath);
file.createNewFile();
BufferedReader reader = new BufferedReader(new FileReader(filepath));
String line = reader.readLine();
String[] placeName;
if(line != null){
placeName = line.split(";");
}else{
placeName = new String[0];
}
for(int i=0; i<placeName.length; i++)
{
Place place = new Place();
place.setName(placeName[i]);
places.add(place);
}
reader.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public List<Place> getAllPlaces(){
return places;
}
public boolean removePlace(String name){
boolean removed = false;
for(int i=0; i<places.size(); i++)
{
if(places.get(i).getName().equals(name))
{
places.remove(i);
removed = true;
}
}
return removed;
}
public void addPlace(Place p){
for(int i=0; i<places.size(); i++)
{
if(places.get(i).getName().equals(p.getName()))
{
return;
}
}
places.add(p);
}
public synchronized void saveToFile(){
try {
BufferedWriter writer = new BufferedWriter(new FileWriter(filepath));
StringBuilder builder = new StringBuilder();
for(int i=0; i<places.size(); i++)
{
builder.append(places.get(i).getName());
builder.append(";");
}
writer.write(builder.toString());
writer.flush();
writer.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}