6.096 10-08-97 RECITATION: Finite State Machines (FSM) scribe: Aparna Das FSM - finite number of ststaes - finite number of possible inputs - finite number of possible outputs - transition function - output function Soda Machine - accepts coins - waits for enough coins - lights the soda buttons - accepts press of soda button - releases soda - returns change Inputs: - money - soda button Outputs: - light - soda - return coins - display current money status - processing: count money Soda Machine: public interface SodaMachine { // constants public static final int SODA_BTN = 0; public static final int PRICE = 60; /* method: getInput() returns the next input if the return value > 0, it refers to the amount of money if there is no available input right now, then getInput waits */ public int getInput(); public void showLights (boolean on); public void showDisplay(String s) ; public void giveChange (int howMuch); public void releaseSoda(); } Soda Controller: public class SodaCtrl implements Runnable { //fields private Thread sprite; private SodaMachine soda; private int currentMoney = 0; private int currentState = WAIT_STATE; // constants for state private static final int WAIT_STATE = 0; private static final int TAKING_STATE = 1; private static final int ENOUGH_STATE = 2; /* constructor allows SodaCtrl class to access the * methods of the SodaMachine class */ public SodaCtrl (SodaMachine s) { //label soda gets stuck on SodaMachine with value s this.soda = s; this.sprite = new Thread (this); /* new Thread named sprite created so sprite can * execute the self-animated SodaCtrl class sequence */ this.sprite.start(); //start Thread sprite } public void run() { while (true) //i.e., always { switch (currentState) { case WAIT_STATE: actWaitState(); break; case TAKING_STATE: actTakingState(); break; case ENOUGH_STATE: actEnoughState(); break; default: System.out.println("invalid state"); } } } private void actWaitState() { // display outputs this.soda.showLights (false); this.soda.showDisplay ("BUY ME"); //get input int input = soda.getInput(); // process input if (input > 0) { //got money //add money to currentMoney currentMoney += input; if (currentMoney >= SodaMachine.PRICE) { currentState = ENOUGH_STATE; } else { currentState = TAKING_STATE; } } else { // do nothing and stay in the same state } } private void actTakingState() { //display output this.soda.showLights (false); this.soda.showDisplay (currentMoney + ""); //get input int input = soda.getInput(); // process input if (input > 0) { //got money //add money to currentMoney currentMoney += input; if (currentMoney >= SodaMachine.PRICE) { currentState = ENOUGH_STATE; } else { currentState = TAKING_STATE; } } else { // do nothing and stay in the same state } } private void actEnoughState() { //display outputs this.soda.showLights (true); this.soda.showDisplay (currentMoney + ""); //get input int input = soda.getInput(); //process input if (input == SodaMachine.SODA_BTN) { // soda button was pressed //add money to currentMoney if (currentMoney >= SodaMachine.PRICE) { soda.releaseSoda(); soda.giveChange(currentMoney-SodaMachine.PRICE); currentMoney = 0; currentState = WAIT_STATE; } } else if (input > 0) { //got money currentMoney += input; currentState = ENOUGH_STATE; } else { // do nothing and stay in the same state } } }