Я пробовал в классе ниже разные вещи:
- Раньше я использовал Карты вместо списка.
- Использовал метод Try and Catch.
- Цикл для получения идентификатор
Вот класс:
Код: Выделить всё
public class SoulType {
private static final List SOUL_TYPES = new ArrayList();
private final String id;
private final String displayName;
private final SoulTypeRarities rarity;
private final double corruption;
private double strength;
private SoulType(String id, String displayName, SoulTypeRarities rarity, double corruption, double strength) {
this.id = id;
this.displayName = displayName;
this.rarity = rarity;
this.corruption = corruption;
this.strength = strength;
}
public static List getSoulTypes() { // Neue Getter-Methode
return SOUL_TYPES;
}
public String getId() {
return id;
}
public String getDisplayName() {
return displayName;
}
public SoulTypeRarities getRarity() {
return rarity;
}
public Double getCorruption() {
return corruption;
}
public Double getStrength() {
return strength;
}
public void setStrength(double newStrength) {
this.strength = newStrength;
}
public void addStrength(double addStrength) {
this.strength += addStrength;
}
public static SoulType register(String id, String displayName, SoulTypeRarities rarity, double corruption, double strength) throws IllegalArgumentException {
double minCorruption = 0;
double maxCorruption = 2;
double minStrength = 0;
double maxStrength = 5;
if (corruption < minCorruption || corruption > maxCorruption) {
throw new IllegalArgumentException("Corruption must be between " + minCorruption + " and " + maxCorruption + " .");
}
if (strength < minStrength || strength > maxStrength) {
throw new IllegalArgumentException("Strength must be between " + minStrength + " and " + maxStrength + " .");
}
SoulType newType = new SoulType(id, displayName, rarity, corruption, strength);
SOUL_TYPES.add(newType);
for (SoulType soulType : SOUL_TYPES) {
if (soulType.getId().equals(id)) {
throw new IllegalArgumentException("SoulType with id '" + id + "' already exists.");
}
}
return newType;
}
public static SoulType fromId(String id) {
for (SoulType soulType : SOUL_TYPES) {
if (soulType.getId().equals(id)) {
return soulType;
}
}
throw new IllegalArgumentException("SoulType with id '" + id + "' not found.");
}
}
Код: Выделить всё
public class SoulTypes {
public static final SoulType HARMONY = SoulType.register("harmony", "Harmonie", SoulTypeRarities.COMMON, 0.0, 1.0);
public static final SoulType BOLLWERK = SoulType.register("bollwerk", "Bollwerk", SoulTypeRarities.RARE, 0.8, 1.5);
}
Подробнее здесь: https://stackoverflow.com/questions/787 ... -execution