Question

Pyhton Program : Write a coffee vending machine class. Include fields giving the number of cups...

Pyhton Program : Write a coffee vending machine class. Include fields giving the number of cups of coffee available, the cost of one cup of coffee, and the total amount of money inserted by the user. This machine requires exact change. Include one constructor which stocks the machine with a quantity and price of coffee specified as parameters. Include the following methods; menu() // display the quantity and price of coffee insert(int quarters, int dimes, int nickels) // insert the given amount select() // dispenses a cup of coffee if user has inserted enough // money and coffee is available, otherwise displays a message refund() // returns the money inserted Write the main() program to create some vending machines and test their operation thoroughly.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Hi,

Please find sample python code below:

###############

import math

class VendingMachine:
    def __init__(self,cups,cost):
        self.noOfCups = cups
        self.costPerCup = cost
        self.totalAmountInserted = 0
  
    def get_cups(self):
        return self.noOfCups
      
    def set_cups(self, cups):
        self.noOfCups = cups
      
    def get_cost_per_cup(self):
        return self.costPerCup
      
    def set_cost_per_cup(self, cost):
        self.costPerCup = cost
      
    def get_total_amount_inserted(self):
        return self.totalAmountInserted
      
    def set_total_amount_inserted(self, amount):
        self.totalAmountInserted = amount
      
    def menu(self):
        print("########Coffee Menu############")       
        print("Quantity:=", self.noOfCups, "\nPrice of coffee:= $",self.costPerCup/100)       
        print("-------------------------------")       

    def insert(self, quarters, dimes, nickels):
        print("Inserted:\nQuarters:=",quarters,"\nDimes:=",dimes,"\nNickels:=",nickels)       
        self.totalAmountInserted= quarters*25 + dimes*10 + nickels*1
  
    def refund(self):
        quarters = math.floor(self.totalAmountInserted/25)
        dimes= math.floor((self.totalAmountInserted - quarters*25 )/10)
        nickels = (self.totalAmountInserted - quarters*25 - dimes*10)   
        if(quarters > 0 or dimes > 0 or nickels > 0):
            print("Refund Money: \nQuarters:=", int(quarters), "\nDimes:=",int(dimes), "\nNickels=",int(nickels))       

  
    def select(self):
        if(self.noOfCups > 0):
          
            if(self.totalAmountInserted/self.costPerCup >= 1):
                self.noOfCups=self.noOfCups-1
                print("One cup of coffee issued..")       
                self.totalAmountInserted = self.totalAmountInserted - self.costPerCup
                self.refund()
            else:
                self.refund()
              
        else:
            print("No coffee stock...")       
      
## Testing code      
machineOne = VendingMachine(10,75)
machineOne.menu()
machineOne.insert(5,0,5)
machineOne.select()
machineOne.menu()

#################################

sample output

########Coffee Menu############
Quantity:= 10
Price of coffee:= $ 0.75
-------------------------------
Inserted:
Quarters:= 5
Dimes:= 0
Nickels:= 5
One cup of coffee issued..
Refund Money:
Quarters:= 2
Dimes:= 0
Nickels= 5
########Coffee Menu############
Quantity:= 9
Price of coffee:= $ 0.75
-------------------------------

Hope this helps.
Thanks.

