import java.awt.*;
import java.io.*;
import java.util.*;
import javax.swing.*;
import java.applet.*;


interface FindFunc { String find (CellObject obj); }

class NameFindFunc implements FindFunc {
   public String find (CellObject obj) {
      return ((Programmer)(obj)).getName();
   }
}

class IDFindFunc implements FindFunc {
   public String find (CellObject obj) {
      return ((Programmer)(obj)).getIdent();
   }
}


class Employee extends CellObject {
   String name, ident;

   Employee (String nm, String id) {  name = nm;  ident = id;  }
   void setHours (int mn, double hrs) {}
   void setWages (double wgs) {}
   double computePay (int mn) {  return 0.0;  }
   void display(JTextArea text) {}
   void display(int mn, JTextArea text) {}
}

class WageEmployee extends Employee {
  String month[] = { "January", "February", "March", "April", "May",
                     "June", "July", "August", "September", "October",
                     "November", "December" };
   double wages;
   double hours[] = new double[12];

   WageEmployee (String nm, double wgs) {
      super(nm, "");
      wages = wgs;
   }

   WageEmployee (String nm, String id, double wgs) {
      super(nm, id);
      wages = wgs;
   }

   double computePay(int mn) {  return hours[mn]*wages; }
   void setWages (double wgs) {  wages = wgs;  }
   void setHours (int mn, double hrs) { hours[mn] = hrs; }

   void display (JTextArea text) {
      text.append("Wage employee name: " + super.name
           + "\tID: " + super.ident
           + "\tWages: $" + wages + " /Hr\n");
   }

   void display (int mn, JTextArea text) {
      text.append("Wage employee name: " + super.name
           + "\tID: " + super.ident
           + "\tWages: $" + wages + " /Hr\n   Month: " + month[mn]
           + "\tHours: " + hours[mn] + "\tPay: $" + computePay(mn)+"\n");
   }
}

class Programmer extends WageEmployee {
   Programmer (String nm) {  super(nm, 0);  }
   Programmer (String nm, double wgs) {  super(nm, wgs);  }
   Programmer (String nm, String id, double wgs) { super(nm, id, wgs); }
   void display (JTextArea text) {
      text.append("Programmer name: " + name
           + "\tID: " + ident
           + "\tWages: $" + wages + " /Hr\n");
   }
}

class CellObject {
   void display (JTextArea text) { text.append("Sorry\n");  }
   String locate () { return null; }
}

class Cell {
   CellObject object;
   Cell next;

   Cell (CellObject obj, Cell nxt)  {  object = obj;  next = nxt;  }
}

class Queue {
   Cell head, tail;
   FindFunc locator;

   Queue () {  head = tail = null; locator = null;  }
   Queue (FindFunc loc) { head = tail = null; locator = loc; }

   void enqueue (CellObject obj) {
      if (head == null) { head = tail = new Cell(obj, null);  return; }
      tail.next = new Cell(obj, null);
      tail = tail.next;
   }

   CellObject dequeue() {
      if (head == null) return null;

      CellObject obj = head.object;
      head = head.next;
      return obj;
   }

   boolean empty () { return head == null; }

   void display(JTextArea text) {
      if (head == null) {
         text.appendText("(empty)\n");
         return;
      }
      for (Cell p = head ; p != null ; p = p.next)
         p.object.display(text);
      text.append("\n");
   }

   CellObject find(String id) {
      for (Cell p = head ; p != null ; p = p.next)
         if (locator.find(p.object).equals(id)) return p.object;
      return null;
   }

   void setLocator (FindFunc loc) { locator = loc; }
}

public class Prog14 extends Applet {
   JButton doit;
   JTextField name, ss, hours, wages, msg;
   JComboBox op, month, type;
   JTextArea text;
   String displayDBStr = "Display DB";
   String setHoursStr = "Set Hours";
   String addDBStr = "Add to DB";
   String empDispStr = "Worker Info";
   Queue employees;

