用Java比较状态

时间:2019-03-06 18:32:25

标签: java oop comparison state

在我的项目中,我们必须创建一个系统,在该系统中创建对象设施,并且必须对其进行检查和维护以检查其是否正常运行。我们创建了一个Facility对象,然后创建了一个状态机,该状态机将Facility的状态更改为休息,工作,维护或工作。

这是超设施类:

import java.util.Map;

public interface SuperFacility {
    public void setName(String name);
    public void setAddress(String address);
    public void setDescription(String description);
    public void setRefNumber(int refNumber);
    public void setCapacity(int capacity);
    public void setCost(double cost);
    public void setProblemRate(int problemRate);
    public String getName();
    public String getAddress();
    public String getDescription();
    public int getRefNumber();
    public int getCapacity();
    public double getCost();
    public int getProblemRate();
    public void oxygenator(boolean oxygenator);
    public void nuclearReactor(boolean nuclearReactor);
    public void innerAirlocks(boolean innerAirlocks);
    public void externalAirlocks(boolean externalAirlocks);
    public void comms(boolean comms);
    public void waterMaking(boolean waterMaking);
    public void startMachines();
    public Map getMap();
    public void getFacilityStatus();
    public void getFacilityStatus(Map<String, Boolean> map);
} 

这是设施类:

import java.util.*;

public class Facility extends StateMachine implements SuperFacility {

    public String name, address, description;
    public int refNumber, capacity, problemRate;
    private double cost;
    private Map<String, Boolean> map = new HashMap<String, Boolean>();
    private boolean[] machines = new boolean[6];
    private boolean oxygenator, innerAirlocks, externalAirlocks,
            comms, nuclearReactor, waterMaking;
    private final int numberOfMachines = 6; // Number of Machines inside Facility

    // Setters
    public void setName(String name){
        this.name = name;
    }

    public void setAddress(String address){
        this.address = address;
    }

    public void setDescription(String description){
        this.description = description;
    }

    public void setRefNumber(int refNumber){
        this.refNumber = refNumber;
    }

    public void setCapacity(int capacity){
        this.capacity = capacity;
    }

    public void setCost(double cost){
        this.cost = cost;
    }

    public void setProblemRate(int problemRate){
        this.problemRate = problemRate;
    }

    // Getters
    public String getName(){
        return name;
    }

    public String getAddress(){
        return address;
    }

    public String getDescription(){
        return description;
    }

    public int getRefNumber(){
        return refNumber;
    }

    public int getCapacity(){
        return capacity;
    }

    public double getCost(){
        return cost;
    }

    public int getProblemRate(){
        return problemRate;
    }

    public void oxygenator(boolean oxygenator){
        this.oxygenator = oxygenator;
    }

    public void nuclearReactor(boolean nuclearReactor){
        this.nuclearReactor = nuclearReactor;
    }

    public void innerAirlocks(boolean innerAirlocks){
        this.innerAirlocks = innerAirlocks;
    }

    public void externalAirlocks(boolean externalAirlocks){
        this.externalAirlocks = externalAirlocks;
    }

    public void comms(boolean comms){
        this.comms = comms;
    }

    public void waterMaking(boolean waterMaking){
        this.waterMaking = waterMaking;
    }

    public boolean[] getMachines(){
        machines[0] = oxygenator;
        machines[1] = nuclearReactor;
        machines[2] = innerAirlocks;
        machines[3] = externalAirlocks;
        machines[4] = comms;
        machines[5] = waterMaking;
        return machines;
    }

    // Set machines to false
    public void breakMachines(){
        oxygenator(false);
        nuclearReactor(false);
        innerAirlocks(false);
        externalAirlocks(false);
        comms(false);
        waterMaking(false);

        map.clear();
        initializeMap(map);
    }

    public void startMachines(){
        // Set all of the booleans from this Facility to true;
        // This booleans are what we call "the machines from the Facility"
        oxygenator(true);
        nuclearReactor(true);
        innerAirlocks(true);
        externalAirlocks(true);
        comms(true);
        waterMaking(true);

        map.clear();
        initializeMap(map);
    }

    public void initializeMap(Map<String, Boolean> map){
        this.map.put("Oxygenator", oxygenator);
        this.map.put("Inner Airlocks", innerAirlocks);
        this.map.put("External Airlocks", externalAirlocks);
        this.map.put("Nuclear Reactor", nuclearReactor);
        this.map.put("Comms", comms);
        this.map.put("WaterMaking", waterMaking);
    }

    public Map<String, Boolean> getMap(){
        return map;
    }

    public void getFacilityStatus(){ // The status of the map in this object
        for (Map.Entry<String, Boolean> i: map.entrySet()){
            System.out.println(i.getKey() + ": " + i.getValue());
        }
    }

    public void getFacilityStatus(Map<String, Boolean> map){ // The status of any Facility map
        for (Map.Entry<String, Boolean> i: map.entrySet()){
            System.out.println(i.getKey() + ": " + i.getValue());
        }
    }
} 

这是StateMachine类:

 public class StateMachine {

    public State state = State.RESTING;

    enum State {
        WORKING, RESTING, MAINTENANCE, BROKEN
    }

    public State getFacilityState(){
        return state;
    }

    public void setStateWorking(Facility fac){
        fac.state = State.WORKING;
    }

    public void setStateResting(Facility fac){
        fac.state = State.RESTING;
    }

    public void setStateMaintenance(Facility fac){
        fac.state = State.MAINTENANCE;
    }

    public void setStateBroken(Facility fac) { fac.state = State.BROKEN;}

    public State getState(){
        return state;
    }
}

在我的Inspection类中,我有两种方法必须检查Facility的状态以查看其是否正常运行,但是我的if语句遇到了问题:

 import java.util.*;

public class Inspection {

    private Facility fac;

    public boolean isBroken(){

        if (fac.state == State.BROKEN)
            return true;
        else
            return false;
    }

    public void makeMaintenanceRequest(Control c){

        if (fac.state == State.BROKEN){
            c.scheduleMaintenance(fac);
        }
    } 

我希望这些方法能够将设施的当前状态与“损坏”状态进行比较。我应该如何比较状态?我不断收到State.BROKEN的“找不到符号”错误

2 个答案:

答案 0 :(得分:1)

尽管我不太了解您要做什么,但是我可以告诉您Inspection类可能已经可以按照您的意愿工作了。

我看到您注释掉了构造函数,为什么呢?可以在Facility注入一个Inspection实例。但是,您应该改为接受StateMachine

public class Inspection {
   private final StateMachine stateMachine;

   public Inspection(final StateMachine stateMachine) {
      this.stateMachine = stateMachine;
   }

   ...
}

然后,在您的Inspection#isBroken方法内

public boolean isBroken() {
   return this.stateMachine.getFacilityState() == State.BROKEN; // "this" not necessary
}

随着Facility 扩展 StateMachine,它公开了一个getFacilityState()方法。
而且由于Facility 扩展了 StateMachine,所以Inspection可以接受它。

final Facility facility = new Facility(...);
final Inspection inspection = new Inspection(facility);
final boolean isBroken = inspection.isBroken();

答案 1 :(得分:0)

一个简单的解决方案是用StateMachine.State.BROKEN替换State.BROKEN

布尔布尔值isBroken(){

    if (fac.state == StateMachine.State.BROKEN)
        return true;
    else
        return false;
}