Python Forum
Vending Machine Project - OOP Issues
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Vending Machine Project - OOP Issues
#1
Hi,

I have been working on a vending machine project which is outlined below.

After selecting 'b' in the first menu, I am having trouble with comparing the selected item cost with the total amount within the vending machine (self.total). I have worked out how to reference the product selected via a dictionary (VendingMachine.order_product), however this only works as a string and I cannot subtract the the value of the product (product.price), from the self.total - This can be seen at self.total -= {product.price} in VendingMachine.order_product

Once the order has been completed, and the product received, I want to subtract 1 product from the item.stock which can be selected in the first menu under option c - 'Transactions'. If there are none left in stock, I want to pull the item out of the dict via the .remove method I am assuming.

I don't have any friends who code, so its been tough getting this far! Any feedback would be appreciated!

Apologies if I did not explain it very well.


accepted_coins= [0.05, 0.10, 0.20, 0.50, 1.00, 2.00] 
options = ["a", "b", "c"]


class Item:
    def __init__(self, code, name, price, amount):
        self.code = code
        self.name = name
        self.price = price
        self.amount = amount


class VendingMachine:
    print("Machine booting up")

    def __init__(self):
        self.total = 0.00

        self.items = {
           '1' : Item(1, "Tea", 0.50, 10),
           '2' : Item(2, "Coffee", 1.00, 10),
           "3" : Item(3, "Coke", 1.50, 6),
           "4" : Item(4, "Orange Juice", 1.00, 5)
        }

       
        
    def select_option(self):
        print(" ")
        print("Welcome to My Vending Machine")
        print ("{} Display Products".format(options[0]))
        print ("{} Choose Product".format(options[1]))
        print ("{} Transactions".format(options[2]))
        option = input("Please select option: ")
        if option == options[0]:
            VendingMachine.display_items(self)
            VendingMachine.select_option(self)
        elif option == options[1]:
            VendingMachine.order_product(self)
        elif option == options[2]:
            VendingMachine.display_items(self)
        elif option not in (options):
            print("This option is not recongised")
            VendingMachine.select_option(self)
           
                
        
    def display_items(self):
        print (" ")
        print ("All items are currently stocked and available")
        
        print ("Display items here")
            
            
                   
    def order_product(self):
        choice = (input("Please insert the item code: "))
        product = self.items[choice]       
        print (f"You have selected {product.name} for ${product.price}. Currently {product.amount} in stock")
               
        
        coin = float(input("Please insert your coins: "))
        if float(coin) not in (accepted_coins):
            print ("The Vending Machine accepts only {}" . format(accepted_coins), end = ' ')
        else:
            self.total += coin
            
        print (f" You have {self.total} in the Machine. You need ${product.price} for {product.name}")
            
        print("[1] - Confirm")
        print("[2] - Change Order")
        print("[2] - Cancel")
        confirm = float(input('Currently there is a total of ${:.2f} in the Machine. Confirm Order?: '.format(self.total)))
        if float(confirm) == 1:
            self.total -= Items.price
            print(f'Please take your {product.name} for a total of ${product.price}. Currently there is a total of ${self.total} left in the Vending Machine')
        elif float(confirm) == 2:
            print ("Please select again")
            self.total = 0.00
            VendingMachine.select_option(self)
        elif float(confirm) == 3:
            print("Please take your money, and have a nice day!")
            self.total = 0.00
            VendingMachine.order_product(self)
        elif float(confirm) < 3:
            print("Incorrect Answer - Please take your money and try again")
            self.total = 0.00
            VendingMachine.order_product(self)

            
                
            
        

def main():

    vending_machine = VendingMachine()
    vending_machine.select_option()
    
    

    return 0


if __name__ == "__main__":
    import sys
    sys.exit(main())
    
Reply
#2
please fix the indentation of the code
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
(Apr-05-2020, 05:10 AM)buran Wrote: please fix the indentation of the code

Sorry Buran, I have updated now
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Vending machine help wiggles 5 8,429 Apr-04-2020, 05:28 AM
Last Post: wiggles

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020