nailasadia's picture
Rename app.py to appP.py
cfc4e6e verified
Raw History Blame Contribute Delete
7.67 kB
import streamlit as st
import json
import os
from datetime import datetime
st.set_page_config(page_title="ATM Simulator", layout="centered")
# Apply Soft Pastel Theme CSS
st.markdown("""
<style>
body {
background-color: #F6F5F5;
color: #2E2E2E;
}
.stButton > button {
background-color: #5EAAA8;
color: white;
border-radius: 8px;
padding: 0.5rem 1rem;
margin: 0.3rem;
border: none;
transition: all 0.3s ease;
}
.stButton > button:hover {
background-color: #056676;
transform: scale(1.05);
}
.stTextInput > div > input, .stNumberInput > div > input {
background-color: #fff;
color: #2E2E2E;
border: 1px solid #A3D2CA;
border-radius: 6px;
padding: 0.3rem;
}
.stTextInput > label, .stNumberInput > label {
color: #2E2E2E;
}
.stSelectbox > div {
background-color: #fff;
color: #2E2E2E;
border: 1px solid #A3D2CA;
}
</style>
""", unsafe_allow_html=True)
# User session state
def init_session():
if 'users' not in st.session_state:
st.session_state.users = {}
if 'logged_in' not in st.session_state:
st.session_state.logged_in = False
if 'current_user' not in st.session_state:
st.session_state.current_user = None
if 'history' not in st.session_state:
st.session_state.history = {}
# File handling for user data
USER_FILE = "users.json"
def load_users():
if os.path.exists(USER_FILE):
with open(USER_FILE, "r") as f:
st.session_state.users = json.load(f)
def save_users():
with open(USER_FILE, "w") as f:
json.dump(st.session_state.users, f, indent=4)
# Register user
def register_user():
st.subheader("Register")
username = st.text_input("Username", key="register_username")
pin = st.text_input("PIN", type="password", key="register_pin")
deposit = st.number_input("Initial Deposit", min_value=0.0, step=10.0, key="register_deposit")
if st.button("Register", key="register_button") or st.session_state.get("register_submit", False):
if username in st.session_state.users:
st.warning("Username already exists.")
else:
st.session_state.users[username] = {
"pin": pin,
"balance": deposit,
"transactions": [
{"type": "Initial Deposit", "amount": deposit, "date": datetime.now().strftime("%Y-%m-%d %H:%M:%S")}
]
}
save_users()
st.success("Registered successfully. Please log in.")
st.session_state.show_login = True
# Login user
def login_user():
st.subheader("Login")
username = st.text_input("Username", key="login_username")
pin = st.text_input("PIN", type="password", key="login_pin")
if st.button("Login", key="login_button") or st.session_state.get("login_submit", False):
if username in st.session_state.users and st.session_state.users[username]["pin"] == pin:
st.session_state.logged_in = True
st.session_state.current_user = username
else:
st.error("Invalid credentials.")
# Menu operations
def show_menu():
user = st.session_state.current_user
user_data = st.session_state.users[user]
st.markdown(f"### Welcome, {user}")
st.markdown(f"Your current balance is: ${user_data['balance']:.2f}")
col1, col2, col3 = st.columns(3)
with col1:
if st.button("Deposit", key="deposit_btn"):
st.session_state.operation = "deposit"
with col2:
if st.button("Withdraw", key="withdraw_btn"):
st.session_state.operation = "withdraw"
with col3:
if st.button("Change PIN", key="change_pin_btn"):
st.session_state.operation = "change_pin"
col4, col5, col6 = st.columns(3)
with col4:
if st.button("Transaction History", key="history_btn"):
st.session_state.operation = "history"
with col5:
if st.button("Export History", key="export_btn"):
export_history()
with col6:
if st.button("Log Out", key="logout_btn"):
st.session_state.logged_in = False
st.session_state.current_user = None
st.session_state.operation = None
# Deposit
def deposit():
st.subheader("Deposit")
amount = st.number_input("Enter amount to deposit:", min_value=0.0, step=10.0, key="deposit_amount")
if st.button("Submit Deposit", key="submit_deposit"):
st.session_state.users[st.session_state.current_user]["balance"] += amount
st.session_state.users[st.session_state.current_user]["transactions"].append({
"type": "Deposit", "amount": amount, "date": datetime.now().strftime("%Y-%m-%d %H:%M:%S")
})
save_users()
st.success(f"Deposited ${amount:.2f}")
if st.button("Back to Menu", key="back_deposit"):
st.session_state.operation = None
# Withdraw
def withdraw():
st.subheader("Withdraw")
amount = st.number_input("Enter amount to withdraw:", min_value=0.0, step=10.0, key="withdraw_amount")
if st.button("Submit Withdraw", key="submit_withdraw"):
balance = st.session_state.users[st.session_state.current_user]["balance"]
if amount > balance:
st.error("Insufficient funds.")
else:
st.session_state.users[st.session_state.current_user]["balance"] -= amount
st.session_state.users[st.session_state.current_user]["transactions"].append({
"type": "Withdraw", "amount": amount, "date": datetime.now().strftime("%Y-%m-%d %H:%M:%S")
})
save_users()
st.success(f"Withdrawal ${amount:.2f}")
if st.button("Back to Menu", key="back_withdraw"):
st.session_state.operation = None
# Change PIN
def change_pin():
st.subheader("Change PIN")
new_pin = st.text_input("Enter new PIN", type="password", key="new_pin")
if st.button("Update PIN", key="submit_pin"):
st.session_state.users[st.session_state.current_user]["pin"] = new_pin
save_users()
st.success("PIN updated successfully")
if st.button("Back to Menu", key="back_pin"):
st.session_state.operation = None
# View History
def transaction_history():
st.subheader("Transaction History")
history = st.session_state.users[st.session_state.current_user]["transactions"]
st.table(history)
if st.button("Back to Menu", key="back_history"):
st.session_state.operation = None
# Export history
def export_history():
history = st.session_state.users[st.session_state.current_user]["transactions"]
filename = f"{st.session_state.current_user}_history.txt"
with open(filename, "w") as f:
for entry in history:
f.write(f"{entry['date']} - {entry['type']}: ${entry['amount']:.2f}\n")
with open(filename, "r") as f:
st.download_button("Download History", data=f.read(), file_name=filename, key="download_button")
# App Start
init_session()
load_users()
if not st.session_state.logged_in:
if not st.session_state.get("show_login"):
register_user()
else:
login_user()
else:
if 'operation' not in st.session_state or st.session_state.operation is None:
show_menu()
elif st.session_state.operation == "deposit":
deposit()
elif st.session_state.operation == "withdraw":
withdraw()
elif st.session_state.operation == "change_pin":
change_pin()
elif st.session_state.operation == "history":
transaction_history()