   public void init () {
      setLayout(new BorderLayout(10,10));
      JPanel r = new JPanel();
      r.setLayout(new BorderLayout());
      r.add("North", new JLabel("Messages"));
      r.add("Center", msg = new JTextField());
      add("North", r);
      JPanel p = new JPanel();
      p.setLayout(new BorderLayout());
      p.add("North", new JLabel("Results"));
      p.add("Center", new JScrollPane(text = new JTextArea(12,30)));
      add("Center", p);
      JPanel q = new JPanel();
      q.setLayout(new GridLayout(2,4,10,10));
      JPanel q1 = new JPanel();
      q1.setLayout(new BorderLayout());
      q1.add("North", new JLabel("Name"));
      q1.add("Center", name = new JTextField());
      q.add(q1);
      JPanel q2 = new JPanel();
      q2.setLayout(new BorderLayout());
      q2.add("North", new JLabel("SS#"));
      q2.add("Center", ss = new JTextField());
      q.add(q2);
      JPanel q3 = new JPanel();
      q3.setLayout(new BorderLayout());
      q3.add("North", new JLabel("Operation"));
      q3.add("Center", op = new JComboBox());
      op.addItem(addDBStr);
      op.addItem(empDispStr);
      op.addItem(displayDBStr);
      op.addItem(setHoursStr);
      q.add(q3);
      JPanel q4 = new JPanel();
      q4.setLayout(new BorderLayout());
      q4.add("North", new JLabel("  "));
      q4.add("Center", doit = new JButton("Do It!"));
      q.add(q4);
      JPanel q5 = new JPanel();
      q5.setLayout(new BorderLayout());
      q5.add("North", new JLabel("Hours"));
      q5.add("Center", hours = new JTextField());
      q.add(q5);
      JPanel q6 = new JPanel();
      q6.setLayout(new BorderLayout());
      q6.add("North", new JLabel("Wages"));
      q6.add("Center", wages = new JTextField());
      q.add(q6);
      JPanel q7 = new JPanel();
      q7.setLayout(new BorderLayout());
      q7.add("North", new JLabel("Month"));
      q7.add("Center", month = new JComboBox());
      month.addItem("January");
      month.addItem("February");
      month.addItem("March");
      month.addItem("April");
      month.addItem("May");
      month.addItem("June");
      month.addItem("July");
      month.addItem("August");
      month.addItem("September");
      month.addItem("October");
      month.addItem("November");
      month.addItem("December");
      q.add(q7);
      JPanel q8 = new JPanel();
      q8.setLayout(new BorderLayout());
      q8.add("North", new JLabel("Employee type"));
      q8.add("Center", type = new JComboBox());
      type.addItem("Programmer");
      type.addItem("Salesman");
      type.addItem("Manager");
      q.add(q8);
      add("South", q);
      doit.addActionListener(this);
      employees = new Queue(new IDFindFunc());
   }

   public void actionPerformed (ActionEvent e) {
	   if (e.getSource() == doit) {
         if (!((String)type.getSelectedItem()).equals("Programmer")) {
            msg.setText("Sorry, only programmers are accounted for now");
            return;
         }
         String select = (String)op.getSelectedItem();
         if (select.equals(displayDBStr)) employees.display(text);
         else if (select.equals(addDBStr)) {
            boolean errors = false;
            msg.setText("");
            if (name.getText().equals("")) {
               errors = true;
               msg.setText(msg.getText()+"Name");
            }
            if (ss.getText().equals("")) {
               if (errors) msg.setText(msg.getText()+", ");
               errors = true;
               msg.setText(msg.getText()+"SS number");
            }
            if (wages.getText().equals("")) {
               if (errors) msg.setText(msg.getText()+", ");
               errors = true;
               msg.setText(msg.getText()+"Wages ");
            }
            if (errors) msg.setText(msg.getText()+" Required");
            else {
               employees.enqueue(new Programmer(name.getText(), ss.getText(),
                                 Float.valueOf(wages.getText()).floatValue()));
               msg.setText("Employee added to Data Base");
            }
         } else if (select.equals(setHoursStr)) {
            boolean errors = false;
            msg.setText("");
            if (name.getText().equals("") && ss.getText().equals("")) {
               errors = true;
               msg.setText(msg.getText()+"Name or SS number Required");
            }
            if (hours.getText().equals("")) {
               if (errors) msg.setText(msg.getText()+", ");
               errors = true;
               msg.setText(msg.getText()+"Hours Required");
            }
            if (!errors) {
               double hrs;
               String nm;

               hrs = (double)Float.valueOf(hours.getText()).floatValue();
               int mnth = month.getSelectedIndex();

               if (!name.getText().equals("")) {
                  employees.setLocator(new NameFindFunc());					
                  nm = name.getText();
               } else {
                  employees.setLocator(new IDFindFunc());			 
                  nm = ss.getText();
               }
               ((Employee)(employees.find(nm))).setHours(mnth, hrs);
               msg.setText("Hours set for employee");
            }
         } else if (select.equals(empDispStr)) {
            boolean errors = false;
            msg.setText("");
            if (name.getText().equals("") && ss.getText().equals("")) {
               errors = true;
               msg.setText(msg.getText()+"Name or SS number Required");
            }
            if (!errors) {
               String nm;
               int mnth = month.getSelectedIndex();

               if (!name.getText().equals("")) {
                  employees.setLocator(new NameFindFunc());					
                  nm = name.getText();
               } else {
                  employees.setLocator(new IDFindFunc());			 
                  nm = ss.getText();
               }
               Employee pp = ((Employee)(employees.find(nm)));
               if (pp != null) pp.display(mnth, text);
               else msg.setText("Request Not Found");
            }
         }
      }
   }
}