Protect the airport! Encapsulation and TDD

Emma Pr
5 min readMar 6, 2021

After a lovely first week, working through the Boris Bikes challenge, I was quite excited to start working on the weekend challenge, in which we were asked to design an airport, that would be able to have planes arriving and departing, and that would judge the safety of the weather. Great challenge! I decided to spice it up a bit and add some stuff, as it did not take me a long time to finish the initial challenge.

I decided to give it a safety check, which would have a couple of safety breaches, and resolve them as well. For this airport challenge, just like the weather is randomly picked, I wanted the safety breaches to be randomized as well. I started with setting up a spec file for my safety breach class.

require ‘safety_breach’

describe SafetyBreach do
end

Next, I created a function for all the safety breaches that I wanted to use and gave them a message to return.

describe ‘#unattended_item’ do
it { is_expected.to respond_to :unattended_item }
it ‘should give a warning about the threat’ do
message = ‘Warning, unattended item spotted, safety protocol started, safety level medium.’
expect { subject.unattended_item }.to output(message).to_stdout
end
end

describe ‘#drugs_on_person’ do
it { is_expected.to respond_to :drugs_on_person }
it ‘should give a warning about the threat’ do
message = ‘Drugs found on a person, authorities have been notified, stay alert.’
expect { subject.drugs_on_person }.to output(message).to_stdout
end
end

describe ‘#drugs_in_package’ do
it { is_expected.to respond_to :drugs_in_package }
it ‘should give a warning about the threat’ do
message = ‘Drugs found in a package, authorities have been notified, stay alert.’
expect { subject.drugs_in_package }.to output(message).to_stdout
end
end

describe ‘#weapon’ do
it { is_expected.to respond_to :weapon }
it ‘should give a warning about the threat’ do
message = ‘weapons spotted, no planes will take off until safety restored, authorities have been notified, safety level dangerous.’
expect { subject.weapon }.to output(message).to_stdout
end
end

describe ‘#passenger_unwell’ do
it { is_expected.to respond_to :passenger_unwell }
it ‘should give a warning about the threat’ do
message = ‘A passenger seems unwell, care personal have been notified, safety threat low.’
expect { subject.passenger_unwell }.to output(message).to_stdout
end
end

describe ‘#agressive_passenger’ do
it { is_expected.to respond_to :agressive_passenger }
it ‘should give a warning about the threat’ do
message = ‘A passenger is acting aggressive, authorities have been notified, stay alert, safety level medium.’
expect { subject.agressive_passenger }.to output(message).to_stdout
end
end

describe ‘#terrorist’ do
it { is_expected.to respond_to :terrorist }
it ‘should give a warning about the threat’ do
message = ‘A terrorist threat has been made at this airport. Immediate close-down, nobody is to leave until the safety level is restored. Safety level extremely dangerous.’
expect(subject.terrorist).to eq message
end
end

Next, I created all these functions.

def unattended_item
print ‘Warning, unattended item spotted, safety protocol started, safety level medium.’
end

def drugs_on_person
print ‘Drugs found on a person, authorities have been notified, stay alert.’
end

def drugs_in_package
print ‘Drugs found in a package, authorities have been notified, stay alert.’
end

def weapon
print ‘weapons spotted, no planes will take off until safety restored, authorities have been notified, safety level dangerous.’
end

def passenger_unwell
print ‘A passenger seems unwell, care personal have been notified, safety threat low.’
end

def agressive_passenger
print ‘A passenger is acting aggressive, authorities have been notified, stay alert, safety level medium.’
end

def terrorist
‘A terrorist threat has been made at this airport. Immediate close-down, nobody is to leave until the safety level is restored. Safety level extremely dangerous.’
End

Now, I did not want all these functions to have to be called individually, I wanted to check for security breaches and return the right message, so I had to create another function that would be able to do that.

describe ‘#safety_assesment’ do
it { is_expected.to respond_to :safety_assesment }
end

