-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathMessageHandler.java
More file actions
187 lines (163 loc) · 6.21 KB
/
MessageHandler.java
File metadata and controls
187 lines (163 loc) · 6.21 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
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
import java.io.*;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Objects;
//import java.util.Scanner;
/**
* Message Handler class handles all message-related functions. Uses I/O file
* system to handle messages.
*
* @author Randy Pham
*/
public class MessageHandler implements Serializable {
private String fileDir = "Messages/";
// default constructor
public MessageHandler() {
}
/**
* This method will either create a new <code>File</code> that will hold the
* contents of the message between the Doctor and Patient or write to an
* existing <code>File</code> that holds previous messages between Doctor and
* Patient. The file is created with the hashcodes of the doctor object and
* patient object respectfully in the "Messages/" folder. Example file:
* Messages/111111222222.txt
*
* @param account - the doctor object
* @param patient - the patient object
* @param message - the message itself
* @throws IOException
*/
public void writeMessage(Account account, Patient patient, String message) throws IOException {
File messageFile;
FileWriter fileWriter = null;
BufferedWriter bufferedWriter = null;
Doctor doctor = (Doctor) account;
// using hash code for doctor and patient to link messages together
// converted both hashes to strings
String accountHash = doctor.getDoctorFirstName();
String patientHash = patient.getPatientFirstName();
// file name for the file
String fileName = fileDir + accountHash + patientHash + ".txt";
Path dir = Paths.get("./Messages");
if(!Files.isDirectory(dir)){
new File("./Messages").mkdirs();
}
messageFile = new File(fileName);
// this checks if the file already exists
if (messageFile.exists()) {
// if file already exists, no need to create a new file
// simply append message to existing file
// filewriter in append mode
fileWriter = new FileWriter(messageFile, true);
bufferedWriter = new BufferedWriter(fileWriter);
// create a new line after previous message and write new message to file
bufferedWriter.newLine();
bufferedWriter.write(message);
// close writer to prevent leak
bufferedWriter.close();
System.out.println(fileName + " updated");
} else {
// create new file with that file name
messageFile.createNewFile();
// writing to new file
fileWriter = new FileWriter(messageFile);
bufferedWriter = new BufferedWriter(fileWriter);
// write the message to the file
bufferedWriter.write(message);
// close writer to prevent leak
bufferedWriter.close();
System.out.println("new file writing done at: " + fileName);
}
}
/**
* This method reads existing files from the Messages/ folder and returns a
* string containing the contents of the file. If there is no message found, it
* throws an exception. Note: it already appends "Messages/" to the param
* filename
*
* @param filename - the name of the file
* @throws IOException
*/
public String readMessage(String filename) throws IOException {
File messageFile;
// Scanner reader;
String content; // flag, message;
// int numOfLines = 0, rowNum = 0;
messageFile = findMessage(fileDir + filename);
if (messageFile == null) {
// UH OH THERE IS NO EXISTING MESSAGE
System.out.println("Cannot find message at: " + messageFile);
return "No existing messages";
} else {
/*
* // count number of lines numOfLines = countLines(fileDir + filename); reader
* = new Scanner(messageFile);
*
* // make an array to store the messages // array has row = numOfLines and col
* = 2 messageArray = new String[numOfLines][2];
*
* while (reader.hasNextLine()) { line = reader.nextLine(); flag =
* line.substring(0, 2); message = line.substring(2); // store in array
* messageArray[rowNum][0] = flag; messageArray[rowNum][1] = message; rowNum++;
* }
*
*
*
* reader.close();
*/
/*
* reader = new Scanner(messageFile);
*
* while (reader.hasNextLine()) { line = reader.nextLine(); // display the line
* to the gui System.out.println(line); }
*
* reader.close();
*/
Path pathFromFile = messageFile.toPath();
content = Files.readString(pathFromFile);
return content;
}
}
/**
* This method finds existing messages. If there is no message, then we return
* null
*
* @param filename - the name of the message
* @return either the file or null if file not found
*/
public File findMessage(String filename) {
File fileToReturn, tempFile;
tempFile = new File(filename);
if (tempFile.exists()) {
fileToReturn = tempFile;
} else {
fileToReturn = null;
}
return fileToReturn;
}
/**
* This method returns the hashcode for the object
*
* @param object - the object that is sending the message
* @return the hashcode for the sender
*/
public int getHashCode(Object object) {
int hashCode = Objects.hashCode(object);
return hashCode;
}
/**
* This helper method is to read the number of lines in a file
*
* @param filename - the name of the file
* @return the number of lines in a file
*
* private int countLines(String filename) throws IOException { int
* count = 0; BufferedReader reader = new BufferedReader(new
* FileReader(filename));
*
* while (reader.readLine() != null) { count++; }
*
* reader.close(); return count; }
*/
}