11require_relative 'item'
22require_relative 'invoice'
3+ require_relative 'list'
4+ require_relative 'shop'
35require_relative 'order_line'
46
57# HandleInput class
68class HandleInput
7- attr_reader :order , :invoice
9+ attr_reader :order , :invoice , :list , :shop
810
911 # Actions
1012 ACTIONS = %w[ LIST SHOP VIEW ] . freeze
1113
1214 def initialize ( order )
1315 @order = order
1416 @invoice = Invoice . new
17+ @list = List . new
18+ @shop = Shop . new ( order )
1519 end
1620
1721 # Interpret method
@@ -20,57 +24,10 @@ def initialize(order)
2024 def interpret ( command )
2125 return unless ACTIONS . detect { |a | a == command }
2226
23- if command . match? ( 'LIST' )
24- # list all availble items and package sizes
25- end
27+ return list . selection if command . match? ( 'LIST' )
2628
27- return shop_menu if command . match? ( 'SHOP' )
29+ return shop . menu if command . match? ( 'SHOP' )
2830
2931 return invoice . print_order ( order ) if command . match? ( 'VIEW' )
3032 end
31-
32- private
33-
34- def shop_menu
35- $stdout. print "Type BACK at any time to return to the main menu. \n Add qty and items to backet, example input: 3 watermelons \n "
36-
37- loop do
38- input = $stdin. gets . chomp
39-
40- next if input . empty?
41-
42- break if 'BACK' . match? ( input )
43-
44- shop ( input )
45- next
46- end
47- end
48-
49- # Order input pattern
50- PATTERN = /^\s *\d +\s *(watermelons||pineapples||rockmelons)$/
51-
52- # Order items
53- def shop ( input )
54- unless input . match? ( PATTERN )
55- puts "That's not a valid input"
56- return
57- end
58-
59- # Break input down into qty, item
60- line = input . split ( /\W +/ )
61-
62- exec ( OrderLine . new ( line [ 0 ] . to_i , Item . new ( line [ 1 ] ) ) )
63- end
64-
65- # Add item order_line to order
66- def exec ( order_line )
67- order . add_item (
68- order_line ,
69- order_line . present_line (
70- order_line . optimal ( order_line . order_item . name )
71- )
72- )
73-
74- order_line
75- end
7633end
0 commit comments