-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgps_2.2_electronic_grocery_list_teacher.rb
160 lines (120 loc) · 3.41 KB
/
gps_2.2_electronic_grocery_list_teacher.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
# https://github.com/4rlm/phase-0-tracks/blob/master/ruby/gps2_2.rb
def list_creator(items)
grocery_list = {}
puts "\n----------------------\n"
puts "Add items to your grocery list:"
puts "Ex format: 'carrots apples cereal pizza"
puts "\n----------------------\n"
items = gets.chomp
items_array = items.split(' ')
items_array.each do |item|
grocery_list.store(item, 0)
end
grocery_list
list_printer(grocery_list)
end
def item_adder(grocery_list, items)
items_array = items.split(' ')
items_array.each do |item|
grocery_list.store(item, 0)
end
grocery_list
end
def item_remover(grocery_list, items)
# puts "Enter item to remove:"
# puts "Ex format: 'carrots apples cereal pizza"
items = gets.chomp
items_array = items.split(' ')
items_array.each do |item|
grocery_list.delete(item)
puts "Removed #{item}."
end
grocery_list
list_printer(grocery_list)
end
def quantity_updater(grocery_list, key)
# puts "\n----------------------\n"
# puts "What would you like to update?"
# key = gets.chomp
# puts "\n----------------------\n"
puts "You have '#{key}: #{grocery_list[key]}' on your list."
puts "Enter new quantity for #{key}:"
value = gets.chomp
grocery_list[key] = value
puts "Item(s) count updated."
# list_printer(grocery_list)
grocery_list
list_printer(grocery_list)
end
def list_printer(grocery_list)
puts "\n----------------------\n"
puts "Here's a list of your items:"
grocery_list.each{|key,value|
puts "#{key}: #{value}"
}
end
def option_list(choice)
option_list = [
"\n----------------------\n",
"You may update your list by entering:",
"'add' to add an item.",
"'remove' to remove an item.",
"'change' to change the quantity.",
"'done' if list is complete.",
"\n----------------------\n"]
options = option_list.each{|option| option}
puts options
response = gets.chomp
if response == 'add'
item_adder(grocery_list)
elsif response == 'remove'
item_remover(grocery_list)
elsif response == 'change'
quantity_updater(grocery_list)
elsif response == 'done'
puts "Have a nice day!"
exit
else
puts "You entered an invalid selection."
puts options
end
end
# Calls list_creator
new_list = list_creator(items)
# Calls item_adder
puts "Add more items."
items = gets.chomp
item_adder(new_list, items)
puts "New item(s) added."
# Calls list_printer
# items = gets.chomp
list_printer(grocery_list)
# Calls option_list
# choice = gets.chomp
# option_list(choice)
# Calls item_remover
puts "Enter item to remove:"
puts "Ex format: 'carrots apples cereal pizza"
items = gets.chomp
item_remover(grocery_list, items)
# Calls quantity_updater
puts "What would you like to update?"
key = gets.chomp
quantity_updater(grocery_list, key)
############
def run_grocery_program
puts "\n----------------------\n"
puts "Add items to your grocery list:"
puts "Ex format: 'carrots apples cereal pizza"
puts "\n----------------------\n"
items = gets.chomp
new_list = list_creator(items)
puts "Add more items."
items = gets.chomp
item_adder(new_list, items)
items = gets.chomp
puts "New item(s) added."
list_printer(grocery_list)
end
run_grocery_program # outer program to interface with the user
#############