|
| 1 | +package fj.demo; |
| 2 | + |
| 3 | +import fj.F2; |
| 4 | +import fj.data.List; |
| 5 | +import fj.data.State; |
| 6 | + |
| 7 | +import static fj.demo.StateDemo_VendingMachine.Input.COIN; |
| 8 | +import static fj.demo.StateDemo_VendingMachine.Input.TURN; |
| 9 | + |
| 10 | +/** |
| 11 | + * Created by MarkPerry on 20/07/2014. |
| 12 | + */ |
| 13 | +public class StateDemo_VendingMachine { |
| 14 | + |
| 15 | + public enum Input { COIN, TURN }; |
| 16 | + |
| 17 | + public static class VendingMachine { |
| 18 | + |
| 19 | + private boolean locked; |
| 20 | + private int items; |
| 21 | + private int coins; |
| 22 | + |
| 23 | + public VendingMachine(boolean lock, int things, int numCoins) { |
| 24 | + locked = lock; |
| 25 | + items = things; |
| 26 | + coins = numCoins; |
| 27 | + } |
| 28 | + |
| 29 | + /** |
| 30 | + * Equals generated by Intellij |
| 31 | + */ |
| 32 | + @Override |
| 33 | + public boolean equals(Object o) { |
| 34 | + if (this == o) return true; |
| 35 | + if (o == null || getClass() != o.getClass()) return false; |
| 36 | + |
| 37 | + VendingMachine that = (VendingMachine) o; |
| 38 | + |
| 39 | + if (coins != that.coins) return false; |
| 40 | + if (items != that.items) return false; |
| 41 | + if (locked != that.locked) return false; |
| 42 | + |
| 43 | + return true; |
| 44 | + } |
| 45 | + |
| 46 | + /** |
| 47 | + * HashCode generated by Intellij |
| 48 | + */ |
| 49 | + @Override |
| 50 | + public int hashCode() { |
| 51 | + int result = (locked ? 1 : 0); |
| 52 | + result = 31 * result + items; |
| 53 | + result = 31 * result + coins; |
| 54 | + return result; |
| 55 | + } |
| 56 | + |
| 57 | + public String toString() { |
| 58 | + return String.format("VendingMachine(locked=%b,items=%d,coins=%d)", locked, items, coins); |
| 59 | + } |
| 60 | + |
| 61 | + VendingMachine next(Input i) { |
| 62 | + if (items == 0) { |
| 63 | + return this; |
| 64 | + } else if (i == COIN && !locked) { |
| 65 | + return this; |
| 66 | + } else if (i == TURN && locked) { |
| 67 | + return this; |
| 68 | + } else if (i == COIN && locked) { |
| 69 | + return new VendingMachine(false, items, coins + 1); |
| 70 | + } else if (i == TURN && !locked) { |
| 71 | + return new VendingMachine(true, items - 1, coins); |
| 72 | + } else { |
| 73 | + return this; |
| 74 | + } |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + static State<VendingMachine, VendingMachine> simulate(List<Input> list) { |
| 79 | + return list.foldLeft((s, i) -> s.map(m -> m.next(i)), State.<VendingMachine>init()); |
| 80 | + } |
| 81 | + |
| 82 | + static void test() { |
| 83 | + State<VendingMachine, VendingMachine> s = simulate(List.list(COIN, TURN, TURN, COIN, COIN, TURN)); |
| 84 | + VendingMachine m = s.eval(new VendingMachine(true, 5, 0)); |
| 85 | + VendingMachine oracle = new VendingMachine(true, 3, 2); |
| 86 | + System.out.printf("m1: %s, oracle: %s, equals: %b", m, oracle, m.equals(oracle)); |
| 87 | + } |
| 88 | + |
| 89 | + public static void main(String args[]) { |
| 90 | + test(); |
| 91 | + } |
| 92 | + |
| 93 | +} |
0 commit comments