From f1cd5021720c6d194b1fbfb0d8f8db12c4dd66ae Mon Sep 17 00:00:00 2001 From: Officialahmed Date: Sun, 30 Jul 2023 05:11:43 +0400 Subject: [PATCH] Code Factorization --- Hotel-Management.py | 286 +++++++++++++++++++------------------------- 1 file changed, 122 insertions(+), 164 deletions(-) diff --git a/Hotel-Management.py b/Hotel-Management.py index b8e4cfcbfeb..edfdec0934a 100644 --- a/Hotel-Management.py +++ b/Hotel-Management.py @@ -1,72 +1,74 @@ -def menu(): - print("") - print("") - print(" Welcome to Hotel Database Management Software") - print("") - print("") - - print("1-Add new customer details") - print("2-Modify already existing customer details") - print("3-Search customer details") - print("4-View all customer details") - print("5-Delete customer details") - print("6-Exit the program") - print("") - - user_input = int(input("Enter your choice(1-6): ")) - - if user_input == 1: - add() - elif user_input == 2: - modify() +def menu(): - elif user_input == 3: - search() + options = { + 1 : { + "title" : "Add new customer details", + "method": lambda : add() + }, + + 2 : { + "title" : "Modify already existing customer details", + "method": lambda : modify() + }, + + 3 : { + "title" : "Search customer details", + "method": lambda : search() + }, + + 4 : { + "title" : "View all customer details", + "method": lambda : view() + }, + + 5 : { + "title" : "Delete customer details", + "method": lambda : remove() + }, + + 6 : { + "title" : "Exit the program", + "method": lambda : exit() + } + } - elif user_input == 4: - view() + print(f"\n\n{' '*25}Welcome to Hotel Database Management Software\n\n") - elif user_input == 5: - remove() + for num, option in options.items(): + print(f"{num}: {option.get('title')}") + print() - elif user_input == 6: - exit() + options.get( int(input("Enter your choice(1-6): ")) ).get("method")() def add(): - print("") - Name1 = input("Enter your first name: ") - print("") - - Name2 = input("Enter your last name: ") - print("") - - Phone_Num = input("Enter your phone number(without +91): ") - print("") + Name1 = input("\nEnter your first name: \n") + Name2 = input("\nEnter your last name: \n") + Phone_Num = input("\nEnter your phone number(without +91): \n") print("These are the rooms that are currently available") print("1-Normal (500/Day)") print("2-Deluxe (1000/Day)") print("3-Super Deluxe (1500/Day)") print("4-Premium Deluxe (2000/Day)") - print("") - Room_Type = int(input("Which type you want(1-4): ")) - print("") - if Room_Type == 1: - x = 500 - Room_Type = "Normal" - elif Room_Type == 2: - x = 1000 - Room_Type = "Deluxe" - elif Room_Type == 3: - x = 1500 - Room_Type = "Super Deluxe" - elif Room_Type == 4: - x = 2000 - Room_Type = "Premium" + Room_Type = int(input("\nWhich type you want(1-4): \n")) + + match Room_Type: + case 1: + x = 500 + Room_Type = "Normal" + case 2: + x = 1000 + Room_Type = "Deluxe" + case 3: + x = 1500 + Room_Type = "Super Deluxe" + case 4: + x = 2000 + Room_Type = "Premium" Days = int(input("How many days you will stay: ")) Money = x * Days @@ -85,11 +87,10 @@ def add(): print("Online payment") print("") - File = open("Management.txt", "r") - string = File.read() - string = string.replace("'", '"') - dictionary = json.loads(string) - File.close() + with open("Management.txt", "r") as File: + string = File.read() + string = string.replace("'", '"') + dictionary = json.loads(string) if len(dictionary.get("Room")) == 0: Room_num = "501" @@ -114,12 +115,10 @@ def add(): dictionary["Price"].append(Money) dictionary["Room"].append(Room_num) - File = open("Management.txt", "w", encoding="utf-8") - File.write(str(dictionary)) - File.close() + with open("Management.txt", "w", encoding="utf-8") as File: + File.write(str(dictionary)) - print("") - print("Your data has been successfully added to our database.") + print("\nYour data has been successfully added to our database.") exit_menu() @@ -128,109 +127,83 @@ def add(): import json filecheck = os.path.isfile("Management.txt") -if filecheck == False: - File = open("Management.txt", "a", encoding="utf-8") - temp1 = { - "First_Name": [], - "Last_Name": [], - "Phone_num": [], - "Room_Type": [], - "Days": [], - "Price": [], - "Room": [], - } - File.write(str(temp1)) - File.close() +if not filecheck: + with open("Management.txt", "a", encoding="utf-8") as File: + temp1 = { + "First_Name": [], + "Last_Name": [], + "Phone_num": [], + "Room_Type": [], + "Days": [], + "Price": [], + "Room": [], + } + File.write(str(temp1)) def modify(): - File = open("Management.txt", "r") - string = File.read() - string = string.replace("'", '"') - dictionary = json.loads(string) - File.close() + with open("Management.txt", "r") as File: + string = File.read() + string = string.replace("'", '"') + dictionary = json.loads(string) dict_num = dictionary.get("Room") dict_len = len(dict_num) if dict_len == 0: - print("") - print("There is no data in our database") - print("") + print("\nThere is no data in our database\n") menu() else: - print("") - Room = input("Enter your Room Number: ") + Room = input("\nEnter your Room Number: ") listt = dictionary["Room"] index = int(listt.index(Room)) - print("") - print("1-Change your first name") + print("\n1-Change your first name") print("2-Change your last name") print("3-Change your phone number") - print("") - choice = input("Enter your choice: ") - print("") - - File = open("Management.txt", "w", encoding="utf-8") - - if choice == str(1): - user_input = input("Enter New First Name: ") - listt1 = dictionary["First_Name"] + choice = int(input("\nEnter your choice: ")) + print() + + with open("Management.txt", "w", encoding="utf-8") as File: + + match choice: + case 1: + category = "First_Name" + case 2: + category = "Last_Name" + case 3: + category = "Phone_num" + + user_input = input(f"Enter New {category.replace('_', ' ')}") + listt1 = dictionary[category] listt1[index] = user_input - dictionary["First_Name"] = None - dictionary["First_Name"] = listt1 - File.write(str(dictionary)) - File.close() + dictionary[category] = None + dictionary[category] = listt1 - elif choice == str(2): - user_input = input("Enter New Last Name: ") - listt1 = dictionary["Last_Name"] - listt1[index] = user_input - dictionary["Last_Name"] = None - dictionary["Last_Name"] = listt1 File.write(str(dictionary)) - File.close() - - elif choice == str(3): - user_input = input("Enter New Phone Number: ") - listt1 = dictionary["Phone_num"] - listt1[index] = user_input - dictionary["Phone_num"] = None - dictionary["Phone_num"] = listt1 - File.write(str(dictionary)) - File.close() - - print("") - print("Your data has been successfully updated") + print("\nYour data has been successfully updated") exit_menu() def search(): - File = open("Management.txt", "r") - string = File.read() - string = string.replace("'", '"') - dictionary = json.loads(string) - File.close() + with open("Management.txt") as File: + dictionary = json.loads(File.read().replace("'", '"')) dict_num = dictionary.get("Room") dict_len = len(dict_num) + if dict_len == 0: - print("") - print("There is no data in our database") - print("") + print("\nThere is no data in our database\n") menu() else: - print("") - Room = input("Enter your Room Number: ") - print("") + Room = input("\nEnter your Room Number: ") - listt = dictionary["Room"] - index = int(listt.index(Room)) + listt_num = dictionary.get("Room") + index = int(listt_num.index(Room)) listt_fname = dictionary.get("First_Name") listt_lname = dictionary.get("Last_Name") @@ -238,38 +211,29 @@ def search(): listt_type = dictionary.get("Room_Type") listt_days = dictionary.get("Days") listt_price = dictionary.get("Price") - listt_num = dictionary.get("Room") - print("") - print("First Name:", listt_fname[index]) - print("Last Name:", listt_lname[index]) - print("Phone number:", listt_phone[index]) - print("Room Type:", listt_type[index]) - print("Days staying:", listt_days[index]) - print("Money paid:", listt_price[index]) - print("Room Number:", listt_num[index]) + print(f"\nFirst Name: {listt_fname[index]}") + print(f"Last Name: {listt_lname[index]}") + print(f"Phone number: {listt_phone[index]}") + print(f"Room Type: {listt_type[index]}") + print(f"Days staying: {listt_days[index]}") + print(f"Money paid: {listt_price[index]}") + print(f"Room Number: {listt_num[index]}") exit_menu() def remove(): - File = open("Management.txt", "r") - string = File.read() - string = string.replace("'", '"') - dictionary = json.loads(string) - File.close() + with open("Management.txt") as File: + dictionary = json.loads(File.read().replace("'", '"')) dict_num = dictionary.get("Room") dict_len = len(dict_num) if dict_len == 0: - print("") - print("There is no data in our database") - print("") + print("\nThere is no data in our database\n") menu() else: - print("") - Room = input("Enter your Room Number: ") - print("") + Room = input("\nEnter your Room Number: ") listt = dictionary["Room"] index = int(listt.index(Room)) @@ -311,9 +275,8 @@ def remove(): dictionary["Room"] = None dictionary["Room"] = listt_num - file1 = open("Management.txt", "w", encoding="utf-8") - file1.write(str(dictionary)) - file1.close() + with open("Management.txt", "w", encoding="utf-8") as file1: + file1.write(str(dictionary)) print("Details has been removed successfully") @@ -322,18 +285,13 @@ def remove(): def view(): - File = open("Management.txt", "r") - string = File.read() - string = string.replace("'", '"') - dictionary = json.loads(string) - File.close() + with open("Management.txt") as File: + dictionary = json.loads(File.read().replace("'", '"')) dict_num = dictionary.get("Room") dict_len = len(dict_num) if dict_len == 0: - print("") - print("There is no data in our database") - print("") + print("\nThere is no data in our database\n") menu() else: