Class: AddingMultiples::CLI

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

Instance Method Summary collapse

Constructor Details

#initializeCLI

greeting and directions for the user



7
8
9
# File 'lib/adding_multiples/CLI.rb', line 7

def initialize
    puts "Hello World!"
end

Instance Method Details

#calculate_sumObject

add multiples to sum



75
76
77
# File 'lib/adding_multiples/CLI.rb', line 75

def calculate_sum
    @sum += @multiple
end

#callObject

the order in which methods are called



12
13
14
# File 'lib/adding_multiples/CLI.rb', line 12

def call
    get_input
end

#check_multiplesObject

loop through multiples and compare to range [1-1000], consider allowing user input for range



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/adding_multiples/CLI.rb', line 56

def check_multiples
    @range = [1, 1000]
    @multiple = 0
    @sum = 0

    @integer.each do |i|
        binding.pry
        integer = i.to_i
        while @multiple < @range[1]
            calculate_sum
            @multiple += integer
        end
    end

    puts "The sum of the multiples of the integers #{@integer} that are less than #{@range[1]} is #{@sum}"

end

#get_another_integerObject

ask user for another integer



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/adding_multiples/CLI.rb', line 38

def get_another_integer
    input = ""
    puts "Would you like to enter another? [y/n]"

    while input != "y" && input != "n"
        puts "Please enter 'y' or 'n'"
        input = gets.strip.downcase
    end

    if input == "y"
        get_positive_integer   
    else
        check_multiples
    end
end

#get_inputObject

get input from the user



17
18
19
20
21
22
# File 'lib/adding_multiples/CLI.rb', line 17

def get_input
    @i = 0
    @integer = []

    get_positive_integer 
end

#get_positive_integerObject

get a positive integer from the user



25
26
27
28
29
30
31
32
33
34
35
# File 'lib/adding_multiples/CLI.rb', line 25

def get_positive_integer
    puts "Please enter a positive integer:"
    @integer << gets.strip

    if @integer[@i].match(/\d+/)
        @i += 1 
        get_another_integer  
    else
        get_positive_integer
    end  
end