345449.vhj5l3oj7.asia VendingMachine.java 2001-12-26T16:00:00Z 2001-12-26T16:00:00Z <br/><TEXTAREA name="code" class="java" rows="16" cols="100">//: c04:vendingmachine:VendingMachine.java // Demonstrates use of StateMachine.java package c04.vendingmachine; import c04.statemachine2.*; final class VM extends State { private VM(String nm) { super(nm); } public final static VM quiescent = new VM(&quot;Quiesecent&quot;), collecting = new VM(&quot;Collecting&quot;), selecting = new VM(&quot;Selecting&quot;), unavailable = new VM(&quot;Unavailable&quot;), wantMore = new VM(&quot;Want More?&quot;), noChange = new VM(&quot;Use Exact Change Only&quot;), makesChange = new VM(&quot;Machine makes change&quot;); } final class HasChange implements Input { private String name; private HasChange(String nm) { name = nm; } public String toString() { return name; } public final static HasChange yes = new HasChange(&quot;Has change&quot;), no = new HasChange(&quot;Cannot make change&quot;); } class ChangeAvailable extends StateMachine { public ChangeAvailable() { super(VM.makesChange); buildTable(new Object[][][]{ { {VM.makesChange}, // Current state // Input, test, transition, next state: {HasChange.no, null, null, VM.noChange}}, { {VM.noChange}, // Current state // Input, test, transition, next state: {HasChange.yes, null, null, VM.makesChange}}, }); } } final class Money implements Input { private String name; private int value; private Money(String nm, int val) { name = nm; value = val; } public String toString() { return name; } public int getValue() { return value; } public final static Money quarter = new Money(&quot;Quarter&quot;, 25), dollar = new Money(&quot;Dollar&quot;, 100); } final class Quit implements Input { private Quit() {} public String toString() { return &quot;Quit&quot;; } public final static Quit quit = new Quit(); } final class FirstDigit implements Input { private String name; private int value; private FirstDigit(String nm, int val) { name = nm; value = val; } public String toString() { return name; } public int getValue() { return value; } public final static FirstDigit A = new FirstDigit(&quot;A&quot;, 0), B = new FirstDigit(&quot;B&quot;, 1), C = new FirstDigit(&quot;C&quot;, 2), D = new FirstDigit(&quot;D&quot;, 3); } final class SecondDigit implements Input { private String name; private int value; private SecondDigit(String nm, int val) { name = nm; value = val; } public String toString() { return name; } public int getValue() { return value; } public final static SecondDigit one = new SecondDigit(&quot;one&quot;, 0), two = new SecondDigit(&quot;two&quot;, 1), three = new SecondDigit(&quot;three&quot;, 2), four = new SecondDigit(&quot;four&quot;, 3); } class ItemSlot { int price; int quantity; static int counter = 0; String id = Integer.toString(counter++); public ItemSlot(int prc, int quant) { price = prc; quantity = quant; } public String toString() { return id; } public int getPrice() { return price; } public int getQuantity() { return quantity; } public void decrQuantity() { quantity--; } } public class VendingMachine extends StateMachine{ StateMachine changeAvailable = new ChangeAvailable(); int amount = 0; FirstDigit first = null; ItemSlot[][] items = new ItemSlot[4][4]; Condition notEnough = new Condition() { public boolean condition(Input input) { int i1 = first.getValue(); int i2 = ((SecondDigit)input).getValue(); return items[i1][i2].getPrice() &gt; amount; } }; Condition itemAvailable = new Condition() { public boolean condition(Input input) { int i1 = first.getValue(); int i2 = ((SecondDigit)input).getValue(); return items[i1][i2].getQuantity() &gt; 0; } }; Condition itemNotAvailable = new Condition() { public boolean condition(Input input) { return !itemAvailable.condition(input); //int i1 = first.getValue(); //int i2 = ((SecondDigit)input).getValue(); //return items[i1][i2].getQuantity() == 0; } }; Transition clearSelection = new Transition() { public void transition(Input input) { int i1 = first.getValue(); int i2 = ((SecondDigit)input).getValue(); ItemSlot is = items[i1][i2]; System.out.println( &quot;Clearing selection: item &quot; + is + &quot; costs &quot; + is.getPrice() + &quot; and has quantity &quot; + is.getQuantity()); first = null; } }; Transition dispense = new Transition() { public void transition(Input input) { int i1 = first.getValue(); int i2 = ((SecondDigit)input).getValue(); ItemSlot is = items[i1][i2]; System.out.println(&quot;Dispensing item &quot; + is + &quot; costs &quot; + is.getPrice() + &quot; and has quantity &quot; + is.getQuantity()); items[i1][i2].decrQuantity(); System.out.println(&quot;New Quantity &quot; + is.getQuantity()); amount -= is.getPrice(); System.out.println(&quot;Amount remaining &quot; + amount); } }; Transition showTotal = new Transition() { public void transition(Input input) { amount += ((Money)input).getValue(); System.out.println(&quot;Total amount = &quot; + amount); } }; Transition returnChange = new Transition() { public void transition(Input input) { System.out.println(&quot;Returning &quot; + amount); amount = 0; } }; Transition showDigit = new Transition() { public void transition(Input input) { first = (FirstDigit)input; System.out.println(&quot;First Digit= &quot;+ first); } }; public VendingMachine() { super(VM.quiescent); // Initial state for(int i = 0; i &lt; items.length; i++) for(int j = 0; j &lt; items[i].length; j++) items[i][j] = new ItemSlot((j+1)*25, 5); items[3][0] = new ItemSlot(25, 0); buildTable(new Object[][][]{ { {VM.quiescent}, // Current state // Input, test, transition, next state: {Money.class, null, showTotal, VM.collecting}}, { {VM.collecting}, // Current state // Input, test, transition, next state: {Quit.quit, null, returnChange, VM.quiescent}, {Money.class, null, showTotal, VM.collecting}, {FirstDigit.class, null, showDigit, VM.selecting}}, { {VM.selecting}, // Current state // Input, test, transition, next state: {Quit.quit, null, returnChange, VM.quiescent}, {SecondDigit.class, notEnough, clearSelection, VM.collecting}, {SecondDigit.class, itemNotAvailable, clearSelection, VM.unavailable}, {SecondDigit.class, itemAvailable, dispense, VM.wantMore}}, { {VM.unavailable}, // Current state // Input, test, transition, next state: {Quit.quit, null, returnChange, VM.quiescent}, {FirstDigit.class, null, showDigit, VM.selecting}}, { {VM.wantMore}, // Current state // Input, test, transition, next state: {Quit.quit, null, returnChange, VM.quiescent}, {FirstDigit.class, null, showDigit, VM.selecting}}, }); } } ///:~ </TEXTAREA><br><br/><script type="text/javascript"><!--google_ad_client = "pub-9426659565807829";google_ad_slot = "9359905831";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script> 2001-12-26T16:00:00Z VendingMachineTest.java 2001-12-26T16:00:00Z 2001-12-26T16:00:00Z <br/><TEXTAREA name="code" class="java" rows="16" cols="100">//: c04:vendingmachine:VendingMachineTest.java // Demonstrates use of StateMachine.java package c04.vendingmachine; import c04.statemachine2.*; import com.bruceeckel.test.*; public class VendingMachineTest extends UnitTest{ VendingMachine vm = new VendingMachine(); Input[] inputs = { Money.quarter, Money.quarter, Money.dollar, FirstDigit.A, SecondDigit.two, FirstDigit.A, SecondDigit.two, FirstDigit.C, SecondDigit.three, FirstDigit.D, SecondDigit.one, Quit.quit, }; public void test() { for(int i = 0; i &lt; inputs.length; i++) vm.nextState(inputs[i]); } public static void main(String[] args) { new VendingMachineTest().test(); } } ///:~ </TEXTAREA><br><br/><script type="text/javascript"><!--google_ad_client = "pub-9426659565807829";google_ad_slot = "9359905831";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script> 2001-12-26T16:00:00Z makefile 2001-12-26T16:00:00Z 2001-12-26T16:00:00Z <br/><TEXTAREA name="code" class="" rows="16" cols="100"># From Thinking in Patterns (with Java) by Bruce Eckel # At http://www.BruceEckel.com # (c)2001 Bruce Eckel # Copyright notice in Copyright.txt # Automatically-generated MAKEFILE # For examples in directory .\c04\vendingmachine # using the JDK 1.3 compiler # Invoke with: make HOME := ../../ ifndef MAKECMDGOALS MAKECMDGOALS := javac endif # Command.com is too weak to build this under Windows NT/2000: ifeq ($(OS),Windows_NT) COMSPEC=$(SYSTEMROOT)\system32\cmd.exe endif ifneq ($(MAKECMDGOALS),clean) include $(HOME)/$(MAKECMDGOALS).mac endif .SUFFIXES : .class .java .java.class : $(JVC) $(JVCFLAGS) $&lt; javac: \ VendingMachine.class \ VendingMachineTest.class jikes: \ VendingMachine.class \ VendingMachineTest.class clean: ifeq ($(notdir $(SHELL)),COMMAND.COM) del *.class else rm -f *.class endif VendingMachine.class: VendingMachine.java VendingMachineTest.class: VendingMachineTest.java $(JVC) $(JVCFLAGS) $&lt; java com.bruceeckel.test.RunUnitTests c04.vendingmachine.VendingMachineTest </TEXTAREA><br><br/><script type="text/javascript"><!--google_ad_client = "pub-9426659565807829";google_ad_slot = "9359905831";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script> 2001-12-26T16:00:00Z