Engineer-Areeb commited on
Commit
e074b0c
·
verified ·
1 Parent(s): 519dca4

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +69 -94
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, account_number, owner, balance=0.0):
9
- self.account_number = account_number
10
- self.owner = owner
11
- self.balance = balance
12
 
13
  def deposit(self, amount):
14
  if amount > 0:
15
  self.balance += amount
16
- return f"Deposited {amount} to {self.owner}'s account."
17
- return "Invalid deposit amount."
18
 
19
  def withdraw(self, amount):
20
  if 0 < amount <= self.balance:
21
  self.balance -= amount
22
- return f"Withdrew {amount} from {self.owner}'s account."
23
- return "Insufficient balance or invalid amount."
24
-
25
- def to_dict(self):
26
- return {
27
- "Account Number": self.account_number,
28
- "Owner": self.owner,
29
- "Balance": f"${self.balance:.2f}"
30
- }
31
-
32
- class Bank:
33
- def __init__(self):
34
- self.accounts = {}
35
-
36
- def create_account(self, account_number, owner):
37
- if account_number in self.accounts:
38
- return "Account number already exists."
39
- self.accounts[account_number] = BankAccount(account_number, owner)
40
- return f"Account created for {owner}."
41
-
42
- def get_account(self, account_number):
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.error("Account not found.")
84
-
85
- elif menu == "Withdraw":
86
- st.subheader("💸 Withdraw from Account")
87
- acc_num = st.text_input("Enter Account Number", key="withdraw_acc_num")
88
- amount = st.number_input("Enter Amount to Withdraw", min_value=0.0, key="withdraw_amt")
89
- if st.button("Withdraw"):
90
- bank = st.session_state.bank
91
- account = bank.get_account(acc_num)
 
 
 
 
92
  if account:
93
- message = account.withdraw(amount)
94
- st.success(message)
 
 
 
 
 
 
 
 
95
  else:
96
- st.error("Account not found.")
97
-
98
- # ------------------------
99
- # Display Account List
100
- # ------------------------
101
-
102
- st.divider()
103
- st.subheader("📋 All Bank Accounts")
104
- accounts = st.session_state.bank.get_all_accounts()
105
-
106
- if accounts:
107
- st.table(accounts)
 
 
 
 
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