Problem referencing an object
I am writing a program which analyzes the effects of the failure of parts in a system. The user enters the parts that have failed, and the program analyzes which portions of the system remain functional.
I created a class called component. Each element of the system is an instantiation of this class. When the user enters a failed component, I want to run a method in the component class called setStatusFail. So, for example, if the user enters "pump", I want to run the method pump.setStatusFail.
How can I take the user's input, and use it to reference the object and invoke the method?
[610 byte] By [
ferresefta] at [2007-10-2 17:09:50]

in my opinion
1. u need a list of possible inputs from the user (e'g pumps etc)
2. then u need to map all of these inputs to seperate classes
3. now define the setStatus method in the component class, giving it a public access modifier. The parameter to this method can be a String say, "failed" or "working"
4. then in each of the other classes, define an instance of the component class like so
Component myComp = new Component()
5. then access the setStatus method of the Component class like so
myComp.setStatus("failed")
> I created a class called component. Each element of
> the system is an instantiation of this class. When
> the user enters a failed component, I want to run a
> method in the component class called setStatusFail.
> So, for example, if the user enters "pump", I want
> t to run the method pump.setStatusFail.
>
If the above means the classes extend the component class, they can implement their own versions of setStatusFail so when you have a pump you just call pump.setStatusFail() and the pump's version of the method will be used. Or, if setStatusFail() does the same thing in the component class as in the pump class, then just call it.
I understand what you have both suggested, however, my question is, once the user provides his input, which is in the form of a String, how to I take that string and use it to reference the object for which I want to call the setStatusFail method.
For example,
The user inputs that Pump1 has failed. If the operator input is stored in a String called opinput, and opinput is storing the String value Pump1, and I want to call Pump1.setStatusFail, how to I do it?
Thanks -
> I understand what you have both suggested, however,
> my question is, once the user provides his input,
> which is in the form of a String, how to I take that
> string and use it to reference the object for which I
> want to call the setStatusFail method.
>
> For example,
>
> The user inputs that Pump1 has failed. If the
> operator input is stored in a String called opinput,
> and opinput is storing the String value Pump1, and I
> want to call Pump1.setStatusFail, how to I do it?
>
> Thanks -
It sounds like you can use something like a HashMap. You would initialize the HashMap, using Strings that correspond to user input as keys for the objects you want to use. Here's a (not tested) example.HashMap map = new HashMap();
Pump pump = new Pump();
map.put("Pump1",pump);
Then, when you get user input, just get
the value at the key. I am assuming all the components extend YourComponent.String userinput = getUserInput();
YourComponent component = (YourComponent) map.get(userinput);
if(component == null) {
doInputError();
} else {
component.setStatusFail();
}
I haven't upgrade to JDK 1.5 but if you have 1.4. the above code can use generics to make it a litttle simpler.
