Jan-05-2019, 02:08 PM
So for one of my tasks in my assignment, it is required to create a object oriented program with atleast 4 classes and with atleast 1 superclass. We were able to choose whatever programming language that is appropriate to the task in which I chose Python because I have past experience with it. So I asked my lecturer if it was fine to code a virtual vending machine program in which he agreed and I did it, the source code is below however I don't know how to create classes and how to make methods/attributes private or public etc and I know only the basics. So I want the superclass to be "vendingMachine" and the three subclasses to be "Balance", "Items" and "Keypad". If anyone could help that would be greatly appreciated as I am stuck and need to move onto other tasks in the assignment but this is one of the main tasks.
def getBalance(): balance = 0 #Variable that tracks total amount of coins inserted into the vending machine value = [0.10, 0.20, 0.50, 1.00] #List of all the accepted coins that can be inserted into the vending machine return balance, value def items(): itemName = ["Water", "Pepsi", "Coke", "Dr Pepper", "Sprite", "Mountain Dew", "Tango Orange", "Tango Cherry"] #These are all the names of every item itemPrice = ["0.60", "1.40", "1.50", "1.30", "1.20", "1.10", "1.10", "1.00"] #These are all the prices for each item itemCode = ["1a", "1b", "1c", "1d", "1e", "1f", "1g", "1h"] #These are the item codes of all of the items itemCode[0] = "1a" itemCode[1] = "1b" itemCode[2] = "1c" itemCode[3] = "1d" #These are the indexed values from the variable itemCode itemCode[4] = "1e" itemCode[5] = "1f" itemCode[6] = "1g" itemCode[7] = "1h" itemPrice[0] = 0.60 itemPrice[1] = 1.40 itemPrice[2] = 1.50 itemPrice[3] = 1.30 #These are the indexed values from the variable itemPrice itemPrice[4] = 1.20 itemPrice[5] = 1.10 itemPrice[6] = 1.10 itemPrice[7] = 1.00 return itemCode, itemName, itemPrice, itemCode[0], itemCode[1], itemCode[2], itemCode[3], itemCode[5], itemCode[6], itemCode[7], itemPrice[0], itemPrice[1], itemPrice[2], itemPrice[3], itemPrice[4], itemPrice[5], itemPrice[6], itemPrice[7] def menu(): itemCode, itemName, itemPrice, itemCode[0], itemCode[1], itemCode[2], itemCode[3], itemCode[5], itemCode[6], itemCode[7], itemPrice[0], itemPrice[1], itemPrice[2], itemPrice[3], itemPrice[4], itemPrice[5], itemPrice[6], itemPrice[7] = items() #The line of code above allows this function to use the variables from the function items() balance, value = getBalance() #This line of code allows this function to use the variables from the function getBalance() print(" ") print("============= Vending Machine =============") print(itemCode[0], itemName[0], "£{:.2f}".format(itemPrice[0])) print(itemCode[1], itemName[1], "£{:.2f}".format(itemPrice[1])) print(itemCode[2], itemName[2], "£{:.2f}".format(itemPrice[2])) print(itemCode[3], itemName[3], "£{:.2f}".format(itemPrice[3])) #For each line of the menu where it displays the item code, item name and item price it obtain those values from the list in the items() function print(itemCode[4], itemName[4], "£{:.2f}".format(itemPrice[4])) #This is the menu GUI which displays the different item codes, item names and item price print(itemCode[5], itemName[5], "£{:.2f}".format(itemPrice[5])) print(itemCode[6], itemName[6], "£{:.2f}".format(itemPrice[6])) print(itemCode[7], itemName[7], "£{:.2f}".format(itemPrice[7])) print("===========================================") print(" ") def keypad(): itemCode, itemName, itemPrice, itemCode[0], itemCode[1], itemCode[2], itemCode[3], itemCode[5], itemCode[6], itemCode[7], itemPrice[0], itemPrice[1], itemPrice[2], itemPrice[3], itemPrice[4], itemPrice[5], itemPrice[6], itemPrice[7] = items() #The line of code above allows this function to use the variables from the function items() balance, value = getBalance() #This line of code allows this function to use the variables from the function getBalance() choice = input("Enter the item code of an item you would like to purchase: ") #Asks the user to input the item code of an item they would like to purchase and it is stored in a variable called choice if choice == itemCode[0]: #If the variable choice is equal to 1a then... print("The price of that item is £{:.2f}".format(itemPrice[0])) #Display the price of Water while balance != itemPrice[0]: #While the variable balance is not equal to the price of Water... coins = float(input("Insert a coin into the vending machine: ")) #Ask the user to input a coin which is stored in the coins variable if coins not in value: #if coins is not in the variable value, which means if it is not in the list of accepted coins then... print("The vending machine doesn't accept this coin\nThe vending machine only accepts 0.10, 0.20, 0.50 and 1.00") #if the user inputs a coin that is not accepted, this message will be displayed elif coins in value: #elif coins is not in the variable value, which means if a coin is in the list of accepted coins then... balance = coins + balance #Add the value that is stored in the variable coins with the value stored in the variable balance print("Balance: £{:.2f}".format(balance)) #Displays the balance once a coin has been inserted if balance > itemPrice[0]: #if balance is greater than the price of Water then... balance = 0 #Reset the value stored in the variable balance to 0 print("That is too much money for the price of", itemName[0],"please try again") #Display that the coins inserted is too much money for the price of Water print("The price of that item is £{:.2f}".format(itemPrice[0])) #Display the price of Water once the balance has been reset print("Balance: £{:.2f}".format(balance)) #Displays the balance once a coin has been inserted if balance == itemPrice[0]: #if balance is equal to the price of Water then... print("Correct!") elif choice == itemCode[1]: print("The price of that item is £{:.2f}".format(itemPrice[1])) while balance != itemPrice[1]: coins = float(input("Insert a coin into the vending machine: ")) if coins not in value: print("The vending machine doesn't accept this coin\nThe vending machine only accepts 0.10, 0.20, 0.50 and 1.00") elif coins in value: balance = coins + balance print("Balance: £{:.2f}".format(balance)) if balance > itemPrice[1]: balance = 0 print("That is too much money for the price of", itemName[1],"please try again") print("The price of that item is £{:.2f}".format(itemPrice[1])) print("Balance: £{:.2f}".format(balance)) if balance == itemPrice[1]: print("Correct!") elif choice == itemCode[2]: print("The price of that item is £{:.2f}".format(itemPrice[2])) while balance != itemPrice[2]: coins = float(input("Insert a coin into the vending machine: ")) if coins not in value: print("The vending machine doesn't accept this coin\nThe vending machine only accepts 0.10, 0.20, 0.50 and 1.00") elif coins in value: balance = coins + balance print("Balance: £{:.2f}".format(balance)) if balance > itemPrice[2]: balance = 0 print("That is too much money for the price of", itemName[2],"please try again") print("The price of that item is £{:.2f}".format(itemPrice[2])) print("Balance: £{:.2f}".format(balance)) if balance == itemPrice[2]: print("Correct!") elif choice == itemCode[3]: print("The price of that item is £{:.2f}".format(itemPrice[3])) while balance != itemPrice[3]: coins = float(input("Insert a coin into the vending machine: ")) if coins not in value: print("The vending machine doesn't accept this coin\nThe vending machine only accepts 0.10, 0.20, 0.50 and 1.00") elif coins in value: balance = coins + balance print("Balance: £{:.2f}".format(balance)) if balance > itemPrice[3]: balance = 0 print("That is too much money for the price of", itemName[3],"please try again") print("The price of that item is £{:.2f}".format(itemPrice[3])) print("Balance: £{:.2f}".format(balance)) if balance == itemPrice[3]: print("Correct!") elif choice == itemCode[4]: print("The price of that item is £{:.2f}".format(itemPrice[4])) while balance != itemPrice[4]: coins = float(input("Insert a coin into the vending machine: ")) if coins not in value: print("The vending machine doesn't accept this coin\nThe vending machine only accepts 0.10, 0.20, 0.50 and 1.00") elif coins in value: balance = coins + balance print("Balance: £{:.2f}".format(balance)) if balance > itemPrice[4]: balance = 0 print("That is too much money for the price of", itemName[4],"please try again") print("The price of that item is £{:.2f}".format(itemPrice[4])) print("Balance: £{:.2f}".format(balance)) if balance == itemPrice[4]: print("Correct!") elif choice == itemCode[5]: print("The price of that item is £{:.2f}".format(itemPrice[5])) while balance != itemPrice[5]: coins = float(input("Insert a coin into the vending machine: ")) if coins not in value: print("The vending machine doesn't accept this coin\nThe vending machine only accepts 0.10, 0.20, 0.50 and 1.00") elif coins in value: balance = coins + balance print("Balance: £{:.2f}".format(balance)) if balance > itemPrice[5]: balance = 0 print("That is too much money for the price of", itemName[5],"please try again") print("The price of that item is £{:.2f}".format(itemPrice[5])) print("Balance: £{:.2f}".format(balance)) if balance == itemPrice[5]: print("Correct!") elif choice == itemCode[6]: print("The price of that item is £{:.2f}".format(itemPrice[6])) while balance != itemPrice[6]: coins = float(input("Insert a coin into the vending machine: ")) if coins not in value: print("The vending machine doesn't accept this coin\nThe vending machine only accepts 0.10, 0.20, 0.50 and 1.00") elif coins in value: balance = coins + balance print("Balance: £{:.2f}".format(balance)) if balance > itemPrice[6]: balance = 0 print("That is too much money for the price of", itemName[6],"please try again") print("The price of that item is £{:.2f}".format(itemPrice[6])) print("Balance: £{:.2f}".format(balance)) if balance == itemPrice[6]: print("Correct!") elif choice == itemCode[7]: print("The price of that item is £{:.2f}".format(itemPrice[7])) while balance != itemPrice[7]: coins = float(input("Insert a coin into the vending machine: ")) if coins not in value: print("The vending machine doesn't accept this coin\nThe vending machine only accepts 0.10, 0.20, 0.50 and 1.00") elif coins in value: balance = coins + balance print("Balance: £{:.2f}".format(balance)) if balance > itemPrice[7]: balance = 0 print("That is too much money for the price of", itemName[7],"please try again") print("The price of that item is £{:.2f}".format(itemPrice[7])) print("Balance: £{:.2f}".format(balance)) if balance == itemPrice[7]: print("Correct!") menu() keypad()