Answer the question
In order to leave comments, you need to log in
How to make enum.valueOf() return the desired value?
Hello. I have three classes: Motorcycle, MotorcycleManager (main), Color (enum)
In the Motorcycle class I have a toString() method and variables are set:
public class Motorcycle {
String name;
int yearOfProduction;
int price;
int weight;
Color color;
String engineType;
boolean isReadyToDrive;
public Motorcycle(String name, int yearOfProduction, int price, int weight, Color color, String engineType,
boolean isReadyToDrive) {
this.name = name;
this.yearOfProduction = yearOfProduction;
this.price = price;
this.weight = weight;
this.color = color;
this.engineType = engineType;
this.isReadyToDrive = isReadyToDrive;
}
@Override
public String toString() {
return "Motorcycle [name=" + name + ", yearOfProduction=" + yearOfProduction + ", price=" + price + ", weight="
+ weight + ", color=" + color + ", engineType=" + engineType + ", isReadyToDrive=" + isReadyToDrive
+ "]";
}
}
public class MotorcycleManager {
public static void main(String[] args) {
Motorcycle suzuki = new Motorcycle("Suzuki GSX-R1000", 2021, 16000, 600, Color.BLACK, "diesel", true);
Motorcycle yamaha = new Motorcycle("Yamaha FZ1", 2007, 9000, 700, Color.ORANGE, "gas", false);
System.out.println(suzuki);
System.out.println(yamaha);
}
}
public enum Color {
WHITE, GREEN, BLACK, BLUE, ORANGE
}
Answer the question
In order to leave comments, you need to log in
enum Color {
WHITE("white"), BLACK("black");
private final String color;
Color(String color) {
this.color = color;
}
@Override
public String toString() {
return color;
}
}
Color c = Color.WHITE;
System.out.println(c);
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question