Question

In: Computer Science

Program must use Python 3 Your program must have a welcome message for the user. Your...

  • Program must use Python 3
  • Your program must have a welcome message for the user.
  • Your program must have one class called CashRegister.
    • Your program will have an instance method called addItem which takes one parameter for price. The method should also keep track of the number of items in your cart.
    • Your program should have two getter methods.
      • getTotal – returns totalPrice
      • getCount – returns the itemCount of the cart
  • Your program must create an instance of the CashRegister class.
  • Your program should have a loop which allows the user to continue to add items to the cart until they request to quit.
  • Your program should print the total number of items in the cart.
  • Your program should print the total $ amount of the cart.
    • The output should be formatted as currency. Be sure to investigate the locale class. You will need to call locale.setlocale and locale.currency.

Solutions

Expert Solution

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:

     
          

                      
          
  


Related Solutions

(CODE IN PYTHON) Program Input: Your program will display a welcome message to the user and...
(CODE IN PYTHON) Program Input: Your program will display a welcome message to the user and a menu of options for the user to choose from. Welcome to the Email Analyzer program. Please choose from the following options: Upload text data Find by Receiver Download statistics Exit the program Program Options Option 1: Upload Text Data If the user chooses this option, the program will Prompt the user for the file that contains the data. Read in the records in...
Using python, produce code that mimics some ATM transactions: a. A welcome message must be displayed...
Using python, produce code that mimics some ATM transactions: a. A welcome message must be displayed initially reflecting the appropriate time of day (for example: Good Night, Welcome to Sussex Bank). b. Assume the user’s account balance is $5375.27. c. Allow the user to enter a pin number that does not have to be validated: def atm_login(): pin_number = input("Please enter your (4 digit) pin number: ") # display welcome message welcome_message() d. The message should be followed by a...
Using Python, write a program named hw3b.py that meets the following requirements: Welcome the user to...
Using Python, write a program named hw3b.py that meets the following requirements: Welcome the user to Zion's Pizza Restaurant and ask the user how many people are in their dinner group. If the answer is more than eight (8), print a message saying they'll have to wait for a table. Otherwise, report that their table is ready and take their order. Assume the client orders one pizza, and ask what he/she would like on their pizza, include a loop that...
PYTHON program that: Asks for a name, and then displays "Welcome student" plus your name on...
PYTHON program that: Asks for a name, and then displays "Welcome student" plus your name on the screen. (concatenate your name after the string) and prompts the user for 2 numbers, then calculates and displays the sum, product and average.
Create a message encoder/decoder. PLEASE USE BASIC PYTHON METHODS/FUNCTIONS. The user enters a message that could...
Create a message encoder/decoder. PLEASE USE BASIC PYTHON METHODS/FUNCTIONS. The user enters a message that could only include alphabetic letters and space. There are 26 alphabetic letters. Consider space the 27th letter. The user then enters a shift code that should be an integer between -26 and 26. The application will show the encoded/decoded message based on the shift code entered. If you encode a message, each letter in the message will be moved forward through the alphabet according to...
In Python b) Modify your program that reads 3 grades from the user (and computes the...
In Python b) Modify your program that reads 3 grades from the user (and computes the average and letter grade) so that it uses a while loop to read the 3 grades. In the loop body, you will just read one grade and update other variables appropriately. The loop header will ensure 3 iterations. c) Modify your program in part b so that it asks the user how many grades there are and uses a while loop to read that...
Need this program in python. The data must be taken from user as input. Write a...
Need this program in python. The data must be taken from user as input. Write a program that prompts the user to select either Miles-to-Kilometers or Kilometers-to-Miles, then asks the user to enter the distance they wish to convert. The conversion formula is: Miles = Kilometers X 0.6214 Kilometers = Miles / 0.6214 Write two functions that each accept a distance as an argument, one that converts from Miles-to-Kilometers and another that converts from Kilometers-to-Miles The conversion MUST be done...
In Python, your program will read in a number (no need to prompt the user), and...
In Python, your program will read in a number (no need to prompt the user), and then reads in that number of lines from the terminal. Then the program should print an array of strings formatted in a nice regular box. So if the user inputs this: 5 Grim visaged war has smooth’d his wrinkled front And now, instead of mounting barded steeds To fright the souls of fearful adversaries He capers nimbly in a lady’s chamber To the lascivious...
PYTHON Write a python program that encrypts and decrypts the user input. Note – Your input...
PYTHON Write a python program that encrypts and decrypts the user input. Note – Your input should be only lowercase characters with no spaces. Your program should have a secret distance given by the user that will be used for encryption/decryption. Each character of the user’s input should be offset by the distance value given by the user For Encryption Process: Take the string and reverse the string. Encrypt the reverse string with each character replaced with distance value (x)...
Use Visual Python or equivalent, to write a program that allows the user to observe the...
Use Visual Python or equivalent, to write a program that allows the user to observe the following: Damped and forced harmonic oscillators. The program should be user friendly and have default values for the initial velocities, positions, masses, and spring constants as well as damping constants. Values and frequencies for the forced oscillators should also be given. It should allow the user to input values for the velocities, positions, spring constants, and masses. The program should determine automatically the scale...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT