USING PYTHON You will have to do the following for this assi
USING PYTHON, 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.
For example, the main function may be called as follows:
main([\'55555555\ \',\'socks 12345\ \',\'$4.56\ \',\'socks 12345\ \',\'$4.56\ \'])
which would return a list of objects identical to that list produced by the following code:
shipments = []
item = Item(\'socks\',12345,\'$4.56\')
shipment = Shipment(55555555)
shipment.addItem(item)
item = Item(\'socks\',12345,\'$4.56\')
shipment.addItem(item)
shipments.append(shipment)
You will find the isdigit() method of strings especially useful for this project.
Your goal with this function is to take a list of strings, and turn them into a list of objects.
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!
Step 3: Testing Your Code
The following part is the driver needed to test code
from project5 import *
import traceback
import subprocess
def checkEqual(shipments1,shipments2):
ctr = 0
if len(shipments1) != len(shipments2):
return False
while ctr < len(shipments1):
shipment1 = shipments1[ctr]
shipment2 = shipments2[ctr]
if str(shipment1.getId()) != str(shipment2.getId()):
return False
if len(shipment1.getItems()) != len(shipment2.getItems()):
return False
i = 0
while i < len(shipment1.getItems()):
item1 = shipment1.getItems()[i]
item2 = shipment2.getItems()[i]
if str(item1) != str(item2):
return False
i = i + 1
ctr = ctr + 1
return True
def test1():
item = Item(\'socks\',12345,\'$4.56\')
return str(item) == \'socks 12345 $4.56\'
def test2():
item = Item(\'socks\',12345,\'$4.56\')
return str(item.getName()) == \'socks\'
def test3():
item = Item(\'socks\',12345,\'$4.56\')
return str(item.getId()) == \'12345\'
def test4():
item = Item(\'socks\',12345,\'$4.56\')
return str(item.getPrice()) == \'$4.56\'
def test5():
shipment = Shipment(55555555)
return str(shipment) == \'55555555: []\'
def test6():
shipment = Shipment(55555555)
return str(shipment.getId()) == \'55555555\'
def test7():
shipment = Shipment(55555555)
return shipment.getItems() == []
def test8():
item = Item(\'socks\',12345,\'$4.56\')
shipment = Shipment(55555555)
shipment.addItem(item)
return str(shipment) == \'55555555: [socks 12345 $4.56]\'
def test9():
item = Item(\'socks\',12345,\'$4.56\')
shipment = Shipment(55555555)
shipment.addItem(item)
item = Item(\'shirt\',33333,\'$14.78\')
shipment.addItem(item)
return str(shipment) == \'55555555: [socks 12345 $4.56,shirt 33333 $14.78]\'
def test10():
item = Item(\'socks\',12345,\'$4.56\')
shipment = Shipment(55555555)
shipment.addItem(item)
result = main([\'55555555\ \',\'socks 12345\ \',\'$4.56\ \'])
return checkEqual([shipment],result)
def test11():
shipments = []
item = Item(\'socks\',12345,\'$4.56\')
shipment = Shipment(55555555)
shipment.addItem(item)
item = Item(\'shorts\',33333,\'$42.56\')
shipment.addItem(item)
shipments.append(shipment)
result = main([\'55555555\ \',\'socks 12345\ \',\'$4.56\ \',\'shorts 33333\ \',\'$42.56\ \'])
return checkEqual(shipments,result)
def test12():
shipments = []
item = Item(\'socks\',12345,\'$4.56\')
shipment = Shipment(55555555)
shipment.addItem(item)
item = Item(\'shorts\',33333,\'$42.56\')
shipment.addItem(item)
shipments.append(shipment)
item = Item(\'books\',12345,\'$4.56\')
shipment = Shipment(66678)
shipment.addItem(item)
item = Item(\'mirror\',33333,\'$42.56\')
shipment.addItem(item)
item = Item(\'necklace\',33333,\'$42.56\')
shipment.addItem(item)
shipments.append(shipment)
item = Item(\'shoes\',12345,\'$4.56\')
shipment = Shipment(1)
shipment.addItem(item)
item = Item(\'pencils\',33333,\'$42.56\')
shipment.addItem(item)
item = Item(\'eraser\',33333,\'$42.56\')
shipment.addItem(item)
shipments.append(shipment)
result = main([\'55555555\ \',\'socks 12345\ \',\'$4.56\ \',\'shorts 33333\ \',\'$42.56\ \',\'66678\ \',\'books 12345\ \',\'$4.56\ \',\'mirror 33333\ \',\'$42.56\ \' ,\'necklace 33333\ \',\'$42.56\ \',\'1\ \',\'shoes 12345\ \',\'$4.56\ \',\'pencils 33333\ \',\'$42.56\ \',\'eraser 33333\ \',\'$42.56\ \'])
return checkEqual(shipments,result)
def test13():
shipments = []
item = Item(\'socks\',12345,\'$4.56\')
shipment = Shipment(55555555)
shipment.addItem(item)
item = Item(\'socks\',12345,\'$4.56\')
shipment.addItem(item)
shipments.append(shipment)
result = main([\'55555555\ \',\'socks 12345\ \',\'$4.56\ \',\'socks 12345\ \',\'$4.56\ \'])
return checkEqual(shipments,result)
def test14():
result = False
try:
main([\'55555555\ \',\'socks12345\ \',\'$4.56\ \',\'socks 12345\ \',\'$4.56\ \'])
except ItemException:
result = True
return result
def test15():
result = False
try:
main([\'55555555\ \',\'socks 12345\ \',\'4.56\ \',\'socks 12345\ \',\'$4.56\ \'])
except ItemException:
result = True
return result
def test16():
result = False
try:
main([\'55555555\ \',\'socks 12345\ \',\'$4.567\ \',\'socks 12345\ \',\'$4.56\ \'])
except ItemException:
result = True
return result
def test17():
result = False
try:
main([\'55555555\ \',\'socks 12345\ \',\'$4.56\ \',\'socks 12345\ \',\'$-4.56\ \'])
except ItemException:
result = True
return result
def test18():
result = False
try:
main([\'55555555\ \',\'socks 12345\ \',\'$4.56\ \',\'socks 12345\ \',\'$4.56.56\ \'])
except ItemException:
result = True
return result
ctr = 1
failed = False
while ctr <= 18:
try:
if eval(\"test\"+str(ctr)+\"()\") == True:
print \"PASSED test\"+str(ctr)+\"!\"
else:
print \"Please check your code for test\"+str(ctr)
failed = True
except Exception as e:
traceback.print_exc()
print \"Please check your code for test\"+str(ctr)+\", it raised an undesired exception\"
failed = True
ctr = ctr + 1
if failed:
result = subprocess.check_output(\"curl -k https://cs.gmu.edu/~kdobolyi/sparc/process.php?user=sparc_JjNcihjCCN7CpKRh-project5-PROGRESS\", shell=True)
else:
result = subprocess.check_output(\"curl -k https://cs.gmu.edu/~kdobolyi/sparc/process.php?user=sparc_JjNcihjCCN7CpKRh-project5-COMPLETED\", shell=True)
_copy the entire code for the driver in the part above into a file called driver.py. To use this driver, make sure your project.py (which is the code in the first part) and driver.py (code in 2nd part) are all in the same directory, and from a terminal in that directory, type:
python driver.py
| A constructor that takes a name, id, and price. The constructor creates these three attributes, and sets them to the incoming arguments. |
| Getters for all attributes (getName, getId, getPrice). |
| A to-string method that can be called with str(...), which returns the name of the item, followed by a space, followed by the id, followed by a space, followed by the price. For example, a constructor call of item = Item(\'socks\',12345,\'$4.56\') would yield the string \'socks 12345 $4.56\'. |
Solution
from operator import methodcaller
class Item:
def __init__(self,name,id,price):
self.name=name
self.id=id
self.price=price
def getName():
return self.name
def getId():
return self.id
def getPrice():
return self.price
def __str__(self):
print (\"\'\", self.name, \" \",self.id, \" $\", self.price, \"\'.\")
class Shipment:
item = Item(0,0,0)
def __init__(self,id):
self.id=id
items = []
def getId(self):
return self.id
def getItems(self):
return self.items
def addItem(self,item):
self.item=item
self.items.append(self.item)
def __str__(self):
print (self.id,\": [\" ,items ,\"]\")
class ItemException(Exception):
def __init__(self,msg):
self.msg=msg
items = []
def __str__(self):
print (self.msg)
def main(*items):
print (items)
items = map(lambda s: s.strip(), items)
shipments = []
v_shipId = map(0,items)
shipment = Shipment(v_shipId)
i=1
j=1
while i < 5:
B = map(methodcaller(\"split\", \" \"), items)
print (B)
if j == 1:
v_name = map(0,B)
v_id = map(1,B)
j=2
i=i+1
if j == 2:
v_price=map(0,B)
i=i+1
item = Item(v_name,v_id,v_price)
shipment.addItem(item)
shipments.append(shipment)
main([\'55555555\ \',\'socks 12345\ \',\'$4.56\ \',\'socks 12345\ \',\'$4.56\ \'])






