U E D R , A S I H C RSS

Seminar How To Program It/Pipe/vendingmachine.py


#vendingmachine.py

import unittest

class VendingMachine:
    menu = {'white':100, 'black':100, 'sugarwhite':100, 'sugarblack':100,
            'cocoa':200}
    def __init__(self):
        self.reset()
    def reset(self):
        self.account=0
        self.button=''
        self.dispenser='empty'
    def putCoin(self, aMoney):
        self.account += aMoney
    def getCurrentMoney(self):
        return self.account
    def pushButton(self, aButton):
        self.button=aButton
        price = self.menu[aButton]
        if self.account >= price:
            self.dispenser=aButton
            self.account -=price

    def getCurrentDispenser(self):
        return self.dispenser
    def getCurrentButton(self):
        return self.button
    def verifyCoin(self, aMoney):
        return self.account == aMoney
    def verifyButton(self, aButton):
        return self.button == aButton

    def rejectLever(self):
        returnMoney = self.account
        self.account =0
        return returnMoney

    def verifyDispenser(self, aKind):
        return self.dispenser == aKind

class TestVendingMachine(unittest.TestCase):
    def testPutCoin(self):
        vm = VendingMachine()
        vm.putCoin(10)
        expected = 10
        self.assertEquals(expected, vm.getCurrentMoney())

    def testPushButtonTrue(self):
        vm = VendingMachine()
        vm.putCoin(100)
        vm.pushButton('white')
        expected = 'white'
        self.assertEquals(expected, vm.getCurrentButton())
        expected = 'white'
        self.assertEquals(expected, vm.getCurrentDispenser())

    def testPushButtonFalse(self):
        vm = VendingMachine()
        vm.putCoin(90)
        vm.pushButton('white')
        expected = 'white'
        self.assertEquals(expected, vm.getCurrentButton())
        expected = 'empty'
        self.assertEquals(expected, vm.getCurrentDispenser())

    def testRejectLever(self):
        vm = VendingMachine()
        vm.putCoin(500)
        returnMoney = vm.rejectLever()
        self.assertEquals(500, returnMoney)
        self.assertEquals(0, vm.getCurrentMoney())

    def testReset(self):
        vm = VendingMachine()
        vm.putCoin(500)
        vm.pushButton('white')
        vm.reset()
        self.assertEquals(0, vm.getCurrentMoney())
        self.assertEquals('', vm.getCurrentButton())


class TestVendingMachineVerification(unittest.TestCase):
    def testVerifyCoinFalse(self):
        vm = VendingMachine()
        vm.putCoin(50)
        expected = 0
        self.assertEquals(expected, vm.verifyCoin(100))

    def testVerifyButtonTrue(self):
        vm = VendingMachine()
        vm.pushButton('white')
        expected = 1
        self.assertEquals(expected, vm.verifyButton('white'))

    def testVerifyButton(self):
        vm = VendingMachine()
        vm.pushButton('black')
        expected = 0
        self.assertEquals(expected, vm.verifyButton('white'))

    def testVerifyCoinTrue(self):
        vm = VendingMachine()
        vm.putCoin(100)
        expected = 1
        self.assertEquals(expected, vm.verifyCoin(100))

    def testVerifyDispenserTrue(self):
        vm = VendingMachine()
        vm.putCoin(100)
        vm.pushButton('white')
        expected = 1
        self.assertEquals(expected, vm.verifyDispenser('white'))

    def testVerifyDispenserFalse(self):
        vm = VendingMachine()
        vm.putCoin(100)
        vm.pushButton('white')
        expected = 0
        self.assertEquals(expected, vm.verifyDispenser('black'))


if __name__=='__main__':
    unittest.main()

#ì~K¤í~V~Iì~X~H
"""
Welcome to Vending Machine Simulator!
Enter a command or type 'help'
: put 100
: put 100
: verify money 200
1
: verify money 220
0
: put 50
: put 232
"Console", line 1: Unexpected money type
: put 100
: verify money 350
1
: verify money 400
0
: help

Type 'help <topic>' for info on:
================================
help            quit



push            put             include

:

"""
Valid XHTML 1.0! Valid CSS! powered by MoniWiki
last modified 2021-02-07 05:28:01
Processing time 0.0141 sec