1 /** 
   2  * MementoPattern example
   3  * Dice game
   4  */
   5 
   6 import java.util.Vector;
   7 import java.util.Iterator;
   8 import java.util.Random;
   9 
  10 class Memento {
  11     int money;
  12     Vector fruits;
  13     public int getMoney() {
  14         return money;
  15     }
  16     Memento(int money) {
  17         this.money = money;
  18         this.fruits = new Vector();
  19     }
  20     void addFruit(String fruit) {
  21         fruits.add(fruit);
  22     }
  23 }
  24 
  25 class Gamer {
  26     private int money;
  27     private Vector fruits = new Vector();
  28     private Random random = new Random();
  29     private static String[] fruitsname = {
  30         "apple","grape","banana","orange"
  31     };
  32     public Gamer(int money) {
  33         this.money = money;
  34     }
  35     public int getMoney() {
  36         return money;
  37     }
  38     public void bet() {
  39         int dice = random.nextInt(6) + 1;
  40         if(dice == 1) {
  41             money += 100;
  42             System.out.println("increase 100 money");
  43         } else if(dice == 2) {
  44             money /= 2;
  45             System.out.println("decrease half money");
  46         } else if(dice == 6) {
  47             String f = getFruit();
  48             System.out.println("take fruit "+f);
  49             fruits.add(f);
  50         } else {
  51             System.out.println("Nothing happen");
  52         }
  53     }
  54     public Memento createMemento() {
  55         Memento m = new Memento(money);
  56         Iterator it = fruits.iterator();
  57         while(it.hasNext()) {
  58             String f = (String)it.next();
  59             if(f.startsWith("delicious")) {
  60                 m.addFruit(f);
  61             }
  62         }
  63         return m;
  64     }
  65     public void restoreMemento(Memento memento) {
  66         this.money = memento.money;
  67         this.fruits = memento.fruits;
  68     }
  69     public String toString() {
  70         return "[money = "+money+", fruits = "+fruits+"]";
  71     }
  72     private String getFruit() {
  73         String prefix = "";
  74         if(random.nextBoolean()) {
  75             prefix = "delicious ";
  76         }
  77         return prefix + fruitsname[random.nextInt(fruitsname.length)];
  78     }
  79 }
  80 
  81 public class MementoDiceGame {
  82     public static void main(String[] args) {
  83         Gamer gamer = new Gamer(100);
  84         Memento memento = gamer.createMemento();
  85         for(int i=0; i < 100; i++) {
  86             System.out.println("==== "+i);
  87             System.out.println("present state: "+gamer);
  88             gamer.bet();
  89             System.out.println("money become "+gamer.getMoney());
  90 
  91             if(gamer.getMoney() > memento.getMoney()) {
  92                 System.out.println("    memorize state due to increase");
  93                 memento = gamer.createMemento();
  94             } else if(gamer.getMoney() < memento.getMoney()/2) {
  95                 System.out.println("    restore due to decrease");
  96                 gamer.restoreMemento(memento);
  97             }
  98             try {
  99                 Thread.sleep(1000);
 100             } catch(InterruptedException e) {
 101             }
 102             System.out.println("");
 103         }
 104     }
 105 }
web biohackers.net