Skip to content

Instantly share code, notes, and snippets.

@bragboy
Created December 31, 2015 12:43
Show Gist options
  • Save bragboy/bc8a7430f3cbf7ea8d5a to your computer and use it in GitHub Desktop.
Save bragboy/bc8a7430f3cbf7ea8d5a to your computer and use it in GitHub Desktop.
# models/floor.rb
class Floor
attr_accessor :main_corridors
attr_accessor :sub_corridors
def initialize(num_of_mc, num_of_sc)
@main_corridors = []
@sub_corridors = []
num_of_mc.times do
mc = Corridor.new
mc.lights.each {|light| light.turn_on}
mc.air_conditioners.each {|ac| ac.turn_on}
@main_corridors << mc
end
num_of_sc.times{ @sub_corridors << Corridor.new }
check_and_reset_power
end
def current_status
status = ''
@main_corridors.each_with_index do |mc, i|
status << " Main Corridor #{i+1} " + "\n"
status << mc.current_status
end
@sub_corridors.each_with_index do |sc, i|
status << " Sub Corridor #{i+1} " + "\n"
status << sc.current_status
end
status << " Max permissible for this floor: #{max_power}\n"
status << " Current Power consumption: #{self.power_consumption}\n"
status
end
def check_and_reset_power
return if power_consumption == max_power
if power_consumption < max_power #Scope to turn Sub Corridor ACs ON
@sub_corridors.each do |sc|
sc.air_conditioners.each {|ac| ac.turn_on}
end
else #Need to turn off the Sub Corridor ACs
catch (:done) do
@sub_corridors.each do |sc|
sc.air_conditioners.each do |ac|
ac.turn_off
throw :done if power_consumption <= max_power
end
end
end
end
end
def power_consumption
power_in_units = 0
power_in_units += @main_corridors.inject(0) {|r, mc| r + mc.power_consumption}
power_in_units += @sub_corridors.inject(0) {|r, sc| r + sc.power_consumption}
power_in_units
end
def max_power
@main_corridors.size * 15 + @sub_corridors.size * 10
end
end
@vimalraj485
Copy link

Can I get the program in Java?

@bragboy
Copy link
Author

bragboy commented May 17, 2021

@vimalraj485 that would mean at least 4x number of lines as Ruby.

@vimalraj485
Copy link

Bro I need that full Hotel controller project in java...Please help me!

@vimalraj485
Copy link

Bro atleast give some ideas to create...like what are the class to create and how join them like those stuff!

@vimalraj485
Copy link

I need to submit within this wednesday please help me Bro!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment