diff --git a/lib/Queue.rb b/lib/Queue.rb index 3edfd61c..3e8a7925 100644 --- a/lib/Queue.rb +++ b/lib/Queue.rb @@ -1,22 +1,29 @@ class Queue def initialize + @store = Array.new end def enqueue(element) + @store << element end def dequeue + @store.shift end def front + @store.first end def size + @store.length end def empty? + @store.empty? end def to_s + return @store.to_s end end diff --git a/lib/Stack.rb b/lib/Stack.rb index 26a25067..939f1baa 100644 --- a/lib/Stack.rb +++ b/lib/Stack.rb @@ -4,18 +4,23 @@ def initialize end def push(element) + @store << element end def pop + @store.pop end def top + @store.last end def size + @store.length end def empty? + @store.empty? end def to_s diff --git a/lib/job-simulation.rb b/lib/job-simulation.rb index f299404a..dd7052cc 100644 --- a/lib/job-simulation.rb +++ b/lib/job-simulation.rb @@ -1,24 +1,51 @@ -require './Stack.rb' -require './Queue.rb' +require_relative 'Stack' +require_relative 'Queue' class JobSimulation - attr_reader :workers, :waiting, :roll + attr_reader :employed, :waiting, :waitlist def initialize (jobs_available, job_seekers) + #job_seekers must be larger than jobs_available + @waiting = job_seekers - jobs_available + give_jobs(jobs_available) + create_wait_list + end + def give_jobs(jobs_available) + @number = 1 + @employed = Stack.new + jobs_available.times do + @employed.push("Worker \##{@number}") + @number += 1 + end + @employed + end + + def create_wait_list + @waitlist = Queue.new + @waiting.times do + @waitlist.enqueue("Worker \##{@number}") + @number += 1 + end + @waitlist end - def cycle + def cycle + die_roll = rand(1..6) + die_roll.times {@waitlist.enqueue(@employed.pop)} + die_roll.times {@employed.push(@waitlist.dequeue)} end + end ## Allows us to run our code and see what's happening: sim = JobSimulation.new(6,10) + puts "------------------------------" puts "Before the simulation starts" -puts "Employed: #{sim.workers}" -puts "Waitlist: #{sim.waiting}" +puts "Employed: #{sim.employed}" +puts "Waitlist: #{sim.waitlist}" puts "------------------------------" print "\n" @@ -26,6 +53,6 @@ def cycle until gets.chomp != "" puts "-------Cycle #{count+=1}-------" sim.cycle - puts "Employed: #{sim.workers}" - puts "Waitlist: #{sim.waiting}" + puts "Employed: #{sim.employed}" + puts "Waitlist: #{sim.waitlist}" end diff --git a/specs/test-queue-implementation.rb b/specs/test-queue-implementation.rb index ec80ffe8..701355b8 100644 --- a/specs/test-queue-implementation.rb +++ b/specs/test-queue-implementation.rb @@ -10,14 +10,14 @@ end it "adds something to an empty Queue" do - skip + # skip q = Queue.new q.enqueue(10) q.to_s.must_equal "[10]" end it "adds multiple somethings to a Queue" do - skip + # skip q = Queue.new q.enqueue(10) q.enqueue(20) @@ -26,14 +26,14 @@ end it "starts the size of a Queue at 0" do - skip + # skip q = Queue.new q.size.must_equal 0 q.empty?.must_equal true end it "removes something from the Queue" do - skip + # skip q = Queue.new q.enqueue(5) removed = q.dequeue @@ -43,7 +43,7 @@ end it "removes the right something (LIFO)" do - skip + # skip q = Queue.new q.enqueue(5) q.enqueue(3) @@ -55,7 +55,7 @@ end it "properly adjusts the size with enqueueing and dequeueing" do - skip + # skip q = Queue.new q.empty?.must_equal true q.enqueue(-1) @@ -70,7 +70,7 @@ end it "returns the front element in the Queue" do - skip + # skip q = Queue.new q.enqueue(40) q.enqueue(22) diff --git a/specs/test-stack-implementation.rb b/specs/test-stack-implementation.rb index 326e858f..a76fc532 100644 --- a/specs/test-stack-implementation.rb +++ b/specs/test-stack-implementation.rb @@ -10,14 +10,12 @@ end it "pushes something onto a empty Stack" do - skip s = Stack.new s.push(10) s.to_s.must_equal "[10]" end it "pushes multiple somethings onto a Stack" do - skip s = Stack.new s.push(10) s.push(20) @@ -26,14 +24,12 @@ end it "starts the size of a Stack at 0" do - skip s = Stack.new s.size.must_equal 0 s.empty?.must_equal true end it "removes something from the stack" do - skip s = Stack.new s.push(5) removed = s.pop @@ -43,7 +39,6 @@ end it "removes the right something (LIFO)" do - skip s = Stack.new s.push(5) s.push(3) @@ -55,7 +50,7 @@ end it "properly adjusts the size with pushing and poping" do - skip + # skip s = Stack.new s.empty?.must_equal true s.push(-1) @@ -70,7 +65,7 @@ end it "returns the top element on the stack" do - skip + # skip s = Stack.new s.push(40) s.push(22)