-
Notifications
You must be signed in to change notification settings - Fork 0
/
ledger.rb
250 lines (191 loc) · 5.9 KB
/
ledger.rb
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
require './account.rb'
class Ledger
def initialize
@accounts = []
end
def login_workflow
print_welcome_prompt
response = gets.chomp
if is_yes?(response)
login
elsif is_no?(response)
print_create_account_prompt
response = gets.chomp
if is_yes?(response)
create_account_workflow
else
print_goodbye_message
exit
end
else
print_generic_error
login_workflow
end
end
def login
print_username_prompt
username = gets.chomp
if has_account?(username)
account = find_account_by_username(username)
print_password_prompt
password = gets.chomp
if valid_password?(username, password)
print_account_options_welcome(account)
account_workflow(account)
else
print_invalid_password_error
login
end
else
print_no_account_error
create_account_workflow
end
end
def create_account_workflow
print_new_account_username_prompt
username = gets.chomp
if has_account?(username)
print_username_taken_error
create_account_workflow
end
print_new_account_password_prompt
password = gets.chomp
print_initial_funds_prompt
response = gets.chomp
if is_yes?(response)
puts "Great! How much would you like to deposit? Please enter a valid number."
initial_deposit = gets.chomp.to_f.abs # Happy path only at this time
elsif is_no?(response)
initial_deposit = 0.0
end
new_account = Account.new(username, password, 0)
new_account.make_deposit(initial_deposit) if initial_deposit > 0
@accounts << new_account
print_account_create_success
login_workflow
end
def account_workflow(account)
print_account_options_prompt
loop do
response = gets.chomp
case response
when "logout"
logout
when "balance"
show_account_balance(account)
when "deposit"
do_the_deposit(account)
when "withdrawal"
do_the_withdrawal(account)
when "history"
show_history(account)
else
print_generic_error
end
prompt_for_further_action
end
end
private
def show_account_balance(account)
puts "Your current balance is #{account.balance}\n"
end
def do_the_deposit(account)
puts "Please enter the amount you would like to deposit:"
deposit_amount = gets.chomp.to_f.abs # Next steps: validate this input as a Float
account.make_deposit(deposit_amount)
puts "Success! #{deposit_amount} has been added to your account. Your new balance is #{account.balance}\n"
end
def do_the_withdrawal(account)
puts "Please enter the amount you would like to withdraw:"
withdrawal_amount = gets.chomp.to_f.abs # Next steps: validate this input as a Float
if account.make_withdrawal(withdrawal_amount)
puts "Success! #{withdrawal_amount} has been withdrawn from your account. Your new balance is #{account.balance}\n"
end
end
def show_history(account)
account.transaction_list.each { |transaction| transaction.to_s }
end
def is_yes?(string)
return true if string =~ /^([Yy][Ee][Ss]|[Yy])$/
return false
end
def is_no?(string)
return true if string =~ /^([Nn][Oo]|[Nn])$/
return false
end
def logout
puts "you are now logged out."
login_workflow
end
def has_account?(username)
find_account_by_username(username) != nil
end
def find_account_by_username(username)
@accounts.select { |account| account.username == username }.first
end
def valid_password?(username, password)
account_to_verify = find_account_by_username(username)
account_to_verify.password == password
end
# --- Print helper methods ---
def print_a_pretty_line
puts "~*" * 40
end
def print_account_options_prompt
print_a_pretty_line
puts "Typing [logout] will log you out of your account."
puts "Typing [balance] will display your current account balance."
puts "Typing [deposit] will help you deposit funds to your account."
puts "Typing [withdrawal] will help you withdraw funds from your account."
puts "typing [history] will display your transaction history"
print_a_pretty_line
end
def prompt_for_further_action
puts "Would you like to do anything else?"
end
def print_welcome_prompt
print_a_pretty_line
puts "Hello! Welcome to The World's Greatest Banking Application."
puts "\n"
puts "Do you already have an account with us? [Y/N]"
end
def print_create_account_prompt
puts "Would you like to create an account with us? [Y/N]"
end
def print_username_prompt
puts "Please enter your account username:"
end
def print_password_prompt
puts "Please enter your password:"
end
def print_account_options_welcome(account)
puts "Hi, #{account.username}! What would you like to do with this account?"
end
def print_invalid_password_error
puts "We're sorry, but the password you provided is invalid. Please try again."
end
def print_no_account_error
puts "Sorry, it does not appear that you have an account. Please create one in order to use our application."
end
def print_new_account_username_prompt
puts "Please enter a username for your new account:"
end
def print_username_taken_error
puts "We're sorry, but that username is already taken. Please try another one."
end
def print_new_account_password_prompt
puts "Thank you! Now please enter a password for your new account:"
end
def print_initial_funds_prompt
puts "Thank you! Do you have any funds that you would like to deposit right now? [Y/N]"
end
def print_account_create_success
puts "Everything looks good! Your account has been created. Now it's time to log in!"
end
def print_goodbye_message
puts "Thanks for stopping by! See you next time."
end
def print_generic_error
puts "I'm sorry, but that is not a valid response at this time. Please try again."
end
end