This assignment will ask you to write some classes and a function that will proc
ID: 3571358 • Letter: T
Question
This assignment will ask you to write some classes and a function that will process a list of strings, and turn that into a list of objects (and aggregate objects).
You will have to do the following for this assignment:
Create and complete the methods of the Item class.
Create and complete the methods of the Shipment class.
Create and complete the methods of the ItemException class.
Write a main function in a file called project5.py.
You may complete all three components in a single file, or you may create multiple files as long as you import them correctly.Step 1: Writing Test Cases
Ideally, you should write your test cases before you write your code. However, testing objects, such as those of the Shipment class, are more difficult due to their stateful nature. We can no longer test all methods individually, without at least calling the constructor. For this reason, we will not be writing formal test cases for the project; we need a better testing infrastructure, which you will learn about in CS211 if you take it. Instead, we've provided a sample test harness inside the driver below. Feel free to modify this harness (by including print statements?) should you need to.
You will be creating a Item class with the following methods:
You will be creating a Shipment class with the following methods:
You will be creating a ItemException class that extends the Exception class with the following method:
You will also write a main function in project5.py that has the following functionality:
A string in the list will either be a new Shipment, or data of an Item belonging to a Shipment.
Each shipment will contain only the numeric digits of the shipment id, followed by a newline.
Each item will be two lines long. On the first line will be the name of an item, followed by a single space, followed by the five digit item id, followed by a newline. On the next line, the item's price will appear. A price starts with a $, followed by an integer, followed by a period, followed by two numeric digits, followed by a newline.
Each item belongs to the most recently processed shipment. We will guarantee that a shipment will always appear before an item in the list.
There may be multiple shipments in the list (each with their own items).
It is possible for items to be malformed. While we will guarantee that the items' ids will always be five digits in length, there are two main problems that could occur with processing items: 1) there is a missing space between the item name and its id, and 2) the price is not valid (prices must be a non-negative dollar-and-cents amount with two digits for the cents portion). If either of these situations arises, your code must raise an ItemException immediately and discontinue processing the rest of the file.
Step 2: Writing Code
You will need the return statement to get your functions to return a value - DO NOT use the print statement for this!
Explanation / Answer
Solution:
# class for the items
class Item:
# constructor
def __init__(self,ITname,ITid,Itprice):
self.ITname = ITname
self.Itid = ITid
self.Itprice = Itprice
# method to get item name
def getItName(self):
return self.ITname
# method to get item price
def getItPrice(self):
return self.Itprice
# method to get item id
def getItId(self):
return self.Itid
# method to print string
def __str__(self):
return str(self.ITname)+ ' ' + str(self.Itid)+ ' ' + str(self.Itprice)
# class for shipment
class Shipment:
# constructor
def __init__(self,ITid):
self.ITid = ITid
self.Iitems = []
# method to get shipment id
def getItId(self):
return self.ITid
# method to get shipment items
def getItItems(self):
return self.Iitems
# method to add the item
def addIItem(self,Iitem):
self.Iitems.append(Iitem)
# method to print the shipitem in string
def __str__(self):
Iresult = str(self.ITid) + ': ['
Ictr = 0
while Ictr < len(self.Iitems):
if Ictr == len(self.Iitems)-1:
Iresult += str(self.Iitems[Ictr])
else:
Iresult += str(self.Iitems[Ictr]) + ','
Ictr += 1
return Iresult + ']'
# class for exception
class ItemException:
def __init__(self,excmessage):
self.excmessage = excmessage
self.empty_lis = []
def __str__(self):
return str(self.excmessage)
# main method
def main(Ilists):
Ifinal = []
IshipmentId = Ilists[0][:-1]
Ishipment = Shipment(IshipmentId)
It = 1
while It + 1 < len(Ilists):
if not Ilists[It][:-1].isdigit():
shipIitem = Ilists[It].split(' ')[0]
if len(Ilists[It].split(' ')) == 1:
raise ItemException(Exception)
itemIId = Ilists[It].split(' ')[It][:-1]
itemIPrice = Ilists[It+1][:-1]
if itemIPrice[0] != '$' or itemIPrice[1] == '$':
raise ItemException(Exception)
Iitem = Item(shipIitem, itemIId, itemIPrice)
Ishipment.addIItem(Iitem)
It += 1
Ifinal.append(Ishipment)
return Ifinal
# code to test main method
Ishipments = []
Iitem = Item('socks',12345,'$4.56')
Ishipment = Shipment(55555555)
Ishipment.addIItem(Iitem)
Iitem = Item('socks',12345,'$4.56')
Ishipment.addIItem(Iitem)
Ishipments.append(Ishipment)
Iresult = main(['55555555 ','socks 12345 ','$4.56 ','socks 12345 ','$4.56 '])
print Iresult