[Tutor] PYTHON OOP First Project - Coke Machine
Moshe Zadka
Moshe Zadka <moshez@math.huji.ac.il>
Thu, 8 Jun 2000 20:33:39 +0300 (IDT)
On Thu, 8 Jun 2000, Thomas A. Williams wrote:
> Hi All,
> I'm a newbie to PYTHON. For my first Python
> project, I am embarking on a OOP project using
> Python to simulate a Coca-Cola (or Pepsi :-)
> vending machine. Does anyone have any suggestions,
> directions, or ideas to get me started? Is there any
> existing code (any type of vending machine)
> available which I may be able to study?
> Any information will be greatly appreciated.
Well, standard OO design practice is to first think: what does a vending
machine *do*? IOW, what is the interface of a vending machine?
Well, you can have a look at the vending machine, and see items are
available and what each costs. That would imply methods:
.get_products() -> List of products (say strings, at the moment)
and
.cost(item) -> integer
The other thing, a vending machine does *not* show you how much Coke cans
are in the machine, but it does light a LED to show you if an item is not
availble:
.available(item) -> boolean
Now, you should be able to buy from the machine:
.buy(item, account) -> void
What is an account? It is a representation of someone's bank account, so
the coke machine can take out the costs of the item.
What is the interface of an account?
.deposit(amount) -> void
.withdraw(amount) -> void
.available() -> integer
A complete implementation would probably use the pickle module, together
with the shelve module, to present an interface like
>>> user moshez buys coke
>>> user moshez buys coke
Error: no more cokes
>>> user moshez buys sprite
Error: moshez has no more money
>>> user moshez works
>>> user moshez buys sprite
(Use the cmd module for this kind of work. With enough sophistication, you
may write a virtual coke machine, available through the web (use cgi
module for that))
--
Moshe Zadka <moshez@math.huji.ac.il>
http://www.oreilly.com/news/prescod_0300.html
http://www.linux.org.il -- we put the penguin in .com