Question

In: Computer Science

* Python * * Python Programming * Part 1: Product Class Write a class named Product...

* Python *

* Python Programming *

Part 1: Product Class

Write a class named Product that holds data about an item in a retail store.   The class should store the following data in attributes: product id, item description, units in inventory, and price. Write the __init__ method to require all four attributes. Also write a __str__ method for debugging output.   

Once you have written the class, write a main() function that creates three Product objects and stores the following data in them. Use the __str__ method to confirm the data is stored properly.

ID         Description              Quantity        Price
1          Jacket                          12                    59.95
2          Designer Jeans            40                    34.95
3          Shirt                             20                    24.95

Submit your python file (.py) to Sakai

Part 2: Inventory Module

Create an Inventory module by moving the Product Class definition to another file called inventory.py.   Add three __doc__ strings: one that describe the inventory module (include your name and the date here), one that describes the Product class, and one that describes the Product class __init__ method.

Rename your main() function to test_inventory.py
Add an import at the top of the file to read the Product class definition.
Add these print statements to test the __doc__ string:

Print(inventory.__doc__)

Print(Product.__doc__)

Print(Product.__init__..__doc__)

Test the program to be sure it still works. NOTE: If using IDLE, you will have to explicitly save the inventory.py file after making changes.  

Solutions

Expert Solution

Part1

product.py

class Product(object):
    def __init__(self, product_id, item_desc, units, price):
        self.product_id = product_id
        self.item_desc = item_desc
        self.units = units
        self.price = price

    def __str__(self):
        return 'Product ID: %s, Itesm: %s, Units: %d, price: %.2f' %(self.product_id, self.item_desc, self.units, self.price)


def main():
    p1 = Product('1', 'Jacket', 12, 59.95)
    p2 = Product('2', 'Designer Jeans', 40, 34.95)
    p3 = Product(3, 'Shirt', 20, 24.95)
    print(p1)
    print(p2)
    print(p3)

main()

# OUT

part 2 NOTE: update your name in below place holder

inventory.py

"""
Name: <PLACE YOUR NAME>
DAte: 18-09-2019
"""


class Product(object):
    """
    prodcut class
    representataion of product that have id, name units and price
    """

    def __init__(self, product_id, item_desc, units, price):
        """
        constructor of product class
        :param product_id:  id of the product
        :param item_desc: item detail
        :param units: no of units
        :param price: price per unit
        """
        self.product_id = product_id
        self.item_desc = item_desc
        self.units = units
        self.price = price

    def __str__(self):
        return 'Product ID: %s, Itesm: %s, Units: %d, price: %.2f' % (
            self.product_id, self.item_desc, self.units, self.price)

test_inventory.py

import inventory  # importing inventory
Product = inventory.Product


def main():
    # printing doc
    print(inventory.__doc__)
    print(Product.__doc__)
    print(Product.__init__.__doc__)

    # creating and printing product
    p1 = Product('1', 'Jacket', 12, 59.95)
    p2 = Product('2', 'Designer Jeans', 40, 34.95)
    p3 = Product(3, 'Shirt', 20, 24.95)
    print(p1)
    print(p2)
    print(p3)


main()

# OUT


Related Solutions

