20-CS-122-001 Computer Science II Spring 2012
Inheritance: Employee Data Base

Virtual functions, classes, inheritance, lists, queues, stacks, applications

   
import java.io.*;
import java.util.*;

class Cell {
   Object data;
   Cell next;

   Cell (Object dat, Cell nxt) {
      data = dat;
      next = nxt;
   }
}

class List {
   Cell head;
   Cell tail;

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

class Employee {
   String name;

   Employee() {}
   Employee (String nm) { name = nm; }
   double computePay() { return -1; }          // pure virtual
   void   display () {}
   void   setHours(double hrs) {}
   void   setSales(double sales) {}
   void   setSalary(double salary) { System.out.println("NO!"); }   
}

class EmployeeList extends List {

   EmployeeList() {}
   
   Employee find (String name) {
      for (Cell f = head ; f != null ; f = f.next)
         if (((Employee)(f.data)).name == name)         
            return (Employee)f.data;
      return null;
   }

   void setHours(String name, int hrs) {
      Employee employee;
      
      if ((employee = find(name)) != null) employee.setHours(hrs);
   }

   void setSalary(String name, float salary) {
      Employee employee;
      
      if ((employee = find(name)) != null) employee.setSalary(salary);
   }
   
   void setSales(String name, double sales) {
      Employee employee;
      
      if ((employee = find(name)) != null) employee.setSales(sales);
   }

   double payroll() {
      double total=0;

      for (Cell f = head ; f != null ; f = f.next)
         total += ((Employee)(f.data)).computePay();
      return total;
   }

   void display() {
      for (Cell f = head ; f != null ; f = f.next)
         ((Employee)(f.data)).display();
      System.out.println();
   }
}

class WageEmployee extends Employee {
   double rate;
   double hours;

   WageEmployee(String nm) { super(nm); }
   WageEmployee(String nm, double r) { super(nm); rate = r; }
   void setRate(double r)    { rate  = r; }
   void setHours(double hrs) { hours = hrs; }
   double computePay()       { return rate*hours; }
}

class Programmer extends WageEmployee {
   Programmer (String nm, double w) { super(nm, w); }
   
   void display() {
      System.out.println("Name: "+name+"\tHours: "+hours
                          +"\tRate: "+rate);
   }
}

class SalesPerson extends WageEmployee {
   double commission;
   double SalesMade;

   SalesPerson (String nm, double c) {
      super(nm);
      commission = c;
   }
   
   void setCommission(double comm) { commission = comm; }
   
   void setSales(double sales) { SalesMade = sales; }
   
   double computePay() { return commission*SalesMade; }
   
   void display() {
      System.out.println("Name: "+name+"\tCommission: "+commission
                          +"\tSales: "+SalesMade);
   }
}

class Manager extends Employee {
   double monthlysalary;

   Manager () { super(""); }
   
   Manager(String nm, double w)  { super(nm); monthlysalary = w; }
   
   void setSalary(double salary) { monthlysalary = salary; }
   
   double computePay()           { return monthlysalary; }
   
   void display() {
      System.out.println("Name: "+name+"\tMonthly Salary: "+
                          monthlysalary);
   }
}

interface ManagerInterface {
   double managerComputePay();
   void managerDisplay();
}

class SalesManager extends SalesPerson implements ManagerInterface {
   double monthlysalary;
   
   SalesManager(String nm, double w) { super (nm, w); }

   public double managerComputePay() {
      return monthlysalary;
   }
   
   double computePay() {
      return super.computePay() + managerComputePay();
   }
   
   public void managerDisplay() {
      System.out.println("Name: "+name+"\tMonthly Salary: "+monthlysalary);
   }

   void display() {
      super.display();
      managerDisplay();
   }
}

public class Wages {
   public static void main(String argv[]) {
      EmployeeList emp = new EmployeeList();
      emp.enqueue(new SalesManager("Gee", 1000));
      emp.enqueue(new SalesManager("Gal", 1000));
      emp.enqueue(new SalesManager("Gem", 1000));
      emp.enqueue(new SalesPerson("John", 0.03));
      emp.enqueue(new SalesPerson("Joan", 0.04));
      emp.enqueue(new SalesPerson("Jack", 0.02));
      emp.enqueue(new Manager("Fred", 10000));
      emp.enqueue(new Manager("Frank", 5000));
      emp.enqueue(new Manager("Florence", 3000));
      emp.enqueue(new Programmer("Linda", 7));
      emp.enqueue(new Programmer("Larry", 5));
      emp.enqueue(new Programmer("Lewis", 3));

      emp.setHours("Linda", 35);
      emp.setHours("Larry", 23);
      emp.setHours("Lewis", 3);
      emp.setSales("John", 12000);
      emp.setSales("Joan", 10000);
      emp.setSales("Jack", 5000);
      emp.setSales("Gee", 4000);
      emp.setSales("Gal", 3000);
      emp.setSales("Gem", 2000);
      emp.setSalary("Gee", 1000);
      emp.setSalary("Gal", 2000);
      emp.setSalary("Gem", 3000);
      emp.display();
      System.out.println("Payroll: "+emp.payroll());
   }
}


// Sample Output
//   NO!
//   NO!
//   NO!
//   Name: Gee       Commission: 1000.0      Sales: 4000.0
//   Name: Gee       Monthly Salary: 0.0
//   Name: Gal       Commission: 1000.0      Sales: 3000.0
//   Name: Gal       Monthly Salary: 0.0
//   Name: Gem       Commission: 1000.0      Sales: 2000.0
//   Name: Gem       Monthly Salary: 0.0
//   Name: John      Commission: 0.03        Sales: 12000.0
//   Name: Joan      Commission: 0.04        Sales: 10000.0
//   Name: Jack      Commission: 0.02        Sales: 5000.0
//   Name: Fred      Monthly Salary: 10000.0
//   Name: Frank     Monthly Salary: 5000.0
//   Name: Florence  Monthly Salary: 3000.0
//   Name: Linda     Hours: 35.0     Rate: 7.0
//   Name: Larry     Hours: 23.0     Rate: 5.0
//   Name: Lewis     Hours: 3.0      Rate: 3.0
//
//   Payroll: 9019229.0