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
|
# File 'lib/daniel_terminal_app.rb', line 17
def start
system 'clear'
loop do
select = @view.
loggedin = false
system 'clear'
case select
when 'Login'
username = @view.displaylogin
if File.exist?("#{username}.json")
current_user = User.from_json("#{username}.json")
password = @view.displaypw
if password == current_user.password
loggedin = true
else
puts 'Invalid password'
end
else
puts 'No user with that name'
end
when 'Sign up'
current_user = sign_up
loggedin = true
when 'Search for a country'
search_for_country
when 'Close'
exit
end
while loggedin
input = @view.
system 'clear'
case input
when 'Add a travel entry'
country = @view.display_search
country = country.split.map(&:capitalize) * ' '
if @countries.search_country(country)
date = @view.get_date_input('When did you travel there?(Enter in DD-MM-YYYY)')
current_user.new_travel_entry(country, @countries.search_country(country)['region'], date)
else
puts 'No country with that name'
end
system 'clear'
when 'Search for a country'
search_for_country
when 'View my entries'
@view.show_entries(current_user.travel_entries)
when 'Show me my statistics'
new_short_list = @countries.make_shortlist('name', 'region')
user_uni_travel = current_user.travel_entries.map { |p| { 'country' => p['country'], 'region' => p['region'] } }.uniq
unique_regions = new_short_list.map { |p| p['region'] }.uniq
@view.show_stat(user_uni_travel.length, new_short_list.length, 'The World')
unique_regions.each do |region|
count = 0
user_uni_travel.each do |index|
count += 1 if index['region'] == region
end
@view.show_stat(count, @countries.region_count(region), region)
end
when 'Log out'
puts 'logging out'
loggedin = false
jsondata = current_user.to_json
File.write("#{current_user.login}.json", jsondata)
else
puts 'Invalid Entry'
end
end
end
end
|