relationship

  1. lab has
    1. classRM
      1. table
        1. PC
        2. screen
        3. keyboard&mouse
      2. beamer
      3. WB
    2. serverRM
      1. rack
        1. server
        2. KVM
        3. switch
      2. library
        1. books
      3. fridge
        1. drink
        2. 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))