def get_nutritional_info(self): file = FileType(CONST.DISH_FILE_NAME, DishType) factInfo = [0 for _ in range(CONST.NUTRITION_ITEMS)] self.__remove_noexist_dish() #for each name of the dishes that make up the day for item in self.values: #get dish dish = file.get_elem(item) #add to total if dish get was successful factInfo = NutritionType("", factInfo) + NutritionType( "", dish.get_nutritional_info()) return factInfo
def get_nutritional_info(self): file = FileType(CONST.FOOD_FILE_NAME, FoodType) factInfo = [0 for _ in range(CONST.NUTRITION_ITEMS)] self.__remove_noexist_food() #every second values is the name of a food for i in range(0, len(self.values), 2): #get food food = file.get_elem(self.values[i]) #nutritional info nutritionalInfo = NutritionType("", food.get_nutritional_info()) #how many more times than 100g there is multiplier = float(self.values[i + 1]) / 100 factInfo = NutritionType("", factInfo) + NutritionType( "", nutritionalInfo * multiplier) return factInfo
def __remove_noexist_dish(self): dish_file = FileType(CONST.DISH_FILE_NAME, DishType) new_values = [] #go through all foods in this dish for item in self.values: #check if food exists if dish_file.elem_exists(item): new_values.append(item) #all the missing foods missing = list(set(self.values) - set(new_values)) if len(missing) != 0: #remove the corresponding dish for dish in missing: index = self.values.index(dish) self.values.pop(index) day_file = FileType(CONST.DAY_FILE_NAME, Day) day_file.remove_elem(self.name) day_file.add_elem(Day(self.name, self.values))
def __remove_noexist_food(self): food_file = FileType(CONST.FOOD_FILE_NAME, FoodType) new_values = [] #go through all foods in this dish for i in range(0, len(self.values), 2): #check if food exists if food_file.elem_exists(self.values[i]): new_values.append(self.values[i]) #all the missing foods missing = list( set([self.values[i] for i in range(0, len(self.values), 2)]) - set(new_values)) if len(missing) != 0: #remove the corresponding food and weight of it for food in missing: index = self.values.index(food) self.values.pop(index) self.values.pop(index) dish_file = FileType(CONST.DISH_FILE_NAME, Dish) dish_file.remove_elem(self.name) dish_file.add_elem(Dish(self.name, self.values))
def __init__(self): #set commands self.commands = ["DAY", "DISH", "FOOD", "EXIT"] self.dict = {self.commands[0]: self.__DAY, \ self.commands[1]: self.__DISH, \ self.commands[2]: self.__FOOD, \ self.commands[3]: self.__EXIT} #set commands #set printing params self.commandValues = ["Access day commands: ", \ "Access dish commands", \ "Access food commands: ", \ "Exit the program: "] self.commandSpaces = self.__get_spaces(self.commandValues) self.nutritionItems = ["Calories:", \ "Fat:", " Saturated Fat:", \ " Trans Fat:", \ "Cholesterol:", \ "Sodium:", \ "Carbohydrates:", \ " Fiber", \ " Sugar", \ "Protien"] self.nutritionSpaces = self.__get_spaces(self.nutritionItems) #set printing params #create printing list temp = [] #replace first item with later with command self.printItems = [] for i in range(0, len(self.commands)): self.printItems.append(self.commandValues[i] + self.commandSpaces[i] + self.commands[i]) temp.append(self.commandValues[i] + self.commandSpaces[i]) spacing = self._get_longest_val(temp) self.command = "\n" + (" " * spacing) + "COMMANDS\n" #create printing list #create files self.nuritionFile = FileType(CONST.NUTRITION_FILE_NAME, NutritionType) self.foodFile = FileType(CONST.FOOD_FILE_NAME, FoodType) self.dishesFile = FileType(CONST.DISH_FILE_NAME, DishType) self.daysFile = FileType(CONST.DAY_FILE_NAME, DayType) #create files #first print print("Instructions:") print() print("All commands are in capital letters. They are case sensitive.\n" + \ "Commands to access an item are under \"COMMANDS\"\n" + \ "Entering a command brings up that items actions\n" + \ "which are ADD, GET, and REMOVE.\n" + \ "Some items have special actions, they will explititly state this.") print() print("ALL is a special word that can be used with the GET and REMOVE actions.\n" + \ "With get it print the names of all of the type of item.\n" + \ "With remove it removes all of the type of item.") print() print("The following are the items.") print("Nutrition Fact:\n") print(" - This is the first information to be entered\n" + \ " - Enter information per 100g") print("Food:\n") print(" - Uses a nutritional information to hold its value") print("Dish:\n") print(" - Consists of any number of foods of any weight") print("Day:\n") print(" - Consists of any number of dishes") print() print("-" * 20 + "SCROLL TO TOP FOR COMPLETE INSTRUCTIONS" + "-" * 20) print() #first print #consle loop while True: self.__print_commands() print() #get function call inp = input("Enter Command: ").strip().upper() print() if inp not in self.dict: comm = "" for item in self.commands[:-1]: comm = comm + item + " or " comm = comm + self.commands[-1] print("Enter valid command of: " + comm) else: #run function call self.dict[inp]() print()
class Main: def __init__(self): #set commands self.commands = ["DAY", "DISH", "FOOD", "EXIT"] self.dict = {self.commands[0]: self.__DAY, \ self.commands[1]: self.__DISH, \ self.commands[2]: self.__FOOD, \ self.commands[3]: self.__EXIT} #set commands #set printing params self.commandValues = ["Access day commands: ", \ "Access dish commands", \ "Access food commands: ", \ "Exit the program: "] self.commandSpaces = self.__get_spaces(self.commandValues) self.nutritionItems = ["Calories:", \ "Fat:", " Saturated Fat:", \ " Trans Fat:", \ "Cholesterol:", \ "Sodium:", \ "Carbohydrates:", \ " Fiber", \ " Sugar", \ "Protien"] self.nutritionSpaces = self.__get_spaces(self.nutritionItems) #set printing params #create printing list temp = [] #replace first item with later with command self.printItems = [] for i in range(0, len(self.commands)): self.printItems.append(self.commandValues[i] + self.commandSpaces[i] + self.commands[i]) temp.append(self.commandValues[i] + self.commandSpaces[i]) spacing = self._get_longest_val(temp) self.command = "\n" + (" " * spacing) + "COMMANDS\n" #create printing list #create files self.nuritionFile = FileType(CONST.NUTRITION_FILE_NAME, NutritionType) self.foodFile = FileType(CONST.FOOD_FILE_NAME, FoodType) self.dishesFile = FileType(CONST.DISH_FILE_NAME, DishType) self.daysFile = FileType(CONST.DAY_FILE_NAME, DayType) #create files #first print print("Instructions:") print() print("All commands are in capital letters. They are case sensitive.\n" + \ "Commands to access an item are under \"COMMANDS\"\n" + \ "Entering a command brings up that items actions\n" + \ "which are ADD, GET, and REMOVE.\n" + \ "Some items have special actions, they will explititly state this.") print() print("ALL is a special word that can be used with the GET and REMOVE actions.\n" + \ "With get it print the names of all of the type of item.\n" + \ "With remove it removes all of the type of item.") print() print("The following are the items.") print("Nutrition Fact:\n") print(" - This is the first information to be entered\n" + \ " - Enter information per 100g") print("Food:\n") print(" - Uses a nutritional information to hold its value") print("Dish:\n") print(" - Consists of any number of foods of any weight") print("Day:\n") print(" - Consists of any number of dishes") print() print("-" * 20 + "SCROLL TO TOP FOR COMPLETE INSTRUCTIONS" + "-" * 20) print() #first print #consle loop while True: self.__print_commands() print() #get function call inp = input("Enter Command: ").strip().upper() print() if inp not in self.dict: comm = "" for item in self.commands[:-1]: comm = comm + item + " or " comm = comm + self.commands[-1] print("Enter valid command of: " + comm) else: #run function call self.dict[inp]() print() def __print_commands(self): maxLen = 0 for item in self.printItems: maxLen = max(maxLen, len(item)) print("-" * maxLen) #print(self.command) print(self.command) for item in self.printItems: print(item) def __EXIT(self): sys.exit() def __DAY(self): self.__options("day", [ self.__DELETE_DAY, self.__ADD_DAY, self.__GET_DAY, self.__CHANGE_DAY ]) def __GET_DAY(self, name): if name == "ALL": self.__print_all(self.daysFile) else: if not self.daysFile.elem_exists(name): self.__print_name_error("Day with this name does not exist") else: inp = input( "\nGet nutritional information total(T) or for each dish(E): " ) if inp == "T": day = self.daysFile.get_elem(name) nutritionalInfo = day.get_nutritional_info() self.__print_nutrition( name, nutritionalInfo, "Nutritional information for the day ") elif inp == "E": dishes = self.daysFile.get_elem(name).get_values() #list of list of values #[[calories, ..., protien], [calories, ..., protien], ...] nutritionalInfos = [] for dish in dishes: nutritionalInfos.append( self.dishesFile.get_elem( dish).get_nutritional_info()) # { element } #[[calories, ..., protien], [calories, ..., protien], ...] printValuesTemp = [] #for each nutrition fact for fact in nutritionalInfos: toPrint = self.__get_print_nutrition_info(fact) longest = self.__get_longest(toPrint) lengthLongest = len(toPrint[longest]) + 1 for i in range(len(toPrint)): toPrint[i] = toPrint[i] + ( (lengthLongest - len(toPrint[i])) * " ") + "| " printValuesTemp.append(toPrint) #[calories from all dishes, ..., protien from all dishes] printValues = [ "" for item in range(len(printValuesTemp[0])) ] #index from calories ... protien for i in range(len(printValuesTemp[0])): #index each element in printValuesTemp for j in range(len(printValuesTemp)): #if it is the first nutrition fact to be printed, add a | on the left if j == 0: printValues[i] = "| " + printValues[ i] + printValuesTemp[j][i] else: printValues[ i] = printValues[i] + printValuesTemp[j][i] #print result print("\n" + ("-" * (len(printValues[0]) - 1))) for line in printValues: print(line[:-1]) print("-" * (len(printValues[0]) - 1)) else: self.__print_name_error("Input should be \"T\" or \"E\"") def __DELETE_DAY(self, name): if name == "ALL": self.__remove_all(self.daysFile) else: if not self.daysFile.elem_exists(name): self.__print_name_error("Day with this name does not exist") else: self.daysFile.remove_elem(name) def __ADD_DAY(self, name): if self.daysFile.elem_exists(name): self.__print_name_error("Day with this name already exists") else: print("\nEnter dishes which make up day") print() values = [] dishName = " " hadError = False while True: dishName = input("Name of dish (hit enter to exit): ") if dishName == "": break if not self.dishesFile.elem_exists(dishName): hadError = True self.__print_name_error("\nDish with this name does not exist." + \ "\nMust create this dish using DISH command." + \ "\nPress enter to exit this command.\n") else: values.append(dishName) if not values == []: self.daysFile.add_elem(DayType(name, values)) def __CHANGE_DAY(self, name): if not self.daysFile.elem_exists(name): self.__print_name_error("Dish with this name does not exist") else: day = self.daysFile.get_elem(name) dishes = day.get_values() longest = self._get_longest_val(dishes) print("\nDishes that make up day \"" + name + "\"") for item in dishes: print("Dish: " + ((longest - len(item)) * " ") + item) print() print("To remove dish: enter R, then enter a dish name in the day (R name)\n" + \ "To add dish: enter A, then enter a dish name not in the day (A name)") while True: print() inp = input( "Type letter A or R then dish name (press enter to exit): " ).strip() inputs = inp.split(' ', 1) if len(inputs) == 1: break elif inputs[0] == 'R': if inputs[1] in dishes: dishes.pop(dishes.index(inputs[1])) else: print("\nDish not in this day.") elif inputs[0] == 'A': if self.dishesFile.elem_exists(inputs[1]): dishes.append(inputs[1]) else: print("\nDish does not exist.") #after all commands have been run, delete old day and add the new one newDay = DayType(name, dishes) self.daysFile.remove_elem(name) self.daysFile.add_elem(newDay) #need to make it so that a food can only be added ONCE to a dish def __DISH(self): self.__options("dish", [ self.__DELETE_DISH, self.__ADD_DISH, self.__GET_DISH, self.__CHANGE_DISH ]) def __GET_DISH(self, name): if name == "ALL": self.__print_all(self.dishesFile) else: if not self.dishesFile.elem_exists(name): self.__print_name_error("Dish with this name does not exist") else: dish = self.dishesFile.get_elem(name) nutritionalInfo = dish.get_nutritional_info() self.__print_nutrition(name, nutritionalInfo, "Nutritional information for one ") def __DELETE_DISH(self, name): if name == "ALL": self.__remove_all(self.dishesFile) else: if not self.dishesFile.elem_exists(name): self.__print_name_error("Dish with this name does not exist") else: self.dishesFile.remove_elem(name) def __ADD_DISH(self, name): if self.dishesFile.elem_exists(name): self.__print_name_error("Dish with this name already exists") else: print("\nEnter food name(s) and weight(s).") values = [] foodName = " " hadError = False #get values to create dish while True: foodName = input("\nName of food (hit enter to exit): ") if foodName == "": break if not self.foodFile.elem_exists(foodName): #hadError = True self.__print_name_error("\nFood with this name does not exist." + \ "\nMust create this dish using FOOD command." + \ "\nPress enter to exit this command.\n") else: foodWeight = input("Weight of food to be added to dish: ") values.append(foodName) values.append(foodWeight) #if not hadError: if not values == []: self.dishesFile.add_elem(DishType(name, values)) def __CHANGE_DISH(self, name): if not self.dishesFile.elem_exists(name): self.__print_name_error("Dish with this name does not exist") else: dish = self.dishesFile.get_elem(name) values = dish.get_values() foods = [] weights = [] for i in range(0, len(values), 2): foods.append(values[i]) weights.append(values[i + 1]) longest = self._get_longest_val(foods) print("\nFoods that make up dish \"" + name + "\":\n") for i in range(len(foods)): print("Food: " + ((longest - len(foods[i])) * " ") + foods[i] + " | Weight: " + str(weights[i]) + " g") print() print("To remove food: enter R, then food name (R name)\n" + \ "To add food: enter A, then food name, then weight (A name number)\n" + \ "To change weight of food already in dish: enter C, then food name, then weight (C name number)") while True: print() inp = input("Type input (press entr to exit): ") info = inp.strip().split(" ", 1) comm = info[0] if comm == "R": food = info[1] if not food in foods: print("\nFood with this name is not in dish.") else: #get index of food name in the list of values comprising the dish index = values.index(food) #remove the food name from the list values.pop(index) #remove the food weight from the list values.pop(index) elif comm == "A": food = info[1].rsplit(' ', 1)[0] if food in values: print( "\nFood with this name is already in dish. Try using C." ) elif not self.foodFile.elem_exists(food): print("\nFood with this name does not exist.") else: #check that number is actually number if info[1].rsplit(' ', 1)[1].replace(".", "", 1).isnumeric(): values.append(food) values.append(info[1].rsplit(' ', 1)[1]) elif comm == "C": food = info[1].rsplit(' ', 1)[0] #check food is in the values if not food in values: print("\nFood with this name not in values.") else: #check that number is actually number if info[1].rsplit(' ', 1)[1].replace(".", "", 1).isnumeric(): #change weight of food values[values.index(food) + 1] = info[1].rsplit( ' ', 1)[1] elif comm == "": break else: print("\nFirst letter must be R or A or C") #after all commands have been run, delete old dish and add the new one newDish = DishType(name, values) self.dishesFile.remove_elem(name) self.dishesFile.add_elem(newDish) def __FOOD(self): self.__options("food", [ self.__DELETE_FOOD, self.__ADD_FOOD, self.__GET_FOOD, self.__CHANGE_FOOD ]) def __GET_FOOD(self, name): if name == "ALL": self.__print_all(self.foodFile) else: if not self.foodFile.elem_exists(name): self.__print_name_error("Food with this name does not exist") else: food = self.foodFile.get_elem(name) nutritionalInfo = food.get_nutritional_info() self.__print_nutrition(name, nutritionalInfo) def __DELETE_FOOD(self, name): if name == "ALL": self.__remove_all(self.foodFile) else: if not self.foodFile.elem_exists(name): self.__print_name_error("Food with this name does not exist") else: self.foodFile.remove_elem(name) self.nuritionFile.remove_elem(name) def __ADD_FOOD(self, name): if self.foodFile.elem_exists(name): self.__print_name_error("Food with this name already exists") else: if self.__ADD_NUTRITION(name): food = FoodType(name, [name, 100]) self.foodFile.add_elem(food) def __CHANGE_FOOD(self, name): if not self.foodFile.elem_exists(name): self.__print_name_error("Food with this name does not exist") else: food = self.foodFile.get_elem(name) values = food.get_nutritional_info() print( "To change the number of a nutritional item type the number before\n" + "that then the new value. Typing \"9 50\" would change protien to 50g." ) print("Current nutritional information for \"" + name + "\" per 100g.\n") printVals = self.__get_print_nutrition_info(values) for i in range(len(printVals)): print(str(i) + ") " + printVals[i]) while True: inp = input( "\nEnter number from 0 to 9, then number (press enter to exit): " ) tupe = inp.strip().split(" ") if len(tupe) == 2 and tupe[0].isnumeric() and 0 <= int( tupe[0]) <= 9 and tupe[1].isnumeric(): values[int(tupe[0])] = tupe[1] elif tupe[0] == "": break else: print("\nEnter valid input.") self.__CHANGE_NUTRITION(name, values) newNutrition = self.nuritionFile.get_elem(name) def __NUTRITION(self): self.__options("nutrition fact", [ self.__DELETE_NUTRITION, self.__ADD_NUTRITION, self.__GET_NUTRITION ]) def __GET_NUTRITION(self, name): if name == "ALL": self.__print_all(self.nuritionFile) else: if not self.nuritionFile.elem_exists(name): self.__print_name_error( "Nurition fact with this name does not exist") else: fact = self.nuritionFile.get_elem(name) self.__print_nutrition(name, fact.get_values()) def __DELETE_NUTRITION(self, name): if name == "ALL": self.__remove_all(self.nuritionFile) else: if not self.nuritionFile.elem_exists(name): self.__print_name_error( "Nurition fact with this name does not exist") else: self.nuritionFile.remove_elem(name) def __ADD_NUTRITION(self, name): if self.nuritionFile.elem_exists(name): self.__print_name_error( "Nurition fact with this name already exists") else: #get info print("\nEnter per 100g") values = [] values.append(input("Enter number of calories: ")) values.append(input("Enter grams of total fat: ")) values.append(input("Enter grams of saturated fat: ")) values.append(input("Enter grams of trans fat: ")) values.append(input("Enter milligrams of cholesterol: ")) values.append(input("Enter milligrams of sodium: ")) values.append(input("Enter grams of total carbs: ")) values.append(input("Enter grams of fiber: ")) values.append(input("Enter grams of sugar: ")) values.append(input("Enter grams of protien: ")) nutrition = NutritionType(name, values) if not nutrition.is_created(): print('\nEnter valid name') return False if nutrition.is_valid(): self.nuritionFile.add_elem(nutrition) return True print("\nEnter valid values") return False return False def __CHANGE_NUTRITION(self, name, values): if not self.nuritionFile.elem_exists(name): self.__print_name_error("Nutrition fact does not exist.") else: self.nuritionFile.remove_elem(name) toAdd = NutritionType(name, values) self.nuritionFile.add_elem(toAdd) #print when a name error occurs def __print_name_error(self, toPrint): print("\n***" + toPrint + "***\n") #print all the items from a file def __print_all(self, file): allNames = file.get_all() allNames.sort() print() for item in allNames: print(item) #delete all the intems from a file def __remove_all(self, file): allNames = file.get_all()[::-1] for name in allNames: file.remove_elem(name) #get list of string of spaces compareed to the #max length of a string in values def __get_spaces(self, values): toRet = [] maxLen = 0 for item in values: maxLen = max(maxLen, len(item)) for item in values: toRet.append(" " * (maxLen - len(item))) return toRet #toPrint - what should be printed #functions - list of functions to be called with input as item name def __options(self, toPrint, functions): print("Remove " + toPrint + ": REMOVE\n" + \ "Add " + toPrint + ": ADD\n" + \ "Get " + toPrint + ": GET\n" + \ "Change " + toPrint + ": CHANGE\n") inp = input("Enter action: ").strip().upper() if inp != "REMOVE" and inp != "ADD" and inp != "GET" and inp != "CHANGE": print("\nEnter valid action of: REMOVE or ADD or GET or CHANGE") else: print() name = input("Enter " + toPrint + " name: ") if inp == "REMOVE": functions[0](name) elif inp == "ADD": functions[1](name) elif inp == "GET": functions[2](name) elif inp == "CHANGE": functions[3](name) #name - name to be printed #fact - list of values from nutrition fact to be printed def __print_nutrition(self, name, values, text="Nutritional information per 100g for "): print() longestLine = text + "\"" + name + "\":" print("-" * len(longestLine)) print(longestLine + "\n") printValues = self.__get_print_nutrition_info(values) print(printValues[0]) print(printValues[1]) print(printValues[2]) print(printValues[3]) print(printValues[4]) print(printValues[5]) print(printValues[6]) print(printValues[7]) print(printValues[8]) print(printValues[9]) print("-" * len(longestLine)) #returns list of values where each line is the output for a nutrition fact #list of length 10 def __get_print_nutrition_info(self, values): toRet = [ self.nutritionItems[0] + self.nutritionSpaces[0] + " " + str(values[0]) + " cal", self.nutritionItems[1] + self.nutritionSpaces[1] + " " + str(values[1]) + " g", self.nutritionItems[2] + self.nutritionSpaces[2] + " " + str(values[2]) + " g", self.nutritionItems[3] + self.nutritionSpaces[3] + " " + str(values[3]) + " g", self.nutritionItems[4] + self.nutritionSpaces[4] + " " + str(values[4]) + " mg", self.nutritionItems[5] + self.nutritionSpaces[5] + " " + str(values[5]) + " mg", self.nutritionItems[6] + self.nutritionSpaces[6] + " " + str(values[6]) + " g", self.nutritionItems[7] + self.nutritionSpaces[7] + " " + str(values[7]) + " g", self.nutritionItems[8] + self.nutritionSpaces[8] + " " + str(values[8]) + " g", self.nutritionItems[9] + self.nutritionSpaces[9] + " " + str(values[9]) + " g" ] return toRet #returns indicie of longest element in list def __get_longest(self, values): longest = 0 for i in range(len(values)): longest = i if len(values[i]) > len(values[longest]) else longest return longest #return size of longest element in list def _get_longest_val(self, values): longest = 0 for item in values: longest = len(item) if len(item) > longest else longest return longest
def get_nutritional_info(self): file = FileType(CONST.NUTRITION_FILE_NAME, NutritionType) elem = file.get_elem(self.get_values()[0]) return elem.get_values()