Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Comparing changes

Choose two branches to see what's changed or to start a new pull request. If you need to, you can also compare across forks.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also compare across forks.
base fork: justinxreese/cco_challenge
base: 6aa0d9963a
...
head fork: justinxreese/cco_challenge
compare: 837d0a9544
Checking mergeability… Don't worry, you can still create the pull request.
  • 2 commits
  • 2 files changed
  • 0 commit comments
  • 1 contributor
Showing with 27 additions and 16 deletions.
  1. +16 −15 lib/shelf_stocker.rb
  2. +11 −1 stock.rb
View
31 lib/shelf_stocker.rb
@@ -2,13 +2,13 @@
class ShelfStocker
- def initialize()
- @products = JSON.parse(File.read('dat/products.json'))['products']
- @shelves = JSON.parse(File.read('dat/shelves.json'))['shelves']
-
- # The stock is the list of products, accounting for quantity on hand
+ def initialize(products,shelves)
@stock = []
- @products.each do |product|
+ @shelves = JSON.parse(shelves.read)['shelves']
+ products = JSON.parse(products.read)['products']
+
+ # Stock is the list of products, accounting for quantity
+ products.each do |product|
product['qty'].times do
product['qty'] = 1
@stock << product
@@ -18,29 +18,30 @@ def initialize()
self
end
+ # Uses instance variables to fill shelves and displays the
+ # resulting shelf configuration as plain text output
def fill_shelves
# Stock the most visible shelf first
@shelves.sort_by{|x| -1*x['visibility']}.each do |shelf|
capacity = shelf['capacity']
+ shelf_items = []
# with the most valuable item
@stock.sort_by!{|x| -1*x['value']}.delete_if do |item|
- # as long as it fits
- if item['size'] <= capacity
+ if item['size'] <= capacity # as long as it fits
capacity -= item['size']
- puts item['name']
+ shelf_items << item
true
- else
- # If it doesn't fit, keep it as stock
- false
+ else # If it doesn't fit, keep it as stock
+ false
end
end
- puts '^== '+shelf['row']+' ==^ ('+capacity.to_s+'/'+shelf['capacity'].to_s+')'
-
+ puts '==== '+shelf['row']+' Shelf, Remaining Capacity: '+capacity.to_s+' ===='
+ puts shelf_items.collect{|x| x['name']}.join(',')
end
- puts @stock.size.to_s+" left over items"
+ puts @stock.size.to_s+" items not stocked"
end
end
View
12 stock.rb
@@ -1,4 +1,14 @@
require './lib/shelf_stocker.rb'
-stocker = ShelfStocker.new
+begin
+ products = File.new('dat/products.json')
+ shelves = File.new('dat/shelves.json')
+rescue Exception => e
+ puts e
+ exit
+end
+
+stocker = ShelfStocker.new(products,shelves)
stocker.fill_shelves
+
+# So that's what things would be like if I'd invented the fing-longer

No commit comments for this range

Something went wrong with that request. Please try again.