How do I apply the sellProduct method to each choice in the menu?

31 Views Asked by At

I don't understand why I can't connect the processSale method to the sellProduct method. I think that the classes and other methods don't need any change because I only followed the criterias that were given to me.

#candy machine
class CashRegister:
    def __init__(self, cashOnHand = 500,):
        if cashOnHand < 0:
            self.cashOnHand = 500
        else:
            self.cashOnHand = cashOnHand

    def currentBalance(self):
        return self.cashOnHand

    def acceptAmount(self, cashIn):
        self.cashOnHand += cashIn

class Dispenser:
    def __init__(self, numberOfItems = 50, productCost = 50):
            if numberOfItems < 0:
                self.numberOfItems = 50
            else:
                self.numberOfItems = numberOfItems
            if productCost < 0:
                self.productCost = 50
            else:
                self.productCost = productCost
    
    def getCount(self):
        return self.numberOfItems

    def getProductCost(self):
        return self.productCost

    def makeSale(self):
        self.numberOfItems -= 1

class MainProgram:
    def showMenu(self):
        global userInput
        print("**** Welcome to Eros' Candy Shop ****")
        print("To select an item enter")
        print("""1 for Candy
2 for Chips
3 for Gum
4 for Cookies
0 to View Balance
9 to Exit""")
        userInput = int(input("Enter your choice: "))

    def sellProduct(self, useDispenser = Dispenser(), useRegister = CashRegister()):
        try:
            self.useDispenser = useDispenser
            self.useRegister = useRegister
            if self.useDispenser.getCount != 0:
                print(f"It costs {self.useDispenser.getProductCost} cents")
                cash = int(input("Please enter your payment: "))
                change = cash - self.useDispenser.getProductCost
                if change < 0:
                    print("Insufficient money!")
                    print(f"You need {self.useDispenser.getProductCost - cash} cents more")
                    return
                else:
                    print(f"Your change is {change} cents")
                    self.useRegister.acceptAmount(self.useDispenser.getProductCost)
                    self.useDispenser.makeSale
                    return
            elif self.useDispenser.getCount == 0:
                print("The product you chose is sold out! Try the other itmes")
                return
        except ValueError:
            print("You entered an incorrect value. Please use the numbers on the menu only")

    def processSale(self):
        Register = CashRegister()
        Candy = Dispenser()
        Chips = Dispenser()
        Gum = Dispenser()
        Cookies = Dispenser()
        while True:
            self.showMenu
            if userInput == 1:
                self.sellProduct(Candy, Register)
            elif userInput == 2:
                self.sellProduct(Chips, Register)
            elif userInput == 3:
                self.sellProduct(Gum, Register)
            elif userInput == 4:
                self.sellProduct(Cookies, Register)
            elif userInput == 0:
                print("Current Balance is" + str(Register.currentBalance))
            elif userInput == 9:
                break

mainProgram = MainProgram()
mainProgram.showMenu()

How do i use sellProduct method on userInput 1-4. I get confused when applying the properties of a class and how to connect them. Can you point out what mistakes I made and what other improvements I can do.

1

There are 1 best solutions below

1
rafidini On

here are some points you can improve :

  1. When you call your methods do not forget the parenthesis :
self.useDispenser.getCount()
self.useDispenser.getProductCost()
  1. Create an infinite loop to continuously ask for input within showMenu and delete the one within processSale (for example):
    def showMenu(self):
        global userInput 
        userInput = 0
        print("**** Welcome to Eros' Candy Shop ****")
        while userInput != 9:
            print("To select an item enter")
            print(MENU)
            userInput = int(input("Enter your choice: "))
            
            if userInput < 9:
                self.processSale()

But please update the whole program accordingly.

Hope it helps !