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
  • 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...

  • 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 Java class that implements the concept of Coins, assuming the following attributes (variables): number...

    Write a Java class that implements the concept of Coins, assuming the following attributes (variables): number of quarters, number of dimes, number of nickels, and number of pennies. Include two constructors (non-argument constructor that assigns 1 to each coin, and another constructor that takes necessary arguments to create an object), needed getter and setter methods, method toString (to print coins objects in a meaningful way), and method equals (to compare two coins objects based on number of coins). Also include...

  • ) Object-Oriented Design 7 Write a program that simulates a vending machine. Products can be purc...

    Write in java as simple as possible coding.. ) Object-Oriented Design 7 Write a program that simulates a vending machine. Products can be purchased by inserting coins with a value at least equal to the cost of the product. A user selects product from a list of available products, adds coins, and either gets the productor gets the coins returned. The coins are returned if insufficient money was supplied or if the product is sold out. The machine does not...

  • Write a complete C program for an automatic teller machine that dispenses money. The user should...

    Write a complete C program for an automatic teller machine that dispenses money. The user should enter the amount desired (a multiple of 10 dollars) and the machine dispenses this amount using the least number of bills. The bills dispenses are 50s, 20s, and 10s. Write a function that determines how many of each kind of bill to dispense. When writing your function begin to pass your variables using pointers (or rather "pointing" to your data". Use the TimeSpace program...

  • Overview - Vending Machine For this project, we will simulate a vending machine where, for simplicity,...

    Overview - Vending Machine For this project, we will simulate a vending machine where, for simplicity, all of the items in the machine are the same price of $1.25 The vending machine can receive Nickels, Dimes, Quarters, and one dollar Bills. The user can add money, ask for the product, ask for their money back, or walk away (exit). The program will interact with the user and allow the following commands: N - adds nickel D - adds dime Q...

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
ADVERTISEMENT