// Skeleton Vending Machine program
// Uses text - based output to demonstrate two-way Object interaction
// J Harvey 2002
// Version 2 - passes Id as a parameter to identify machine
import java.awt.*;
import java.applet.*;
public class VendingMachine2 extends Applet
{
public void init()
{
Controller con1 = new Controller();
}
public void paint(Graphics g)
{
}
public class Controller
{
public Controller()
{
System.out.println("Controller object constructed");
Machine mach1 = new Machine(this, 1); // amended
Machine mach2 = new Machine(this, 2); // amended
mach1.turnOnMachine();
mach2.turnOnMachine();
}
public void runningOut(int which) // amended
{
System.out.println("Machine " + which +" reports low stock"); // amended
}
}// end class B
public class Machine
{
Controller con1;
int stockRemaining=3;
int Id; // new line
public Machine(Controller bobj, int machineId) // amended
{
con1 = bobj;
Id = machineId; // new line
System.out.println("Machine object " + Id + " constructed "); // amended
}
public void turnOnMachine()
{
System.out.println("Machine " + Id + " turned on"); // amended
if (stockRemaining < 5)
con1.runningOut(Id); // amended
}
} // end Machine
}
|