1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
Levart [38]
3 years ago
11

Develop a program working as a soda vending machine. The program should show the product menu with price and take user input for

product number, quantity and pay amount. The output should show the total sale price including tax and the change. Organize the program with 4 functions and call them in main() based on the requirements.
Computers and Technology
1 answer:
Kipish [7]3 years ago
8 0

Answer:

Explanation:

The following vending machine program is written in Python. It creates various functions to checkItems, check cash, check stock, calculate refund etc. It calls all the necessary functions inside the main() function. The output can be seen in the attached picture below.

class Item:

   def __init__(self, name, price, stock):

       self.name = name

       self.price = price

       self.stock = stock

   def updateStock(self, stock):

       self.stock = stock

   def buyFromStock(self):

       if self.stock == 0:

           # raise not item exception

           pass

       self.stock -= 1

class VendingMachine:

   def __init__(self):

       self.amount = 0

       self.items = []

   def addItem(self, item):

       self.items.append(item)

   def showItems(self):

       print('Items in Vending Machine \n----------------')

       for item in self.items:

           if item.stock == 0:

               self.items.remove(item)

       for item in self.items:

           print(item.name, item.price)

       print('----------------\n')

   def addCash(self, money):

       self.amount = self.amount + money

   def buyItem(self, item):

       if self.amount < item.price:

           print('You can\'t but this item. Insert more coins.')

       else:

           self.amount -= item.price

           item.buyFromStock()

           print('You chose ' +item.name)

           print('Cash remaining: ' + str(self.amount))

   def containsItem(self, wanted):

       ret = False

       for item in self.items:

           if item.name == wanted:

               ret = True

               break

       return ret

   def getItem(self, wanted):

       ret = None

       for item in self.items:

           if item.name == wanted:

               ret = item

               break

       return ret

   def insertAmountForItem(self, item):

       price = item.price

       while self.amount < price:

               self.amount = self.amount + float(input('insert ' + str(price - self.amount) + ': '))

   def calcRefund(self):

       if self.amount > 0:

           print(self.amount + " refunded.")

           self.amount = 0

       print('Thank you!\n')

def main():

   machine = VendingMachine()

   item1 = Item('Kit Kat',  1.5,  2)

   item2 = Item('Potato Chips', 1.75,  1)

   item3 = Item('Snickers',  2.0,  3)

   item4 = Item('Gum',  0.50, 1)

   item5 = Item('Doritos',0.75,  3)

   machine.addItem(item1)

   machine.addItem(item2)

   machine.addItem(item3)

   machine.addItem(item4)

   machine.addItem(item5)

   print('Welcome!\n----------------')

   continueBuying = True

   while continueBuying == True:

       machine.showItems()

       selected = input('select item: ')

       if machine.containsItem(selected):

           item = machine.getItem(selected)

           machine.insertAmountForItem(item)

           machine.buyItem(item)

           a = input('buy something else? (y/n): ')

           if a == 'n':

               continueBuying = False

               machine.calcRefund()

           else:

               continue

       else:

           print('Item not available. Select another item.')

           continue

if __name__ == "__main__":

   main()

You might be interested in
You want to find information about Paris, France in the last five years, written in English. What is the best way to search for
KonstantinChe [14]
I say It could be B
8 0
4 years ago
Read 2 more answers
9. Government and corporate officials concerned about security threats do not bring their own cell phones or laptops when travel
tatuchka [14]

Answer:

True

Explanation:

It is known as Don-Not-Carry rules are implemmented to avoid

-Unauthorized full disk copies (it have been made while the laptop owner was out of the hotel room on overseas travel)

-Laptops steals

- Monitoring by third parties of wireless using.

3 0
3 years ago
As a member of the accounting group,
Anastasy [175]

Answer:

rtg

Explanation:

rth

3 0
4 years ago
The value of the mathematical constant e can be expressed as an infinite series: e=1+1/1!+1/2!+1/3!+... Write a program that app
LUCKY_DIMON [66]

Answer:

// here is code in c++ to find the approx value of "e".

#include <bits/stdc++.h>

using namespace std;

// function to find factorial of a number

double fact(int n){

double f =1.0;

// if n=0 then return 1

if(n==0)

return 1;

for(int a=1;a<=n;++a)

       f = f *a;

// return the factorial of number

return f;

}

// driver function

int main()

{

// variable

int n;

double sum=0;

cout<<"enter n:";

// read the value of n

cin>>n;

// Calculate the sum of the series

  for (int x = 0; x <= n; x++)

  {

     sum += 1.0/fact(x);

  }

  // print the approx value of "e"

    cout<<"Approx Value of e is: "<<sum<<endl;

return 0;

}

Explanation:

Read the value of "n" from user. Declare and initialize variable "sum" to store the sum of series.Create a function to Calculate the factorial of a given number. Calculate the sum of all the term of the series 1+1/1!+1/2!.....+1/n!.This will be the approx value of "e".

Output:

enter n:12

Approx Value of e is: 2.71828

6 0
3 years ago
Before donating a computer you should use a program to wipe the hard disk to remove all of its data
Elenna [48]
Yes you should. highly recommended
7 0
3 years ago
Read 2 more answers
Other questions:
  • Give two examples of desktop publishing software
    7·1 answer
  • The Apple II is an IBM-compatible PC "clone".<br> True?<br> False?
    10·1 answer
  • After placing her insertion point after Grandma’s Kitchen, order the steps Danica needs to follow to insert and format the regis
    8·2 answers
  • You type. The word "weather" when you ment "whether" when will the writer or word flag this as a misspelling or a grammar proble
    13·1 answer
  • Without a well-designed, accurate database, executives, managers, and others do not have access to the ____________________ they
    15·1 answer
  • What is the FTC used car rule
    12·1 answer
  • which of the following cells can't be recarged? A. Electrode cell B. wet cell C. primary cell D. storage cell
    13·1 answer
  • Steve left his computer switched on in his room and went out to have breakfast. When he returned, he saw that the monitor had be
    9·1 answer
  • Which of the following is NOT true about procedural abstraction?
    9·1 answer
  • A 4"x6" photo is digitized using 10,000 pixels. An 11"x7" photo is digitized using 30,000 pixels. Which image will have the bett
    6·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!