Reputation: 129
i have a question. i have to create 3 methods testUreaRisk, testProteinRisk and printResults. and the below is 2 out of the 3.
public class Lab {
public static String testUreaRisk(double ureaLevel)
{
if ((ureaLevel < 0) || (ureaLevel > 10))
return "0";
else if (ureaLevel <= 4.0)
return "-1";
else
return "1";
}
public static String testProteinRisk(double proLevel)
{
if ((proLevel < 0) || (proLevel >150))
return "0";
else if (proLevel >= 67.0)
return "1";
else
return "-1";
}
so my problem is can i put a value into the return number 1, 0,-1 as -1 = low risk, 0 = cannot be defined and 1 = high risk? if can,how? because the 3rd method could only allow me to return a string that show the result (low risk,cannot be defined and high risk) instead of the number(-1,0,1).thanks
Upvotes: 0
Views: 87
Reputation: 35597
But better way is use an Enum
.
Eg:
public enum Enum {
LOW("-1"), NOT_DETERMINED("0"), HIGH("1");
}
Eg:
My Enum
class
public enum Enum {
LOW("-1"), NOT_DETERMINED("0"), HIGH("1");
private String code;
private Enum(String c) {
this.code = c;
}
public String getCode() {
return this.code;
}
public static Enum getEnum(String code) {
switch (code) {
case "-1":
return LOW;
case "0":
return NOT_DETERMINED;
case "1":
return HIGH;
default:
return null;
}
}
}
Now
System.out.println(Enum.getEnum(testProteinRisk(10)));
Will give you
LOW
Upvotes: 3
Reputation: 11620
You should go with an Enum class here. If you want to have int values, you could create in each enum a emthod that will re`enter code hereturn this value:)
Enum { LOW(-1), NOT_DETERMINED(0), HIGH(1);
// getters
}
Upvotes: 1