Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.LinkedList;
- public class PassAnEnum {
- public static void main(String[] args) {
- MagicGun gun = new MagicGun();
- gun.loadWith(Ammo.PEBBLE);
- gun.loadWith(Ammo.BULLET);
- gun.loadWith(Ammo.MISSILE);
- for(int i=0; i<5; i++){
- try {
- Ammo shot = gun.shoot();
- System.out.printf("You've %s the target with a %s!\n", shot.getEffect(), shot.toString().toLowerCase());
- } catch (GunIsEmptyException e) {
- System.err.printf("click\n");
- }
- }
- }
- }
- class MagicGun {
- LinkedList<Ammo> ammos = new LinkedList<Ammo>();
- public void loadWith(Ammo ammo){
- ammos.add(ammo);
- }
- public Ammo shoot() throws GunIsEmptyException{
- if (ammos.isEmpty()) {
- throw new GunIsEmptyException();
- }
- return ammos.remove();
- }
- }
- class GunIsEmptyException extends Exception { }
- enum Ammo {
- PEBBLE("annoyed"), BULLET("holed"), MISSILE("obliterated");
- private String effect;
- private Ammo(String effect) {
- this.effect = effect;
- }
- public String getEffect() {
- return effect;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement