The 'checkout' method prints a receipt for the cart contents. It prints a title line showing the customer name and cart number, then iterates through the cart dictionary and prints a line for each item (name, quantity, and extended price), and a total line at the bottom showing the total cost for all items in the cart. Here's an example: Checkout for Mary Smith Cart# 1 $ $ 3.98 $ 9.50 milk 1 3.99 2 eggs salmon 1 Total $ 17.47
The 'checkout' method prints a receipt for the cart contents. It prints a title line showing the customer name and cart number, then iterates through the cart dictionary and prints a line for each item (name, quantity, and extended price), and a total line at the bottom showing the total cost for all items in the cart. Here's an example: Checkout for Mary Smith Cart# 1 $ $ 3.98 $ 9.50 milk 1 3.99 2 eggs salmon 1 Total $ 17.47
Computer Networking: A Top-Down Approach (7th Edition)
7th Edition
ISBN:9780133594140
Author:James Kurose, Keith Ross
Publisher:James Kurose, Keith Ross
Chapter1: Computer Networks And The Internet
Section: Chapter Questions
Problem R1RQ: What is the difference between a host and an end system? List several different types of end...
Related questions
Question
follow the instructions and this is the test script to follow:
Expert Solution
Step 1
Answers
The Python Code for the given problem is:
# class Item
class Item:
def __init__(self, name, price) -> None:
# constructor with item name and price
self.name = name
self.price = price
class Cart:
# static variable to store count of carts
counter = 1
# constructor of cart, takes customer name and initialize members
def __init__(self, customer_name) -> None:
self.custName = customer_name
self.seqNo = '# ' + str(Cart.counter)
self.contents = {}
Cart.counter = Cart.counter + 1
# add anitem to contents dictionary
def addItem(self, anItem):
# check if anItem is of type Item, else return false
if not isinstance(anItem, Item):
return False, 'not an item type'
# if item is not in dictionary already, add it with quantity 1
# else increase the quantity of item by 1
if anItem.name not in self.contents.keys():
self.contents[anItem.name] = [1, anItem]
else:
self.contents[anItem.name][0] = self.contents[anItem.name][0] + 1
return True, 'added'
# prints the reciept of cart
def checkout(self):
total = 0
print('Checkout for %s \tCart %s\n'%(self.custName, self.seqNo))
for key, value in self.contents.items():
print("%s \t\t\t%d\t$\t%.2f\n"%(key, value[0], value[1].price * value[0]))
total = total + (value[0] * value[1].price)
print("Total \t\t\t\t$\t%.2f\n"%(total))
# test data
item1 = Item('milk', 3.99)
item2 = Item('eggs', 1.99)
item3 = Item('salmon', 9.50)
aCart = Cart('Marry Smith')
aCart.addItem(item1)
aCart.addItem(item2)
aCart.addItem(item2)
aCart.addItem(item3)
aCart.checkout()
Trending now
This is a popular solution!
Step by step
Solved in 2 steps with 1 images
Recommended textbooks for you
Computer Networking: A Top-Down Approach (7th Edi…
Computer Engineering
ISBN:
9780133594140
Author:
James Kurose, Keith Ross
Publisher:
PEARSON
Computer Organization and Design MIPS Edition, Fi…
Computer Engineering
ISBN:
9780124077263
Author:
David A. Patterson, John L. Hennessy
Publisher:
Elsevier Science
Network+ Guide to Networks (MindTap Course List)
Computer Engineering
ISBN:
9781337569330
Author:
Jill West, Tamara Dean, Jean Andrews
Publisher:
Cengage Learning
Computer Networking: A Top-Down Approach (7th Edi…
Computer Engineering
ISBN:
9780133594140
Author:
James Kurose, Keith Ross
Publisher:
PEARSON
Computer Organization and Design MIPS Edition, Fi…
Computer Engineering
ISBN:
9780124077263
Author:
David A. Patterson, John L. Hennessy
Publisher:
Elsevier Science
Network+ Guide to Networks (MindTap Course List)
Computer Engineering
ISBN:
9781337569330
Author:
Jill West, Tamara Dean, Jean Andrews
Publisher:
Cengage Learning
Concepts of Database Management
Computer Engineering
ISBN:
9781337093422
Author:
Joy L. Starks, Philip J. Pratt, Mary Z. Last
Publisher:
Cengage Learning
Prelude to Programming
Computer Engineering
ISBN:
9780133750423
Author:
VENIT, Stewart
Publisher:
Pearson Education
Sc Business Data Communications and Networking, T…
Computer Engineering
ISBN:
9781119368830
Author:
FITZGERALD
Publisher:
WILEY