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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import json
|
| 3 |
+
import os
|
| 4 |
+
from datetime import datetime
|
| 5 |
+
import base64
|
| 6 |
+
|
| 7 |
+
# Set page config
|
| 8 |
+
st.set_page_config(page_title="ATM Simulator", layout="centered")
|
| 9 |
+
|
| 10 |
+
# File for storing user data
|
| 11 |
+
USER_FILE = "users.json"
|
| 12 |
+
|
| 13 |
+
# Load or initialize user data
|
| 14 |
+
def load_users():
|
| 15 |
+
if os.path.exists(USER_FILE):
|
| 16 |
+
with open(USER_FILE, "r") as f:
|
| 17 |
+
return json.load(f)
|
| 18 |
+
return {}
|
| 19 |
+
|
| 20 |
+
def save_users(users):
|
| 21 |
+
with open(USER_FILE, "w") as f:
|
| 22 |
+
json.dump(users, f, indent=4)
|
| 23 |
+
|
| 24 |
+
# Encrypt PIN (very basic)
|
| 25 |
+
def encrypt_pin(pin):
|
| 26 |
+
return base64.b64encode(pin.encode()).decode()
|
| 27 |
+
|
| 28 |
+
def decrypt_pin(enc):
|
| 29 |
+
return base64.b64decode(enc).decode()
|
| 30 |
+
|
| 31 |
+
# Initialize session state
|
| 32 |
+
if "page" not in st.session_state:
|
| 33 |
+
st.session_state.page = "register"
|
| 34 |
+
if "user" not in st.session_state:
|
| 35 |
+
st.session_state.user = None
|
| 36 |
+
|
| 37 |
+
# Frosty Ice CSS styling
|
| 38 |
+
st.markdown("""
|
| 39 |
+
<style>
|
| 40 |
+
div.stButton > button {
|
| 41 |
+
width: 100%;
|
| 42 |
+
background: linear-gradient(135deg, #c2e9fb, #a1c4fd);
|
| 43 |
+
color: black;
|
| 44 |
+
border: none;
|
| 45 |
+
border-radius: 10px;
|
| 46 |
+
padding: 0.5rem 1rem;
|
| 47 |
+
font-weight: bold;
|
| 48 |
+
transition: all 0.2s ease-in-out;
|
| 49 |
+
}
|
| 50 |
+
div.stButton > button:hover {
|
| 51 |
+
background: linear-gradient(135deg, #a1c4fd, #c2e9fb);
|
| 52 |
+
transform: scale(1.05);
|
| 53 |
+
}
|
| 54 |
+
.row {
|
| 55 |
+
display: flex;
|
| 56 |
+
gap: 1rem;
|
| 57 |
+
justify-content: center;
|
| 58 |
+
margin-bottom: 1rem;
|
| 59 |
+
}
|
| 60 |
+
.row button {
|
| 61 |
+
flex: 1;
|
| 62 |
+
max-width: 45%;
|
| 63 |
+
}
|
| 64 |
+
.center {
|
| 65 |
+
display: flex;
|
| 66 |
+
justify-content: center;
|
| 67 |
+
margin-top: 1rem;
|
| 68 |
+
}
|
| 69 |
+
.main-menu {
|
| 70 |
+
display: grid;
|
| 71 |
+
grid-template-columns: repeat(3, 30%);
|
| 72 |
+
gap: 1rem;
|
| 73 |
+
justify-content: center;
|
| 74 |
+
}
|
| 75 |
+
.styled-table {
|
| 76 |
+
border-collapse: collapse;
|
| 77 |
+
width: 100%;
|
| 78 |
+
margin-top: 10px;
|
| 79 |
+
}
|
| 80 |
+
.styled-table th, .styled-table td {
|
| 81 |
+
border: 1px solid #ddd;
|
| 82 |
+
padding: 8px;
|
| 83 |
+
}
|
| 84 |
+
.styled-table th {
|
| 85 |
+
background-color: #a1c4fd;
|
| 86 |
+
}
|
| 87 |
+
.header-row {
|
| 88 |
+
display: flex;
|
| 89 |
+
justify-content: space-between;
|
| 90 |
+
align-items: center;
|
| 91 |
+
}
|
| 92 |
+
</style>
|
| 93 |
+
""", unsafe_allow_html=True)
|
| 94 |
+
|
| 95 |
+
# Main Menu
|
| 96 |
+
def main_menu():
|
| 97 |
+
st.title("ATM Machine Simulator")
|
| 98 |
+
users = load_users()
|
| 99 |
+
balance = users[st.session_state.user]['balance']
|
| 100 |
+
|
| 101 |
+
st.markdown("""
|
| 102 |
+
<div class='header-row'>
|
| 103 |
+
<h4>Welcome, {}</h4>
|
| 104 |
+
<h4>Your current balance: ${:.2f}</h4>
|
| 105 |
+
</div>
|
| 106 |
+
<hr>
|
| 107 |
+
""".format(st.session_state.user, balance), unsafe_allow_html=True)
|
| 108 |
+
|
| 109 |
+
st.markdown("### Main Menu")
|
| 110 |
+
col1, col2, col3 = st.columns(3)
|
| 111 |
+
if col1.button("Deposit", key="menu_deposit"):
|
| 112 |
+
st.session_state.page = "deposit"
|
| 113 |
+
if col2.button("Withdraw", key="menu_withdraw"):
|
| 114 |
+
st.session_state.page = "withdraw"
|
| 115 |
+
if col3.button("Change PIN", key="menu_pin"):
|
| 116 |
+
st.session_state.page = "change_pin"
|
| 117 |
+
|
| 118 |
+
col4, col5, col6 = st.columns(3)
|
| 119 |
+
if col4.button("Transaction History", key="menu_history"):
|
| 120 |
+
st.session_state.page = "history"
|
| 121 |
+
if col5.button("Export History", key="menu_export"):
|
| 122 |
+
st.session_state.page = "export"
|
| 123 |
+
if col6.button("Logout", key="menu_logout"):
|
| 124 |
+
st.session_state.user = None
|
| 125 |
+
st.session_state.page = "login"
|
| 126 |
+
|
| 127 |
+
# Registration
|
| 128 |
+
def register_user():
|
| 129 |
+
st.title("Register")
|
| 130 |
+
username = st.text_input("Username", key="reg_user")
|
| 131 |
+
pin = st.text_input("4-digit PIN", type="password", key="reg_pin")
|
| 132 |
+
deposit = st.number_input("Initial Deposit", min_value=0.00, format="%.2f")
|
| 133 |
+
|
| 134 |
+
if st.button("Register", key="register_btn"):
|
| 135 |
+
users = load_users()
|
| 136 |
+
if username in users:
|
| 137 |
+
st.warning("Username already exists.")
|
| 138 |
+
return
|
| 139 |
+
if len(pin) != 4 or not pin.isdigit():
|
| 140 |
+
st.warning("PIN must be exactly 4 digits.")
|
| 141 |
+
return
|
| 142 |
+
users[username] = {
|
| 143 |
+
"pin": encrypt_pin(pin),
|
| 144 |
+
"balance": deposit,
|
| 145 |
+
"history": [{
|
| 146 |
+
"type": "Initial Deposit",
|
| 147 |
+
"amount": deposit,
|
| 148 |
+
"timestamp": datetime.now().strftime("%Y-%m-%d %H:%M:%S")
|
| 149 |
+
}]
|
| 150 |
+
}
|
| 151 |
+
save_users(users)
|
| 152 |
+
st.success("Registration successful!")
|
| 153 |
+
st.session_state.page = "login"
|
| 154 |
+
|
| 155 |
+
# Login
|
| 156 |
+
def login_user():
|
| 157 |
+
st.title("Login")
|
| 158 |
+
username = st.text_input("Username", key="login_user")
|
| 159 |
+
pin = st.text_input("PIN", type="password", key="login_pin")
|
| 160 |
+
if st.button("Login", key="login_btn"):
|
| 161 |
+
users = load_users()
|
| 162 |
+
if username in users and decrypt_pin(users[username]['pin']) == pin:
|
| 163 |
+
st.session_state.user = username
|
| 164 |
+
st.session_state.page = "menu"
|
| 165 |
+
else:
|
| 166 |
+
st.error("Invalid credentials")
|
| 167 |
+
|
| 168 |
+
# Deposit
|
| 169 |
+
def deposit_page():
|
| 170 |
+
st.title("ATM Machine Simulator")
|
| 171 |
+
st.subheader("Deposit Money")
|
| 172 |
+
amount = st.number_input("Enter amount", min_value=1.0, format="%.2f")
|
| 173 |
+
col1, col2 = st.columns(2)
|
| 174 |
+
if col1.button("Submit", key="deposit_submit"):
|
| 175 |
+
users = load_users()
|
| 176 |
+
users[st.session_state.user]['balance'] += amount
|
| 177 |
+
users[st.session_state.user]['history'].append({
|
| 178 |
+
"type": "Deposit",
|
| 179 |
+
"amount": amount,
|
| 180 |
+
"timestamp": datetime.now().strftime("%Y-%m-%d %H:%M:%S")
|
| 181 |
+
})
|
| 182 |
+
save_users(users)
|
| 183 |
+
st.success(f"Deposited ${amount:.2f}")
|
| 184 |
+
if col2.button("Main Menu", key="back_deposit"):
|
| 185 |
+
st.session_state.page = "menu"
|
| 186 |
+
if st.button("Logout", key="logout_deposit"):
|
| 187 |
+
st.session_state.user = None
|
| 188 |
+
st.session_state.page = "login"
|
| 189 |
+
|
| 190 |
+
# Withdraw
|
| 191 |
+
def withdraw_page():
|
| 192 |
+
st.title("ATM Machine Simulator")
|
| 193 |
+
st.subheader("Withdraw Money")
|
| 194 |
+
amount = st.number_input("Enter amount", min_value=1.0, format="%.2f")
|
| 195 |
+
col1, col2 = st.columns(2)
|
| 196 |
+
if col1.button("Submit", key="withdraw_submit"):
|
| 197 |
+
users = load_users()
|
| 198 |
+
if users[st.session_state.user]['balance'] >= amount:
|
| 199 |
+
users[st.session_state.user]['balance'] -= amount
|
| 200 |
+
users[st.session_state.user]['history'].append({
|
| 201 |
+
"type": "Withdraw",
|
| 202 |
+
"amount": amount,
|
| 203 |
+
"timestamp": datetime.now().strftime("%Y-%m-%d %H:%M:%S")
|
| 204 |
+
})
|
| 205 |
+
save_users(users)
|
| 206 |
+
st.success(f"Withdrew ${amount:.2f}")
|
| 207 |
+
else:
|
| 208 |
+
st.error("Insufficient funds.")
|
| 209 |
+
if col2.button("Main Menu", key="back_withdraw"):
|
| 210 |
+
st.session_state.page = "menu"
|
| 211 |
+
if st.button("Logout", key="logout_withdraw"):
|
| 212 |
+
st.session_state.user = None
|
| 213 |
+
st.session_state.page = "login"
|
| 214 |
+
|
| 215 |
+
# Change PIN
|
| 216 |
+
def change_pin():
|
| 217 |
+
st.title("ATM Machine Simulator")
|
| 218 |
+
st.subheader("Change PIN")
|
| 219 |
+
new_pin = st.text_input("Enter new 4-digit PIN", type="password")
|
| 220 |
+
col1, col2 = st.columns(2)
|
| 221 |
+
if col1.button("Submit", key="pin_submit"):
|
| 222 |
+
if len(new_pin) != 4 or not new_pin.isdigit():
|
| 223 |
+
st.error("PIN must be exactly 4 digits.")
|
| 224 |
+
else:
|
| 225 |
+
users = load_users()
|
| 226 |
+
users[st.session_state.user]['pin'] = encrypt_pin(new_pin)
|
| 227 |
+
save_users(users)
|
| 228 |
+
st.success("PIN changed successfully.")
|
| 229 |
+
if col2.button("Main Menu", key="back_pin"):
|
| 230 |
+
st.session_state.page = "menu"
|
| 231 |
+
if st.button("Logout", key="logout_pin"):
|
| 232 |
+
st.session_state.user = None
|
| 233 |
+
st.session_state.page = "login"
|
| 234 |
+
|
| 235 |
+
# Transaction History
|
| 236 |
+
def view_history():
|
| 237 |
+
st.title("ATM Machine Simulator")
|
| 238 |
+
st.subheader("Transaction History")
|
| 239 |
+
users = load_users()
|
| 240 |
+
history = users[st.session_state.user]["history"]
|
| 241 |
+
if not history:
|
| 242 |
+
st.info("No transaction history.")
|
| 243 |
+
else:
|
| 244 |
+
st.markdown('<table class="styled-table"><tr><th>Type</th><th>Amount</th><th>Timestamp</th></tr>', unsafe_allow_html=True)
|
| 245 |
+
for txn in history:
|
| 246 |
+
st.markdown(f"<tr><td>{txn['type']}</td><td>${txn['amount']:.2f}</td><td>{txn['timestamp']}</td></tr>", unsafe_allow_html=True)
|
| 247 |
+
st.markdown("</table>", unsafe_allow_html=True)
|
| 248 |
+
if st.button("Main Menu", key="back_history"):
|
| 249 |
+
st.session_state.page = "menu"
|
| 250 |
+
if st.button("Logout", key="logout_history"):
|
| 251 |
+
st.session_state.user = None
|
| 252 |
+
st.session_state.page = "login"
|
| 253 |
+
|
| 254 |
+
# Export History
|
| 255 |
+
def export_history():
|
| 256 |
+
st.title("ATM Machine Simulator")
|
| 257 |
+
st.subheader("Export Transaction History")
|
| 258 |
+
users = load_users()
|
| 259 |
+
history = users[st.session_state.user]["history"]
|
| 260 |
+
content = "Type,Amount,Timestamp\n" + "\n".join(
|
| 261 |
+
f"{txn['type']},{txn['amount']},{txn['timestamp']}" for txn in history
|
| 262 |
+
)
|
| 263 |
+
b64 = base64.b64encode(content.encode()).decode()
|
| 264 |
+
href = f'<a href="data:file/txt;base64,{b64}" download="transaction_history.txt">Download History</a>'
|
| 265 |
+
st.markdown(href, unsafe_allow_html=True)
|
| 266 |
+
if st.button("Main Menu", key="back_export"):
|
| 267 |
+
st.session_state.page = "menu"
|
| 268 |
+
if st.button("Logout", key="logout_export"):
|
| 269 |
+
st.session_state.user = None
|
| 270 |
+
st.session_state.page = "login"
|
| 271 |
+
|
| 272 |
+
# Render current screen
|
| 273 |
+
if st.session_state.page == "register":
|
| 274 |
+
register_user()
|
| 275 |
+
elif st.session_state.page == "login":
|
| 276 |
+
login_user()
|
| 277 |
+
elif st.session_state.page == "menu":
|
| 278 |
+
main_menu()
|
| 279 |
+
elif st.session_state.page == "deposit":
|
| 280 |
+
deposit_page()
|
| 281 |
+
elif st.session_state.page == "withdraw":
|
| 282 |
+
withdraw_page()
|
| 283 |
+
elif st.session_state.page == "change_pin":
|
| 284 |
+
change_pin()
|
| 285 |
+
elif st.session_state.page == "history":
|
| 286 |
+
view_history()
|
| 287 |
+
elif st.session_state.page == "export":
|
| 288 |
+
export_history()
|