Skip to content

Commit 828dc25

Browse files
committed
Ensure user input option is valid
1 parent 2c1cb3a commit 828dc25

4 files changed

Lines changed: 57 additions & 6 deletions

File tree

app.rb

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,22 @@
11
#!/usr/bin/ruby
22

3-
# Entry point
3+
require_relative './lib/handle_input'
4+
5+
command = HandleInput.new
6+
7+
$stdout.print "Would you like to LIST available products, SHOP, VIEW basket, EXIT without placing an order? \n"
8+
9+
# Read user input
10+
loop do
11+
input = gets.chomp
12+
13+
next if input.empty?
14+
15+
unless 'EXIT'.match?(input)
16+
command.interpret(input)
17+
next
18+
end
19+
20+
$stdout.print "Goodbye! \n"
21+
break
22+
end

lib/handle_input.rb

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
require_relative 'item'
2+
3+
# HandleInput class
4+
class HandleInput
5+
# Actions
6+
ACTIONS = %w[LIST SHOP VIEW EXIT].freeze
7+
8+
# Interpret method
9+
# @param command [String]
10+
# @return
11+
def interpret(command)
12+
return unless ACTIONS.include?(command)
13+
14+
$stdout.print "#{command} \n"
15+
end
16+
end

lib/watermelon.rb

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,14 @@ def initialize(name)
1111
@pack = nil
1212
end
1313

14-
def pack_size(selection)
15-
return unless OPTIONS.include?(selection)
16-
17-
return @pack = Pack.new(3, Money.new(699, 'NZD')) if selection == 3
18-
@pack = Pack.new(5, Money.new(899, 'NZD')) if selection == 5
14+
# Pack size method
15+
# @Param qty [Int]
16+
# @Return @pack
17+
def pack_size(qty)
18+
return unless OPTIONS.include?(qty)
19+
20+
return @pack = Pack.new(3, Money.new(699, 'NZD')) if qty == 3
21+
@pack = Pack.new(5, Money.new(899, 'NZD')) if qty == 5
1922
end
2023
end
2124

spec/handle_input_spec.rb

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
require 'rspec'
2+
require 'spec_helper'
3+
require './lib/handle_input'
4+
5+
describe HandleInput do
6+
describe '#interpret' do
7+
it 'should return nil unless command matches a value in ACTIONS array' do
8+
instance = HandleInput.new
9+
10+
expect(instance.interpret('hello')).to be_nil
11+
end
12+
end
13+
end

0 commit comments

Comments
 (0)