Home > Blockchain >  The vending machine programme should be able to purchase products when the product number is selecte
The vending machine programme should be able to purchase products when the product number is selecte

Time:01-22

In Python, I want to add products from the standard input owner and purchase products by user.

First, register the information of the goods by entering the owner, then 4, in the standard input of the attribute selection. Then, in the standard input of the attribute selection, select User and choose the number of the goods or Cancel. However, when I select a product number, I cannot display the information for the product that matches that product number, and I would like to know how to do this. For example, I add one product in Owner and the product list is 1, product name, temperature, price, 2, cancelled. I would like to display the elements 1, product name, temperature and price when I select a user in the attribute selection and enter the product number 1.

import sys

#stock
class Stock:
    
    def __init__(self,name,temperature,price,maxstock):
        self.name=name 
        self.temperature=temperature #Temperature: temperature of the product. String "HOT" or "COLD".
        self.price=price
        self.maxstock=maxstock
        self.sales=0
        self.stock=0

#inventory control
class StockManager:

    def __init__(self):
        self.StockList=[]
    
    #Additional products
    def addStock(self,name,temperature,price,maxstock):
        for i in self.StockList:
            if (name==i.name and temperature==i.temperature):
                print("This product has already been added to the list.")
                return
        self.StockList.append(Stock(name,temperature,price,maxstock))
        return

class VendingMachine:
    
    def __init__(self): 
        self.ISInterface=ItemSelectionInterface()
        self.stockman=StockManager()
        self.cman=CoinManager()
    
    def selectActor(self):
        actor=input('Enter your attributes.: "User", "Supplier", "Owner" or "Exit"')
        if actor=="User":
            self.actUser()
        elif actor=="Supplier":
            self.actSupplier()
        elif actor=="Owner":
            self.actOwner()
        elif actor=="Exit":
            sys.exit()
        else:
            print("Invalid input. Exit.")
            sys.exit()

    #Basic flow: owner
    def actOwner(self):
        selectnumber=input("Please enter the number of the function you want to select: 1 Report 2 Delete an item 3 Change an item 4 Add an item 5 Add change")
     if selectnumber=="4":
            newname=input("Product name:")
            if newname=="":
                print("Invalid input. Exit.")
                return
            newtemperature=input('Temperature: ("HOT"or"COLD") ')
            if(newtemperature!="HOT" and newtemperature!="COLD"):
                print("Invalid input. Exit.")
                return
            newprice=input('price:')
            if not newprice.isdigit() or int(newprice) != 0 or int(newprice)<=0:
                print("Invalid input. Exit.")
                return
            newmaxstock=input('prescribed number(max stock):')
            if not newmaxstock.isdigit() or int(newmaxstock)>100 or int(newmaxstock)<1:
                print("Invalid input. Exit.")
                return
            self.stockman.addStock(newname, newtemperature, int(newprice), int(newmaxstock))
        else:
            print("Invalid input. Exit.")
            return
def actUser(self):
        nof500 = input("Enter the number of 500 coins:")
        nof100 = input("Enter the number of 100 coins:")
        nof50 = input("Enter the number of 50 coins")
        nof10 = input("Enter the number of 100 coins")
    
            j = 1
            for i in self.stockman.StockList:
                print(",".join([str(j),i.name,i.temperature,str(i.price)]))    
                j =1
            print(str(j) ",cancellation")    
            selectnumber=int(input("Enter the product or cancellation number:"))

I tried to get the following to print the corresponding product when a number is specified: self.stockman.StockList[selectnumber-1] is the product information, but the product information is not printed. Also, the cancellation number comes after the last product, how should I write this in the if statement?


            #Problem code.

            if len(self.stockman.StockList)>=10 or len(self.stockman.StockList)<=0:
                print("The number is incorrect.")
            else:           
                if selectnumber == len(self.stockman.StockList):
                    Item = self.stockman.StockList[selectnumber-1]
                    print(Item)
                else:
                    print("cancel")



CodePudding user response:

If I understood your issue correctly, this code should fix it:

def actUser(self):
    nof500 = input("Enter the number of 500 coins:")
    nof100 = input("Enter the number of 100 coins:")
    nof50 = input("Enter the number of 50 coins")
    nof10 = input("Enter the number of 100 coins")

    j = 1
    for i in self.stockman.StockList:
        print(",".join([str(j),i.name,i.temperature,str(i.price)]))    
        j =1
    print(str(j) ",cancellation")    
    selectnumber=int(input("Enter the product or cancellation number:"))
    
    # Check if the input is valid
    if selectnumber > 0 and selectnumber <= len(self.stockman.StockList):
        selected_product = self.stockman.StockList[selectnumber - 1]
        print(",".join([str(selectnumber),selected_product.name,selected_product.temperature,str(selected_product.price)]))
    elif selectnumber == j:
        print("Cancelled")
    else:
        print("Invalid input. Exit.")
        return
  • Related