carolineartz
8/25/2015 - 4:48 AM

example

example

require 'forwardable'

GroceryItem = Struct.new(:name, :quantity, :in_cart) do
  def to_s
    text = "#{quantity} : #{name}"
    in_cart ? "√ #{text}" : "  #{text}"
  end
end


class GroceryList
  extend Forwardable

  def_delegator :@list, :push, :add
  def_delegator :@list, :delete, :remove

  def initialize
    @list = []
  end

  def check(item)
    item.in_cart = true
  end

  def display
    banner
    @list.each { |item| puts item }
  end

  private
  def banner
    puts "~~~~~~~~ Grocery List ~~~~~~~~"
    30.times { print "~" }; puts
  end
end

my_list = GroceryList.new
bread = GroceryItem.new("bread", "1 loaf")
milk = GroceryItem.new("milk", "1 half gallon")
cheese = GroceryItem.new("cheddar cheese", "1 block")
cereal = GroceryItem.new("chocolate chex", "18 boxes")
fruit = GroceryItem.new("apples", "1 bushel")

my_list.add(bread, milk, cheese, cereal, fruit)
my_list.display
puts
my_list.remove(fruit)
my_list.display
puts
my_list.check(milk)
my_list.check(cereal)
my_list.display