```

attr_reader :threat_scale

def initialize(threat_scale = [1, 2, 3, 4, 5, 6, 7, 8])
@possible_threat = threat_scale.sample
end

def safety_assesment
case @possible_threat
when 1
‘No safety breach.’
when 2
unattended_item
when 3
drugs_on_person
when 4
drugs_in_package
when 5
weapon
when 6
passenger_unwell
when 7
agressive_passenger
when 8
terrorist
end
end

```

This function, in combination with the initiated thread_scale, can randomly pick any of the threats that I created and return the appropriate message. Next, I wanted to look at possible responses to the threats, and return the appropriate messages for them as well. For example:

def unattended_item
@possible_threat = [1, 4, 8].sample

print ‘Warning, unattended item spotted, safety protocol started, safety level medium.’
safety_assesment
end

I did that for all functions, except for the terrorist threat. With that one I want to shut down the airport, so that it can only be manually overwritten, to not risk accidentally letting terrorists get away with their actions. This way, every time this case is called, it will either result in safety, or in a terrorist attack. The next step was to use this class, inside of my airport class. To not confuse all of my previously created tests, I set a default threat_scale that would never return a terrorist.

let(:safe_situation) { SafetyBreach.new([1]) }
let(:airport) { Airport.new(‘Schiphol’, capacity, weather, safe_situation) }

This way, I can change my input when I want to introduce a terrorist in my tests, but I won’t risk getting a random terrorist taking over another test. I initialized the SafetyBreach class as well, with a default input of an array from 1 to 8. Next, I wanted to create functions that would check the class.

describe ‘#safety_breach’ do
it { is_expected.to respond_to :safety_breach }
end

describe ‘#terrorist_protocol’ do
it { is_expected.to respond_to :terrorist_protocol }
it ‘should know if the airport is under a terrorist threat’ do
airport_under_attack = Airport.new(‘Heathrow’, capacity, weather, SafetyBreach.new([8]))
expect(airport_under_attack.safety_breach).to eq true
end

As you can see, I gave a different input in my last test, that would always return the presence of a terrorist.

def safety_breach
@safety_check_result = @safety_status.safety_assesment
terrorist_protocol
end

def terrorist_protocol
@safety_check_result.include?(‘terrorist’)
End

These functions allow me to return all messages to tell me the security level of the airport, and it will give me a boolean. This Boolean, I can now use to set an error. I want to make sure that no planes can leave the airport, or come in. this Boolean is dependent on the word terrorist, which only exists in the message that is returned when the airport is being threatened by a terrorist.

Describe ‘arrive’ do
it ‘should not allow planes to arrive when under terrorist attack.’ do
airport_under_attack = Airport.new(‘Heathrow’, capacity, weather, SafetyBreach.new([8]))
expect { airport_under_attack.arrive(plane) }.to raise_error ‘This airport is in a quarantaine situation, no access.’
End
End

Describe ‘depart’ do
it ‘should not allow planes to leave whilst under terrorist attack’ do
airport.arrive(plane)
airport_under_attack = Airport.new(‘Heathrow’, capacity, weather, SafetyBreach.new([8]))
expect { airport_under_attack.depart(plane) }.to raise_error ‘This airport is in a quirantaine situation, no planes can leave.’
End
End

Once again, I changed the input of the safety breach to something that will always return a terrorist attack. I set appropriate error messages to fit the situation.

def arrive(plane)
fail ‘This airport is in a quarantine situation, no access.’ if safety_breach
fail ‘Airport is at max capacity.’ if full?
fail ‘It is not safe to land here at the moment.’ unless check_weather

arriving_planes(plane)
end

def depart(plane)
fail ‘This airport is in a quarantine situation, no planes can leave.’ if safety_breach
fail ‘There are no planes at your disposal.’ if empty?
fail ‘It is not safe to depart at the moment.’ unless check_weather

@planes.delete(plane)
End

Next, I added these errors into the already existing functions. I put it before any other errors would be checked because safety is the most important thing! This might look confusing, as the project contains many more files, cases, modules, and other dependencies. If you are curious to see what the project looks like in total, check out my repo on Github: https://github.com/Emmapr123/airport_challenge

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

No responses yet

Write a response