Exercise 04: Programming with state machines

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

Coding phase 1

main.py

'''
Created on Oct 5, 2010

@author: hevi
'''

import logging
log = logging.getLogger(__name__)


import exer04.user as mu
import exer04.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

'''
Created on Oct 5, 2010

@author: hevi
'''

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

'''
Created on Oct 5, 2010

@author: hevi
'''

import logging
log = logging.getLogger(__name__)

from exer04.interfaces import *

class User(IUser):
  '''
  classdocs
  '''

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

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

vending_machnine.py

'''
Created on Oct 5, 2010

@author: hevi
'''

from exer04.interfaces import *

class VendingMachine(IFrontPanel):
  '''
  classdocs
  '''


  def __init__(self):
    '''
    Constructor
    '''
    self._user = None
    
  def set_user(self,user):
    self._user
Last modified: 2013/07/01 14:42