-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathquestion.rb
86 lines (70 loc) · 1.72 KB
/
question.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
85
86
# frozen_string_literal: true
require_relative 'option'
# Class for options for a question
class Question
attr_accessor :statement, :options, :correct_answer_index
def initialize(statement)
@statement = statement
@options = []
@correct_answer_index = nil
end
def edit
loop do
edit_options
process_choice(gets.chomp.to_i)
break unless multiple_edit_input
end
end
def add_options(index)
loop do
puts "Enter Option #{index + 1}: "
option_text = gets.chomp.strip
break @options << Option.new(option_text) unless option_text.empty?
puts 'Option cannot be empty.'
end
end
def add_options_with_correct_answer
3.times do |i|
add_options(i)
end
set_correct_answer
end
def set_correct_answer
loop do
puts 'Enter the option number of the correct answer (1, 2, or 3): '
@correct_answer_index = gets.chomp.to_i - 1
break if @correct_answer_index.between?(0, 2)
puts 'Invalid answer index. Please select between 1 and 3.'
end
end
def edit_options
puts 'What do you want to edit?'
puts '1. Edit Question Statement'
puts '2. Edit Options'
puts '3. Set Correct Option'
end
def new_statement_input
puts 'Enter new question statement: '
gets.chomp
end
def clear_and_add_options
@options.clear
add_options_with_correct_answer
end
def process_choice(choice)
case choice
when 1
@statement = new_statement_input
when 2
clear_and_add_options
when 3
set_correct_answer
else
puts 'Invalid choice!'
end
end
def multiple_edit_input
puts 'Do you want to edit anything else? (y/n): '
gets.chomp.downcase == 'y'
end
end