Employee Payroll Solution in Java
(see also the comparable C++ solution)
import java.io.*;
import java.util.*;
class Cell
{
Object data;
Cell next;
Cell (Object dat, Cell nxt)
{
data = dat;
next = nxt;
}
void setNext(Cell n) { next = n; }
Cell getNext() { return next; }
Object objectOf() { return data; }
}
class List
{
Cell head;
Cell tail;
List () { head = tail = null; }
void enqueue (Object obj)
{
if (head == null)
{
head = tail = new Cell(obj, null);
return;
}
tail.setNext(new Cell(obj, null));
tail = tail.getNext();
}
Cell getHead() { return head; }
}
class Employee
{
String name;
Employee() {}
Employee (String nm) { name = nm; }
String getName() { return name; }
double computePay() { return -1; } // pure virtual
void display () {}
void setHours(double hrs) {}
void setSales(double sales) {}
}
class EmployeeList extends List
{
EmployeeList() {}
void setHours(String name, int hrs)
{
for (Cell f = getHead() ; f != null ; f = f.getNext())
{
if (((Employee)(f.objectOf())).getName() == name)
{
((Employee)(f.objectOf())).setHours(hrs);
break;
}
}
}
void setSales(String name, double sales)
{
for (Cell f = getHead() ; f != null ; f = f.getNext())
{
if (((Employee)(f.objectOf())).getName() == name)
{
((Employee)(f.objectOf())).setSales(sales);
break;
}
}
}
double payroll()
{
double total=0;
for (Cell f = getHead() ; f != null ; f = f.getNext())
total += ((Employee)(f.objectOf())).computePay();
return total;
}
void display()
{
for (Cell f = getHead() ; f != null ; f = f.getNext())
((Employee)(f.objectOf())).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 getHours() { return hours; }
double getRate() { return rate; }
double computePay() { return rate*hours; }
}
class Programmer extends WageEmployee
{
Programmer (String nm, double w) { super(nm, w); }
void display()
{
System.out.println("Name: "+getName()+
"\tHours: "+getHours()+
"\tRate: "+getRate());
}
}
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: "+getName()+
"\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: "+getName()+
"\tMonthly Salary: "+ monthlysalary);
}
}
public class Wages
{
public static void main(String argv[])
{
EmployeeList emp = new EmployeeList();
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.display();
System.out.println("Payroll: "+emp.payroll());
}
}