Engineer-Areeb commited on
Commit
d01478c
·
verified ·
1 Parent(s): a7fcfe1

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +108 -0
app.py ADDED
@@ -0,0 +1,108 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
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")
64
+ owner = st.text_input("Enter Owner's Name")
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")
75
+ amount = st.number_input("Enter Amount", min_value=0.0)
76
+ if st.button("Deposit"):
77
+ account = st.session_state.bank.get_account(acc_num)
78
+ if account:
79
+ message = account.deposit(amount)
80
+ st.success(message)
81
+ else:
82
+ st.error("Account not found.")
83
+
84
+ elif menu == "Withdraw":
85
+ st.subheader("💸 Withdraw from Account")
86
+ acc_num = st.text_input("Enter Account Number")
87
+ amount = st.number_input("Enter Amount", min_value=0.0)
88
+ if st.button("Withdraw"):
89
+ account = st.session_state.bank.get_account(acc_num)
90
+ if account:
91
+ message = account.withdraw(amount)
92
+ st.success(message)
93
+ else:
94
+ st.error("Account not found.")
95
+
96
+ # ------------------------
97
+ # Display Account List
98
+ # ------------------------
99
+
100
+ st.divider()
101
+ st.subheader("📋 All Bank Accounts")
102
+ accounts = st.session_state.bank.get_all_accounts()
103
+
104
+ if accounts:
105
+ st.table(accounts)
106
+ else:
107
+ st.info("No accounts created yet.")
108
+