diff --git a/lib/airport.rb b/lib/airport.rb new file mode 100644 index 0000000000..30fd6893aa --- /dev/null +++ b/lib/airport.rb @@ -0,0 +1,46 @@ +require_relative 'plane' + +class Airport + attr_reader :stored_planes, :weather + CAPACITY = 5 + + def initialize + @stored_planes = [] + @capacity = CAPACITY + @weather = self.set_weather + end + + def land(plane) + fail 'Airport full!' if full? + fail 'Cannot land - stormy weather!' if @weather == 'Stormy' + @stored_planes << plane + end + + def depart(plane) + fail 'No planes' if empty? + fail 'Plane not in airport' if !@stored_planes.include?(plane) + @stored_planes.delete(plane) + end + + def adjust_capacity(number) + @capacity = number + end + + def set_weather + die = rand(100) + die > 100 ? 'Stormy' : 'Sunny' + end + + private + + attr_accessor :capacity + + def full? + @stored_planes.length >= @capacity + end + + def empty? + @stored_planes == [] + end + +end diff --git a/lib/plane.rb b/lib/plane.rb new file mode 100644 index 0000000000..fa2a84f30c --- /dev/null +++ b/lib/plane.rb @@ -0,0 +1,2 @@ +class Plane +end diff --git a/spec/airport_spec.rb b/spec/airport_spec.rb new file mode 100644 index 0000000000..55c3f82a87 --- /dev/null +++ b/spec/airport_spec.rb @@ -0,0 +1,55 @@ +require 'airport' + +describe Airport do + + describe '#land' do + it 'Can do so' do + plane = Plane.new + expect(subject.land(plane)).to eq([plane]) + end + + it 'Will not do so if the airport is full' do + Airport::CAPACITY.times { subject.land(Plane.new) } + expect { subject.land(Plane.new) }.to raise_error 'Airport full!' + end + + end + + describe '#depart' do + it 'Can instruct a stored plane to take off from the airport' do + plane = Plane.new + subject.land(plane) + subject.depart(plane) + expect(subject.stored_planes).to eq [] + end + + it 'Will not do so if there are no planes in the airport' do + expect { subject.depart(Plane.new) }.to raise_error 'No planes' + end + + it 'Will not take off if the plane is not in the airport' do + subject.land(Plane.new) + expect{subject.depart(Plane.new)}.to raise_error 'Plane not in airport' + end + end + + it 'can have an airport capacity that can be overwritten as appropriate' do + subject.adjust_capacity(1) + subject.land(Plane.new) + expect { subject.land(Plane.new) }.to raise_error 'Airport full!' + end + + it 'has a default capacity' do + Airport::CAPACITY.times { subject.land(Plane.new) } + expect { subject.land(Plane.new) }.to raise_error 'Airport full!' + end + + describe 'initialize' do + it 'will be stormy weather and the plane will not land' do + allow(subject).to receive(:set_weather).and_return('Stormy') + expect { subject.land(Plane.new) }.to raise_error 'Cannot land - stormy weather!' + end + end + + +end diff --git a/spec/plane_spec.rb b/spec/plane_spec.rb new file mode 100644 index 0000000000..f8dd714a06 --- /dev/null +++ b/spec/plane_spec.rb @@ -0,0 +1,5 @@ +require 'plane' + +describe Plane do + +end