Hi
The above cod is perfect, you can also try this code. Hope this will help you
Code:
public class train {
public enum trainType {
ori, mana, tech, opera,
admin, qty,sal, safe
}
private EnumSet<trainType> types = EnumSet.noneOf(trainType.class);
public train(byte b) {
for(trainType t :trainType.values()) {
if((b & 1 << t.ordinal()) != 0) {
types.add(t);
}
}
}
public Set<trainType> getCanDo() {
return Collections.unmodifiableSet(types);
}
public Set<trainType> getCantDo() {
return EnumSet.complementOf(types);
}
public void add(trainType newT) {
types.add(newT);
}
public boolean istrain(trainType testT) {
return types.contains(testT);
}
public static void main(String[] args) {
train test = new train((byte)0xf2);
System.out.printf("Can do: %s%n", test.getCanDo());
System.out.printf("Can't do: %s%n%n", test.getCantDo());
test.add(trainType.tech);
for(trainType type :trainType.values()) {
System.out.printf("Can %s: %s%n", type, test.istrain(type));
}
}
}
Bookmarks