-
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
Lynn Trickey's Stacks, Queues and Job Simulation #46
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 |
---|---|---|
@@ -1,31 +1,58 @@ | ||
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 | ||
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. It's fine to make this variable and use it. But if doing so, it should be a local variable, not an instance variable, as it won't be (shouldn't be) used in other methods as well. |
||
@employed = Stack.new | ||
jobs_available.times do | ||
@employed.push("Worker \##{@number}") | ||
@number += 1 | ||
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. Another solution that's a little easier to read is:
|
||
@employed | ||
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. It's fine to break out this task into its own method - but if doing so, it should be a private method that is used within the class, but not available outside the class (which is what making it private does) |
||
|
||
def create_wait_list | ||
@waitlist = Queue.new | ||
@waiting.times do | ||
@waitlist.enqueue("Worker \##{@number}") | ||
@number += 1 | ||
end | ||
@waitlist | ||
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. same note about style of loop and using |
||
end | ||
|
||
def cycle | ||
|
||
def cycle | ||
die_roll = rand(1..6) | ||
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. This should be @roll as it's the variable that I indicated in the |
||
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 "<enter to cycle>\n" | ||
|
||
count = 0 | ||
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 |
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.
You shouldn't change the variables that I put here. If this is the spec that's given to you then these are the names of the variables that will be expected in other places, so if you change it here, other programs expecting these names won't work properly. Also no adding variables (
waitlist
) or removing variables (roll
)