4bd2
| |||||||||||||
| |||||||||||||
|
inherited and associated / composite objects
Applying OO design techniques and best practise example from a Well House Consultants training course
More on Applying OO design techniques and best practise [link]
Source code: t6 Module: Y116
# We have TABLES of various sorts in a room # Associated with each table we have a waitress and multiple customers # We're running a restuarant, seating guests, etc! class person (object): def getname(this): return this.name class customer(person): def __init__(this,name,partysize): this.name = name this.partysize = partysize def getsize(this): return this.partysize class waitress(person): def __init__(this,name,capacity): this.name = name this.capacity = capacity this.ccc = 0 # current customer count / not yet used # --------------------------------------------------- class table(object): def __init__(current,material,w,h,wt): current.sng(material,wt) current.w = w current.h = h current.place = 925 def getbums(current): side = int(current.w/current.place) wide = int(current.h/current.place) return 2 * (side + wide) def setplace(current,p): current.place = p def getdoggie(current): if current.w < current.h: return current.w/2 return current.h/2 def sng(current,material,wt): current.wt = wt current.m = material current.parties = [] def getwaitstaff(self): return self.wt.getname() def seatguests(self,pname): so_far = self.getseated() # already at table adding = pname.getsize() # wanting to join holds = self.getbums() # capacity of table if holds >= so_far + adding: self.parties.append(pname) return True return None def getseated(self): so_far = 0 for pty in self.parties: so_far += pty.getsize() return so_far class recttable(table): pass class squaretable(table): def __init__(current,material,w,wt): current.sng(material,wt) current.w = w current.h = w current.place = 925 class roundtable(table): def __init__(current,material,w,wt): current.sng(material,wt) current.w = w current.h = w current.place = 800 def getbums(current): side = int(current.w * 3.14159265 / current.place) return side # ---------------------------------------------- if __name__ == "__main__": lead = waitress("Heather",12) assist = waitress("Poppy",10) cafe = [recttable("Cherry",2100,980,lead), recttable("Plastic",1800,750,assist), roundtable("Ash",1500,lead), squaretable("oak",5000,assist) ] for tab in cafe: ps1 = tab.getbums() dog = tab.getdoggie() order = tab.getwaitstaff() print "We can seat",ps1,"and doggie distance is",dog,"order from",order if cafe[0].seatguests(customer("Alex",4)): print "We have seated Alex" if cafe[0].seatguests(customer("Simon",3)): print "We have seated Simon" for tab in cafe: ps1 = tab.getbums() ps2 = tab.getseated() order = tab.getwaitstaff() print "We have",ps2,"at a table for",ps1,"looked after by",order # ------------------------------------------------------ """ Sample Output munchkin:opy grahamellis$ python tabby We can seat 6 and doggie distance is 490 order from Heather We can seat 2 and doggie distance is 375 order from Poppy We can seat 5 and doggie distance is 750 order from Heather We can seat 20 and doggie distance is 2500 order from Poppy We have seated Alex We have 4 at a table for 6 looked after by Heather We have 0 at a table for 2 looked after by Poppy We have 0 at a table for 5 looked after by Heather We have 0 at a table for 20 looked after by Poppy munchkin:opy grahamellis$ """ 1d7d Learn about this subject
This module and example are covered on the following public courses:
* Learning to program in Python * Python Programming * Intermediate Python Also available on on site courses for larger groups Books covering this topic
Yes. We have over 700 books in our library. Books
covering Python are listed here and when you've selected a
relevant book we'll link you on to Amazon to order.
Other Examples
This example comes from our "Applying OO design techniques and best practise" training module. You'll find a description of the topic and some
other closely related examples on the "Applying OO design techniques and best practise" module index page.
Full description of the source code
The complete module that describes this example
is available for download under our Open Training Notes License from
our sister site http://www.training-notes.co.uk.
Other resources
• Our Solutions centre provides a number of longer technical articles.
• Our Opentalk forum archive provides a question and answer centre. • The Horse's mouth provides a daily tip or thought. • Further resources are available via the resources centre. • All of these resources can be searched through through our search engine • And there's a global index here. Purpose of this website
This is a sample program, class demonstration or answer from a
training course. It's main purpose
is to provide an after-course service to customers who have attended our
public private or
on site courses, but the examples are made
generally available under conditions described below.
Web site author
Conditions of use
Past attendees on our training courses are welcome to use individual
examples in the course of their programming, but must check
the examples they use to ensure that they are suitable for their
job. Remember that some of our examples show you how not to do
things - check in your notes. Well House Consultants take no responsibility
for the suitability of these example programs to customer's needs.
This program is copyright Well House Consultants Ltd, but is part of a module released under our Open Training Notes license. You may use it for your own training needs provided that you adhere to the conditions laid down. In summary, these require you (a) to make no charge for the training and (b) to include a full copy of our copyright statement with the material your distribute. Any of our images within this code may NOT be reused on a public URL without our prior permission. For Bona Fide personal use, we will often grant you permission provided that you provide a link back. Commercial use on a website will incur a license fee for each image used - details on request. |
| ||||||||||||
PH: 01144 1225 708225 • FAX: 01144 1225 899360 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho PAGE: http://www.wellho.net/resources/ex.php4 • PAGE BUILT: Sat Apr 13 08:12:34 2013 • BUILD SYSTEM: wizard | |||||||||||||