I have thought about making enums for all my fields, but that doesn’t look to cool either (i have many classes that implements similar methods). Is there a better way?
public void writeAttribute(String attribute, Object value) {
if (attribute.equals("title")) {
title = (String) value;
} else if (attribute.equals("description")) {
description = (String) value;
} else if (attribute.equals("room")) {
room = (Room) value;
} else if (attribute.equals("type")) {
type = AppointmentType.valueOf((String) value);
} else if (attribute.equals("guestCount")) {
guestCount = (Integer) value;
}
}
Depending on the attribute parameter, i want to map the input value to the appropriate field. Is there a way to clean up/optimize my code? Writing .equals for every field isn’t too elegant.
Since Java SE 7 (July 28, 2011) you can switch strings.