forked from dzemlianoi-double/refactoring-example
-
Notifications
You must be signed in to change notification settings - Fork 3
/
account.rb
511 lines (468 loc) · 15.4 KB
/
account.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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
require 'yaml'
require 'pry'
class Account
attr_accessor :login, :name, :card, :password, :file_path
def initialize
@errors = []
@file_path = 'accounts.yml'
end
def console
puts 'Hello, we are RubyG bank!'
puts '- If you want to create account - press `create`'
puts '- If you want to load account - press `load`'
puts '- If you want to exit - press `exit`'
# FIRST SCENARIO. IMPROVEMENT NEEDED
a = gets.chomp
if a == 'create'
create
elsif a == 'load'
load
else
exit
end
end
def create
loop do
name_input
age_input
login_input
password_input
break unless @errors.length != 0
@errors.each do |e|
puts e
end
@errors = []
end
@card = []
new_accounts = accounts << self
@current_account = self
File.open(@file_path, 'w') { |f| f.write new_accounts.to_yaml } #Storing
main_menu
end
def load
loop do
if !accounts.any?
return create_the_first_account
end
puts 'Enter your login'
login = gets.chomp
puts 'Enter your password'
password = gets.chomp
if accounts.map { |a| { login: a.login, password: a.password } }.include?({ login: login, password: password })
a = accounts.select { |a| login == a.login }.first
@current_account = a
break
else
puts 'There is no account with given credentials'
next
end
end
main_menu
end
def create_the_first_account
puts 'There is no active accounts, do you want to be the first?[y/n]'
if gets.chomp == 'y'
return create
else
return console
end
end
def main_menu
loop do
puts "\nWelcome, #{@current_account.name}"
puts 'If you want to:'
puts '- show all cards - press SC'
puts '- create card - press CC'
puts '- destroy card - press DC'
puts '- put money on card - press PM'
puts '- withdraw money on card - press WM'
puts '- send money to another card - press SM'
puts '- destroy account - press `DA`'
puts '- exit from account - press `exit`'
command = gets.chomp
if command == 'SC' || command == 'CC' || command == 'DC' || command == 'PM' || command == 'WM' || command == 'SM' || command == 'DA' || command == 'exit'
if command == 'SC'
show_cards
elsif command == 'CC'
create_card
elsif command == 'DC'
destroy_card
elsif command == 'PM'
put_money
elsif command == 'WM'
withdraw_money
elsif command == 'SM'
send_money
elsif command == 'DA'
destroy_account
exit
elsif command == 'exit'
exit
break
end
else
puts "Wrong command. Try again!\n"
end
end
end
def create_card
loop do
puts 'You could create one of 3 card types'
puts '- Usual card. 2% tax on card INCOME. 20$ tax on SENDING money from this card. 5% tax on WITHDRAWING money. For creation this card - press `usual`'
puts '- Capitalist card. 10$ tax on card INCOME. 10% tax on SENDING money from this card. 4$ tax on WITHDRAWING money. For creation this card - press `capitalist`'
puts '- Virtual card. 1$ tax on card INCOME. 1$ tax on SENDING money from this card. 12% tax on WITHDRAWING money. For creation this card - press `virtual`'
puts '- For exit - press `exit`'
ct = gets.chomp
if ct == 'usual' || ct == 'capitalist' || ct == 'virtual'
if ct == 'usual'
card = {
type: 'usual',
number: 16.times.map{rand(10)}.join,
balance: 50.00
}
elsif ct == 'capitalist'
card = {
type: 'capitalist',
number: 16.times.map{rand(10)}.join,
balance: 100.00
}
elsif ct == 'virtual'
card = {
type: 'virtual',
number: 16.times.map{rand(10)}.join,
balance: 150.00
}
end
cards = @current_account.card << card
@current_account.card = cards #important!!!
new_accounts = []
accounts.each do |ac|
if ac.login == @current_account.login
new_accounts.push(@current_account)
else
new_accounts.push(ac)
end
end
File.open(@file_path, 'w') { |f| f.write new_accounts.to_yaml } #Storing
break
else
puts "Wrong card type. Try again!\n"
end
end
end
def destroy_card
loop do
if @current_account.card.any?
puts 'If you want to delete:'
@current_account.card.each_with_index do |c, i|
puts "- #{c[:number]}, #{c[:type]}, press #{i + 1}"
end
puts "press `exit` to exit\n"
answer = gets.chomp
break if answer == 'exit'
if answer&.to_i.to_i <= @current_account.card.length && answer&.to_i.to_i > 0
puts "Are you sure you want to delete #{@current_account.card[answer&.to_i.to_i - 1][:number]}?[y/n]"
a2 = gets.chomp
if a2 == 'y'
@current_account.card.delete_at(answer&.to_i.to_i - 1)
new_accounts = []
accounts.each do |ac|
if ac.login == @current_account.login
new_accounts.push(@current_account)
else
new_accounts.push(ac)
end
end
File.open(@file_path, 'w') { |f| f.write new_accounts.to_yaml } #Storing
break
else
return
end
else
puts "You entered wrong number!\n"
end
else
puts "There is no active cards!\n"
break
end
end
end
def show_cards
if @current_account.card.any?
@current_account.card.each do |c|
puts "- #{c[:number]}, #{c[:type]}"
end
else
puts "There is no active cards!\n"
end
end
def withdraw_money
puts 'Choose the card for withdrawing:'
answer, a2, a3 = nil #answers for gets.chomp
if @current_account.card.any?
@current_account.card.each_with_index do |c, i|
puts "- #{c[:number]}, #{c[:type]}, press #{i + 1}"
end
puts "press `exit` to exit\n"
loop do
answer = gets.chomp
break if answer == 'exit'
if answer&.to_i.to_i <= @current_account.card.length && answer&.to_i.to_i > 0
current_card = @current_account.card[answer&.to_i.to_i - 1]
loop do
puts 'Input the amount of money you want to withdraw'
a2 = gets.chomp
if a2&.to_i.to_i > 0
money_left = current_card[:balance] - a2&.to_i.to_i - withdraw_tax(current_card[:type], current_card[:balance], current_card[:number], a2&.to_i.to_i)
if money_left > 0
current_card[:balance] = money_left
@current_account.card[answer&.to_i.to_i - 1] = current_card
new_accounts = []
accounts.each do |ac|
if ac.login == @current_account.login
new_accounts.push(@current_account)
else
new_accounts.push(ac)
end
end
File.open(@file_path, 'w') { |f| f.write new_accounts.to_yaml } #Storing
puts "Money #{a2&.to_i.to_i} withdrawed from #{current_card[:number]}$. Money left: #{current_card[:balance]}$. Tax: #{withdraw_tax(current_card[:type], current_card[:balance], current_card[:number], a2&.to_i.to_i)}$"
return
else
puts "You don't have enough money on card for such operation"
return
end
else
puts 'You must input correct amount of $'
return
end
end
else
puts "You entered wrong number!\n"
return
end
end
else
puts "There is no active cards!\n"
end
end
def put_money
puts 'Choose the card for putting:'
if @current_account.card.any?
@current_account.card.each_with_index do |c, i|
puts "- #{c[:number]}, #{c[:type]}, press #{i + 1}"
end
puts "press `exit` to exit\n"
loop do
answer = gets.chomp
break if answer == 'exit'
if answer&.to_i.to_i <= @current_account.card.length && answer&.to_i.to_i > 0
current_card = @current_account.card[answer&.to_i.to_i - 1]
loop do
puts 'Input the amount of money you want to put on your card'
a2 = gets.chomp
if a2&.to_i.to_i > 0
if put_tax(current_card[:type], current_card[:balance], current_card[:number], a2&.to_i.to_i) >= a2&.to_i.to_i
puts 'Your tax is higher than input amount'
return
else
new_money_amount = current_card[:balance] + a2&.to_i.to_i - put_tax(current_card[:type], current_card[:balance], current_card[:number], a2&.to_i.to_i)
current_card[:balance] = new_money_amount
@current_account.card[answer&.to_i.to_i - 1] = current_card
new_accounts = []
accounts.each do |ac|
if ac.login == @current_account.login
new_accounts.push(@current_account)
else
new_accounts.push(ac)
end
end
File.open(@file_path, 'w') { |f| f.write new_accounts.to_yaml } #Storing
puts "Money #{a2&.to_i.to_i} was put on #{current_card[:number]}. Balance: #{current_card[:balance]}. Tax: #{put_tax(current_card[:type], current_card[:balance], current_card[:number], a2&.to_i.to_i)}"
return
end
else
puts 'You must input correct amount of money'
return
end
end
else
puts "You entered wrong number!\n"
return
end
end
else
puts "There is no active cards!\n"
end
end
def send_money
puts 'Choose the card for sending:'
if @current_account.card.any?
@current_account.card.each_with_index do |c, i|
puts "- #{c[:number]}, #{c[:type]}, press #{i + 1}"
end
puts "press `exit` to exit\n"
answer = gets.chomp
exit if answer == 'exit'
if answer&.to_i.to_i <= @current_account.card.length && answer&.to_i.to_i > 0
sender_card = @current_account.card[answer&.to_i.to_i - 1]
else
puts 'Choose correct card'
return
end
else
puts "There is no active cards!\n"
return
end
puts 'Enter the recipient card:'
a2 = gets.chomp
if a2.length > 15 && a2.length < 17
all_cards = accounts.map(&:card).flatten
if all_cards.select { |card| card[:number] == a2 }.any?
recipient_card = all_cards.select { |card| card[:number] == a2 }.first
else
puts "There is no card with number #{a2}\n"
return
end
else
puts 'Please, input correct number of card'
return
end
loop do
puts 'Input the amount of money you want to withdraw'
a3 = gets.chomp
if a3&.to_i.to_i > 0
sender_balance = sender_card[:balance] - a3&.to_i.to_i - sender_tax(sender_card[:type], sender_card[:balance], sender_card[:number], a3&.to_i.to_i)
recipient_balance = recipient_card[:balance] + a3&.to_i.to_i - put_tax(recipient_card[:type], recipient_card[:balance], recipient_card[:number], a3&.to_i.to_i)
if sender_balance < 0
puts "You don't have enough money on card for such operation"
elsif put_tax(recipient_card[:type], recipient_card[:balance], recipient_card[:number], a3&.to_i.to_i) >= a3&.to_i.to_i
puts 'There is no enough money on sender card'
else
sender_card[:balance] = sender_balance
@current_account.card[answer&.to_i.to_i - 1] = sender_card
new_accounts = []
accounts.each do |ac|
if ac.login == @current_account.login
new_accounts.push(@current_account)
elsif ac.card.map { |card| card[:number] }.include? a2
recipient = ac
new_recipient_cards = []
recipient.card.each do |card|
if card[:number] == a2
card[:balance] = recipient_balance
end
new_recipient_cards.push(card)
end
recipient.card = new_recipient_cards
new_accounts.push(recipient)
end
end
File.open('accounts.yml', 'w') { |f| f.write new_accounts.to_yaml } #Storing
puts "Money #{a3&.to_i.to_i}$ was put on #{sender_card[:number]}. Balance: #{recipient_balance}. Tax: #{put_tax(sender_card[:type], sender_card[:balance], sender_card[:number], a3&.to_i.to_i)}$\n"
puts "Money #{a3&.to_i.to_i}$ was put on #{a2}. Balance: #{sender_balance}. Tax: #{sender_tax(sender_card[:type], sender_card[:balance], sender_card[:number], a3&.to_i.to_i)}$\n"
break
end
else
puts 'You entered wrong number!\n'
end
end
end
def destroy_account
puts 'Are you sure you want to destroy account?[y/n]'
a = gets.chomp
if a == 'y'
new_accounts = []
accounts.each do |ac|
if ac.login == @current_account.login
else
new_accounts.push(ac)
end
end
File.open(@file_path, 'w') { |f| f.write new_accounts.to_yaml } #Storing
end
end
private
def name_input
puts 'Enter your name'
@name = gets.chomp
unless @name != '' && @name[0].upcase == @name[0]
@errors.push('Your name must not be empty and starts with first upcase letter')
end
end
def login_input
puts 'Enter your login'
@login = gets.chomp
if @login == ''
@errors.push('Login must present')
end
if @login.length < 4
@errors.push('Login must be longer then 4 symbols')
end
if @login.length > 20
@errors.push('Login must be shorter then 20 symbols')
end
if accounts.map { |a| a.login }.include? @login
@errors.push('Such account is already exists')
end
end
def password_input
puts 'Enter your password'
@password = gets.chomp
if @password == ''
@errors.push('Password must present')
end
if @password.length < 6
@errors.push('Password must be longer then 6 symbols')
end
if @password.length > 30
@errors.push('Password must be shorter then 30 symbols')
end
end
def age_input
puts 'Enter your age'
@age = gets.chomp
if @age.to_i.is_a?(Integer) && @age.to_i >= 23 && @age.to_i <= 90
@age = @age.to_i
else
@errors.push('Your Age must be greeter then 23 and lower then 90')
end
end
def accounts
if File.exists?('accounts.yml')
YAML.load_file('accounts.yml')
else
[]
end
end
def withdraw_tax(type, balance, number, amount)
if type == 'usual'
return amount * 0.05
elsif type == 'capitalist'
return amount * 0.04
elsif type == 'virtual'
return amount * 0.88
end
0
end
def put_tax(type, balance, number, amount)
if type == 'usual'
return amount * 0.02
elsif type == 'capitalist'
return 10
elsif type == 'virtual'
return 1
end
0
end
def sender_tax(type, balance, number, amount)
if type == 'usual'
return 20
elsif type == 'capitalist'
return amount * 0.1
elsif type == 'virtual'
return 1
end
0
end
end