meta data for this page
  •  

Programming with state machines \ Exercise \ CSPA

Implement Vending machine system (user - vending machine) given in exer_fsm by state pattern with python language.

Coding phase 1

python package: exer_fsm_impl

main.py

import logging
log = logging.getLogger(__name__)

import exer_fsm_impl.user as mu
import exer_fsm_impl.vending_machine as mvm

def run():
  vm = mvm.VendingMachine()
  user = mu.User(vm)
  vm.set_user(user)
  user.run()

if __name__ == '__main__':
  logging.basicConfig(level=logging.DEBUG)  
  run()

interfaces.py

class IFrontPanel(object):
  '''
  Interface to VendingMachine.
  User => VendingMachine
  '''
  
  def insert_coin(self,coin):
    raise NotImplementedError()
  
  def return_coins(self):
    raise NotImplementedError()

  def Kola(self):
    raise NotImplementedError()

  def Vissy(self):
    raise NotImplementedError()

  def Jaffa(self):
    raise NotImplementedError()

  def coins_ok(self):
    raise NotImplementedError()

class IUser(object):
  
  def display(self,text):
    raise NotImplementedError()
 
  def error(self,text):
    raise NotImplementedError()

  def delivery(self,text):
    raise NotImplementedError()

user.py

import logging
log = logging.getLogger(__name__)

from exer_fsm_impl.interfaces import *

class User(IUser):

  def __init__(self,vm):
    self._vm = vm

  def run(self):
    log.debug("User.run ..")

vending_machnine.py

from exer_fsm_impl.interfaces import *

class VendingMachine(IFrontPanel):

  def __init__(self):
    self._user = None
    
  def set_user(self,user):
    self._user