Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,110 +1,85 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
|
| 3 |
-
#
|
| 4 |
-
# Bank Account OOP Section
|
| 5 |
-
# ------------------------
|
| 6 |
-
|
| 7 |
class BankAccount:
|
| 8 |
-
def __init__(self,
|
| 9 |
-
self.
|
| 10 |
-
self.
|
| 11 |
-
self.balance =
|
| 12 |
|
| 13 |
def deposit(self, amount):
|
| 14 |
if amount > 0:
|
| 15 |
self.balance += amount
|
| 16 |
-
return
|
| 17 |
-
return
|
| 18 |
|
| 19 |
def withdraw(self, amount):
|
| 20 |
if 0 < amount <= self.balance:
|
| 21 |
self.balance -= amount
|
| 22 |
-
return
|
| 23 |
-
return
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
return self.accounts.get(account_number)
|
| 44 |
-
|
| 45 |
-
def get_all_accounts(self):
|
| 46 |
-
return [acc.to_dict() for acc in self.accounts.values()]
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
# ------------------------
|
| 50 |
-
# Streamlit UI Section
|
| 51 |
-
# ------------------------
|
| 52 |
-
|
| 53 |
-
# Initialize session state for bank object
|
| 54 |
-
if "bank" not in st.session_state:
|
| 55 |
-
st.session_state.bank = Bank()
|
| 56 |
-
|
| 57 |
-
st.title("🏦 Simple Bank Account Manager")
|
| 58 |
-
|
| 59 |
-
menu = st.sidebar.radio("Select Action", ["Create Account", "Deposit", "Withdraw"])
|
| 60 |
-
|
| 61 |
-
if menu == "Create Account":
|
| 62 |
-
st.subheader("🧾 Create New Bank Account")
|
| 63 |
-
acc_num = st.text_input("Enter Account Number", key="create_acc_num")
|
| 64 |
-
owner = st.text_input("Enter Owner's Name", key="create_owner")
|
| 65 |
-
if st.button("Create Account"):
|
| 66 |
-
if acc_num and owner:
|
| 67 |
-
message = st.session_state.bank.create_account(acc_num, owner)
|
| 68 |
-
st.success(message)
|
| 69 |
-
else:
|
| 70 |
-
st.warning("Please fill all fields.")
|
| 71 |
-
|
| 72 |
-
elif menu == "Deposit":
|
| 73 |
-
st.subheader("💰 Deposit to Account")
|
| 74 |
-
acc_num = st.text_input("Enter Account Number", key="deposit_acc_num")
|
| 75 |
-
amount = st.number_input("Enter Amount to Deposit", min_value=0.0, key="deposit_amt")
|
| 76 |
-
if st.button("Deposit"):
|
| 77 |
-
bank = st.session_state.bank
|
| 78 |
-
account = bank.get_account(acc_num)
|
| 79 |
-
if account:
|
| 80 |
-
message = account.deposit(amount)
|
| 81 |
-
st.success(message)
|
| 82 |
else:
|
| 83 |
-
st.
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 92 |
if account:
|
| 93 |
-
|
| 94 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 95 |
else:
|
| 96 |
-
st.
|
| 97 |
-
|
| 98 |
-
#
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 108 |
else:
|
| 109 |
-
st.info("No accounts created yet.")
|
|
|
|
| 110 |
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
|
| 3 |
+
# BankAccount class using OOP
|
|
|
|
|
|
|
|
|
|
| 4 |
class BankAccount:
|
| 5 |
+
def __init__(self, account_id, name):
|
| 6 |
+
self.account_id = account_id
|
| 7 |
+
self.name = name
|
| 8 |
+
self.balance = 0
|
| 9 |
|
| 10 |
def deposit(self, amount):
|
| 11 |
if amount > 0:
|
| 12 |
self.balance += amount
|
| 13 |
+
return True
|
| 14 |
+
return False
|
| 15 |
|
| 16 |
def withdraw(self, amount):
|
| 17 |
if 0 < amount <= self.balance:
|
| 18 |
self.balance -= amount
|
| 19 |
+
return True
|
| 20 |
+
return False
|
| 21 |
+
|
| 22 |
+
# Store accounts in session state
|
| 23 |
+
if 'accounts' not in st.session_state:
|
| 24 |
+
st.session_state.accounts = {}
|
| 25 |
+
|
| 26 |
+
st.set_page_config(page_title="🏦 Bank App", layout="centered")
|
| 27 |
+
st.title("🏦 Bank Account Management System")
|
| 28 |
+
|
| 29 |
+
# Section 1: Create New Account
|
| 30 |
+
st.header("➕ Create New Account")
|
| 31 |
+
with st.form("create_account"):
|
| 32 |
+
new_id = st.text_input("Enter Account ID")
|
| 33 |
+
new_name = st.text_input("Enter Account Holder Name")
|
| 34 |
+
create_btn = st.form_submit_button("Create Account")
|
| 35 |
+
if create_btn:
|
| 36 |
+
if new_id in st.session_state.accounts:
|
| 37 |
+
st.warning("⚠️ Account ID already exists.")
|
| 38 |
+
elif not new_id or not new_name:
|
| 39 |
+
st.warning("⚠️ Please enter both ID and Name.")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
else:
|
| 41 |
+
st.session_state.accounts[new_id] = BankAccount(new_id, new_name)
|
| 42 |
+
st.success(f"✅ Account for {new_name} created with ID {new_id}.")
|
| 43 |
+
|
| 44 |
+
# Section 2: Deposit / Withdraw
|
| 45 |
+
st.header("💸 Deposit / Withdraw Money")
|
| 46 |
+
with st.form("transaction"):
|
| 47 |
+
trans_id = st.text_input("Enter Account ID")
|
| 48 |
+
trans_type = st.radio("Choose Transaction Type", ["Deposit", "Withdraw"], horizontal=True)
|
| 49 |
+
trans_amount = st.number_input("Enter Amount (Rs.)", min_value=0, step=100)
|
| 50 |
+
trans_btn = st.form_submit_button("Submit Transaction")
|
| 51 |
+
|
| 52 |
+
if trans_btn:
|
| 53 |
+
account = st.session_state.accounts.get(trans_id)
|
| 54 |
if account:
|
| 55 |
+
if trans_type == "Deposit":
|
| 56 |
+
if account.deposit(trans_amount):
|
| 57 |
+
st.success(f"✅ Deposited Rs. {trans_amount} to {account.name}'s account.")
|
| 58 |
+
else:
|
| 59 |
+
st.error("❌ Invalid deposit amount.")
|
| 60 |
+
elif trans_type == "Withdraw":
|
| 61 |
+
if account.withdraw(trans_amount):
|
| 62 |
+
st.success(f"✅ Withdrew Rs. {trans_amount} from {account.name}'s account.")
|
| 63 |
+
else:
|
| 64 |
+
st.error("❌ Insufficient balance or invalid amount.")
|
| 65 |
else:
|
| 66 |
+
st.warning("⚠️ Account ID not found.")
|
| 67 |
+
|
| 68 |
+
# Section 3: Show All Accounts
|
| 69 |
+
st.header("📄 All Accounts")
|
| 70 |
+
if st.session_state.accounts:
|
| 71 |
+
account_data = {
|
| 72 |
+
"Account ID": [],
|
| 73 |
+
"Name": [],
|
| 74 |
+
"Balance (Rs.)": []
|
| 75 |
+
}
|
| 76 |
+
for acc in st.session_state.accounts.values():
|
| 77 |
+
account_data["Account ID"].append(acc.account_id)
|
| 78 |
+
account_data["Name"].append(acc.name)
|
| 79 |
+
account_data["Balance (Rs.)"].append(acc.balance)
|
| 80 |
+
|
| 81 |
+
st.table(account_data)
|
| 82 |
else:
|
| 83 |
+
st.info("ℹ️ No accounts created yet.")
|
| 84 |
+
|
| 85 |
|