YOU NEED TO MODIFY THE PROJECT INCLUDED AT THE END AND USE THE INCLUDED DRIVER T
ID: 3796561 • Letter: Y
Question
YOU NEED TO MODIFY THE PROJECT INCLUDED AT THE END AND USE THE INCLUDED DRIVER TO TEST YOUR CODE TO MAKE SURE IT WORK AS EXPECTED. Thanks
USING PYTHON, complete the template below such that you will provide code to solve the following problem:
Please write and/or extend the project5 attached at the end.
You will have to do the following for this assignment:
Create and complete the methods of the PriceException class.
Create the processFile function.
Modify the main function in project5.py.
You will be creating a PriceException class that extends the Exception class with the following methods. Later, you will modify your main method to use this class. Write the following methods for the PriceException class:
You will write a function called processFile:
Finally, modify your main:
Testing Your Code
When you are ready, right-mouse-click to save the file as driver.py to the same directory as your project5.py. This driver already contains the tests, and will tell you which test number you're failing; look at the source code of the driver to see the details of that test. You can run the driver through the terminal with the command: python driver.py
The driver is as follows:
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('sockz',12346,'$4.44')
return str(item) == 'sockz 12346 $4.44'
def test2():
item = Item('sockz',12346,'$4.44')
return str(item.getName()) == 'sockz'
def test3():
item = Item('sockz',12346,'$4.44')
return str(item.getId()) == '12346'
def test4():
item = Item('sockz',12346,'$4.44')
return str(item.getPrice()) == '$4.44'
def test5():
shipment = Shipment(55551555)
return str(shipment) == '55551555: []'
def test6():
shipment = Shipment(55551555)
return str(shipment.getId()) == '55551555'
def test7():
shipment = Shipment(55551555)
return shipment.getItems() == []
def test8():
item = Item('sockz',12346,'$4.44')
shipment = Shipment(55551555)
shipment.addItem(item)
print str(shipment)
return str(shipment) == '55551555: [sockz 12346 $4.44]'
def test9():
item = Item('sockz',12346,'$4.44')
shipment = Shipment(55551555)
shipment.addItem(item)
item = Item('shirt',33233,'$14.78')
shipment.addItem(item)
return str(shipment) == '55551555: [sockz 12346 $4.44,shirt 33233 $14.78]'
def test10():
item = Item('sockz',12346,'$4.44')
shipment = Shipment(55551555)
shipment.addItem(item)
result = main(['55551555 ','sockz 12346 ','$4.44 '])
return checkEqual([shipment],result)
def test11():
shipments = []
shipment = Shipment(55551555)
item = Item('shorts',33233,'$42.56')
shipment.addItem(item)
item = Item('sockz',12346,'$4.44')
shipment.addItem(item)
shipments.append(shipment)
result = main(['55551555 ','sockz 12346 ','$4.44 ','shorts 33233 ','$42.56 '])
return checkEqual(shipments,result)
def test12():
shipments = []
shipment = Shipment(1)
item = Item('eraser',33233,'$42.56')
shipment.addItem(item)
item = Item('pencils',33233,'$42.56')
shipment.addItem(item)
item = Item('shoez',12346,'$4.44')
shipment.addItem(item)
shipments.append(shipment)
item = Item('books',12346,'$4.44')
shipment = Shipment(66678)
shipment.addItem(item)
item = Item('mirror',33233,'$42.56')
shipment.addItem(item)
item = Item('necklace',33233,'$42.56')
shipment.addItem(item)
shipments.append(shipment)
shipment = Shipment(55551555)
item = Item('shorts',33233,'$42.56')
shipment.addItem(item)
item = Item('sockz',12346,'$4.44')
shipment.addItem(item)
shipments.append(shipment)
result = main(['55551555 ','sockz 12346 ','$4.44 ','shorts 33233 ','$42.56 ','66678 ','books 12346 ','$4.44 ','mirror 33233 ','$42.56 ','necklace 33233 ','$42.56 ','1 ','shoez 12346 ','$4.44 ','pencils 33233 ','$42.56 ','eraser 33233 ','$42.56 '])
return checkEqual(shipments,result)
def test13():
shipments = []
item = Item('sockz',12346,'$4.44')
shipment = Shipment(55551555)
shipment.addItem(item)
item = Item('sockz',12346,'$4.44')
shipment.addItem(item)
shipments.append(shipment)
result = main(['55551555 ','sockz 12346 ','$4.44 ','sockz 12346 ','$4.44 '])
return checkEqual(shipments,result)
def test14():
result = False
try:
main(['55551555 ','sockz12346 ','$4.44 ','sockz 12346 ','$4.44 '])
except ItemException:
result = True
return result
def test15():
result = False
try:
main(['55551555 ','sockz 12346 ','4.44 ','sockz 12346 ','$4.44 '])
except PriceException:
result = True
return result
def test16():
result = False
try:
main(['55551555 ','sockz 12346 ','$4.447 ','sockz 12346 ','$4.44 '])
except PriceException:
result = True
return result
def test17():
result = False
try:
main(['55551555 ','sockz 12346 ','$4.44 ','sockz 12346 ','$-4.44 '])
except PriceException:
result = True
return result
def test18():
result = False
try:
main(['55551555 ','sockz 12346 ','$4.44 ','sockz 12346 ','$4.44.56 '])
except PriceException:
result = True
return result
def test19():
file = open("data.txt", "w")
file.write("5555555555 Socks 12345 $5.59 T-shirt 22222 $11.57 ")
file.close()
list = processFile("data.txt")
return list == ["5555555555 ","Socks 12345 ","$5.59 ","T-shirt 22222 ","$11.57 "]
failed = False
ctr = 1
while ctr <= 19:
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 not failed:
print "You PASSED ALL TESTS! Please remember to REMOVE DEBUG PRINT STATEMENTS before submitting to Marmoset."
result = subprocess.check_output("curl -k https://cs.gmu.edu/~kdobolyi/sparc/process.php?user=sparc_JjNcihjCCN7CpKRh-sample_ass9_1-COMPLETED", shell=True)
else:
print "At least one test is not passing yet."
result = subprocess.check_output("curl -k https://cs.gmu.edu/~kdobolyi/sparc/process.php?user=sparc_JjNcihjCCN7CpKRh-sample_ass9_1-PROGRESS", shell=True)
The following part (project5.py) is for reference only:
class Item:
def __init__(self,name,id,price):
self.name = name
self.Id = id
self.price = price
def getName(self):
return self.name
def getPrice(self):
return self.price
def getId(self):
return self.Id
def __str__(self):
return str(self.name)+ ' ' + str(self.Id)+ ' ' + str(self.price)
class Shipment:
def __init__(self,id):
self.id = id
self.items = []
def getId(self):
return self.id
def getItems(self):
return self.items
def addItem(self,item):
self.items.append(item)
def __str__(self):
result = str(self.id) + ': ['
ctr = 0
while ctr < len(self.items):
if ctr == len(self.items)-1:
result += str(self.items[ctr])
else:
result += str(self.items[ctr]) + ','
ctr += 1
return result + ']'
class ItemException:
def __init__(self,message):
self.message = message
self.empty_lis = []
def __str__(self):
return str(self.message)
class PriceException:
def __init__(self):
self.messages = messages
self.empty = []
def __str__(self):
return str(self.message)
def processFile(filename):
file = open('filename','r')
contents = file.readlines()
list = str(contents)
return list
def main(lis):
final = []
ctr = 0
#This will split up multiple shipments into lists within a list (will also leave single shipments in a list of a list)
while ctr < len(lis):
seperate_items = []
if lis[ctr][:-1].isdigit():
seperate_items.append(lis[ctr][:-1])
ctr2 = ctr + 1
while ctr2 < len(lis):
if not lis[ctr2][:-1].isdigit():
seperate_items.append(lis[ctr2][:-1])
else:
break
ctr2 += 1
final.append(seperate_items)
ctr += 1
#[['55555555', 'socks 12345', '$4.56', 'shorts 33333', '$42.56']]
#Now I need to make the code run with the new values like it should to pass
#This section needs to be modified to sort...
#the name of the items by their name in alphabetical order
#and shipments need to be sorted by their ids in numeric order (ascending).
ctr3 = 0
result = []
while ctr3 < len(final):
ctr4 = 1
shipmentId = final[ctr3][ctr4-1]
shipment = Shipment(shipmentId)
while ctr4 < len(final[ctr3]):
if not final[ctr3][ctr4].isdigit():
shipitem = final[ctr3][ctr4].split(' ')[0]
#raise an exception for there being a missing space
#between an item and its ID
if len(final[ctr3][ctr4].split(' ')) == 1:
raise ItemException(Exception)
itemId = final[ctr3][ctr4].split(' ')[1]
itemPrice = final[ctr3][ctr4+1]
if itemPrice[0] != '$' or itemPrice[-3] != '.' or '-' in itemPrice or itemPrice.count('.') > 1:
raise ItemException(Exception)
item = Item(shipitem, itemId, itemPrice)
shipment.addItem(item)
ctr4 += 2
ctr3 += 1
result.append(shipment)
#result is the origonal end of the code in Project 5
#[55555555: [socks 12345 $4.56,shorts 33333 $42.56],66678: [books 12345 $4.56,mirror 33333 $42.56,necklace 333333 $42.56],1: [shoes 12345 $4.56,pencils 33333 $42.56,eraser 33333 $42.56]]
#Make a loop to sort the shipmentId in numerical order (ascending)
new_list = []
while result:
minimum = result[0]
for x in result:
if x < minimum:
minimum = x
new_list.append(minimum)
result.remove(minimum)
#Make a loop to sort the items in alphabetical order
alpha_list = []
ctr5 = 0
while new_list:
minimum2 = new_list[ctr5][0]
for i in new_list[ctr5]:
if i < minimum2:
minimum2 = i
alpha_list.append(minimum2)
ctr5 += 1
return alpha_list
Explanation / Answer
class Item:
def __init__(self,name,id,price):
self.name = name
self.Id = id
self.price = price
def getName(self):
return self.name
def getPrice(self):
return self.price
def getId(self):
return self.Id
def __str__(self):
return str(self.name)+ ' ' + str(self.Id)+ ' ' + str(self.price)
class Shipment:
def __init__(self,id):
self.id = id
self.items = []
def getId(self):
return self.id
def getItems(self):
return self.items
def addItem(self,item):
self.items.append(item)
def __str__(self):
result = str(self.id) + ': ['
ctr = 0
while ctr < len(self.items):
if ctr == len(self.items)-1:
result += str(self.items[ctr])
else:
result += str(self.items[ctr]) + ','
ctr += 1
return result + ']'
class ItemException:
def __init__(self,message):
self.message = message
self.empty_lis = []
def __str__(self):
return str(self.message)
class PriceException:
def __init__(self, message):
self.message = message
self.items = []
def __str__(self):
return str(self.message)
def processFile(filename):
contents = []
with open(filename,'r') as fp:
for line in fp:
contents.append(str(line))
return contents
def main(lis):
final = []
ctr = 0
#This will split up multiple shipments into lists within a list (will also leave single shipments in a list of a list)
while ctr < len(lis):
seperate_items = []
if lis[ctr][:-1].isdigit():
seperate_items.append(lis[ctr][:-1])
ctr2 = ctr + 1
while ctr2 < len(lis):
if not lis[ctr2][:-1].isdigit():
seperate_items.append(lis[ctr2][:-1])
else:
break
ctr2 += 1
final.append(seperate_items)
ctr += 1
#[['55555555', 'socks 12345', '$4.56', 'shorts 33333', '$42.56']]
#Now I need to make the code run with the new values like it should to pass
#This section needs to be modified to sort...
#the name of the items by their name in alphabetical order
#and shipments need to be sorted by their ids in numeric order (ascending).
ctr3 = 0
result = []
while ctr3 < len(final):
ctr4 = 1
shipmentId = final[ctr3][ctr4-1]
shipment = Shipment(shipmentId)
while ctr4 < len(final[ctr3]):
if not final[ctr3][ctr4].isdigit():
#raise an exception for there being a missing space
#between an item and its ID
if len(final[ctr3][ctr4].split(' ')) == 1:
raise ItemException(Exception)
shipitem = final[ctr3][ctr4].split(' ')[0]
itemId = final[ctr3][ctr4].split(' ')[1]
itemPrice = final[ctr3][ctr4+1]
if itemPrice[0] != '$' or itemPrice[-3] != '.' or '-' in itemPrice or itemPrice.count('.') > 1:
raise PriceException(Exception)
item = Item(shipitem, itemId, itemPrice)
shipment.addItem(item)
ctr4 += 2
ctr3 += 1
result.append(shipment)
new_list = []
while result:
minimum = result[0]
for x in result:
if int(x.getId()) < int(minimum.getId()):
minimum = x
new_list.append(minimum)
result.remove(minimum)
#Make a loop to sort the items in alphabetical order
result = []
for shipment in new_list:
new_item_list = []
old_item_list = shipment.getItems()
while old_item_list:
minimum2 = old_item_list[0]
for i in old_item_list:
if i.getName() < minimum2.getName():
minimum2 = i
new_item_list.append(minimum2)
old_item_list.remove(minimum2)
shipment.items = new_item_list
result.append(shipment)
return result
# please read code form following link if indentation is messed up: http://pastebin.com/UMKr9Di3
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.