-
Notifications
You must be signed in to change notification settings - Fork 0
/
oopcalc2.rb
84 lines (69 loc) · 1.36 KB
/
oopcalc2.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
class Calculator
@@calc_count = 0
attr_accessor :value2
def initialize
@@calc_count += 1
puts "Welcome to the Calculator\nPlease enter your first value"
@value1 = gets.chomp.to_i
puts 'Please enter your second value'
@value2 = gets.chomp.to_i
run_operation
print_result(@result)
end
def self.calc_count
@@calc_count
end
#getters
def value1
@value1
end
# attribute accessor takes care of this
# def value2
# @value2
# end
#setters
def value1=(value)
@value1 = value
end
# attribute accessor takes care of this
# def value2=(value)
# @value2 = value
# end
def get_operator
puts "What operation would you like to perform\n1. Add\n2. Subtract\n3. Multipy\n4. Divide"
operation = gets.chomp.to_i
if (1..4) === operation
operation
else
get_operator
end
end
def run_operation(operation=get_operator)
case operation
when 1
add
when 2
subtract
when 3
multiply
when 4
divide
end
end
def add
@result = @value1 + @value2
end
def subtract
@result = @value1 - @value2
end
def multiply
@result = @value1 * @value2
end
def divide
@result = @value1.to_f / @value2.to_f
end
def print_result(result=@result)
puts "Your answer is #{result}"
end
end
@calc = Calculator.new