Skip to content

Instantly share code, notes, and snippets.

@krokrob
Created July 23, 2019 08:37
Show Gist options
  • Save krokrob/c1309831ed4e188dfc0da7e5b1197705 to your computer and use it in GitHub Desktop.
Save krokrob/c1309831ed4e188dfc0da7e5b1197705 to your computer and use it in GitHub Desktop.
require_relative 'patient'
class Room
class OverCapacityReachedError < RuntimeError; end
attr_reader :capacity
attr_accessor :id
def initialize(attributes = {})
@capacity = attributes[:capacity]
@patients = attributes[:patients] || []
end
def add_patient(patient)
if full?
raise(OverCapacityReachedError, 'The room is full!')
else
@patients << patient
patient.room = self
end
end
def full?
@patients.length == @capacity
end
end
first_room = Room.new(capacity: 2)
puts first_room.capacity
second_room = Room.new(capacity: 4)
john = Patient.new(name: 'John', cured: true)
paul = Patient.new(name: 'Paul')
begin
first_room.add_patient(john)
first_room.add_patient(paul)
george = Patient.new(name: 'George')
first_room.add_patient(george)
p first_room
rescue Room::OverCapacityReachedError => error
puts error
# TODO put george in another room
end
puts john.room
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment