Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

My getter and setter methods are not working with my functions.

+1 vote
asked Aug 26 by Jamal Artis (130 points)
class Order:
    
    number = 0
    name = ""
    amount = 0.0
    
  
    
    def __init__(self, number, name, amount):
        self.number = number
        self.name = name
        self.amount = amount
 
    def getnumber(self):
        return self.number
    def setnumber(self,number):
        self.number = number
    def getname(self):
        return self.name
    def setname(self, name):
        self.name = name
    def getamount(self):
        return self.amount
    def setamount(self, amount):
        self.amount = amount

    

class OrderUI:
    
    def __init__(self):
         self.orders = {}
        
        
    def display_order(self):
        print("*****************")
        print("** Order List **")
        print("Order Number: ", Order.number)
        print("Order Type: ", Order.name)
        print("Order Amount: ", Order.amount)
        print("---------")
        print("*****************")
        
    def getSelection(self):
        print("===========Welcome to Pizza Palace - Class Demo===========")
        print("1. Display Orders.")
        print("2. Add New Order.")
        print("3. Remove Order.")
        print("4. Edit Orders.")
        print("5. Save Order")
        print("9. Exit the Order")
        print()
        return int(input("Selection: "))
        
        
        
    def add_order(self):    
        number = int(input("Enter your order number: "))
        name = input("Enter your order type: ")
        amount = float(input("Enter your order amount: $"))
        self.orders[number] = Order(number, name, amount)
        print(number, "has been added.")
        
    def save_order(self):
        fileName = input("Enter a filename: ")
        with open(filename, "wb") as f:
            for order in orders.values():
                number = orders.number
                name = orders.name
                amount = orders.amount
                print(",".join(number, name, amount), file = f)
    def remove_order(self):
        removeNumber = int(input("Enter the Order Number to be Removed: "))
        if removeNumber in self.orders:
            del orders[removeNumber]
        else:
            print("Item Not Found.")
        
    def edit_order(self):
      
        oldNumber = int(input("Enter the Order Number to Edit: "))
        if oldNumber in self.orders:
            newNumber = int(input("Enter your New Order Number:"))
            newName = input("Enter your New Order type: ")
            newAmount = float(input("Enter your New Order Amount: $"))
            Order[oldNumber] = Order(newNumber, newName, newAmount)
        else:
            print("Item Not Found in List")
        
        
while True:
    print("\n")
    ui = OrderUI()
    selection = ui.getSelection()
    if (selection == 1):
        ui.display_order()
    elif (selection == 2):
        ui.add_order()
    elif (selection == 3):
        ui.remove_order()
    elif (selection == 4):
        ui.edit_order()
    elif (selection == 5):
        ui.save_order()
    elif (selection == 9):
        print("Exiting Program....")
        break
    else:
        print("Invalid Entry! Please enter a number from 1-5 or 9 to exit.")
    

if __name__ == "__main__":
    main()

1 Answer

0 votes
answered Sep 22 by Peter Minarik (34,450 points)

Let's start by saying that I'm not a Python guru. Feel free to verify what I say below from other sources.

I don't see any problem with your constructor or setters.

The problem is that display_order() does not iterate through the orders, but displays the values of a static object (which contains the default values of course).

Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.
...