Skip to content
Permalink
Browse files

Ensure user input option is valid

  • Loading branch information...
SelenaSmall committed Sep 2, 2017
1 parent 2c1cb3a commit 828dc25d386207223dc6f67111d8f3427766c5a3
Showing with 57 additions and 6 deletions.
  1. +20 −1 app.rb
  2. +16 −0 lib/handle_input.rb
  3. +8 −5 lib/watermelon.rb
  4. +13 −0 spec/handle_input_spec.rb
21 app.rb
@@ -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
@@ -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
@@ -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

@@ -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.
You can’t perform that action at this time.