def editing_the_collection(collection, file_name): read = input( "Enter a number of order to be changed (enter 0 not ot change anything):" ) if represents_int(read): while collection.get_len() < int(read) or 0 > int(read): read = input( "Reenter a number of order to be changed(enter 0 not to change anything)" ) if int(read) == 0: pass else: to_change_id = int(read) read = input("Enter an info to be changed\n" "1 - id\n" "2 - order status\n" "3 - amount\n" "4 - discount\n" "5 - order date\n" "6 - shipped date\n" "7 - customer email\n") while not Validators.sort_and_changer_validation(int(read)) \ or not represents_int(read): read = input("Reenter an info to be changed") to_change_field = choice_to_attr_dict[read] read = input("Enter new data:") collection.change2(to_change_id, to_change_field, read, file_name)
def str_to_order(self, line): line = line.split(", ") all_is_validated = True error_list = [] line_iter = 0 for field in self.attr_dict: validator_name = Validators.validators_of_order_fields[field] if not getattr(Validators, validator_name)(line[line_iter]): error_list.append(line_iter) all_is_validated = False if line_iter != len(line): line_iter += 1 if dates_check(line[4], line[5]): all_is_validated = Validators.two_dates_validation( line[4], line[5]) if all_is_validated: dictionary = self.attr_dict.copy() i = 0 for name in self.attr_dict: dictionary[name] = line[i].lower() i += 1 res = Order(dictionary) return res else: for error in error_list: print(self.error_messages[error]) return 1
def append_to_file(self, file_name, cut): if Validators.file_exists(file_name): file = open(file_name, "a") line = self.make_str_for_file() if cut: line = line[:-1] file.write(line) else: print("Error occurred while trying to append to file. File does no exists")
def sort_user_choice(): read = input("Enter a parameter to be sorted by:\n" "1 - id\n" "2 - order status\n" "3 - amount of product bought\n" "4 - discount\n" "5 - order date\n" "6 - shipped date\n" "7 - customer email\n") while not Validators.sort_and_changer_validation(read): read = input() sort_choice = choice_to_attr_dict[read] return sort_choice
def read_from_file(self, file_name): if Validators.file_not_empty(file_name): f = open(file_name) lines = f.readlines() line_number = 0 for i in range(len(lines)): to_add = Order line = lines[i] if i != len(lines) - 1: line = line[:-1] # print("line is:", line) if Order.str_to_order(to_add, line) != 1: to_add = Order.str_to_order(to_add, line) self.list_of_orders.append(to_add) else: print("Data error in line " + str(i + 1)) line_number += 1
def main(): menu_choice = 0 collection = Collection() read = input("Enter file name (eg. Text.txt):") while not Validators.validate_filename(read): # валідувати змінні неможливо, тому я не знаю як це замінити декоратором read = input("Reenter file name (eg. Text.txt):") file_name = read if Validators.file_not_empty(file_name): collection.read_from_file(file_name) collection.print() while menu_choice != 6: print( "You are in main menu. Possible options:\n" "1 - search some data through all info\n" "2 - sort collection by some argument\n" "3 - delete any order(by its number in list) and rewrite collection in file\n" "4 - add an order and rewrite collection in file\n" "5 - edit an order and rewrite collection in file\n" "6 - exit program\n" "Choose your option: ") read = input() while not Validators.menu_choice_and_sort_and_changer_validation(read): read = input() menu_choice = int(read) if menu_choice == 1: read = input("Enter data to be searched:") collection.search(read) elif menu_choice == 2: sort_choice = sort_user_choice() collection.sort(sort_choice) collection.print() elif menu_choice == 3: read = input( "Enter a number of order to be deleted(enter 0 not to delete anything)" ) if represents_int(read): while collection.get_len() < int(read) or 0 > int(read): read = input( "Reenter a number of order to be deleted(enter 0 not to delete anything)" ) if int(read) == 0: break else: collection.deleter(int(read), file_name) collection.print() elif menu_choice == 4: print( "Enter a LINE which is similar to (with a ', ' as a delimiter or enter 0 to skip)\n" "ID, order_status (paid, refunded, not paid), amount, discount (percentage), " "order_date, shipped_date, customer_email") read = input() if represents_int(read) and int(read) == 0: pass else: to_add = Order to_add = Order.str_to_order(to_add, read) collection.add_order(to_add) collection.rewriting_to_file(file_name) collection.print() elif menu_choice == 5: editing_the_collection(collection, file_name) collection.print() elif menu_choice == 6: print("Have a nice day! Goodbye!")