你好。我在作业的最后部分遇到了麻烦,并尝试回答它,但我有点困惑

问题描述 投票:0回答:0
  1. 创建以下界面

    界面可修复{ // 打开电源,修复窗口并将错误代码清零

    无效修复();

    // 记录到当前目录中名为 fix.log 的文本文件

    // 打印到控制台,并识别时间和性质

    //修复

    无效日志();

    }

  2. 创建实现 Fixable 的内部类 PowerOn 和 FixWindow。

  3. 向 GreenhouseControls 添加方法

    int getError(); 返回保存的错误代码。

  4. 在 GreenhouseControls 中添加一个方法

    可修复 getFixable(int errorcode);

    返回适当的 Fixable 对象来纠正错误并将错误代码重置为零。

  5. 创建一个Restore类来在关机后恢复系统。当调用此命令行时:

    java GreenhouseControls -d dump.out

    将创建一个恢复对象,并将文件名 dump.put 传递给恢复对象。然后,它检索因紧急关闭而保存的 GreenhouseControls 对象。它将保存的系统状态打印到控制台。它使用适当的 Fixable 将启动关闭的事件更改的任何变量设置回其正常状态(例如,poweron=true)。然后,它会在导致关闭的事件之后的事件中启动系统运行。如果 Restore 对象捕获到任何异常或错误,控制台上应显示正确的错误消息

这是我写的代码

import java.io.\*;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Scanner;

import tme3.\*;

import tme3.Controller;
import tme3.Event;

public class GreenhouseControls extends Controller {
private boolean light = false;
private boolean water = false;
private String thermostat = "Day";
private String eventsFile = "examples1.txt";
private boolean windowork;
private boolean powerOn;
// add an instance varialbe called error code
private int errorCode;

// add boolean instance variable fans
boolean fans = false;

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";
    }
}

// FansOn class
public class FansOn extends Event {
    public FansOn(long delayTime) {
        super(delayTime);
    }

    public void action() {
        // Put hardware control code here.
        fans = true;
    }

    public String toString() {
        return "Fans are on.";
    }
}

// FansOff class
public class FansOff extends Event {
    public FansOff(long delayTime) {
        super(delayTime);
    }

    public void action() {
        // Put hardware control code here.
        fans = false;
    }

    public String toString() {
        return "Fans are off.";
    }
}

// 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);
    }

    // come back to this not finished
    public void action() {
        // add to eventList
        // 2000 msec each
        addEvent(new Bell(delayTime));
    }

    public String toString() {
        return "Bing!";
    }
}

public class WinidowMalfunction extends Event {
    
    public WinidowMalfunction(long delayTime) {
        super(delayTime);
    }
    
    //ControllerException newException = new ControllerException("Error:");

    // After setting the variables, WindowMalfunction or PowerOut should throw an
    // exception specifying the faulty condition
    public void action() throws ControllerException {
        windowork = false;
        // error code in an int variable error code -> 1 for WindowMalfunction
        errorHappened(1, "Error at WinidowMalfunction");
    }
}

public class PowerOut extends Event {
    
    public PowerOut(long delayTime) {
        super(delayTime);
    }
    public void action() throws ControllerException {
        powerOn = false;
        // error code in an int variable error code -> 2 for powerOut
        errorHappened(2, "Error at powerOut event");
    }
}

void errorHappened(int errorCode, String errorMessage) throws ControllerException {
    this.errorCode = errorCode;
    throw new ControllerException(errorMessage);
}

public static class ControllerException extends Exception {
    public ControllerException(String except) {
        super(except);
    }
    public String getMessage() {
        return super.getMessage();
    }
    public void shutdown() {
    }
}
// create interface that fix window 
interface Fixable {
     public boolean setTrue();
}
class Fix implements Fixable {
    private boolean windowork = false;
    public void setTrue() {
        windowork = true;
    }
}
class Bar implements Fixable {

    private boolean poweron = false;
    public void setTrue() {
        poweron = true;
    }
}

public class Restart extends Event {
    public Restart(long delayTime, String filename) {
        super(delayTime);
        eventsFile = filename;
    }

    public void action() {
        Scanner scanner = new Scanner(eventsFile);
        while (scanner.hasNextLine()) {
            String line = scanner.nextLine();
        }

        addEvent(new ThermostatNight(0));
        addEvent(new LightOn(2000));
        addEvent(new WaterOff(8000));
        addEvent(new ThermostatDay(10000));
        addEvent(new Bell(2000));
        addEvent(new WaterOn(6000));
        addEvent(new LightOff(4000));
        addEvent(new Terminate(12000));
        addEvent(new FansOn(5000));
        addEvent(new FansOff(1000));
        // addEvent(new WinidowMalfunction(11000));
    }

    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) {
GreenhouseControls gc = new GreenhouseControls();

     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();
    } catch (ControllerException e) {
        //
        gc.shutdown(e.getMessage());
    }

}

// override this method in GreenhouseControls to accomplish the shutdown.
@Override
protected void shutdown(String message) {
    // super.shutdown();
    DateFormat df = new SimpleDateFormat("dd/MM/yy HH:mm:ss");
    Date dateobj = new Date();

    // logs the time and the reason for the shutdown in a text file ,, Here you will
    // create file and log it
    System.out.println("Shutdown due to Error: " + message + " | Error Code:" + errorCode + " | DateTime:" + df.format(dateobj));
}

}

}

控制器类

package tme3;

import java.util.\*;
import tme3.GreenhouseControls.ControllerException;
public class Controller {
// A class from java.util to hold Event objects:
private List\<Event\> eventList = new ArrayList\<Event\>();

public void addEvent(Event c) {
    eventList.add(c);
}

public void run()throws GreenhouseControls.ControllerException{
    while (eventList.size() > 0)
        // Make a copy so you're not modifying the list
        // while you're selecting the elements in it:
        for (Event e : new ArrayList<Event>(eventList))
            if (e.ready()) {
                System.out.println(e);
                e.action();
                eventList.remove(e);
            }
}
// Add a Method to Controller called shutdown
 protected void shutdown(String message) {
        System.out.println("Shutdown");
      }
} 

活动类

package tme3;
import GreenhouseControls.ControllerException;

//import java.io.\*

public abstract class Event {
private long eventTime;
protected final long delayTime;

public Event(long delayTime) {
    this.delayTime = delayTime;
    start();
}
public void start() { // Allows restarting
    eventTime = System.currentTimeMillis() + delayTime;
}
public boolean ready() {
    return System.currentTimeMillis() \>= eventTime;
}

public abstract void action()throws GreenhouseControls.ControllerException;
}
java exception generics interface
© www.soinside.com 2019 - 2024. All rights reserved.