SecureValutATM / app.py
Amrin's picture
Update app.py
0170736
import streamlit as st
import json
# Function to load user data from a file
@st.cache(allow_output_mutation=True)
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
@st.cache(allow_output_mutation=True)
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. ❌', "❌")