Programming Project 5

This projects will ask you to combine the various programming techniques and syntax you've learned this semester to process a list of data into objects. We encourage you to pay special attention to this assignment, and make sure you're able to complete all parts of it successfully, as unlike other projects this semester, you'll be dependent on the code you write for this project to complete assessments 9. We encourage you to work in groups on this assignment, but also to be aware that passively participating in group discussions will not be a productive way to ensure that you'll do well on these assessments.


This final 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:

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:
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\n','socks 12345\n','$4.56\n','socks 12345\n','$4.56\n'])

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. There are videos (video1, video2) that a student recorded of my lecture on this function that I encourage you to review before starting this function.

Step 2: Writing Code
Once you have written your test cases, you can begin to write your code for the project in the files project3.py and project4.py (and remember the link to the template for this file above).

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

Please log in again to view this part of the page.



Project Hints and Guidelines


Like for assessments, you should not hard-code solutions to work for the release tests; when writing your code, pretend you don't have the release tests. Your code should work for any possible valid test inputs.

back to schedule