gpt4 book ai didi

java - 比较 Java 中的状态

转载 作者:行者123 更新时间:2023-12-02 10:10:30 26 4
gpt4 key购买 nike

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

这是 SuperFacility 类:

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

在我的检查类中,我有两种方法必须检查设施的状态以查看其是否正常工作,但我的 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 的“找不到符号”错误

最佳答案

虽然我不太明白你想要做什么,但我可以告诉你 Inspection 类可能已经按照你想要的方式工作了。

我看到你注释掉了构造函数,为什么?可以在Inspection注入(inject)一个Facility实例。但是,您应该接受 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 扩展 StateMachineInspection 能够接受它。

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

关于java - 比较 Java 中的状态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55029968/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com