class Customer:
# constructor
def __init__(self,cnum,name,cty):
self.cno = cnum
self.cname = name
self.city = cty
# getter
def get_cno(self):
return self.cno
# getter
def get_cname(self):
pass
# getter
def get_city(self):
return self.city
# setter
def set_cno(self,cnum):
pass
# setter
def set_cname(self,name):
pass
# setter
def set_city(self,cty):
pass
# return string representation of Customer object
def __str__(self): # same as toString() method in Java
result = ""
result = result + "CNO:\t" + str(self.cno) + "\n"
result = result + "CNAME:\t" + self.cname + "\n"
result = result + "CITY:\t" + self.city + "\n"
return result
c1 = Customer(9000,"K Jones","Atlanta")
print(c1)
CNO: 9000 CNAME: K Jones CITY: Atlanta
class Customers():
# constructor
def __init__(self):
self.customers = {}
# add Customer object c to dictionary; do not add if customer number already present
def add_customer(self,cno,c):
if cno not in self.customers:
self.customers[cno] = c
# return Customer object corresponding to cno; return None if no customer with cno
def get_customer(self,cno):
pass
# return a list of all customer numbers
def get_cnos(self):
pass
# return string representation of Customers object
def __str__(self):
result = ""
for cno in self.customers:
result = result + str(self.customers[cno]) + "\n"
return result
class Part:
# constructor
def __init__(self,pnum,name,price):
self.pno = pnum
self.pname = name
self.price = price
# getter
def get_pno(self):
return self.pno
# getter
def get_pname(self):
pass
# getter
def get_price(self):
pass
# setter
def set_pno(self,pnum):
pass
# setter
def set_pname(self,name):
pass
# setter
def set_price(self,price):
pass
# return string representation of Part object
def __str__(self): # same as toString() method in Java
result = ""
result = result + str(self.pno) +"\t"
result = result + self.pname +"\t\t"
result = result + str(self.price)
return result
class Parts():
# constructor
def __init__(self):
self.parts = {}
# add Part object p to dictionary; do not add if part number already exists
def add_part(self,p):
if p.get_pno() not in self.parts:
self.parts[p.get_pno()] = p
# return Part object for pno; return None if no part with pno
def get_part(self,pno):
pass
# return a list of all part numbers
def get_pnos(self):
pass
# return string representation of Parts object
def __str__(self):
result = ""
for pno in self.parts:
result = result + str(self.parts[pno]) + "\n"
return result
# load_data function
dname = "data"
fname = dname + "/customers.dat"
with open(fname) as f:
data = f.read().splitlines()
customers = Customers()
for line in data:
xs = line.split(":")
c = Customer(int(xs[0]),xs[1],xs[2])
customers.add_customer(c.get_cno(),c)
print(customers)
fname = dname + "/parts.dat"
with open(fname) as f:
data = f.read().splitlines()
parts = Parts()
for line in data:
xs = line.split(":")
p = Part(int(xs[0]),xs[1],float(xs[2]))
parts.add_part(p)
print(parts)
CNO: 1111 CNAME: John Smith CITY: Atlanta CNO: 2222 CNAME: Alice Stewart CITY: Chicago CNO: 3333 CNAME: Doug Jones CITY: Montgomery 1000 Nut 2.5 1001 Bolt 3.0 1002 Screw 1.25 1003 Crankshaft 12.0 1004 Cylinder 8.0 1005 Casing 22.0