-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFinance.py
More file actions
163 lines (137 loc) · 6.33 KB
/
Copy pathFinance.py
File metadata and controls
163 lines (137 loc) · 6.33 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
import math
import datetime
import os
class Financial_Calculator:
def __init__(self):
self.users = {}
self.current_user = None
self.user_info = {}
def load_users_data(self):
if os.path.exists("users_data.txt"):
with open("users_data.txt", "r") as users_file:
lines = users_file.readlines()
for i in range(0, len(lines), 2):
username = lines[i].strip()
password = lines[i + 1].strip()
self.users[username] = password
def save_users_data(self):
with open("users_data.txt", "w") as users_file:
for username, password in self.users.items():
users_file.write(f"{username}\n{password}\n")
def register_user(self):
username = input("Enter your username: ")
while True:
password = input("Enter your password (at least 8 characters with one uppercase, one lowercase, and one digit): ")
if len(password) >= 8 and any(c.isupper() for c in password) and any(c.islower() for c in password) and any(c.isdigit() for c in password):
break
else:
print("Invalid password. Please make sure it is at least 8 characters long and meets the strength requirements.")
self.users[username] = password
print("Registration successful!")
self.save_users_data()
def login_user(self):
username = input("Enter your username: ")
password = input("Enter your password: ")
if username in self.users and self.users[username] == password:
self.current_user = username
print(f"Login successful, welcome {username}!")
self.load_user_info()
else:
print("Invalid username or password. Please try again.")
def load_user_info(self):
file_name = f"{self.current_user}_data.txt"
if os.path.exists(file_name):
with open(file_name, "r") as user_file:
lines = user_file.readlines()
for line in lines:
key, value = line.strip().split(": ")
nested_key, nested_value = value.split(", ")
self.user_info[key] = {nested_key: float(nested_value)}
def save_user_info(self):
file_name = f"{self.current_user}_data.txt"
with open(file_name, "w") as user_file:
for key, values in self.user_info.items():
user_file.write(f"{key}: {', '.join([f'{k}: {v}' for k, v in values.items()])}\n")
def log_activity(self, activity):
timestamp = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
with open("log.txt", "a") as log_file:
log_file.write(f"{timestamp} - {self.current_user}: {activity}\n")
def display_dashboard(self):
print(f"\n===== Welcome to the Dashboard, {self.current_user}! =====")
print("Choose an option:")
print("1. Calculate Investment")
print("2. Calculate Bond")
print("3. Logout")
def calculate_investment(self):
p = self.get_input("How much are you depositing? R", float)
r = self.get_input("At which interest rate percentile? ", float) / 100 / 12
t = self.get_input("How many years are you planning to invest for? ", float)
simp_comp = self.get_input("Choose 'Simple' or 'Compound' interest: ", str).lower()
if simp_comp == "simple":
total = p * (1 + r * t)
else:
total = p * math.pow((1 + r), t)
print(f"Your interest earned over {t} years will be R{total - p:.2f}")
print(f"Your total amount earned over {t} years will be R{total:.2f}")
self.user_info["Investment"] = {"Principal": p, "Interest Rate": r, "Time": t, "Type": simp_comp}
self.log_activity("Calculated Investment")
def calculate_bond(self):
p = self.get_input("What is the current value of the house? R", float)
i = self.get_input("At which interest rate percentile? ", float) / 100 / 12 / 12
n = self.get_input("How many months you plan to repay? ", float)
monthly = (i * p) / (1 - (1 + i) ** (-n))
total_repayment = monthly * n
print(f"Your monthly repayment will be R{monthly:.2f}")
print(f"Your total repayment will be R{total_repayment:.2f}")
self.user_info["Bond"] = {"Principal": p, "Interest Rate": i, "Months": n}
self.log_activity("Calculated Bond")
def logout_user(self):
self.log_activity("Logged Out")
self.save_user_info()
self.current_user = None
print("Logout successful.")
def get_input(self, prompt, data_type):
while True:
try:
user_input = data_type(input(prompt))
return user_input
except ValueError:
print("Error: Please enter the required info in the input field.")
def financial_calculator(self):
self.load_users_data()
while True:
if not self.current_user:
print("\n===== Welcome to the Financial Calculator! =====")
print("Choose an option:")
print("1. Register")
print("2. Login")
print("3. Exit")
choice = input()
if choice == '1':
self.register_user()
elif choice == '2':
self.login_user()
elif choice == '3':
print("Exiting the program. Goodbye!")
break
else:
print("Invalid choice. Please try again.")
else:
self.display_dashboard()
choice = input()
if choice == '1':
self.calculate_investment()
elif choice == '2':
self.calculate_bond()
elif choice == '3':
self.logout_user()
else:
print("Invalid choice. Please try again.")
restart = input("Do you want to restart the process? (y/n): ").lower()
if restart == "n":
print("Goodbye! Have a great day fam.")
self.save_user_info()
self.save_users_data()
break
calculator = Financial_Calculator()
calculator.financial_calculator()