Add a comment
Know the answer?
Add Answer to:
Pyhton Program : Write a coffee vending machine class. Include fields giving the number of cups...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write a program in Java that simulates a vending machine: The vending machine sells three types...

    Write a program in Java that simulates a vending machine: The vending machine sells three types of food: 1) Potato chips $1.25; 2) Cookies $0.85; 3) Candies $0.95. The program will prompt for the buyer to enter the amount in quarters (25 cents), dimes (10 cents), and nickels (5 cents). The program will then present a selection menu for the foods and prompt the buyer to enter the amount of quarters, dimes and nickels. The machine would then proceed to...

  • Vending Machine (Python 3) Thinking Outside the box Write a program that asks the user to...

    Vending Machine (Python 3) Thinking Outside the box Write a program that asks the user to enter a bill value (1 = $1 bill, 5 = $5 bill, etc.) and the price of an item they want to buy in pennies and calculate their change amount in dollars, quarters, dimes, nickels, and pennies. Your code should start like this: ## # This program simulates a vending machine that gives change # Define constants PENNIES PER DOLLAR = 100 PENNIES PER...

  • Write the algorithm that determines the change to be dispensed from a vending machine. An item...

    Write the algorithm that determines the change to be dispensed from a vending machine. An item in the machine can cost between 5 cents and 1 dollar, in 5-cent increments (5,10,15,......90, 95, or 100), and the machine accepts only a single dollar bill to pay for the item. The user will provide the price and the program will output the price and calculate and output the total change and how many quarters, dimes and nickels need to be dispensed. DO...

  • 1. Create a new multi-class Java program which implements a vending machine simulator which contains the...

    1. Create a new multi-class Java program which implements a vending machine simulator which contains the following functionality: A) At program startup, the vending machine is loaded with a variety of products in a variety of packaging for example soda/tonic/Coke in bottles, peanuts in bags, juice in cartons, etc. Also included is the cost of each item. The program should be designed to easily load a different set of products easily (for example, from a file). Also at program startup,...

  • In C++ Consider a cash register that uses an automated machine. For the “paper money inserted”,...

    In C++ Consider a cash register that uses an automated machine. For the “paper money inserted”, and the “amount of purchase”, the machine returns “paper money and coins”. Write a program that gets from user the following “paper money inserted” and the “amount of purchase”, and display the output as: Purchase 3.08 Payment 10.00 Change 6.92 Dollars 6 Quarters 3 Dimes 1 Nickels 1 Pennies 2 Hint: - to get “dollars” and “coins” use : static_cast < int > -...

  • C++ HW Question Your program will simulate a simple change maker for a vending machine. It...

    C++ HW Question Your program will simulate a simple change maker for a vending machine. It will start with a stock of coins and dollars. It will then repeatedly request the price for an item to be purchased or to quit. If given a price, it will accept nickels, dimes, quarters, one-dollar and five-dollar bills—deposited one at a time—in payment. When the user has deposited enough to cover the cost of the item, the program will calculate the coins to...

  • Please Walk me through this problem. Write a program that simulates the functionality of a vending...

    Please Walk me through this problem. Write a program that simulates the functionality of a vending machine having the following characteristics: The vending machine offers 5 products The vending machine accepts coins, 1 dollar bills, and 5 dollar bills The change is always given in coins, with maximum possible number of coins in each value: 25, 10, 5 or 1 cent. The selections available for user are numbers from 1 to 5. The user enters the money – simulate the...

  • Write a C program that calculates exact change. In order to receive full credit, please remember...

    Write a C program that calculates exact change. In order to receive full credit, please remember that only int arithmetic is exact, so you’ll need to break up your double into two ints, the one before the decimal point and the one after the decimal point. Another point worth mentioning is that the % operator gives the remainder. In other words, when working with int values, 9 / 5 = 1 whereas 9 % 5 = 4. Keep this in...

  • write a program in java that determines the change to be dispensed from a vending machine....

    write a program in java that determines the change to be dispensed from a vending machine. An item in the machine can cost between 25 cents and a dollar, in a 5-cent increments(25,30,35.....,90,95,or 100), and the machine accepts only a single dollar bill to pay for the item. for example a possible dialogue with the user might be "Enter price of item (from 25 cents to a dollar, in 5-cent increments):45 you boughtan item for 45 cents and gave me...

  • write a program in C that determines the change to be dispensed from a vending machine....

    write a program in C that determines the change to be dispensed from a vending machine. An item in the machine can cost between 25 cents and a dollar, in a 5-cent increments(25,30,35.....,90,95,or 100), and the machine accepts only a single dollar bill to pay for the item. for example a possible dialogue with the user might be "Enter price of item (from 25 cents to a dollar, in 5-cent increments):45 you boughtan item for 45 cents and gave me...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT