Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

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!

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'. final assignment will ask you to write some classes and a function that will process alistof strings, and tura that into alistofobjects (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 shapeent class. Create and complete the methods of the Itentxception class. Write file called projects. You may complete all three components a sangle file, or you may create multiple files as long as you amport them comectly. in Step 1: Writing Test Cases Ideally, you should write your test cases before you write your code. However testing objects such as those of he shipment class are more difficult due to their aratefilnature. 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 tevting infrastructure, which you will leans about in CS211 if you take it lavtead, we've provided a sample test hamew iaside the driver below. Feel free to modify thus hamevs (by including print statements should you need to. You be alter class with the follow constructor that takesaname, id, and price. The constructor creates these three attributes, and sets them to the inconaing arguments. Getters for all attributes (getName, eet1d.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 iten Iten('s s',1234s,' sa.se) would yield the ks 12M5 $4.56 class with the A constructor that takes an d The coustructor creates thus attribute, and sets it to the incoming argument. T coustructor also iaitialuzes an empty list of items. Getter for all attributes (getld and getItens). An additen method that takes an Ite as an argument and adds it to the end of the shipment's last of temas A to-string method that can be called with str( returns the id ofthe item. followed by a colon followed space, followed by a list of all its shipments. The list will open and close withtand J and will coutain all the items ofthe shipment in the order ofthe list. usiug their to- ting method to append them with commas as the only separator (see the test cases. For example the following object: tent socks ,12345, S4.56 shipment Shipment (55555555 shipment add Ite Iten shirt 33, 324.78 shipment add Item(iten) would retum the string "ssssssss: t socks 12345 S4.56, shirt You will be creating alte Except lass that esteads the class with the follown constructor that takes amessage as an argument The constructor creanesthis artribute and vet it to the inconaing arguaaent 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 that has the following functional The main takes as argument a list of tring It will procese thr contents of the list to create a list of shipment objectw. This list of shipment objects are then returned. The list arguments contents will be as follows data of A string in the list will either be Each shipment will coutaus ouly the nuamenc digits of the shipment id followed by anewline. Each item will be two lars long. On the first line will be the name of aa item, followed by a single space, followed by the five digit tem id, followed by anewlane On the line the iteau's price will appear Aprice starts with followed by an integer, followed by a period. followed by two numeric digits, followed by newline. Each item belongs to the most recently processed shipment. Wewillguarantee that a shipment will always appear before an item in the list. There may be multiple shipments in the each with their own items) is possible for items to be malformed While we will guarantee that the items ids will always be five digts in length, there are two main problemas that could occur with processing itemas: there is a missing space between the inem name and id and 2)the price is not valid prices its must be a non-negative dollar-and-cents amount with two digits for the cents portion) Ifeither of these situations arises, your code Eaust raise an ItemException immediately and discontinue processing the rest of the file example, the rain fauction may be called which would retumalist of objects identical to that list produced by the following code: 345, $4.56 Shipment(5555555 shipments append (shipment) You will find the isdig method of strings especially useful for this project 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 renam a value DONOT 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

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote