Spaces:
Sleeping
Sleeping
import streamlit as st | |
import json | |
# Function to load user data from a file | |
def load_user_data(): | |
try: | |
with open("user_data.json", "r") as file: | |
data = json.load(file) | |
return data | |
except FileNotFoundError: | |
return {} | |
# Function to save user data to a file | |
def save_user_data(data): | |
with open("user_data.json", "w") as file: | |
json.dump(data, file) | |
# Function to display a separator line | |
def display_separator(): | |
st.write('-' * 50) | |
# Function to display a welcome message | |
def display_welcome_message(): | |
st.write("Welcome to the ATM MACHINE") | |
# Function to display a message with a border | |
def display_message(message, emoji="π"): | |
display_separator() | |
st.write(f"{emoji} {message} {emoji}") | |
display_separator() | |
# Function to display the user's balance | |
def display_balance(user_data): | |
display_separator() | |
st.write(f"π° Current Balance: {user_data['amount']} RUPEES π°") | |
display_separator() | |
# Function to display the main menu options | |
def display_menu(): | |
st.write('\nSelect an option:') | |
st.write('1. View Balance (B)') | |
st.write('2. Withdraw Cash (W)') | |
st.write('3. Deposit Cash (D)') | |
st.write('4. Change PIN (P)') | |
st.write('5. Quit (Q)') | |
# Function to handle balance inquiry | |
def view_balance(user_data): | |
display_balance(user_data) | |
# Function to handle cash withdrawal | |
def withdraw_cash(user_data): | |
amount_to_withdraw = st.number_input('Enter the amount to withdraw:', step=10, format='%d') | |
if amount_to_withdraw % 10 != 0: | |
display_message('π‘ Amount must be in multiples of 10 π‘', "πΈ") | |
elif amount_to_withdraw > user_data['amount']: | |
display_message('β Insufficient balance β', "πΈ") | |
else: | |
user_data['amount'] -= amount_to_withdraw | |
save_user_data(user_data) | |
display_balance(user_data) | |
display_message(f'πΈ Withdrawal successful: {amount_to_withdraw} RUPEES πΈ', "πΈ") | |
# Function to handle cash deposit | |
def deposit_cash(user_data): | |
amount_to_deposit = st.number_input('Enter the amount to deposit:', step=10, format='%d') | |
if amount_to_deposit % 10 != 0: | |
display_message('π‘ Amount must be in multiples of 10 π‘', "πΈ") | |
else: | |
user_data['amount'] += amount_to_deposit | |
save_user_data(user_data) | |
display_balance(user_data) | |
display_message(f'πΈ Deposit successful: {amount_to_deposit} RUPEES πΈ', "πΈ") | |
# Function to handle PIN change | |
def change_pin(user_data): | |
new_pin = st.text_input('Enter a new PIN:', type='password') | |
confirm_pin = st.text_input('Confirm new PIN:', type='password') | |
if new_pin == confirm_pin: | |
user_data['pin'] = new_pin | |
save_user_data(user_data) | |
display_message('π PIN successfully changed π', "π") | |
else: | |
display_message('β PINs do not match. Please try again. β', "β") | |
# Main Program | |
display_welcome_message() | |
# Input user name | |
name = st.text_input('Enter your name:').lower() | |
user_data = load_user_data() | |
if user_data and name in user_data: | |
# Existing user | |
pass | |
else: | |
# New user; create an entry with default values | |
user_data[name] = {'pin': '', 'amount': 0} | |
save_user_data(user_data) | |
display_message(f'Welcome, {name.capitalize()}! You are a new user.') | |
# If the user is new, prompt to create a new PIN | |
if not user_data[name]['pin']: | |
new_pin = st.text_input('Create a new PIN:', type='password') | |
user_data[name]['pin'] = new_pin | |
save_user_data(user_data) | |
display_message('PIN created successfully!') | |
# comparing pin | |
count = 0 | |
while count < 3: | |
display_separator() | |
pin = st.text_input('Please enter your PIN:', type='password') | |
if pin.isdigit() and pin == user_data[name]['pin']: | |
break | |
else: | |
count += 1 | |
display_message('β Invalid PIN. Please try again. β', "β") | |
# in case of a valid pin - continuing, or exiting | |
if count == 3: | |
display_message('β 3 UNSUCCESSFUL PIN ATTEMPTS, EXITING. YOUR CARD HAS BEEN LOCKED β', "β") | |
st.stop() | |
display_message(f'π LOGIN SUCCESSFUL, CONTINUE {name.capitalize()}! π', "π") | |
# Main menu | |
while True: | |
display_menu() | |
response = st.selectbox('Select an option:', ['View Balance (B)', 'Withdraw Cash (W)', 'Deposit Cash (D)', 'Change PIN (P)', 'Quit (Q)']) | |
if response == 'View Balance (B)': | |
view_balance(user_data) | |
elif response == 'Withdraw Cash (W)': | |
withdraw_cash(user_data) | |
elif response == 'Deposit Cash (D)': | |
deposit_cash(user_data) | |
elif response == 'Change PIN (P)': | |
change_pin(user_data) | |
elif response == 'Quit (Q)': | |
save_user_data(user_data) | |
display_message('Thank you for using our ATM. Have a great day!') | |
st.stop() | |
else: | |
display_message('β Invalid response. Please try again. β', "β") | |