-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
219 additions
and
112 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,59 +1,54 @@ | ||
require './box' | ||
require './product' | ||
require './v1/box' | ||
require './v1/product' | ||
|
||
# b1 = Box.new | ||
# hammer_box = Box.new | ||
# hammer = Product.new 'hammer', 10.0 | ||
b1 = Box.new | ||
hammer_box = Box.new | ||
hammer = Product.new 'hammer', 10.0 | ||
|
||
# hammer_box.add_item hammer | ||
# b1.add_item hammer_box | ||
hammer_box.add_item hammer | ||
b1.add_item hammer_box | ||
|
||
# recipt = Product.new 'recipt', 0.1 | ||
# b1.add_item recipt | ||
recipt = Product.new 'recipt', 0 | ||
b1.add_item recipt | ||
|
||
# other_box = Box.new | ||
other_box = Box.new | ||
|
||
# phone_box = Box.new | ||
# phone = Product.new 'phone', 1.0 | ||
# headphones = Product.new 'headpones', 0.1 | ||
# phone_box.add_item phone | ||
# phone_box.add_item headphones | ||
phone_box = Box.new | ||
phone = Product.new 'phone', 1.0 | ||
headphones = Product.new 'headpones', 0.1 | ||
phone_box.add_item phone | ||
phone_box.add_item headphones | ||
|
||
# charger_box = Box.new | ||
# charger = Product.new 'charger', 0.1 | ||
# charger_box.add_item charger | ||
charger_box = Box.new | ||
charger = Product.new 'charger', 0.2 | ||
charger_box.add_item charger | ||
|
||
# other_box.add_item phone_box | ||
# other_box.add_item charger_box | ||
other_box.add_item phone_box | ||
other_box.add_item charger_box | ||
|
||
# b1.add_item other_box | ||
# puts b1.instance_variable_get '@contents' | ||
# puts b1.weight | ||
b1.add_item other_box | ||
|
||
require './shipment' | ||
puts "shipment: #{b1.item_count} item(s), #{b1.weight} lbs" | ||
|
||
outer_box = Box.new do | ||
item Box.new do | ||
item Product.new 'hammer', 10.0 | ||
end | ||
|
||
# item Product.new 'recipt', 0.1 | ||
require './v2/shipment' | ||
|
||
# medium_box = Box.new do |mb| | ||
# phone_box = Box.new do |pb| | ||
# pb.add_item Product.new 'phone', 1 | ||
# pb.add_item Product.new 'headphones', 0.1 | ||
# end | ||
s = Shipment.new do | ||
box do | ||
product 'hammer', 10.0 | ||
end | ||
|
||
# charger_box = Box.new do |cb| | ||
# cb.add_item Product.new 'charger', 0.1 | ||
# end | ||
box do | ||
box do | ||
product 'phone', 1.0 | ||
product 'headphones', 0.1 | ||
end | ||
|
||
# mb.add_item phone_box | ||
# mb.add_item charger_box | ||
# end | ||
box do | ||
product 'charger', 0.2 | ||
end | ||
end | ||
|
||
# b.add_item medium_box | ||
product 'recipt', 0 | ||
end | ||
|
||
puts outer_box.weight | ||
puts "shipment: #{s.item_count} item(s), #{s.weight} lbs" |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
class Product | ||
attr_reader :name, :weight, :item_count | ||
def initialize(name, weight) | ||
@name = name | ||
@weight = weight | ||
@item_count = 1 | ||
end | ||
|
||
def to_s | ||
"<Product: #{name} (#{weight} lbs)>" | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
class Box | ||
attr_reader :contents | ||
|
||
def initialize | ||
@contents = [] | ||
end | ||
|
||
# load an item into this box | ||
# item: either a Box or a Product | ||
def pack(item) | ||
@contents << item | ||
end | ||
|
||
def weight | ||
@contents.inject(0.0) { |total, item| total + item.weight } | ||
end | ||
|
||
def item_count | ||
@contents.inject(0) { |total, item| total + item.item_count } | ||
end | ||
|
||
def to_s | ||
"<Box:#{object_id} #{@contents.length} item(s) (#{weight} lbs) >" | ||
end | ||
end | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.