Chapter 9 : Structures

Example 9.1, Page No 194

In [7]:
class Prod:
    def __init__(self, cName, iId, dPrice, iNo, cSupp):
        self.cName = cName
        self.iId = iId
        self.dPrice = dPrice
        self.iNo = iNo
        self.cSupp = cSupp
    def println(self):
        print self.cName + "\t    ",
        print str(self.iId) + "\t ",
        print str(self.dPrice) + "\t  ",
        print str(self.iNo) + "\t   ",
        print self.cSupp + "\t\n",
if __name__ == '__main__':
    prodOne = Prod("Olive Oil",1001,120.50,250,"Frescati Oil S/A")
    print "Enter information for product"
    cName = raw_input("Start with the product name: ")
    iId = int(raw_input("The product id: "))
    dPrice = float(raw_input("The price: "))
    iNo = int(raw_input("How many items are there in stock: "))
    cSupp = raw_input("Who supplies the product: ")
    prodTwo = Prod(cName,iId,dPrice,iNo,cSupp)
    print "Productname \t Product id \t Price \t Quantity \t Supplier\n"
    prodOne.println()
    prodTwo.println()
Enter information for product
Start with the product name: Coconut Oil
The product id: 1002
The price: 120.50
How many items are there in stock: 230
Who supplies the product: Frescati Oil S/A
Productname 	 Product id 	 Price 	 Quantity 	 Supplier

Olive Oil	     1001	  120.5	   250	    Frescati Oil S/A	
Coconut Oil	     1002	  120.5	   230	    Frescati Oil S/A	

Example 9.2, Page No 200

In [9]:
class Prod:
    def __init__(self, cName, iId, dPrice, iNo, cSupp):
        self.cName = cName
        self.iId = iId
        self.dPrice = dPrice
        self.iNo = iNo
        self.cSupp = cSupp
def printOnScreen(prodOne1):
        print prodOne1.cName + "\t    ",
        print str(prodOne1.iId) + "\t ",
        print str(prodOne1.dPrice) + "\t  ",
        print str(prodOne1.iNo) + "\t   ",
        print prodOne1.cSupp + "\t\n",

if __name__ == '__main__':
    prodOne = Prod("Olive Oil",1001,120.50,250,"Frescati Oil S/A")
    printOnScreen(prodOne)
    
Olive Oil	     1001	  120.5	   250	    Frescati Oil S/A	

Example 9.3, Page No 202

In [11]:
class Prod:
    def __init__(self, cName, iId, dPrice, iNo, cSupp):
        self.cName = cName
        self.iId = iId
        self.dPrice = dPrice
        self.iNo = iNo
        self.cSupp = cSupp
def printOnScreen(p,n):
    for i in range(n):
        print p[i].cName + "\t    ",
        print str(p[i].iId) + "\t ",
        print str(p[i].dPrice) + "\t  ",
        print str(p[i].iNo) + "\t   ",
        print p[i].cSupp + "\t\n",
if __name__ == '__main__':
    sProds = []
    sProds.append(Prod("Food Oil",101,12.50,100,"Felix Ltd"))
    sProds.append(Prod("Baby Oil",102,23.75,25,"Baby Prod"))
    sProds.append(Prod("Boiler Oil",103,6100,123000,"Shell"))
    printOnScreen(sProds,3)
    
Food Oil	     101	  12.5	   100	    Felix Ltd	
Baby Oil	     102	  23.75	   25	    Baby Prod	
Boiler Oil	     103	  6100	   123000	    Shell	

Example 9.4, Page No 205

In [13]:
class Prod:
    def __init__(self, cName, iId, dPrice, iNo, cSupp):
        self.cName = cName
        self.iId = iId
        self.dPrice = dPrice
        self.iNo = iNo
        self.cSupp = cSupp
def printOnScreen(p,n):
    for i in range(n):
        print p[i].cName + "\t    ",
        print str(p[i].iId) + "\t ",
        print str(p[i].dPrice) + "\t  ",
        print str(p[i].iNo) + "\t   ",
        print p[i].cSupp + "\t\n",
if __name__ == '__main__':
    sProds = []
    sProds.append(Prod("Food Oil",101,12.50,100,"Felix Ltd"))
    sProds.append(Prod("Baby Oil",102,23.75,25,"Baby Prod"))
    sProds.append(Prod("Boiler Oil",103,6100,123000,"Shell"))
    pProds = id(sProds)
    printOnScreen(sProds,3)
Food Oil	     101	  12.5	   100	    Felix Ltd	
Baby Oil	     102	  23.75	   25	    Baby Prod	
Boiler Oil	     103	  6100	   123000	    Shell	
In [ ]: