-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.py
executable file
·138 lines (123 loc) · 4.39 KB
/
main.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
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
#!/usr/bin/env python2.7
"""
CISC 327 Assignment 2
Brandon Bloch (10052759),
Bryce Marshall (10113902),
Mitchell Mulder (10137002)
This program loops indefinitely on start,
waiting for the "login" command. When the
user logs in (either to ATM or agent mode),
the respective mode then loops indefinitely
waiting for further transaction commands
until the "logout" command is given. At
this point, the transaction summary log
is written.
"""
import commands
import inputOutput
import sys
import validation
def run_agent_mode():
"""
Prompt the user for commands and listen for the response.
Commands are run with agent parameters and limitations.
When the "logout" command is given, this function returns.
"""
print 'Logged in as agent'
records = []
deleted_accounts = []
while True:
line = inputOutput.prompt_for_input().lower()
if line == 'logout':
inputOutput.write_summary_file(records, sys.argv[2])
print 'Logged out'
return
elif line == 'transfer':
tmp_transfer = commands.transfer(True)
if (validation.validate_deleted(tmp_transfer[1], deleted_accounts) or
validation.validate_deleted(tmp_transfer[2], deleted_accounts)):
records.append(tmp_transfer)
print 'Transfer successful'
else:
print 'Error: Account does not exist'
elif line == 'withdraw':
tmp_withdraw = commands.withdraw(True, {})
if validation.validate_deleted(tmp_withdraw[1], deleted_accounts):
records.append(tmp_withdraw)
print 'Withdraw successful'
else:
print 'Error: Account does not exist'
elif line == 'deposit':
tmp_deposit = commands.deposit(True)
if validation.validate_deleted(tmp_deposit[1], deleted_accounts):
records.append(tmp_deposit)
print 'Deposit successful'
else:
print 'Error: Account does not exist'
elif line == 'create':
records.append(commands.create())
elif line == 'delete':
tmp_delete = commands.delete()
records.append(tmp_delete)
deleted_accounts.append(tmp_delete[1])
else:
print 'Error: command not recognized'
def run_atm_mode():
"""
Prompt the user for commands and listen for the response.
Commands are run with ATM parameters and limitations.
When the "logout" command is given, this function returns.
"""
print 'Logged in as ATM'
records = []
withdrawals = {}
while True:
line = inputOutput.prompt_for_input().lower()
if line == 'logout':
inputOutput.write_summary_file(records, sys.argv[2])
print 'Logged out'
return
elif line == 'transfer':
records.append(commands.transfer(False))
elif line == 'withdraw':
result = commands.withdraw(False, withdrawals)
records.append(result)
if result[1] in withdrawals:
withdrawals[result[1]] += result[2]
else:
withdrawals[result[1]] = result[2]
elif line == 'deposit':
records.append(commands.deposit(False))
elif line == 'create':
print 'Error: privileged command'
elif line == 'delete':
print 'Error: privileged command'
else:
print 'Error: command not recognized'
def run_main_loop():
"""
Prompt the user for input.
This loop waits for the "login" and "atm" or "agent"
commands, and begins the respective mode.
"""
while True:
try:
prompt = inputOutput.prompt_for_input().lower()
if prompt == 'login':
while True:
print 'Log in as ATM or agent?'
mode_prompt = inputOutput.prompt_for_input().lower()
if mode_prompt == 'atm':
run_atm_mode()
break
elif mode_prompt == 'agent':
run_agent_mode()
break
else:
print 'Error: invalid mode'
else:
print 'Error: command not recognized'
except EOFError:
return
if __name__ == "__main__":
run_main_loop()