Created
August 27, 2019 19:03
-
-
Save riftrsps/832e4fa56e3351e71d217deaa79a465f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.*; | |
import java.util.function.*; | |
import java.io.*; | |
class Main { | |
public static void main(String args[]) { | |
BuffGroup group = new BuffGroup(); | |
group.add(new MobBonusXPBuff(10)); | |
Buff buff = group.get(MobBonusXPBuff.class); | |
boolean correctType = buff instanceof MobBonusXPBuff; | |
if(!correctType) { | |
String errorMessage = String.format("Type assertion failed. Expected[%s] Actual[%s]", MobBonusXPBuff.class, buff.getClass()); | |
throw new IllegalStateException(errorMessage); | |
} | |
MobBonusXPBuff castedBuff = (MobBonusXPBuff) buff; | |
boolean correctBonus = castedBuff.getXPBonus() == 10; | |
if(!correctBonus) { | |
String errorMessage = String.format("Bonus assertion failed. Expected[%d] Actual[%d]", 10, castedBuff.getXPBonus()); | |
throw new IllegalStateException(errorMessage); | |
} | |
System.out.println("Passed"); | |
} | |
} | |
abstract class Buff { } | |
class MobBonusXPBuff extends Buff { | |
private final int xpBonus; | |
public MobBonusXPBuff(int xpBonus) { | |
this.xpBonus = xpBonus; | |
} | |
public int getXPBonus() { | |
return xpBonus; | |
} | |
} | |
class BuffGroup { | |
private Map<Class<? extends Buff>, Buff> buffs = new HashMap<>(); | |
public void add(Buff buff) { | |
Class<? extends Buff> type = buff.getClass(); | |
if(buffs.containsKey(type)) | |
return; | |
buffs.put(type, buff); | |
} | |
public Buff get(Class<? extends Buff> type) { | |
return buffs.get(type); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment