-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRoomDataReader.java
More file actions
127 lines (121 loc) · 4.65 KB
/
RoomDataReader.java
File metadata and controls
127 lines (121 loc) · 4.65 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
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Reader;
import java.util.ArrayList;
import java.util.List;
import java.util.zip.DataFormatException;
public class RoomDataReader implements RoomDataReaderInterface{
/**
* This method read the data and form a List of Room objects based on the csv file
* @param inputFileReader that has read the file and read to be manipulated
* @return a list of Rooms interpreted from the csv file
*/
@Override
public ArrayList<Room> readDataSet(Reader inputFileReader)
throws FileNotFoundException, IOException, DataFormatException {
//initialize the list to be returned
ArrayList<Room> rooms = new ArrayList<Room>();
//initialize the temporary storage of the properties of each movie at each line
String roomInfo = "";
//specify each character in the form of int (can be converted to char)
int current;
boolean open = false;
//Since the first line needs to be ignored,
//read the stream through the first line
while((char)inputFileReader.read() != '\n');
//read through the remaining contents from the reader
while((current = inputFileReader.read()) != -1)
{
if(!open && (char)current == '\"')
open = true;
else if(open && (char)current == '\"')
open = false;
if(open && (char)current == '\n') {
continue;
}
//case 1: it is at the end of the line
if(!open && (char)current == '\n' )
{
//summarize the line by storing properties separated
//by ", " into a list of Strings
String[] sp = roomInfo.split(",(\s)*");
ArrayList<String>[] raw_data = (ArrayList<String>[]) new ArrayList[7];
String[] complieData = new String[7];
for(int i = 0; i < 7; ++i) raw_data[i] = new ArrayList<String>();
int cur = 0;
int len = sp.length;
for(int i = 0; i < len; ++i){
if(cur > 6){
throw new DataFormatException("Too many columns");
}
if(!sp[i].isEmpty() && sp[i].charAt(0) == '\"'){
int j;
for(j = i; j < len && sp[j].charAt(sp[j].length()-1) != '\"'; ++j);
if(i == j){
raw_data[cur].add(sp[i].substring(1, sp[i].length()-1));
}else{
raw_data[cur].add(sp[i].substring(1));
for(int k = i+1; k < j; ++k){
raw_data[cur].add(sp[k]);
}
raw_data[cur].add(sp[j].substring(0, sp[j].length()-1));
}
i = j;
++cur;
}else{
raw_data[cur++].add(sp[i]);
}
}
for(int i=0;i<7;i++) {
complieData[i] = new String();
for(String it: raw_data[i]) {
complieData[i] = complieData[i] + it;
}
}
//create
Room oneRoom = new Room(complieData);
rooms.add(oneRoom);
//clear the room information to get ready for the new room
roomInfo = "";
}
//case 2: it is reading at the middle/not at the end of the line
else
{
//add the information being read to the temporary container
roomInfo += (char)current;
}
}
//close the file reader
inputFileReader.close();
//return the list of rooms
return rooms;
}
/**
* This method adds new room to the csv file
* @param roomWriter is the FileWriter that write new room data to the original csv file
* @param newRoom contains the seven properties of the new room
* @return
*/
@Override
public boolean addRoom(FileWriter roomWriter, String[] newRoom)
{
//check if the format of the data is correct
if(newRoom.length != 7)
return false;
try {
//add every property of the room with comma and space to the csv file
for(int i = 0; i < newRoom.length-1; i++)
{
roomWriter.write(newRoom[i] + ",");
}
//add the last property (the price) to the file and a new line to the csv file
roomWriter.write(newRoom[newRoom.length-1] + "\n");
roomWriter.close();
} catch (IOException e) {
e.printStackTrace();
return false;
}
return true;
}
}