Bank_Account / app.py
Engineer-Areeb's picture
Update app.py
e074b0c verified
import streamlit as st
# BankAccount class using OOP
class BankAccount:
def __init__(self, account_id, name):
self.account_id = account_id
self.name = name
self.balance = 0
def deposit(self, amount):
if amount > 0:
self.balance += amount
return True
return False
def withdraw(self, amount):
if 0 < amount <= self.balance:
self.balance -= amount
return True
return False
# Store accounts in session state
if 'accounts' not in st.session_state:
st.session_state.accounts = {}
st.set_page_config(page_title="🏦 Bank App", layout="centered")
st.title("🏦 Bank Account Management System")
# Section 1: Create New Account
st.header("βž• Create New Account")
with st.form("create_account"):
new_id = st.text_input("Enter Account ID")
new_name = st.text_input("Enter Account Holder Name")
create_btn = st.form_submit_button("Create Account")
if create_btn:
if new_id in st.session_state.accounts:
st.warning("⚠️ Account ID already exists.")
elif not new_id or not new_name:
st.warning("⚠️ Please enter both ID and Name.")
else:
st.session_state.accounts[new_id] = BankAccount(new_id, new_name)
st.success(f"βœ… Account for {new_name} created with ID {new_id}.")
# Section 2: Deposit / Withdraw
st.header("πŸ’Έ Deposit / Withdraw Money")
with st.form("transaction"):
trans_id = st.text_input("Enter Account ID")
trans_type = st.radio("Choose Transaction Type", ["Deposit", "Withdraw"], horizontal=True)
trans_amount = st.number_input("Enter Amount (Rs.)", min_value=0, step=100)
trans_btn = st.form_submit_button("Submit Transaction")
if trans_btn:
account = st.session_state.accounts.get(trans_id)
if account:
if trans_type == "Deposit":
if account.deposit(trans_amount):
st.success(f"βœ… Deposited Rs. {trans_amount} to {account.name}'s account.")
else:
st.error("❌ Invalid deposit amount.")
elif trans_type == "Withdraw":
if account.withdraw(trans_amount):
st.success(f"βœ… Withdrew Rs. {trans_amount} from {account.name}'s account.")
else:
st.error("❌ Insufficient balance or invalid amount.")
else:
st.warning("⚠️ Account ID not found.")
# Section 3: Show All Accounts
st.header("πŸ“„ All Accounts")
if st.session_state.accounts:
account_data = {
"Account ID": [],
"Name": [],
"Balance (Rs.)": []
}
for acc in st.session_state.accounts.values():
account_data["Account ID"].append(acc.account_id)
account_data["Name"].append(acc.name)
account_data["Balance (Rs.)"].append(acc.balance)
st.table(account_data)
else:
st.info("ℹ️ No accounts created yet.")