Class: Controller

Inherits:
Object
  • Object
show all
Defined in:
lib/daniel_terminal_app.rb

Instance Method Summary collapse

Constructor Details

#initializeController

Returns a new instance of Controller.



11
12
13
14
15
# File 'lib/daniel_terminal_app.rb', line 11

def initialize
  @users = [User.new('Daniel', 'password', [])]
  @view = View.new
  @countries = Countries.new
end

Instance Method Details

#search_for_countryObject



92
93
94
95
96
# File 'lib/daniel_terminal_app.rb', line 92

def search_for_country
  search = @view.display_search
  search = search.split.map(&:capitalize) * ' '
  @view.promptCountry(@countries.search_country(search))
end

#sign_upObject



98
99
100
101
102
103
104
105
106
107
108
# File 'lib/daniel_terminal_app.rb', line 98

def 
  username = @view.displaylogin
  if File.exist?("#{username}.json")
    puts 'That user already exists, please enter another username:'
    
  else
    password = @view.displaypw
    current_user = User.new(username, password, [])
    current_user
  end
end

#startObject



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.display_start_menu
      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 = 
        loggedin = true
      when 'Search for a country'
        search_for_country
      when 'Close'
        exit
      end

      while loggedin
        input = @view.display_user_menu
        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.}.json", jsondata)
        else
          puts 'Invalid Entry'
        end

      end
    end
end