Untitled

mail@pastecode.io avatar
unknown
plain_text
a month ago
12 kB
2
Indexable
Never
import json

conversionRates = {
    "USD": 50,
    "SAR": 13,
    "EGP": 1
}

try:
    with open("data.json", "r") as file:
        data = json.load(file)
except FileNotFoundError:
    print("Error: data.json file not found. Creating a new file.")
    data = {"users": []}
except json.JSONDecodeError:
    print("Error: Failed to parse JSON data. Initializing empty data.")
    data = {"users": []}

while True:
    try:
        print("     ******************************************Welcome to banat el3am bank management system")
        print("If you have an account please enter 'login'", "If you don't have an account enter 'register'", "To stop the project enter 'exit'", sep="\n")
        firstStep = input("Enter here: ").strip().lower()

        if firstStep == "exit":
            print("EXITING the project")
            break

        elif firstStep == "login":
            print("     ******************************************Welcome to LOGIN page")
            while True:
                try:
                    ID = input("Please enter your ID: ").strip()
                    if ID.isdigit():
                        ID = int(ID)
                        break
                    else:
                        print("Invalid input. Please enter a valid integer for ID.")
                except ValueError:
                    print("Invalid input. Please enter a numeric ID.")

            while True:
                try:
                    password = input("Please enter your password: ").strip()
                    if password.isdigit():
                        password = int(password)
                        break
                    else:
                        print("Invalid input. Please enter a valid integer for password.")
                except ValueError:
                    print("Invalid input. Please enter a numeric password.")

            userFound = False
            for user in data['users']:
                if user['ID'] == ID and user['Password'] == password:
                    userFound = True
                    print(f"Welcome back {user['Name']}")
                    break

            if not userFound:
                print("Your ID or your password is not correct. Please try again.")
                continue

            while True:
                try:
                    print("Choose an operation: deposit, withdraw, transfer, check, logout")
                    operation = input("Enter your operation: ").strip().lower()

                    if operation == "logout":
                        print("LOGGING OUT...")
                        break

                    elif operation == "deposit":
                        try:
                            amountCurrency = input("Enter the amount and currency: ")
                            amount, currency = amountCurrency.split(" ")
                            amount = float(amount)

                            if currency in conversionRates:
                                amountEGP = amount * conversionRates[currency]
                                user['Balance'] += amountEGP
                                with open("data.json", "w") as file:
                                    json.dump(data, file, indent=2)
                                print(f"Deposited {amount} {currency} = {amountEGP} EGP\nNew balance: {user['Balance']} EGP")
                            else:
                                print("Invalid currency.")
                        except ValueError:
                            print("Invalid amount format. Please enter the amount followed by the currency.")

                    elif operation == "withdraw":
                        try:
                            amountCurrency = input("Enter the amount and currency: ")
                            amount, currency = amountCurrency.split(" ")
                            amount = float(amount)

                            if currency in conversionRates:
                                amountEGP = amount * conversionRates[currency]
                                if user['Balance'] >= amountEGP:
                                    user['Balance'] -= amountEGP
                                    with open("data.json", "w") as file:
                                        json.dump(data, file, indent=2)
                                    print(f"Withdrew {amount} {currency} = {amountEGP} EGP\nNew balance: {user['Balance']} EGP")
                                else:
                                    print("Insufficient balance.")
                            else:
                                print("Invalid currency.")
                        except ValueError:
                            print("Invalid amount format. Please enter the amount followed by the currency.")

                    elif operation == "transfer":
                        try:
                            recipientID = input("Enter recipient ID: ").strip()
                            if recipientID.isdigit():
                                recipientID = int(recipientID)
                                recipient = None
                                for u in data['users']:
                                    if u['ID'] == recipientID:
                                        recipient = u
                                        break
                                if recipient:
                                    print(f"Amount will be transferred to {recipient['Name']}")
                                    check = input("Confirm or Enter another ID: ").strip().lower()
                                    if check == "confirm":
                                        amountCurrency = input("Enter the amount and currency: ")
                                        parts = amountCurrency.split(" ")
                                        if len(parts) == 2:
                                            amount, currency = parts
                                            if currency in conversionRates:
                                                amount = float(amount)
                                                if amount >= 0:
                                                    amountEGP = amount * conversionRates[currency]
                                                    if user['Balance'] >= amountEGP:
                                                        user['Balance'] -= amountEGP
                                                        recipient['Balance'] += amountEGP
                                                        with open("data.json", "w") as file:
                                                            json.dump(data, file, indent=2)
                                                        print(
                                                            f"Transferred {amount} {currency} ({amountEGP} EGP) to {recipient['Name']}\nYour new balance: {user['Balance']} EGP")
                                                    else:
                                                        print("Insufficient balance.")
                                                else:
                                                    print("Amount must be non-negative.")
                                            else:
                                                print("Invalid currency.")
                                        else:
                                            print("Invalid format. Please enter the amount followed by the currency.")
                                    else:
                                        print("Operation cancelled.")
                                else:
                                    print("Recipient ID not found.")
                            else:
                                print("Invalid ID. Please enter a valid numeric ID.")
                        except ValueError:
                            print("Invalid input. Please enter a valid number.")

                    elif operation == "check":
                        print(f"Name: {user['Name']}")
                        print(f"Phone Number: {user['Phone Number']}")
                        print(f"Email: {user['Mail']}")
                        print(f"Age: {user['Age']}")
                        print(f"City: {user['City']}")
                        print(f"Balance: {user['Balance']} EGP")
                        print(f"ID: {user['ID']}")

                    else:
                        print("Invalid operation. Please try again.")

                except ValueError as e:
                    print(f"Error: {e}")
                except Exception as e:
                    print(f"An unexpected error occurred: {e}")

        elif firstStep == "register":
            print("     ******************************************Welcome to REGISTER page")
            try:
                name = input("Please enter your name: ")

                while True:
                    phone_number = input("Please enter your phone number (11 digits): ")
                    if len(phone_number) == 11 and phone_number.isdigit():
                        break
                    else:
                        print("Invalid phone number. Please enter exactly 11 digits.")

                mail = input("Please enter your email: ")

                while True:
                    age = input("Please enter your age: ")
                    if age.isdigit():
                        age = int(age)
                        if age > 0:
                            break
                        else:
                            print("Age must be greater than 0. Please enter a valid age.")
                    else:
                        print("Invalid age. Please enter a valid integer value for age.")

                city = input("Please enter your city: ")

                while True:
                    try:
                        balance = input("Please enter your balance 'amount and currency': ")
                        amount, currency = balance.split(" ")
                        if currency in conversionRates:
                            amount = float(amount)
                            if amount >= 0:
                                balance_in_egp = amount * conversionRates[currency]
                                break
                            else:
                                print("Amount must be non-negative.")
                        else:
                            print("Invalid currency.")
                    except ValueError:
                        print("Invalid input. Please enter a valid number and currency.")

                while True:
                    password = input("Please enter your password: ").strip()
                    if password.isdigit():
                        password = int(password)
                        break
                    else:
                        print("Invalid input. Please enter a valid integer for password.")

                maxId = max(user["ID"] for user in data["users"]) if data["users"] else 0
                newID = maxId + 1
                newUser = {
                    "Name": name,
                    "Phone Number": phone_number,
                    "Mail": mail,
                    "Age": age,
                    "City": city,
                    "Balance": balance_in_egp,
                    "Password": password,
                    "ID": newID
                }
                data['users'].append(newUser)
                with open("data.json", "w") as file:
                    json.dump(data, file, indent=2)
                print("Registration successful! You can now log in.", end="...")
                print(f"Your ID is {newID}")

            except ValueError as e:
                print(f"Error: {e}")
            except Exception as e:
                print(f"An unexpected error occurred: {e}")

        else:
            print("Invalid option. Please enter 'login' or 'register' or 'exit'.")

    except Exception as e:
        print(f"An unexpected error occurred in the main menu: {e}")
Leave a Comment