Interfaces - Find
import java.io.*;
import java.util.*;
class CellObject
{
void display() { System.out.println("Sorry"); }
String locate() { return null; }
}
class IntObject extends CellObject
{
int number;
IntObject (int numb) { number = numb; }
void display () { System.out.print(number + " "); }
}
class StringObject extends CellObject
{
String string;
StringObject (String str) { string = str; }
void display () { System.out.print(string + " "); }
}
class NullObject extends CellObject
{
NullObject () {}
}
class Employee extends CellObject
{
String name;
String ident;
Employee (String nm, String id) { name = nm; ident = id; }
String getName () { return name; }
String getIdent () { return ident; }
void setHours (int mn, double hrs) {}
void setWages (double wgs) {}
double computePay (int mn) { return 0.0; }
void display() {}
void display(int mn) {}
}
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 getWages () { return wages; }
double getHours (int mn) { return hours[mn]; }
void setWages (double wgs) { wages = wgs; }
void setHours (int mn, double hrs) { hours[mn] = hrs; }
void display ()
{
System.out.println("Wage employee name: " + super.getName()
+ "\tID: " + super.getIdent()
+ "\tWages: $" + wages + " /Hr");
}
void display (int mn)
{
System.out.println("Wage employee name: " + super.getName()
+ "\tID: " + super.getIdent()
+ "\tWages: $" + wages + " /Hr\n Month: " + month[mn]
+ "\tHours: " + hours[mn] + "\tPay: $" + computePay(mn));
}
double computePay(int mn) { return hours[mn]*wages; }
}
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 ()
{
System.out.println("Programmer name: " + getName()
+ "\tID: " + getIdent()
+ "\tWages: $" + getWages() + " /Hr");
}
}
//
// Change Cell to store objects - force enqueued items to be objects.
// Then we have full control over what functions are available for objects.
//
class Cell
{
CellObject object;
Cell next;
Cell (CellObject obj, Cell nxt) { object = obj; next = nxt; }
CellObject objectOf() { return object; }
Cell getNext() { return next; }
void setNext(Cell nxt) { next = nxt; }
}
class Queue
{
Cell head;
Cell 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.setNext(new Cell(obj, null));
tail = tail.getNext();
}
CellObject dequeue()
{
if (head == null) return null;
CellObject obj = head.objectOf();
head = head.getNext();
return obj;
}
boolean empty () { return head == null; }
void display()
{
for (Cell p = head ; p != null ; p = p.getNext())
p.objectOf().display();
System.out.println();
}
CellObject find(String id)
{
for (Cell p = head ; p != null ; p = p.getNext())
if (locator.find(p.objectOf()) == id) return p.objectOf();
return null;
}
void setLocator (FindFunc loc) { locator = loc; }
}
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();
}
}
public class Prog14
{
public static void main (String argv[])
{
Queue q = new Queue(new IDFindFunc());
q.enqueue(new Programmer("John", "034-38-3231", 34.33));
q.enqueue(new Programmer("Jim", "923-12-4422", 62.12));
q.enqueue(new Programmer("Joan", "888-34-3434", 12.34));
q.display();
((Employee)(q.find("923-12-4422"))).setHours(0, 34);
((Employee)(q.find("923-12-4422"))).setHours(1, 22);
((Employee)(q.find("923-12-4422"))).setHours(2, 29);
((Employee)(q.find("923-12-4422"))).display(0);
((Employee)(q.find("923-12-4422"))).display(1);
((Employee)(q.find("923-12-4422"))).display(2);
q.setLocator(new NameFindFunc());
((Employee)(q.find("Jim"))).display(0);
((Employee)(q.find("Jim"))).display(1);
((Employee)(q.find("Jim"))).display(2);
}
}