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
10
11
12
# File 'lib/adding_multiples/CLI.rb', line 7

def initialize
    puts
    puts "Hello! Thank you for taking the time to try this out."
    puts "This tool will add the multiples (up to 1,000) of any positive integers you choose." 
    puts "In the prompt below, please enter a positive integer to get started."
end

Instance Method Details

#calculate_sumObject

add multiples to sum



77
78
79
# File 'lib/adding_multiples/CLI.rb', line 77

def calculate_sum
    @sum += @multiple
end

#callObject

the order in which methods are called



15
16
17
# File 'lib/adding_multiples/CLI.rb', line 15

def call
    get_input
end

#check_multiplesObject

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



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

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

    @integer.each do |i|
        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



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/adding_multiples/CLI.rb', line 41

def get_another_integer
    input = ""
    puts "\nWould you like to enter another number?"

    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



20
21
22
23
24
25
# File 'lib/adding_multiples/CLI.rb', line 20

def get_input
    @i = 0
    @integer = []

    get_positive_integer 
end

#get_positive_integerObject

get a positive integer from the user



28
29
30
31
32
33
34
35
36
37
38
# File 'lib/adding_multiples/CLI.rb', line 28

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

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