-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuser.py
50 lines (35 loc) · 1.43 KB
/
user.py
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
class User:
def __init__(self, name):
with open(name, 'r') as file:
file_data = file.read()
user_details = file_data.split('\n')
self.name = user_details[0]
self.age = int(user_details[2])
self.gender = user_details[3]
self.balance = float(user_details[4])
def update_balance(self, deposit):
self.balance += deposit
with open(self.name, "r+") as file:
file_data = file.read()
details = file_data.split('\n')
current_balance = details[4]
updated_balance = str(self.balance)
file_data = file_data.replace(current_balance, updated_balance)
file.seek(0)
file.truncate(0)
file.write(file_data)
file.close()
return self.balance
def withdraw(self, amount):
self.balance -= amount
with open(self.name, "r+") as file:
file_data = file.read()
details = file_data.split('\n')
current_balance = details[4]
updated_balance = str(self.balance)
file_data = file_data.replace(current_balance, updated_balance)
file.seek(0)
file.truncate(0)
file.write(file_data)
file.close()
return self.balance