Question: USING PYTHON This assignment will ask you to write some classes and a function that will process a list of strings, and turn that into
USING PYTHON
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. You will be creating a Item class with the following methods:
| 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'. |
You will be creating a Shipment class with the following methods:
| A constructor that takes an id. The constructor creates this attribute, and sets it to the incoming argument. The constructor also initializes an empty list of items. |
| Getters for all attributes (getId and getItems). |
| An addItem method that takes an Item as an argument and adds it to the end of the shipment's list of items. |
| A to-string method that can be called with str(...), which returns the id of the item, followed by a colon, followed space, followed by a list of all its shipments. The list will open and close with [ and ], and will contain all the items of the shipment, in the order of the list, using their to-string method to append them with commas as the only separator (see the test cases. For example the following object: item = Item('socks',12345,'$4.56') shipment = Shipment(55555555) shipment.addItem(item) item = Item('shirt',33333,'$14.78') shipment.addItem(item) would return the string '55555555: [socks 12345 $4.56,shirt 33333 $14.78]'. |
You will be creating a ItemException class that extends the Exception class with the following method:
| A constructor that takes a message as an argument. The constructor creates this attribute, and sets it to the incoming argument. The constructor also initializes an empty list of items. |
| A to-string method that can be called with str(...), which returns the message of the exception. |
You will also write a main function in project5.py that has the following functionality:
| The main takes as argument a list of string. It will process the contents of the list to create a list of Shipment objects. This list of Shipment objects are then returned. The list argument's contents will be as follows: 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 I have included a driver. Click on this link and copy that code from the browser into a file called driver.py. To use this driver, make sure your project5.py and driver.py are all in the same directory, and from a terminal in that directory, type: python driver.py
The driver is included below:
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)
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
