Hi guys,
I have this assignment and I am puzzled. Please help. Thanks.
Step 2: In this part, we add implementation to existing design
1. Add a boolean instance variable to GreenhouseControls called fans. Create two Event classes called FansOn and FansOff. The action() of these two classes should modify fan to true or false respectively.
2. Modify Bell Event so that it will be able to run an arbitrary number of times separated by 2000 msec each. To facilitate this requirement, you should generate the number of Bell Events specified in the rings parameter in the examples file. Please pay special attention to the possibility that other events might be run in between the various Bell events.
3. Modify Restart.action() to start the the system by reading events from a text file. Use Scanner and an appropriate regular expression.
4. Try running GreenhouseControls by:
java GreenhouseControls -f examples1.txtu
and
java GreenhouseControls -f examples2.txt
5. The -f argument must be present. It must be either -f or -d. Please see Part 4. The event information must be in the format specified in examples1.txt and examples2.txt.
Code with my modifications:
import java.io.*; import java.util.Calendar; import tme3.*; public class GreenhouseControls extends Controller { private boolean fans=false; private boolean light = false; private boolean water = false; private String thermostat = "Day"; private String eventsFile = "examples1.txt"; public class FansOn extends Event { public FanOn(long delayTime) { super(delayTime); } public void action() { // Put hardware control code here to // physically turn on the fan. fans = true; } public String toString() { return "Fan is on"; } } public class FansOff extends Event { public FansOff(long delayTime) { super(delayTime); } public void action() { // Put hardware control code here to // physically turn off the fan. fans = false; } public String toString() { return "Fan is off"; } } public class LightOn extends Event { public LightOn(long delayTime) { super(delayTime); } public void action() { // Put hardware control code here to // physically turn on the light. light = true; } public String toString() { return "Light is on"; } } public class LightOff extends Event { public LightOff(long delayTime) { super(delayTime); } public void action() { // Put hardware control code here to // physically turn off the light. light = false; } public String toString() { return "Light is off"; } } public class WaterOn extends Event { public WaterOn(long delayTime) { super(delayTime); } public void action() { // Put hardware control code here. water = true; } public String toString() { return "Greenhouse water is on"; } } public class WaterOff extends Event { public WaterOff(long delayTime) { super(delayTime); } public void action() { // Put hardware control code here. water = false; } public String toString() { return "Greenhouse water is off"; } } public class ThermostatNight extends Event { public ThermostatNight(long delayTime) { super(delayTime); } public void action() { // Put hardware control code here. thermostat = "Night"; } public String toString() { return "Thermostat on night setting"; } } public class ThermostatDay extends Event { public ThermostatDay(long delayTime) { super(delayTime); } public void action() { // Put hardware control code here. thermostat = "Day"; } public String toString() { return "Thermostat on day setting"; } } // An example of an action() that inserts a // new one of itself into the event list: public class Bell extends Event { public Bell(long delayTime) { super(delayTime); } public void action() { // nothing to do addEvent(new Bell(2000)); } public String toString() { return "Bing!"; } } public class Restart extends Event { public Restart(long delayTime, String filename) { super(delayTime); eventsFile = filename; } public void action() { addEvent(new ThermostatNight(0)); addEvent(new LightOn(2000)); addEvent(new WaterOff(8000)); addEvent(new ThermostatDay(10000)); addEvent(new Bell(9000)); addEvent(new WaterOn(6000)); addEvent(new LightOff(4000)); addEvent(new Terminate(12000)); } public String toString() { return "Restarting system"; } } public class Terminate extends Event { public Terminate(long delayTime) { super(delayTime); } public void action() { System.exit(0); } public String toString() { return "Terminating"; } } public static void printUsage() { System.out.println("Correct format: "); System.out.println(" java GreenhouseControls -f <filename>, or"); System.out.println(" java GreenhouseControls -d dump.out"); } //--------------------------------------------------------- public static void main(String[] args) { try { String option = args[0]; String filename = args[1]; if ( !(option.equals("-f")) && !(option.equals("-d")) ) { System.out.println("Invalid option"); printUsage(); } GreenhouseControls gc = new GreenhouseControls(); if (option.equals("-f")) { gc.addEvent(gc.new Restart(0,filename)); } gc.run(); } catch (ArrayIndexOutOfBoundsException e) { System.out.println("Invalid number of parameters"); printUsage(); } } } ///:~