-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtransactions.py
101 lines (80 loc) · 3.53 KB
/
transactions.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
97
98
99
100
101
import bank_account
import threading
from event_scheduler import EventScheduler
# Instantiating the event scheduler with the name
# EventScheduler(thread_name)
scheduler = EventScheduler("transaction_threads")
# Scheduler has been started and is able to take actions
scheduler.start()
account = bank_account.BankAccount(100)
def is_transaction_successful(successful, location):
if successful:
print(location + " ATM Transaction Successful")
else:
print(location + " ATM Transaction Failed")
def atm_chicago_transactions(transactions):
for transaction in transactions:
delay, priority, amount = transaction
if amount < 0:
# scheduler.enter(delay, priority, argument=(), kwargs={})
scheduler.enter(delay, priority, account.withdraw, [amount, is_transaction_successful, "Chicago"])
else:
# scheduler.enter(delay, priority, argument=(), kwargs={})
scheduler.enter(delay, priority, account.deposit, [amount, is_transaction_successful, "Chicago"])
def atm_los_angeles_transactions(transactions):
for transaction in transactions:
delay, priority, amount = transaction
if amount < 0:
# scheduler.enter(delay, priority, argument=(), kwargs={})
scheduler.enter(delay, priority, account.withdraw,
[amount, is_transaction_successful, "Los Angeles"])
else:
# scheduler.enter(delay, priority, argument=(), kwargs={})
scheduler.enter(delay, priority, account.deposit,
[amount, is_transaction_successful, "Los Angeles"])
# Current balance before transactions: 100
# Example 1: Los Angeles has higher priority it will execute first
thread_atm_chicago = threading.Thread(target=atm_chicago_transactions, args=[[(1, 1, -90)]], name="ATM Chicago")
thread_atm_los_angeles = threading.Thread(
target=atm_los_angeles_transactions, args=[[(1, 0, -20)]], name="ATM Los Angeles")
thread_atm_chicago.start()
thread_atm_los_angeles.start()
thread_atm_chicago.join()
thread_atm_los_angeles.join()
'''
Los Angeles ATM Transaction Successful
You have withdrawn: 20
The new balance is: 80
Chicago ATM Transaction Failed
Insufficient funds
'''
# Current balance before transactions: 80
# Example 2: Chicago's ATM will deposit first for having the lowest delay with 3. Los Angele's ATM withdrawal of a 100
# dollars has the 2nd lowest delay ATM which is why it goes next and can withdraw from the new balance. There are two
# transactions left, Los Angele's ATM deposit and Chicago's ATM withdrawal. They both have the same delay of 5.
# Los Angele's ATM deposit of 60 dollars will go first since it has a priority of one over Chicago ATM's 20 dollar
# withdrawal which has 2.
thread_atm_chicago = threading.Thread(
target=atm_chicago_transactions, args=[[(3, 1, 20), (5, 2, -20)]], name="ATM Chicago")
thread_atm_los_angeles = threading.Thread(
target=atm_los_angeles_transactions, args=[[(4, 1, -100), (5, 1, 60)]], name="ATM Los Angeles")
thread_atm_chicago.start()
thread_atm_los_angeles.start()
thread_atm_chicago.join()
thread_atm_los_angeles.join()
'''
Chicago ATM Transaction Successful
You have deposited: 20
The new balance is: 100
Los Angeles ATM Transaction Successful
You have withdrawn: 100
The new balance is: 0
Los Angeles ATM Transaction Successful
You have deposited: 60
The new balance is: 60
Chicago ATM Transaction Successful
You have withdrawn: 20
The new balance is: 40
'''
# Stopping the scheduler so no more actions can be added to the queue
scheduler.stop()