def loadOrderFromTextFile(filename): order = Order() txtfile = open(filename, 'r') sandwichstrings = txtfile.readlines() for sandwichstring in sandwichstrings: sandwichstring = sandwichstring[0:-1] print(sandwichstring) sandwichvars = json.loads(sandwichstring) sandwich = Sandwich(sandwichvars['name']) if (sandwichvars['bread'] != None): sandwich.setBread(sandwichvars['bread']) if (sandwichvars['cheese'] != None): sandwich.setCheese(sandwichvars['cheese']) if (sandwichvars['meat'] != None): sandwich.setMeat(sandwichvars['meat']) if (sandwichvars['condiments'] != None): for condiment in sandwichvars['condiments']: sandwich.addCondiment(condiment) if (sandwichvars['toasted'] != None): sandwich.setToasted(sandwichvars['toasted']) order.addSandwich(sandwich) return order
s2.setCheese("cheddar") s2.addCondiment("Mayo") print(s2) print("Order 1:") order = Order() print(order) order.addSandwich(s1) print(order) print("Total Price:" + str(order.price())) order.addSandwich(s2) print(order) print("Total Price:" + str(order.price())) s3 = Sandwich("Gary") s3.setBread("Sourdough") s3.addCondiment("Horseradish") s3.setToasted(True) print("Order 2:") order2 = Order() order2.addSandwich(s1) order2.addSandwich(s2) order2.addSandwich(s3) print(order2) print("Order 1:") print(order) print("\nExtra Credit Option 2 Save/Load From Text:") saveOrderAsTextFile(order2, "order.txt") order3 = loadOrderFromTextFile("order.txt") print(order3)
# Objectives: # Create a new, reusable class # Develop a program to test the new class that is separate from the new class itself # this allows us to use the objects and methods defined in the sandwich.py module from sandwich import Sandwich # the following tests are from the assignment page themselves print('\n--------------------------------------') print('Test data in the assignment') print('--------------------------------------') s = Sandwich("Bennie") print(s) print(s.price()) s.setBread("wheat") print(s) print(s.price()) s.setCheese("Cheddar") print(s) print(s.price()) s.setMeat("turkey") print(s) print(s.price()) s.addCondiment("mayo") print(s) print(s.price()) s.addCondiment("mustard") print(s) print(s.price()) s.addCondiment("lettuce")
# create Sandwich objects s1 = Sandwich("Joe") s1.setMeat("steak") s1.addCondiment("Lettuce") print(s1) print(s1.getPrice()) s2 = Sandwich("Mary") s2.setCheese("cheddar") s2.addCondiment("Mayo") print(s2) print(s2.getPrice()) s3 = Sandwich("Elizabeth") s3.setBread("sourdough") s3.setMeat("ham") s3.setCheese("swiss") s3.addCondiment("mayo") s3.addCondiment("mustard") s3.setToasted(True) print(s3) print(s3.getPrice()) # create 1 Order object # print and get the price for the order order = Order() print(order) order.addSandwich(s1) print(order) print(order.price())
# Test data by creating another order print('Test data by creating another order:') order2 = Order() try: print(order2) except ValueError: print('No sandwiches in the order.') # 2nd Order, 1st sandwich s3 = Sandwich("Teo") s3.setCheese("Pepperjack") s3.setMeat('Ham') s3.addCondiment("Mayo") s3.addCondiment("Mustard") s3.addCondiment("Catsup") s3.setBread('Flatbread') s3.setToasted(True) s3.addCondiment("Lettuce") print(s3) order2.addSandwich(s3) myDict = {} for attr, value in s1.__dict__.items(): myDict[attr] = value createFile(myDict) # 2nd Order, 2nd sandwich s4 = Sandwich("Josie") s4.setCheese("Pepperjack") s4.setMeat('Turkey')