diff --git a/README.md b/README.md index 6dd4fa6bc9..3d4d12fd15 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,6 @@ Airport Challenge ================= +The program simulates the control flow of planes at an airport. Planes can land and take off, provided there is no storm. ``` ______ @@ -13,13 +14,12 @@ Airport Challenge ``` -Instructions + +Walkthrough --------- -* Feel free to use google, your notes, books, etc. but work on your own -* If you refer to the solution of another coach or student, please put a link to that in your README -* If you have a partial solution, **still check in a partial solution** -* You must submit a pull request to this repo with your code by 10am Monday morning +Please see below a walkthrough of the steps + Steps ------- @@ -27,12 +27,66 @@ Steps 1. Fork this repo, and clone to your local machine 2. Run the command `gem install bundler` (if you don't have bundler already) 3. When the installation completes, run `bundle` -4. Complete the following task: -Task + +Instructions +---------- + +**Getting Started** +Navigate to the project directory and start a new IRB requiring the `airport.rb` file: + +``` +laura@Lauras-Air airport_challenge % irb -r ./lib/airport.rb +3.0.2 :001 > airport = Airport.new + => # +3.0.2 :002 > plane = Plane.new + => # + +``` + +**The Airport** +The default airport capacity is 3 planes. Use `planes_at_airport` method to see the planes currently stored at the airport. + +``` +laura@Lauras-Air airport_challenge % irb -r ./lib/airport.rb +3.0.2 :001 > airport = Airport.new + => # +3.0.2 :002 > plane = Plane.new + => # +3.0.2 :003 > airport.planes_at_airport + => [] +3.0.2 :004 > + +``` + +**The Planes** +Use `land` and `take_off` to navigate the planes. + +``` +laura@Lauras-Air airport_challenge % irb -r ./lib/airport.rb +3.0.2 :001 > airport = Airport.new + => # +3.0.2 :002 > plane = Plane.new + => # +3.0.2 :003 > airport.planes_at_airport + => [] +3.0.2 :004 > airport.land(plane) + => "# has landed" +3.0.2 :005 > airport.land(plane) + => "# has landed" +3.0.2 :006 > airport.take_off(plane) + => "# has taken off" +3.0.2 :007 > airport.land(plane) + => "# has landed" +3.0.2 :008 > airport.planes_at_airport + => [#, #] + +``` + +Testing ----- -We have a request from a client to write the software to control the flow of planes at an airport. The planes can land and take off provided that the weather is sunny. Occasionally it may be stormy, in which case no planes can land or take off. Here are the user stories that we worked out in collaboration with the client: +**User Stories** ``` As an air traffic controller @@ -60,30 +114,23 @@ To ensure safety I want to prevent landing when weather is stormy ``` -Your task is to test drive the creation of a set of classes/modules to satisfy all the above user stories. You will need to use a random number generator to set the weather (it is normally sunny but on rare occasions it may be stormy). In your tests, you'll need to use a stub to override random weather to ensure consistent test behaviour. - -Your code should defend against [edge cases](http://programmers.stackexchange.com/questions/125587/what-are-the-difference-between-an-edge-case-a-corner-case-a-base-case-and-a-b) such as inconsistent states of the system ensuring that planes can only take off from airports they are in; planes that are already flying cannot take off and/or be in an airport; planes that are landed cannot land again and must be in an airport, etc. - -For overriding random weather behaviour, please read the documentation to learn how to use test doubles: https://www.relishapp.com/rspec/rspec-mocks/docs . There’s an example of using a test double to test a die that’s relevant to testing random weather in the test. +**Edge Cases** -Please create separate files for every class, module and test suite. +* ensuring planes can only take off from airports they are in +* planes that are already flying cannot take off and/or be in an airport +* planes that are landed cannot land again and must be in an airport, etc. -In code review we'll be hoping to see: +**Running Tests** -* All tests passing -* High [Test coverage](https://github.com/makersacademy/course/blob/main/pills/test_coverage.md) (>95% is good) -* The code is elegant: every class has a clear responsibility, methods are short etc. +Tests were run using RSpec (installed as per instructions above), +to run tests: -Reviewers will potentially be using this [code review rubric](docs/review.md). Referring to this rubric in advance will make the challenge somewhat easier. You should be the judge of how much challenge you want this at this moment. - -**BONUS** - -* Write an RSpec **feature** test that lands and takes off a number of planes - -Note that is a practice 'tech test' of the kinds that employers use to screen developer applicants. More detailed submission requirements/guidelines are in [CONTRIBUTING.md](CONTRIBUTING.md) - -Finally, don’t overcomplicate things. This task isn’t as hard as it may seem at first. +``` +cd navigate/to/airport_challenge -* **Submit a pull request early.** +rspec +``` -* Finally, please submit a pull request before Monday at 10am with your solution or partial solution. However much or little amount of code you wrote please please please submit a pull request before Monday at 10am. +Thoughts +----- +The program is not finished, methods should be shorter and could be re-organised, tests could be restructured and organised. \ No newline at end of file diff --git a/lib/airport.rb b/lib/airport.rb new file mode 100644 index 0000000000..b0192ed3f9 --- /dev/null +++ b/lib/airport.rb @@ -0,0 +1,46 @@ +require_relative 'plane' + +class Airport + DEFAULT_CAPACITY = 3 + + # makes capacity variable + attr_accessor :capacity + attr_accessor :planes_at_airport + + def initialize(capacity = DEFAULT_CAPACITY) + @planes_at_airport = [] + @capacity = capacity + end + + def land(_plane) + fail 'Airport full' if full? + fail 'Not able to land due to storm' if storm? + + planes_at_airport << Plane.new + landed_plane = planes_at_airport.last + "#{landed_plane} has landed" + end + + def take_off(_plane) + fail 'No planes available' if empty? + fail 'Not able to take off due to storm' if storm? + + departed_plane = planes_at_airport.pop + "#{departed_plane} has taken off" + end + + private + + def full? + planes_at_airport.count >= capacity + end + + def empty? + planes_at_airport.empty? + end + + def storm? + rand(10) >= 8 + end + +end diff --git a/lib/plane.rb b/lib/plane.rb new file mode 100644 index 0000000000..63820dfa1b --- /dev/null +++ b/lib/plane.rb @@ -0,0 +1,18 @@ +class Plane +end + # def initialize + # @airport = 0 + # end + + # def airport + # @airport + # end + + # def land + # fail 'Airport full' if @airport >= 10 + # landport = Airport.new + # end + + # def take_off + # "plane has left the airport" + # end diff --git a/spec/airport_spec.rb b/spec/airport_spec.rb new file mode 100644 index 0000000000..14af49326e --- /dev/null +++ b/spec/airport_spec.rb @@ -0,0 +1,58 @@ +require 'airport' +require 'plane' + +describe Airport do + let(:plane) { double :plane } + + describe '#land' do + it { is_expected.to respond_to(:land).with(1).argument } + + it 'raises an error when airport full' do + allow(subject).to receive(:storm?).and_return(false) + allow(subject).to receive(:full?).and_return(true) + expect { subject.land(plane) }.to raise_error 'Airport full' + end + + it 'does not raise an error when airport available' do + allow(subject).to receive(:storm?).and_return(false) + allow(subject).to receive(:full?).and_return(false) + expect { subject.land(plane) }.not_to raise_error + end + + it 'raises an error when bad weather' do + allow(subject).to receive(:full?).and_return(false) + allow(subject).to receive(:storm?).and_return(true) + expect { subject.land(plane) }.to raise_error 'Not able to land due to storm' + end + + it 'does not raise error when good weather' do + allow(subject).to receive(:full?).and_return(false) + allow(subject).to receive(:storm?).and_return(false) + expect { subject.land(plane) }.not_to raise_error + end + end + + describe '#take_off' do + it { is_expected.to respond_to(:take_off).with(1).argument } + + it 'raises an error when no planes at airport to take_off' do + # allow(subject).to receive(:storm?).and_return(false) + allow(subject).to receive(:empty?).and_return(true) + expect { subject.take_off(plane) }.to raise_error 'No planes available' + end + + it 'does not raise an error when planes at airport to take_off' do + allow(subject).to receive(:storm?).and_return(false) + allow(subject).to receive(:empty?).and_return(false) + expect { subject.take_off(plane) }.not_to raise_error + end + + it 'raises an error when bad weather for take off' do + allow(subject).to receive(:empty?).and_return(false) + allow(subject).to receive(:storm?).and_return(true) + expect { subject.take_off(plane) }.to raise_error 'Not able to take off due to storm' + end + + end + +end diff --git a/spec/plane_spec.rb b/spec/plane_spec.rb new file mode 100644 index 0000000000..ea71aa0014 --- /dev/null +++ b/spec/plane_spec.rb @@ -0,0 +1,8 @@ +require 'plane' + +describe Plane do + it 'can create an instance of plane' do + plane = Plane.new + expect(plane).to be_kind_of(Plane) + end +end