-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpython.py
More file actions
209 lines (192 loc) · 7.35 KB
/
Copy pathpython.py
File metadata and controls
209 lines (192 loc) · 7.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
import os
import json
import time
# Constants
DATA_FILE = "bank_data.json"
LINE_WIDTH = 70
INVESTMENT_RATE = 0.05 # Fixed interest rate of 5%
# Utility Functions
def separator(symbol="=", length=LINE_WIDTH):
print(symbol * length)
def fancy_header(title):
separator("*")
print(f"{title.center(LINE_WIDTH)}")
separator("*")
def loading_animation(message="Processing"):
for _ in range(3):
print(f"{message}{'.' * _}", end="\r")
time.sleep(0.5)
print(" " * 30, end="\r") # Clear the line
def slow_print(message, delay=0.05):
for char in message:
print(char, end="", flush=True)
time.sleep(delay)
print()
def load_data():
if os.path.exists(DATA_FILE):
with open(DATA_FILE, 'r') as file:
return json.load(file)
return {}
def save_data(data):
with open(DATA_FILE, 'w') as file:
json.dump(data, file, indent=4)
def validate_positive_number(prompt):
while True:
try:
value = float(input(prompt))
if value > 0:
return value
else:
print("⚠️ Error: Enter a positive number!")
except ValueError:
print("⚠️ Error: Invalid input. Please enter a numeric value!")
def validate_account_number(prompt, existing_accounts=None):
while True:
account_number = input(prompt).strip()
if account_number.isdigit() and len(account_number) == 10:
if existing_accounts and account_number in existing_accounts:
print("⚠️ Error: Account number already exists!")
else:
return account_number
else:
print("⚠️ Error: Account number must be a 10-digit numeric value.")
def authenticate_user(data, account_number):
if account_number not in data:
slow_print("⚠️ Error: Account not found!")
return False
for attempt in range(3):
entered_password = input("🔒 Enter account password: ")
if entered_password == data[account_number]["password"]:
slow_print("✅ Access granted!")
return True
else:
print(f"⚠️ Incorrect password! {2 - attempt} attempts remaining.")
slow_print("❌ Access denied! Returning to the main menu.")
return False
def pause(message="Press Enter to continue..."):
"""Pause to allow users to read messages."""
input(message)
# Core Banking Functions
def create_account(data):
fancy_header("CREATE ACCOUNT")
account_number = validate_account_number("Enter a 10-digit account number: ", data)
name = input("Enter account holder's full name: ").strip()
password = input("Set a password for this account: ")
initial_deposit = validate_positive_number("Enter initial deposit amount: ")
data[account_number] = {
"name": name,
"password": password,
"balance": initial_deposit,
"transactions": [{"type": "Deposit", "amount": initial_deposit, "time": time.ctime()}],
"investments": []
}
slow_print(f"✅ Account created successfully for {name}!")
save_data(data)
def view_account(data):
fancy_header("VIEW ACCOUNT")
account_number = input("Enter account number: ").strip()
if not authenticate_user(data, account_number):
return
account = data[account_number]
print(f"\n📄 Account Details")
print (f"Account Number: {account_number}")
print(f"Account Holder : {account['name']}")
print(f"Balance : ₹{account['balance']:.2f}\n")
print("🧾 Transaction History:")
separator("-")
for txn in account['transactions']:
print(f"{txn['type']:<10} ₹{txn['amount']:.2f} | Date: {txn['time']}")
separator("-")
def deposit_money(data):
fancy_header("DEPOSIT MONEY")
account_number = input("Enter account number: ").strip()
if not authenticate_user(data, account_number):
return
amount = validate_positive_number("Enter amount to deposit: ")
data[account_number]['balance'] += amount
data[account_number]['transactions'].append({"type": "Deposit", "amount": amount, "time": time.ctime()})
loading_animation("Updating balance")
slow_print(f"✅ Deposit successful! New balance: ₹{data[account_number]['balance']:.2f}")
save_data(data)
def withdraw_money(data):
fancy_header("WITHDRAW MONEY")
account_number = input("Enter account number: ").strip()
if not authenticate_user(data, account_number):
return
amount = validate_positive_number("Enter amount to withdraw: ")
if amount > data[account_number]['balance']:
slow_print("⚠️ Error: Insufficient balance!")
return
data[account_number]['balance'] -= amount
data[account_number]['transactions'].append({"type": "Withdrawal", "amount": amount, "time": time.ctime()})
loading_animation("Processing withdrawal")
slow_print(f"✅ Withdrawal successful! New balance: ₹{data[account_number]['balance']:.2f}")
save_data(data)
# Investment Functions
def invest_money(data):
fancy_header("INVEST MONEY")
account_number = input("Enter account number: ").strip()
if not authenticate_user(data, account_number):
return
amount = validate_positive_number("Enter amount to invest: ")
if amount > data[account_number]['balance']:
slow_print("⚠️ Error: Insufficient balance to invest!")
return
investment = {
"amount": amount,
"rate": INVESTMENT_RATE,
"start_time": time.ctime()
}
data[account_number]['balance'] -= amount
data[account_number]['investments'].append(investment)
loading_animation("Processing investment")
slow_print(f"✅ Successfully invested ₹{amount:.2f} at a rate of {INVESTMENT_RATE * 100:.1f}%!")
save_data(data)
def view_investments(data):
fancy_header("VIEW INVESTMENTS")
account_number = input("Enter account number: ").strip()
if not authenticate_user(data, account_number):
return
investments = data[account_number]['investments']
if not investments:
slow_print("⚠️ No active investments!")
return
print(f"📈 Active investments for account {account_number}:")
separator("-")
for i, inv in enumerate(investments, start=1):
print(f"{i}. Amount: ₹{inv['amount']:.2f}, Rate: {inv['rate'] * 100:.1f}%, Start Date: {inv['start_time']}")
separator("-")
# Main Menu
def main_menu():
data = load_data()
while True:
fancy_header("BANK MANAGEMENT SYSTEM")
print("1️ Create Account")
print("2️ View Account")
print("3️ Deposit Money")
print("4️ Withdraw Money")
print("5️ Invest Money")
print("6️ View Investments")
print("7️ Exit")
separator()
choice = input("Enter your choice: ").strip()
if choice == '1':
create_account(data)
elif choice == '2':
view_account(data)
elif choice == '3':
deposit_money(data)
elif choice == '4':
withdraw_money(data)
elif choice == '5':
invest_money(data)
elif choice == '6':
view_investments(data)
elif choice == '7':
slow_print("Thank you for using the HA Bank Management System! Goodbye!")
break
else:
slow_print("⚠️ Error: Invalid choice, please try again!")
pause()
if __name__ == "__main__":
main_menu