-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathuser.py
96 lines (70 loc) · 3.06 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
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
#!/usr/bin/python
# -*- coding: utf-8 -*-
import math
import logging
# Logger setup
logger = logging.getLogger(__name__)
logging.basicConfig(level=logging.CRITICAL)
class User(object):
""" A user class that can send and receive money on a ledger """
def __init__(self, name, ledger):
self._name = name
self._credit = 0 # used to populate the first user
self._ledger = ledger
def send(self, amount, recipient):
""" Send money on the ledger if the user has enough to send """
try:
if self.is_balance_gt_0(amount):
# add to the ledger
self._ledger.add_transaction(
self.name(), recipient.name(), amount)
except Exception as e:
logger.exception(
'Unable to determine if user has enough monies to send')
raise e
def is_balance_gt_0(self, amount):
""" Return true if the current balance is greater than 0 """
try:
# if you are the Anon account, you are okay
if self._credit - amount > 0 and self.name() == 'Anon':
return True
# if you have enough money to send you are okay
elif self._ledger.transactions_by_user[self.name()]['running_balance'][-1] + amount >= 0:
return True
else:
return False
except Exception as e:
logger.exception('Ledger does not have a running balance for user')
raise e
def increase_credit(self, amount):
""" Increase the credit of a user """
# this is only used for the Anon account
self._credit += amount
def name(self):
""" Return the name of the user """
return self._name
def total_credit(self):
""" Return the total credit a user has / received """
return self._ledger.transactions_by_user[self.name()]['received_totals']
def total_debit(self):
""" Return the total debit / money owed for a user """
return self._ledger.transactions_by_user[self.name()]['sent_totals']
def total_value(self):
""" Return the balance for a user """
sent = self._ledger.transactions_by_user[self.name()]['sent_totals']
received = self._ledger.transactions_by_user[self.name(
)]['received_totals']
return sent - received
def total_transaction(self):
""" Return the total transactions a user was involved with """
sent_total = self._ledger.transactions_by_user[self.name(
)]['sent_transactions']
received_total = self._ledger.transactions_by_user[self.name(
)]['received_transactions']
return sent_total + received_total
def total_credit_transaction(self):
""" Return the total transactions a user received """
return self._ledger.transactions_by_user[self.name()]['received_transactions']
def total_debit_transaction(self):
""" Return the total sent transactions for a user """
return self._ledger.transactions_by_user[self.name()]['sent_transactions']