-
Notifications
You must be signed in to change notification settings - Fork 34
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
Queues - Cara Comfort - stacks-queues #37
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,12 +5,52 @@ class JobSimulation | |
attr_reader :workers, :waiting, :roll | ||
|
||
def initialize (jobs_available, job_seekers) | ||
# assume jobs_available and job_seekers are ints | ||
# assume always more job_seekers than jobs_available | ||
raise ArgumentError.new("The economy is bad. There will always be more job seekers than jobs.") if jobs_available > job_seekers | ||
|
||
@workers = Stack.new | ||
(1..jobs_available).each do |worker| | ||
@workers.push("Worker ##{worker}") | ||
end | ||
|
||
@waiting = Queue.new | ||
# seekers_left = job_seekers - jobs_available | ||
((jobs_available + 1)..job_seekers).each do |worker| | ||
@waiting.enqueue("Worker ##{worker}") | ||
end | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yes! nice! |
||
|
||
@roll | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Remove this as it's not needed here |
||
end | ||
|
||
def cycle | ||
roll | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just put the rolling method body here - it's only one line, so no need for a new method for it |
||
puts "Managers roll a #{@roll}" | ||
|
||
# fire workers specified by the roll, removing them from the hired list | ||
# and adding them to the waiting list | ||
(1..@roll).each do | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this would read cleaner if you did |
||
fired_worker = @workers.pop | ||
puts "Fire: #{fired_worker}" | ||
@waiting.enqueue(fired_worker) | ||
end | ||
|
||
# hire workers specified by the roll, removing them from the waiting list | ||
# and adding them to the hired list | ||
(1..@roll).each do | ||
hired_worker = @waiting.dequeue | ||
puts "Hire: #{hired_worker}" | ||
@workers.push(hired_worker) | ||
end | ||
|
||
end | ||
|
||
private | ||
|
||
def roll | ||
# Assume managers rolling a 6 sided die | ||
@roll = rand(1..6) | ||
end | ||
end | ||
|
||
## Allows us to run our code and see what's happening: | ||
|
@@ -29,3 +69,5 @@ def cycle | |
puts "Employed: #{sim.workers}" | ||
puts "Waitlist: #{sim.waiting}" | ||
end | ||
|
||
# Code would probably be better organized by making hired and fired private methods |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
good!