Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

made job-simulation.rb with method to fire and hire #3

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 13 additions & 3 deletions Queue.rb
Original file line number Diff line number Diff line change
@@ -1,13 +1,23 @@
class Queue
attr_reader :store
def initialize
@store = Array.new
end

def dequeue
@store.shift
end

def enqueue(element)
@store << element
end

def size
@store.length
end

def empty?
size == 0
end

end
15 changes: 12 additions & 3 deletions Stack.rb
Original file line number Diff line number Diff line change
@@ -1,14 +1,23 @@
class Stack
attr_reader :store
def initialize
@store = Array.new
end

def pop
@store.pop
end

def push(element)
@store << element
end

def size
@store.length
end

def empty?
size == 0
end

end
38 changes: 38 additions & 0 deletions job-simulation.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,42 @@
require './Stack.rb'
require './Queue.rb'

waiting = Queue.new
workers = Stack.new

workers.push('one')
workers.push('two')
workers.push('three')
workers.push('four')
workers.push('five')
workers.push('six')

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about

6.times do |i|
  workers.push(i)
end


waiting.enqueue("seven")
waiting.enqueue("eight")
waiting.enqueue("nine")
waiting.enqueue("ten")

def roll
rand(1..6)
end

def rehire(workers, waiting)
num = roll
num.times do
fired = workers.pop
waiting.enqueue(fired)
end
num.times do
hired = waiting.dequeue
workers.push(hired)
end
#i'm going to puts num here so that you i can easily see the results of the roll
puts "#{num} was rolled."
end

rehire(workers, waiting)
puts "Current workers: #{workers.store}"
puts "On the wait list: #{waiting.store}"
rehire(workers, waiting)
puts "Current workers: #{workers.store}"
puts "On the wait list: #{waiting.store}"