Answered step by step
Verified Expert Solution
Question
1 Approved Answer
COMP 2 1 6 Networking for Software Developers Lab 3 Shopper Class ( 4 0 Marks ) This is an individual lab. Using a Visual
COMP Networking for Software Developers
Lab Shopper Class Marks
This is an individual lab. Using a Visual Studio Code to write the python statement to define a
Shopper class. The test harness is provided to you by the instructor as well as the resulting output
given.
Description of the Shopper Class
Class variable
There are six class variables ie variables that are shared by all objects of this class
prices
This is a python dict that contains the grocery items as well as their unit price. This is defined as
follows:
prices 'apple': 'bread': 'milk': 'pepper':
saleitems
This is a list that contains all the items that are on sale in this period. A discount of is applied
to the price of each item in this collection. This is defined as follows:
saleitems 'apple banana'.split
Four other class variable that are define as follows:
discountthreshold
When the total cost of a purchase exceeds this amount then a discount is applied to the cost
defaultprice
If an item is not found in the prices dictionary, then this will be the default price
Mark
Mark
Mark
Mark
COMP Networking for Software Developers Classes WeekLab
Winter Page of
volumediscount
This is the rate of discount when the total cost exceeds the discountthreshold
salesdiscount
This is the rate of discount on an item if this item is in the salestime list.
Constructor
The constructor takes two argument that represents the name of the shopper and the amount of
money she has. It assigns the argument to appropriate instance variable. It also creates an empty
list to store the purchases.
Each purchase is a single tuple comprising of the name of the item and the price actually paid for
the item.
Instance property
There is a single instance property that returns the name of this shopper object
Class method
There are three class methods:
One that returns the price list.
One that returns the list of sale items.
One that adds a new item to the sale items list.
Instance method
There are two instance methods:
purchase
This method takes a list of items to be purchase. It calculates the total price of the purchase by
processing each item in the argument as show below:
The price of an item is obtained from the price list. If it is not found in the price list then the
default price is used. How to determine if an item in not in the dict
If the item is in the sales list then a discount is applied to the price
Marks
Marks
Marks
Mark
Mark
Marks
COMP Networking for Software Developers Classes WeekLab
Winter Page of
The item as well as the result price is added to the list as a tuple. How to create a tuple with
name and price and how to add the tuple to the list
When all the items are processed, if the final cost is over the credit threshold, then a
discount is applied to this amount and then subtracted from the amount of money this
object has left over.
str
This method returns a string representing this object ie the name, cash and all its purchases
pertaining to this shopper.
How to do this assignment.
From the above description and test harness and the result output below, try to deduce the
definition of the Shopper class. Code this class in visual studio code and copy the test harness
below. Execute the code and ensure that the output matches EXACTLY with the output on the
following page.
You must use python fstrings for your output.
Documentation.
Because the code is so simple no code documentation is required, however you must put
following information including name, student ID course number and the current date at the top
of your code.
Lab : Shopper Class
Name: FirstName LastName
Student ID:
Course: COMP
Date: January
How to submit this assignment.
Submit COMPLabFirstLastName.py to the course dropbox.
Due Date: pm February
penalty for late submission before am February
No acceptance after am February
Marks
COMP Networking for Software Developers Classes WeekLab
Winter Page of
Test Harness
You may not change the test harness.
printfPrice dict: Shopperpricelist
printfSales list: Shoppersaleitems
tom ShopperTommy #create a shopper object
printf
tom #display the object
items 'bread milk apple'.split #list of items to buy
printf
tomname is purchasing: items
tom.purchaseitems #volume discount
printftom #display the object
dave ShopperDave #new shopper
items 'banana milk'.split #one sale item
printf
davename is purchasing: items
dave.purchaseitems
printfdave #display the object
Shopper.addsaleitempepper #add pepper to the sales items list
COMP Networking for Software Developers Classes WeekLab
Winter
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started