Part 1 – Create a Stock Class Write a class named Stock. Stock Class Specifications Include...
Part 1 – Create a Stock Class Write a class named Stock. Stock Class Specifications Include member variables for name (string), price (double), shares (double). Write a default constructor. Write a constructor that takes values for all member variables as parameters. Write a copy constructor. Implement Get/Set methods for all member variables. Implement the CalculateValue function. This function should multiply the prices by the shares and return that value. Use the following function header: double CalculateValue(). Add a member overload...
THIS IS JAVA PROGRAMMING 1. Create a class named Name that contains the following: • A...
THIS IS JAVA PROGRAMMING 1. Create a class named Name that contains the following: • A private String to represent the first name. • A private String to represent the last name. • A public constructor that accepts two values and assigns them to the above properties. • Public methods named getProperty (e.g. getFirstName) to return the value of the property. • Public methods named setProperty ( e.g. setFirstName)to assign values to each property by using a single argument passed...
write the code in python Design a class named PersonData with the following member variables: lastName...
write the code in python Design a class named PersonData with the following member variables: lastName firstName address city state zip phone Write the appropriate accessor and mutator functions for these member variables. Next, design a class named CustomerData , which is derived from the PersonData class. The CustomerData class should have the following member variables: customerNumber mailingList The customerNumber variable will be used to hold a unique integer for each customer. The mailingList variable should be a bool ....
JAVA PROGRAMMING. In this assignment, you are to create a class named Payroll. In the class,...
JAVA PROGRAMMING. In this assignment, you are to create a class named Payroll. In the class, you are to have the following data members: name: String (5 pts) id: String   (5 pts) hours: int   (5 pts) rate: double (5 pts) private members (5 pts) You are to create no-arg and parameterized constructors and the appropriate setters(accessors) and getters (mutators). (20 pts) The class definition should also handle the following exceptions: An employee name should not be empty, otherwise an exception...
LANGUAGE PYTHON 3.7 Write a collection class named "Jumbler". Jumbler takes in an optional list of...
LANGUAGE PYTHON 3.7 Write a collection class named "Jumbler". Jumbler takes in an optional list of strings as a parameter to the constuctor with various strings. Jumbler stores random strings and we access the items based on the methods listed below. Jumbler supports the following methods: add() : Add a string to Jumbler get() : return a random string from Jumbler max() : return the largest string in the Jumbler based on the length of the strings in the Jumbler....
Put In Java Programming The TicketMachine class: Design a class named TicketMachine that contains: • A...
Put In Java Programming The TicketMachine class: Design a class named TicketMachine that contains: • A double data field named price (for the price of a ticket from this machine). • A double data field named balance (for the amount of money entered by a customer). • A double data field named total (for total amount of money collected by the machine). • A constructor that creates a TicketMachine with all the three fields initialized to some values. • A...
PUT IN JAVA PROGRAMMING The Stock class: Design a class named Stock that contains: • A...
PUT IN JAVA PROGRAMMING The Stock class: Design a class named Stock that contains: • A string data field named symbol1 for the stock’s symbol. • A string data field named name for the stock’s name. • A double data field named previousClosingPrice that stores the stock price for the previous day. • A double data field named currentPrice that stores the stock price for the current time. • A constructor that creates a stock with the specified symbol and...
Python Please (The Fan class) Design a class named Fan to represent a fan. The class...
Python Please (The Fan class) Design a class named Fan to represent a fan. The class contains: ■ Three constants named SLOW, MEDIUM, and FAST with the values 1, 2, and 3 to denote the fan speed. ■ A private int data field named speed that specifies the speed of the fan. ■ A private bool data field named on that specifies whether the fan is on (the default is False). ■ A private float data field named radius that...
THIS IS JAVA PROGRAMMING Design a class named Account (that contains 1. A private String data...
THIS IS JAVA PROGRAMMING Design a class named Account (that contains 1. A private String data field named id for the account (default 0). 2. A private double data field named balance for the account (default 0). 3. A private double data field named annualInterestRate that stores the current interest rate (default 0). 4. A private Date data field named dateCreated that stores the date when the account was created. 5. A no-arg constructor that creates a default account. 6....
PUT IN JAVA PROGRAMMING The StockB class: Design a class named StockB that contains the following...
PUT IN JAVA PROGRAMMING The StockB class: Design a class named StockB that contains the following properties: • Name of company • Number of shares owned • Value of each share • Total value of all shares and the following operations: • Acquire stock in a company • Buy more shares of the same stock • Sell stock • Update the per-share value of a stock • Display information about the holdings • The StockB class should have the proper...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT