Question

Python Write the class SodaMachine that will represent a typical soda vending machine (product name, price)....

Python

Write the class SodaMachine that will represent a typical soda vending machine (product name, price). An instance of SodaMachine has access to three methods, purchase, deposit and restock that will describe its interaction with the user returning strings. Tip: use the string format method

Here is the base code=====

class SodaMachine:
'''
Creates instances of the class SodaMachine. Takes a string and an integer
'''
def __init__(self, product, price):
#-- start code here ---

#-- ends here ---

def purchase(self):
#-- start code here ---

#-- ends here ---

def deposit(self, amount):
#-- start code here ---

#-- ends here ---

def restock(self, amount):
#-- start code here ---

#-- ends here ---

Here are some example out puts=====

>>> m = SodaMachine('Coke', 10)

>>> m.purchase() 'Product out of stock'

>>> m.restock(2) 'Current soda stock: 2'

>>> m.purchase() 'Please deposit $10'

>>> m.deposit(7) 'Balance: $7'

>>> m.purchase() 'Please deposit $3'

>>> m.deposit(5) 'Balance: $12'

>>> m.purchase()

'Coke dispensed, take your $2'

>>> m.deposit(10) 'Balance: $10'

>>> m.purchase() 'Coke dispensed'

>>> m.deposit(15)

'Sorry, out of stock. Take your $15 back'

>>> x = SodaMachine('Dr. Pepper', 8)

>>> x.restock(1) 'Current soda stock: 1'

>>> x.deposit(8) 'Balance: $8'

>>> x.purchase()

'Dr. Pepper dispensed'

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

Python Code:

class SodaMachine:
   '''
       Creates instances of the class SodaMachine. Takes a string and an integer
   '''
   def __init__(self, product, price):
       '''
           Constructor
       '''
       self.__product = product
       self.__price = price
       self.__inStock = 0
       self.__deposit = 0
      
   def purchase(self):
       '''
           Purchase method
       '''
       #Checking stock
       if self.__inStock == 0:
           print("Product out of stock")
       else:
           # Checking amount
           if self.__deposit < self.__price:
               print("Please deposit $"+str(self.__price-self.__deposit))
           else:
               # Checking for change
               if self.__price-self.__deposit < 0:
                   print(self.__product + " dispensed, take your $" + str(self.__deposit - self.__price))
               else:  
                   print(self.__product + " dispensed")
               # Set deposit to 0
               self.__deposit = 0
               # Reset stock
               self.__inStock -= 1
              
      
   def deposit(self, amount):
       '''
           Deposits an amount
       '''
       # Checking stock
       if self.__inStock == 0:
           print("Sorry, out of stock. Take your $"+str(amount)+" back");
       else:
           self.__deposit += amount
           print("Balance: $" + str(self.__deposit))

      
   def restock(self, amount):
       '''
           Restocks the load
       '''
       self.__inStock += amount
       print("Current soda stock: " + str(self.__inStock))

____________________________________________________________________________________

Sample Run:

Python 3.5.2 Shell File Edit Shell Debug Options Window Help > class SodaMachine: Creates instances of the class SodaMachine Takes a string and an integer def init _(self, product, price) Constructor self. product product self. priceprice self. inStock0 self.deposit0 def purchase (self) Purchase method #Checking stock if self. inStock 0: print( Product out of stock) else # Checking amount if self._deposit< self. price print(Please deposit tstr (self. price-self-__deposit)) else: # Checking for change if self.__price-self._deposit 0 print(self- product + dispensed, take your 두 + str(self. deposit - self. price)) else print (self. productdispensed) # Set deposit to 0 self- deposit=0 # Reset stock self. _inStock -1 def deposit (self, amount) Deposits an amount # Checking stock if self.instock-0: - print(Sorry, out of stock. Take your ststr (amount)+back) else self. deposit amount print (Balance : 두 + str(self.--deposit))

Add a comment
Know the answer?
Add Answer to:
Python Write the class SodaMachine that will represent a typical soda vending machine (product name, price)....
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 class for DFA type objects. Deterministic Finite Automata are commonly defined as a quintuple...

    Write a class for DFA type objects. Deterministic Finite Automata are commonly defined as a quintuple consisting of a set of states, a set of symbals, a transition function, a start state and a set of accept states For this implementation let the alphabet be given as a string of symbols, the transition function as list of lists which represent an n by m matrix where the n rows represent the states and the m columns represent the alphabet symbols,...

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

  • 9p This is for Python I need help. Pet #pet.py mName mAge class Pet: + __init__(name,...

    9p This is for Python I need help. Pet #pet.py mName mAge class Pet: + __init__(name, age) + getName + getAge0 def init (self, name, age): self.mName = name self.mAge = age Dog Cat def getName(self): return self.mName mSize mColor + __init__(name, age,size) + getSize() + momCommento + getDog Years() +_init__(name, age,color) + getColor + pretty Factor + getCatYears def getAge(self): return self.mAge #dog.py import pet #cat.py import pet class Dog (pet. Pet): class Cat (pet. Pet): def init (self,...

  • Write a program that does the following in Python Code: Write a new Class called Famous_Day_Born which is a subclass of Famous_Person. Add a method to calculate the day of the week the person was born...

    Write a program that does the following in Python Code: Write a new Class called Famous_Day_Born which is a subclass of Famous_Person. Add a method to calculate the day of the week the person was born and print out all of the corresponding information using the overridden print method. Use the following code below as a starting point. ////////////////////////////////////////////////////// from datetime import datetime class Famous_Person(object): def __init__(self, last, first, month,day, year): self.last = last self.first = first self.month = month...

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

  • Python REALLY NEED HELP !!!!!!!!! [10pts] Write the class Vector that supports the basic vector operations....

    Python REALLY NEED HELP !!!!!!!!! [10pts] Write the class Vector that supports the basic vector operations. Such operations are addition (+) and subtraction (-) of vectors of the same length, dot product (*) and multiplication (*) of a vector by a scalar. All methods must return (not print) the result. Your class should also support the rich comparison for equality (==) - You must use the special methods for those 4 operators in order to override their behavior - You...

  • python Programming assignment: Let's think about doubly-linked lists. Define a class ListNode2, with three attributes: item,...

    python Programming assignment: Let's think about doubly-linked lists. Define a class ListNode2, with three attributes: item, left, and rightL. Left link points to the previous node in the list, right link points to the next node in the list. You can also add the display method to this class (like we did it in class for the ListNode class). Then test your class. For example, create a linked list of 5 values: 34,1, 23, 7, and 10. Display it. Then...

  • Python question class LinkNode: def __init__(self,value,nxt=None): assert isinstance(nxt, LinkNode) or nxt is None self.value = value...

    Python question class LinkNode: def __init__(self,value,nxt=None): assert isinstance(nxt, LinkNode) or nxt is None self.value = value self.next = nxt Question 2.1. Empty Node In some cases in it convenient to have a notion of an empty linked list. Usually it means that the linked list does not have any elements in it. In order to keep things simple (for now) we will assume that the list is empty, if it has a single node and its value is None. Add...

  • In Python 3 Write a LinkedList class that has recursive implementations of the display, remove, contains,...

    In Python 3 Write a LinkedList class that has recursive implementations of the display, remove, contains, insert, and normal_list methods. You may use default arguments and/or helper functions. The file must be named: LinkedList.py Here is what I have for my code so far. The methods I need the recursive implementations for will be bolded: class Node: """ Represents a node in a linked list (parent class) """ def __init__(self, data): self.data = data self.next = None class LinkedList: """...

  • Python 3 Question: All I need is for someone to edit the program with comments that...

    Python 3 Question: All I need is for someone to edit the program with comments that explains what the program is doing (for the entire program) def main(): developerInfo() #i left this part out retail = Retail_Item() test = Cash_Register() test.data(retail.get_item_number(), retail.get_description(), retail.get_unit_in_inventory(), retail.get_price()) answer = 'n' while answer == 'n' or answer == 'N': test.Menu() print() print() Number = int(input("Enter the menu number of the item " "you would like to purchase: ")) if Number == 8: test.show_items() else:...

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