-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdata_manager.py
More file actions
74 lines (66 loc) · 2.04 KB
/
data_manager.py
File metadata and controls
74 lines (66 loc) · 2.04 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
# imports
import json
import os
# define data file path
data_file = os.path.join(os.path.dirname(__file__), 'accounts.json')
# load data file
def load_accounts():
if not os.path.exists(data_file):
return {}
with open(data_file, 'r') as file:
return json.load(file)
# save accounts for signup
def save_accounts(accounts):
with open(data_file, 'w') as file:
json.dump(accounts, file)
# signup function
def signup(username, password, name):
accounts = load_accounts()
if username in accounts:
return False
accounts[username] = {
'password': password,
'name': name,
'balance': 0.0
}
save_accounts(accounts)
return True
# login function
def login(username, password):
accounts = load_accounts()
if username in accounts and accounts[username]['password'] == password:
return accounts[username]
return None
# load account data
def get_account(username):
accounts = load_accounts()
return accounts.get(username)
#transaction function
def transaction(username, receiver, amount):
accounts = load_accounts()
#cannot to self
if username == receiver:
return False, "You cannot transfer to yourself."
#receiver not found
if username not in accounts or receiver not in accounts:
return False, "Receiver does not have an account."
#insufficient balance
if accounts[username]['balance'] < amount:
return False, "Insufficient balance."
accounts[username]['balance'] -= amount
accounts[receiver]['balance'] += amount
save_accounts(accounts)
return True, "Transaction successful."
# topup function
def topup(username, amount):
accounts = load_accounts()
# sanity check for fatal errors
if username not in accounts:
return False, "User does not exist."
# amount must be positive
if amount <= 0:
return False, "Amount must be positive."
# logic
accounts[username]['balance'] += amount
save_accounts(accounts)
return True, f"Successfully topped up {amount}!"