package simulator; import java.io.BufferedWriter; import java.io.FileWriter; import java.io.IOException; /* * FileLogger.java * Created on Jan 6, 2005 * */ /** * FileLogger - This is a particular implementatioon of the Logger interface * In particular, it logs all the events to a pre-specified file. * * @see Logger * @author Ping * */ public class FileLogger implements Logger { protected final static String FILE_NAME = "park_log.txt"; //private AmusementPark park; protected BufferedWriter writer; protected Time clock; public FileLogger() { } /** * This function is called when the park first opens, and initializes * the file writers. * * @param openingTime - The time when the park opens */ public void parkOpened(Time openingTime) { // Remember we are just holding on to a reference of it. // We should not modify it clock = openingTime; // creates a new file writer that appends to the log file try { writer = new BufferedWriter(new FileWriter(FILE_NAME, true)); writer.write("--------------------------------------------------------"); writer.newLine(); writer.write("Park opened at " + clock.toString()); writer.newLine(); writer.write("--------------------------------------------------------"); writer.newLine(); writer.flush(); } catch(IOException e) { System.out.println("Unable to open file writer."); e.printStackTrace(); } } /** * This function should be called when the park closes and it will close * the file writer. */ public void parkClosed() { try { writer.write("--------------------------------------------------------"); writer.newLine(); writer.write("Park closed at " + clock.toString()); writer.newLine(); writer.write("--------------------------------------------------------"); writer.newLine(); writer.flush(); // close the writer writer.close(); } catch(IOException e) { e.printStackTrace(); } } /** * This function logs the event of a customer arriving at a particular attraction * @param customer - The arriving customer * @param attraction - The attraction that the customer arrived at */ public void arriveAtAttraction(Customer customer, Attraction attraction) { String output = "Rider " + customer.getName() + " arrived at " + attraction.getName() + " at " + clock.toString(); try { writer.write(output); writer.newLine(); writer.flush(); } catch(IOException e) { // error writing System.out.println("Unable to write \"" + output + "\" to log file."); e.printStackTrace(); } } /** * This function logs the event of a customer actually getting on a * particular attraction * @param customer - The customer * @param attraction - The attraction that the customer boarded */ public void boardAttraction(Customer customer, Attraction attraction) { String output = "Rider " + customer.getName() + " boarded attraction " + attraction.getName() + " at " + clock.toString(); try { writer.write(output); writer.newLine(); writer.flush(); } catch(IOException e) { // error writing System.out.println("Unable to write \"" + output + "\" to log file."); e.printStackTrace(); } } /** * This function logs the event of a customer exiting an attraction * * @param customer - The exiting customer * @param attraction - The attraction that the customer exited from */ public void exitAttraction(Customer customer, Attraction attraction) { String output = "Rider " + customer.getName() + " exited attraction " + attraction.getName() + " at " + clock.toString(); try { writer.write(output); writer.newLine(); writer.flush(); } catch(IOException e) { // error writing System.out.println("Unable to write \"" + output + "\" to log file."); e.printStackTrace(); } } /** * This function logs the customer's decision on where to go next. * * @param customer - The customer who has just decided where to go * @param nextStop - The next attraction that the customer decides to go to. Note * this should be null if the customer is heading to the exit. */ public void nextAttraction(Customer customer, Attraction nextStop) { StringBuffer output = new StringBuffer("Rider " + customer.getName() + " is heading towards "); // if the attraction is null, the customer is going home if(nextStop == null) { output.append("the exit"); } else { output.append(nextStop.getName()); } output.append(" at " + clock.toString()); try { writer.write(output.toString()); writer.newLine(); writer.flush(); } catch(IOException e) { // error writing System.out.println("Unable to write \"" + output.toString() + "\" to log file."); e.printStackTrace(); } } }