In: Computer Science
PYTHON CODE:
import locale
class CashRegister:
# constructor to initialize the object
def __init__(self):
self.totalPrice =
0
self.itemCount = 0
# method to add an item to the cart
def addItem(self,price):
self.totalPrice +=
price
self.itemCount += 1
# method to get the total price of the items
in the cart
def getTotal(self):
return
self.totalPrice
# method to get the total number of items in
the cart
def getCount(self):
return
self.itemCount
if __name__=='__main__':
# welcome message
print('* * * Welcome to Simple Cash Register
Program * * *\n')
# creating CashRegister object
cart=CashRegister()
# infinite loop
while True:
# getting input from the user
price = input('Enter price of an item to add (Enter "quit" to end):
')
price=price.lower()
# checking for quit condition
if price == 'quit':
break
try:
# converting string into float
price=float(price)
# adding the item price to the cart
cart.addItem(price)
except ValueError:
print('\nError: Invalid input.\n')
# setting the locale
locale.setlocale(locale.LC_ALL,"us")
# printing the number of items
print('\nTotal number of items in the cart = %d'
% cart.getCount())
# printing the total price of the items
print('\nTotal Amount = %s' %
locale.currency(cart.getTotal()))
SCREENSHOT FOR CODING:
SCREENSHOT FOR OUTPUT: