/** WaitingRoom.java models a doctor's waiting room
 */
import java.util.*; // LinkedList, Scanner, ...
import java.io.*;   // File, FileNotFoundException, ...

public class WaitingRoom {
  	public static void main(String[] args) {
     WaitingRoom self = new WaitingRoom("today.txt");
     self.run();
  }

  public WaitingRoom(String fileName) {
     readAppointmentList(fileName);
     myWaitList = new LinkedList<String>();
  }

  private void readAppointmentList(String fileName) {
     myApptList = new LinkedList<String>();
     Scanner fin = null;
     try {
        fin = new Scanner(new File(fileName));
        while (fin.hasNextLine()) {
           String patient = fin.nextLine();
           myApptList.add(patient);
        }
     } catch (FileNotFoundException fnfe) {
        fnfe.printStackTrace();
     } finally {
        fin.close();
     }
  }

  public void run() {
     while (!myApptList.isEmpty()) {
        processArrival();
     }
  }

  private void processArrival() {
     System.out.println("\nWelcome to Dr. Suture’s office!");
     System.out.print("Enter your name: ");
     String name = keyboard.nextLine();
     if ( name.equals("99") && !myWaitList.isEmpty() ) {
        myWaitList.removeFirst();
     } else if ( myApptList.remove(name) ) {
        myWaitList.add(name);
        System.out.println("Thank you. Please have a seat.");
     } else {
        System.out.println(name + " has no appointment today.");
     }
     System.out.println("Currently waiting: " + myWaitList);
     System.out.println("Today's appointments: " + myApptList);
  }

  private LinkedList<String> myWaitList = null;
  private LinkedList<String> myApptList = null;
  private Scanner keyboard = new Scanner(System.in);
}
