Guest User

Untitled

a guest
Jun 25th, 2018
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.61 KB | None | 0 0
  1. package task3;
  2.  
  3. import java.io.BufferedReader;
  4. import java.io.FileReader;
  5. import java.io.FileWriter;
  6. import java.io.PrintWriter;
  7. import java.util.Date;
  8.  
  9. /*
  10.  * Helpdesk class, containing all file I/O functionality.
  11.  */
  12.  
  13. public class Helpdesk {
  14.    
  15.     /*
  16.      * Class fields -
  17.      *      record -> array containing all other class fields, initialised in constructor
  18.      *      recordID -> unique string used to identify individual questions
  19.      *      question -> user-supplied question
  20.      *      answer -> user-supplied answer
  21.      *      status -> contains either "Pending" or "Answered" depending on state
  22.      *      questionDate -> date that the question was posted
  23.      *      answerDate -> date that the answer was posted
  24.      *      delimiter -> delimiter used to convert Strings to String arrays
  25.      */
  26.    
  27.     private String record[] = null; // null fields are initialised at a later time
  28.     private String recordID = "0", question = "-", answer = "-", status = "Pending";
  29.     private Date questionDate = null, answerDate = null;
  30.     private char delimiter = '|'; // global delimiter
  31.    
  32.     /*
  33.      * Helpdesk constructor -
  34.      *      - Assigns questionDate to the time of object instantiation
  35.      *      - Assigns recordID to the String value of the system time in milliseconds
  36.      *          (the number of milliseconds since 00:00 on January 1st, 1970)
  37.      *      - Assigns question to the contents of the string passed to the constructor
  38.      *          as an argument
  39.      *      - Calls the addQuestion() method
  40.      */
  41.    
  42.     Helpdesk(String question) {
  43.         questionDate = new Date();
  44.         this.recordID = String.valueOf(questionDate.getTime()); // String.valueOf() converts
  45.         this.question = question;                               // an Object to a String
  46.         addQuestion();
  47.     }
  48.    
  49.     /*
  50.      * writeRecord() method -
  51.      *      - Takes one string array argument
  52.      *      - Instantiates a FileWriter object to write to a file named "archive.txt", and
  53.      *          appends to that file.
  54.      *      - Instantiates a PrintWriter object to be used in future references to the file
  55.      *      - Contains a loop that iterates five times, writing the contents of each index to
  56.      *          the file, followed by the delimiter. On the final iteration, the contents of
  57.      *          the index plus one (the final element of the array) is also written, followed
  58.      *          by two delimiters to signify the end of the record.
  59.      *      - Closes the file
  60.      *     
  61.      */
  62.    
  63.     public void writeRecord(String[] record) {
  64.         try {
  65.             FileWriter file = new FileWriter("archive.txt", true); // "true" here signifies appending
  66.             PrintWriter fileOut = new PrintWriter(file);
  67.            
  68.             for (int index  = 0; index < 5; index++) {
  69.                 fileOut.print(record[index] + delimiter); // one delimiter for indexes 0 to 4
  70.                 if (index == 4) {
  71.                     fileOut.println(record[index + 1] + delimiter + delimiter); // two delimiters for index 5
  72.                 }
  73.             }
  74.            
  75.             fileOut.close();
  76.         } catch (Exception exc) {
  77.             exc.printStackTrace();
  78.         }
  79.     }
  80.    
  81.     /*
  82.      * addQuestion() method -
  83.      *      - Assigns the previously declared record field a 6-element String array
  84.      *      - Assigns each element
  85.      *      - Calls the writeRecord() method
  86.      */
  87.    
  88.     public void addQuestion() {
  89.         record = new String[6];
  90.        
  91.         record[0] = this.recordID;
  92.         record[1] = questionDate.toString(); // Object.toString() returns String equivalent of Object
  93.         record[2] = this.question;
  94.         record[3] = status;
  95.         record[4] = "-"; // record[4] and record[5] are initialised to meaningful values later
  96.         record[5] = "-";
  97.        
  98.         writeRecord(record);
  99.     }
  100.    
  101.     /*
  102.      * addAnswer() method -
  103.      *      - Takes two String arguments
  104.      *      - Assigns answerDate to the time that the method is called
  105.      *      - Assigns status to "Answered"
  106.      *      - Assigns the previously declared record field to the return value of readRecord()
  107.      *      - Assigns the last three elements of the record array
  108.      *      - Calls the writeRecord() method
  109.      */
  110.    
  111.     public void addAnswer(String answer, String recordID) {
  112.         answerDate = new Date();
  113.         this.answer = answer;
  114.        
  115.         status = "Answered"; // status is no longer "Pending"
  116.         record = readRecord(recordID);
  117.         if (record == null) { // checks to make sure the record exists, and takes appropriate
  118.             System.out.println("Record does not exist."); // action if it does not
  119.             return; // returns to caller
  120.         }
  121.        
  122.         record[3] = status;
  123.         record[4] = this.answer;
  124.         record[5] = answerDate.toString();
  125.        
  126.         writeRecord(record);
  127.     }
  128.    
  129.     /*
  130.      * readRecord() method -
  131.      *      - Declares a String array variable and assigns it to null
  132.      *      - Instantiates a BufferedReader object used a reference to the file to be read
  133.      *      - Declares a String object to hold a string of class fields
  134.      *      - While there are lines to be read in the file, assigns the String array
  135.      *          variable to the return value of the split String variable. If the first
  136.      *          element matches the argument passed to the method, the array is returned to
  137.      *          the caller
  138.      *      - Closes the file
  139.      *      - Returns null, if no recordArray[0] elements matched the argument passed
  140.      */
  141.    
  142.     public String[] readRecord(String recordID) {
  143.         String[] recordArray = null;
  144.        
  145.         try {
  146.             BufferedReader fileIn = new BufferedReader(new FileReader("archive.txt")); // file name
  147.             String record = null;
  148.             while ((record = fileIn.readLine()) != null) {
  149.                 recordArray = record.split("[" + delimiter + "]"); // split string using delimiter
  150.                 if (recordID.equals(recordArray[0]))
  151.                     return recordArray;
  152.             }
  153.            
  154.             fileIn.close();
  155.         } catch (Exception exc) {
  156.             exc.printStackTrace();
  157.         }
  158.    
  159.         return null;
  160.     }
  161.    
  162.     public static void main(String[] args) {
  163.         Helpdesk helpdesk = new Helpdesk("Question goes here.");
  164.         helpdesk.addAnswer("Answer goes here.", "1328889314890");
  165.     }
  166. }
Add Comment
Please, Sign In to add comment