From 482f5dcc9a5f819e80efc07c5df38698c479d406 Mon Sep 17 00:00:00 2001 From: gabriel becker Date: Tue, 30 Aug 2022 10:54:11 -0300 Subject: [PATCH] Create clipboard and price processing. --- requirements.txt | 2 +- src/__main__.py | 57 +++++++++++++++++++++++++++--------------------- src/order.py | 44 ++++++++++++++++++++++++++++++++----- todo.md | 7 ++++++ 4 files changed, 79 insertions(+), 31 deletions(-) create mode 100644 todo.md diff --git a/requirements.txt b/requirements.txt index f7ca03b..45bd280 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,3 +1,3 @@ pyyaml ipython -pyperclip +clipboard diff --git a/src/__main__.py b/src/__main__.py index 5230244..6689cbe 100644 --- a/src/__main__.py +++ b/src/__main__.py @@ -1,16 +1,11 @@ import os +import clipboard + from src.order import Order -main_menu_options = """ -Choose an option: -1 - Add item -2 - Print Summary/PDV -3 - Parse Ifood into PDVS -4 - Remove Last item -5 - Clear Order -6 - Exit -""" +def return_func(): + return False def add_item(): @@ -42,7 +37,9 @@ def add_item(): def print_summary(): print('Summary:') - print(Order.get_string()) + summary = Order.get_string() + clipboard.copy(summary) + print(summary) def parse_ifood_string(): @@ -57,27 +54,37 @@ def clear_all_items(): Order.clear_all_items() +main_menu_options = { + '1': {'func': add_item, 'name': 'Add item'}, + '2': {'func': print_summary, 'name': 'Copy Summary/PDV to Clipboard'}, + '3': {'func': parse_ifood_string, 'name': 'Parse Ifood into PDVS'}, + '4': {'func': remove_latest_item, 'name': 'Remove Last item'}, + '5': {'func': clear_all_items, 'name': 'Clear Order'}, + '6': {'func': return_func, 'name': 'Exit'}, +} + + +def print_main_menu_options(): + print("Choose an Option:") + for k, c in main_menu_options.items(): + print(f"{k} - {c['name']}") + + def menu(): - while True: - print(main_menu_options) + should_not_exit = True + while should_not_exit: + print_main_menu_options() chosen_option = input() os.system('clear') - if chosen_option == '1': - add_item() - elif chosen_option == '2': - print_summary() - elif chosen_option == '3': - parse_ifood_string() - elif chosen_option == '4': - remove_latest_item() - elif chosen_option == '5': - clear_all_items() - elif chosen_option == '6': - return - else: + chosen_option = main_menu_options.get(chosen_option) + if chosen_option is None: print('Choose a valid option.') + else: + option_callable = chosen_option['func'] + should_not_exit = option_callable() + should_not_exit = True if should_not_exit is None else should_not_exit if __name__ == '__main__': diff --git a/src/order.py b/src/order.py index c110aa0..2d002a8 100644 --- a/src/order.py +++ b/src/order.py @@ -1,6 +1,28 @@ import yaml +class OrderItem: + def __init__(self, name, code=None): + self.name = name + self.code = code + self.number = 0 + + def set_quantity(self, number): + self.number = number + + def add_item(self): + self.number += 1 + + def __eq__(self, other): + return self.name == other.name + + def __repr__(self): + return f'' + + def __add__(self, other): + self.number += other + + class Order: items = list() menu = dict() @@ -16,16 +38,29 @@ class Order: @staticmethod def add_item(item_name): - Order.items.append(item_name) + item = OrderItem(item_name) + if item in Order.items: + previous_item = Order.items[Order.items.index(item)] + previous_item += 1 + else: + Order.items.append(item) + item += 1 @staticmethod def get_string(): menu_as_dict = {x['name']: x for x in Order.menu} - __items = list(map(lambda x: menu_as_dict[x], Order.items)) - - output = [f"**{x['name']}**\n{x['description']}" for x in __items] + price = 0 + output = [] + for item in Order.items: + item_str = ( + f"""**{item.number}x - {menu_as_dict[item.name]['name']}**\n""" + f"""{menu_as_dict[item.name]['description']}""" + ) + price += item.number * menu_as_dict[item.name]['price_wpp'] + output.append(item_str) output = '\n'.join(output) + output += f'\n**Total Price**: R${price}' return output @@ -33,7 +68,6 @@ class Order: def remove_latest_item(): Order.items = Order.items[:-1] - @staticmethod def clear_all_items(): Order.items = list() diff --git a/todo.md b/todo.md new file mode 100644 index 0000000..4fbae30 --- /dev/null +++ b/todo.md @@ -0,0 +1,7 @@ + + + - [ ] PDV creation + - [ ] parsing ifood string + - [ ] create setup.py + - [ ] add cli option to add menu file as a parameter with a default location + - [ ] change manual printing menu to a intelligent lib \ No newline at end of file