Skip to content

Commit

Permalink
Ensure user input option is valid
Browse files Browse the repository at this point in the history
  • Loading branch information
SelenaSmall committed Sep 2, 2017
1 parent 2c1cb3a commit 828dc25
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 6 deletions.
21 changes: 20 additions & 1 deletion app.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,22 @@
#!/usr/bin/ruby

# Entry point
require_relative './lib/handle_input'

command = HandleInput.new

$stdout.print "Would you like to LIST available products, SHOP, VIEW basket, EXIT without placing an order? \n"

# Read user input
loop do
input = gets.chomp

next if input.empty?

unless 'EXIT'.match?(input)
command.interpret(input)
next
end

$stdout.print "Goodbye! \n"
break
end
16 changes: 16 additions & 0 deletions lib/handle_input.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
require_relative 'item'

# HandleInput class
class HandleInput
# Actions
ACTIONS = %w[LIST SHOP VIEW EXIT].freeze

# Interpret method
# @param command [String]
# @return
def interpret(command)
return unless ACTIONS.include?(command)

$stdout.print "#{command} \n"
end
end
13 changes: 8 additions & 5 deletions lib/watermelon.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,14 @@ def initialize(name)
@pack = nil
end

def pack_size(selection)
return unless OPTIONS.include?(selection)

return @pack = Pack.new(3, Money.new(699, 'NZD')) if selection == 3
@pack = Pack.new(5, Money.new(899, 'NZD')) if selection == 5
# Pack size method
# @Param qty [Int]
# @Return @pack
def pack_size(qty)
return unless OPTIONS.include?(qty)

return @pack = Pack.new(3, Money.new(699, 'NZD')) if qty == 3
@pack = Pack.new(5, Money.new(899, 'NZD')) if qty == 5
end
end

Expand Down
13 changes: 13 additions & 0 deletions spec/handle_input_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
require 'rspec'
require 'spec_helper'
require './lib/handle_input'

describe HandleInput do
describe '#interpret' do
it 'should return nil unless command matches a value in ACTIONS array' do
instance = HandleInput.new

expect(instance.interpret('hello')).to be_nil
end
end
end

0 comments on commit 828dc25

Please sign in to comment.