relationship
- lab has
- classRM
- table
- PC
- screen
- keyboard&mouse
- beamer
- WB
- serverRM
- rack
- server
- KVM
- switch
- library
- books
- fridge
- drink
- hardisk
The relationship is in general a “has” relation. Table has two screens, Rack has 10 servers, and etc. Can add/remove child nodes. The elements are stored in a multidimensional list.
The super class “classroom” is not really used, just to try out the inherit. The relationship can be stored explicitly in the list, but I did not do this since the implicit can simplify the code (minus one dimension from the list).
Different classrooms can be added to lab, in the same way different computers, but with same specification, can be added(assigned) to a person.
#!/usr/bin/python classRoomList=[] #class room serverRoomList=[] #server room tableList=[] #table beamerList=[] #projector WBList=[] #while board PCList=[] #computer screenList=[] #TFT screens KMList=[] #keyboard & mouse in pair class classRoom(object): def __init__(self): self.door=1 self.window=1 class classLab(classRoom): def __init__(self,classroom, serverroom): self.addClassRM(classroom) self.addServerRM(serverroom) def __print__(self): print "classlab" def addClassRM(self, classroom): if self.findListByName(classroom,classRoomList)>=0: #name used, search return positive number print "Duplicated \""+classroom+"\", no room added.\n" else: classRoomList.append([classroom,classRM(classroom)]) print "\""+classroom+"\" added, "+str(len(classRoomList))+" classroom(s) in total.\n" def addServerRM(self,serverroom): serverRoomList.append([serverroom,serverRM(serverroom)]) def findListByName(self,name,localList): for i in range(len(localList)): if localList[i][0]==name: return i print i def getClassObj(self,index): return classRoomList[index][1] def removeClassRM(self,classroom): if self.findListByName(classroom,classRoomList)>=0: #name used, search return positive number #now we can remove it print "Nr. "+str(self.findListByName(classroom,classRoomList))+" removed" classRoomList.remove(classRoomList[self.findListByName(classroom,classRoomList)]) print classRoomList else: #we cannot remove print "Nothing to remove. \n" class classRM(classRoom): def __init__(self,name): self.name=name self.description=name def findListByName(self,name,localList): for i in range(len(localList)): if localList[i][0]==name: return i print i class serverRM(classRoom): def __init__(self,name): self.name=name self.description=name x = classLab("classroom1", "serverroom1") x.addClassRM("classroom2") x.addClassRM("classroom1") x.removeClassRM("classroom2") x.removeClassRM("classroom5") print x.getClassObj(x.findListByName("classroom1",classRoomList))