0

I have a query about a project im doing.

the scenario is that there is a space ship with 4 crew members on it and each crew member can handle a certain type of malfunction on the ship, the 4 crew members are:

Space Monkey - he can handle TRIVIAL malfunctions Service Robot - he can handle LOW malfunctions Engineer - he can handle MEDIUM malfunctions Captain - he can handle HIGH/ALL Malfunctions

now i have the chain of command design patter implemented into that if one of the 4 objects are created and handed a malfunction, it will look to see if they can handle it, if not they will pass it down the chain to the next crew member.

That but was fine but now i am doing it so that a Captain could get a LOW priority malfunction which he can handle.

I have a method called processMalfunction which takes an object of type Malfunction (a Malfucntion object takes an enum which is the severity and a String which gives a description of the malfunction)

The malfunction is then compared with the crew member who has been handed the malfunction and in this instance if it was a captain taking a LOW priority malfunction he would be able to handle it.

I can do the if statement to compare if the current malfunction enum matches the enum of the competency level of the crew member, but i need some way of comparing it against the other levels that are below his compitency incase he has been handed a lower level task.

heres a code snippit of the processMalfunction(Malfunction m) method

final protected void processMalfunction(Malfunction m) {

    if (competence.ordinal() >= m.getSeverity().ordinal()) {

        handleProblem(m);
    }
    else {
        next.handleProblem(m);
    }
}

heres a copy of the Malfunction class

public class Malfunction {

/**
 * instance variable which will hold an enum of the severity type.
 */
Severity severity;

/**
 * instance variable which will display a string describing the problem.
 */
String description;

/**
 * This constructor will take a severity level and a string and display the appropriate 
 * message if it is able to handle the problem. If there is no string given, it will
 * display a default message.
 * @param s severity level of type enum
 * @param d string which outputs the description of the problem
 */
public Malfunction(Severity s, String d) {

    if (d == null || d.isEmpty()) {
        this.description = "No description available. Probably serious.";
    } else {
        this.severity = s;
        this.description = d;    
    }
}

/**
 * accessor method which returns an enum showing the level of severity of the problem.
 * @return Severity level enum
 */
public Severity getSeverity() {
    return severity;
}

/**
 * accessor method which returns a string which gives a description of what the problem is.
 * @return Severity level enum
 */
public String getDescription() {
    return description;
}

}

can anyone suggest what the best way is for me to compare the enum type of the crew member agains the malfunction enum passed in right from the crew members competency down to the lowest

i.e. if it was a engineer getting passed a LOW level malfunction id need to check against the enum MEDIUM, LOW and TRIVIAL to ensure that he can process the malfunction as in the if statement if he can process it, he will. So i need it to basically say

If the malfunction level is equal or less than the enum competency level of the crew member, handle malfunction, if not, pass it on.

any help would be greatly appreciated.

kind regards

J

Jordan Atkinson
  • 202
  • 1
  • 6
  • 24
  • the first code snippet, the for loops doesnt work in terms of going in at the right input. i tried ordinal but was unsure, anyone who requires to see the full package, please email me at jordan.atkinson3@gamail.com – Jordan Atkinson Oct 09 '13 at 23:52
  • 1
    You can implement methods within enums. Make a method that compares one enum with another to see if it's higher. see http://docs.oracle.com/javase/tutorial/java/javaOO/enum.html – tom Oct 09 '13 at 23:59

0 Answers0