Advertisement
Guest User

PassAClass

a guest
Sep 26th, 2011
255
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.13 KB | None | 0 0
  1. import java.util.LinkedList;
  2.  
  3. public class PassAClass {
  4.     public static void main(String[] args) {
  5.         MagicGun gun = new MagicGun();
  6.         gun.loadWith(Pebbl.class);
  7.         gun.loadWith(Bullet.class);
  8.         gun.loadWith(NuclearMissl.class);
  9.         for(int i=0; i<5; i++){
  10.             try {
  11.                 String effect = gun.shoot().getName();
  12.                 System.out.printf("You've %sed the target!\n", effect);
  13.             } catch (GunIsEmptyException e) {
  14.                 System.err.printf("click\n");
  15.             }
  16.         }
  17.     }
  18. }
  19.  
  20. class MagicGun {
  21.     LinkedList<Class<? extends Ammo>> ammos = new LinkedList<Class<? extends Ammo>>();
  22.     public void loadWith(Class<? extends Ammo> ammo){
  23.         ammos.add(ammo);
  24.     }
  25.     public Class<? extends Ammo> shoot() throws GunIsEmptyException{
  26.         if (ammos.isEmpty()) {
  27.             throw new GunIsEmptyException();
  28.         }
  29.         return ammos.remove();
  30.     }
  31. }
  32.  
  33. class GunIsEmptyException extends Exception { }
  34.  
  35. interface Ammo { }
  36.  
  37. class Pebbl implements Ammo { }
  38.  
  39. class Bullet implements Ammo { }
  40.  
  41. class NuclearMissl implements Ammo { }
  42.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement