Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package task3;
- import java.io.BufferedReader;
- import java.io.FileReader;
- import java.io.FileWriter;
- import java.io.PrintWriter;
- import java.util.Date;
- /*
- * Helpdesk class, containing all file I/O functionality.
- */
- public class Helpdesk {
- /*
- * Class fields -
- * record -> array containing all other class fields, initialised in constructor
- * recordID -> unique string used to identify individual questions
- * question -> user-supplied question
- * answer -> user-supplied answer
- * status -> contains either "Pending" or "Answered" depending on state
- * questionDate -> date that the question was posted
- * answerDate -> date that the answer was posted
- * delimiter -> delimiter used to convert Strings to String arrays
- */
- private String record[] = null; // null fields are initialised at a later time
- private String recordID = "0", question = "-", answer = "-", status = "Pending";
- private Date questionDate = null, answerDate = null;
- private char delimiter = '|'; // global delimiter
- /*
- * Helpdesk constructor -
- * - Assigns questionDate to the time of object instantiation
- * - Assigns recordID to the String value of the system time in milliseconds
- * (the number of milliseconds since 00:00 on January 1st, 1970)
- * - Assigns question to the contents of the string passed to the constructor
- * as an argument
- * - Calls the addQuestion() method
- */
- Helpdesk(String question) {
- questionDate = new Date();
- this.recordID = String.valueOf(questionDate.getTime()); // String.valueOf() converts
- this.question = question; // an Object to a String
- addQuestion();
- }
- /*
- * writeRecord() method -
- * - Takes one string array argument
- * - Instantiates a FileWriter object to write to a file named "archive.txt", and
- * appends to that file.
- * - Instantiates a PrintWriter object to be used in future references to the file
- * - Contains a loop that iterates five times, writing the contents of each index to
- * the file, followed by the delimiter. On the final iteration, the contents of
- * the index plus one (the final element of the array) is also written, followed
- * by two delimiters to signify the end of the record.
- * - Closes the file
- *
- */
- public void writeRecord(String[] record) {
- try {
- FileWriter file = new FileWriter("archive.txt", true); // "true" here signifies appending
- PrintWriter fileOut = new PrintWriter(file);
- for (int index = 0; index < 5; index++) {
- fileOut.print(record[index] + delimiter); // one delimiter for indexes 0 to 4
- if (index == 4) {
- fileOut.println(record[index + 1] + delimiter + delimiter); // two delimiters for index 5
- }
- }
- fileOut.close();
- } catch (Exception exc) {
- exc.printStackTrace();
- }
- }
- /*
- * addQuestion() method -
- * - Assigns the previously declared record field a 6-element String array
- * - Assigns each element
- * - Calls the writeRecord() method
- */
- public void addQuestion() {
- record = new String[6];
- record[0] = this.recordID;
- record[1] = questionDate.toString(); // Object.toString() returns String equivalent of Object
- record[2] = this.question;
- record[3] = status;
- record[4] = "-"; // record[4] and record[5] are initialised to meaningful values later
- record[5] = "-";
- writeRecord(record);
- }
- /*
- * addAnswer() method -
- * - Takes two String arguments
- * - Assigns answerDate to the time that the method is called
- * - Assigns status to "Answered"
- * - Assigns the previously declared record field to the return value of readRecord()
- * - Assigns the last three elements of the record array
- * - Calls the writeRecord() method
- */
- public void addAnswer(String answer, String recordID) {
- answerDate = new Date();
- this.answer = answer;
- status = "Answered"; // status is no longer "Pending"
- record = readRecord(recordID);
- if (record == null) { // checks to make sure the record exists, and takes appropriate
- System.out.println("Record does not exist."); // action if it does not
- return; // returns to caller
- }
- record[3] = status;
- record[4] = this.answer;
- record[5] = answerDate.toString();
- writeRecord(record);
- }
- /*
- * readRecord() method -
- * - Declares a String array variable and assigns it to null
- * - Instantiates a BufferedReader object used a reference to the file to be read
- * - Declares a String object to hold a string of class fields
- * - While there are lines to be read in the file, assigns the String array
- * variable to the return value of the split String variable. If the first
- * element matches the argument passed to the method, the array is returned to
- * the caller
- * - Closes the file
- * - Returns null, if no recordArray[0] elements matched the argument passed
- */
- public String[] readRecord(String recordID) {
- String[] recordArray = null;
- try {
- BufferedReader fileIn = new BufferedReader(new FileReader("archive.txt")); // file name
- String record = null;
- while ((record = fileIn.readLine()) != null) {
- recordArray = record.split("[" + delimiter + "]"); // split string using delimiter
- if (recordID.equals(recordArray[0]))
- return recordArray;
- }
- fileIn.close();
- } catch (Exception exc) {
- exc.printStackTrace();
- }
- return null;
- }
- public static void main(String[] args) {
- Helpdesk helpdesk = new Helpdesk("Question goes here.");
- helpdesk.addAnswer("Answer goes here.", "1328889314890");
- }
- }
Add Comment
Please, Sign In to add comment