diff --git a/README.md b/README.md index 6dd4fa6bc9..ba0ece5714 100644 --- a/README.md +++ b/README.md @@ -13,28 +13,18 @@ Airport Challenge ``` -Instructions +**Note** --------- -* 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 +I wasn't able to feature test the programme in IRB myself, because of an error that I can't get round: "`require': cannot load such file -- airport (LoadError)", which prevents me from testing in IRB. -Steps -------- +That said, I have pretty thoroughly gone over the unit tests - so hopefully the functional tests work as expected! -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 ------ +Summary +--------- -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: +This programme exists to help safely manage traffic at an airport. The user requirements this programme was built around are: -``` As an air traffic controller So I can get passengers to a destination I want to instruct a plane to land at an airport @@ -58,32 +48,19 @@ I want to prevent takeoff when weather is stormy As an air traffic controller 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. - -Please create separate files for every class, module and test suite. -In code review we'll be hoping to see: -* 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. - -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. - -* **Submit a pull request early.** - -* 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. +Usage +--------- +1. Clone the repo from GitHub +2. Run IRB, and require 'airport.rb' and 'plane.rb' +3. Create an instance of plane (plane = Plane.new) +4. Test the functionality of the programme, as below: + + - Check if a plane can take off + - Take off + - Check if the plane can land + - Land the plane + +5. Landing multiple planes may cause the airport to be at capacity, in which case an error will be thrown if trying to land further planes. +6. Poor weather conditions are randomly generated, and will occasionally cause errors to be thrown when attempting to land/take off. \ No newline at end of file diff --git a/lib/airport.rb b/lib/airport.rb new file mode 100644 index 0000000000..3a7fb45924 --- /dev/null +++ b/lib/airport.rb @@ -0,0 +1,56 @@ +require 'airport' +require 'plane' + +class Airport + + attr_accessor :hanger + + def initialize(capacity = 5) + + @capacity = capacity + @hanger = [] + + end + + def random_weather + + rand(10) + + end + + def can_take_off? + + return unless random_weather <= 3 + fail "It's blowin' a gale out there, hold your horses" + + end + + def take_off(plane) + + fail "That plane is not available for take off" unless @hanger.include?(plane) + @hanger.delete(plane) + + end + + def can_land? + + return unless random_weather <= 3 + fail "It's blowin' a gale out there, hold your horses" + + end + + def land(plane) + + fail "The airport is full, please wait" if full? + fail "That plane has already landed" if @hanger.include?(plane) + @hanger << plane + + end + + def full? + + @hanger.count >= @capacity + + end + +end diff --git a/lib/plane.rb b/lib/plane.rb new file mode 100644 index 0000000000..6654a80685 --- /dev/null +++ b/lib/plane.rb @@ -0,0 +1,6 @@ +require 'airport' +require 'plane' + +class Plane + +end diff --git a/spec/airport_spec.rb b/spec/airport_spec.rb new file mode 100644 index 0000000000..73ceac741d --- /dev/null +++ b/spec/airport_spec.rb @@ -0,0 +1,71 @@ +require 'airport' +require 'plane' + +describe Airport do + + let(:plane) { Plane.new } + + it "lands plane when instructed" do + + subject.land(plane) + expect(subject.hanger).to include plane + + end + + it "takes off from airport when instructed, and is no longer at the airport" do + + subject.hanger << plane + subject.take_off(plane) + expect(subject.hanger).not_to include plane + + end + + it "checks whether the airport hanger is full" do + + 5.times { subject.hanger << plane } + expect(subject.full?).to eq true + + end + + it "prevents landing when the aiport hanger is full" do + + 5.times { subject.hanger << plane } + expect { subject.land(plane) }.to raise_error("The airport is full, please wait") + + end + + it "prevents take off when weather is stormy" do + + allow(subject).to receive(:random_weather) { 3 } + expect { subject.can_take_off? }.to raise_error("It's blowin' a gale out there, hold your horses") + + end + + it "prevents landing when weather is stormy" do + + allow(subject).to receive(:random_weather) { 3 } + expect { subject.can_land? }.to raise_error("It's blowin' a gale out there, hold your horses") + + end + + it "random_weather should generate a random number between 1 and 10" do + + expect(subject.random_weather).to be < 11 + expect(subject.random_weather).to be > 0 + + end + + it "attempting to take off a plane that isn't present should raise an error" do + + expect { subject.take_off(plane) }.to raise_error("That plane is not available for take off") + + end + + it "attempting to land a plane that has already landed should raise an error" do + + subject.hanger << plane + expect { subject.land(plane) }.to raise_error("That plane has already landed") + + end + +end diff --git a/spec/plane_spec.rb b/spec/plane_spec.rb new file mode 100644 index 0000000000..3c31bae814 --- /dev/null +++ b/spec/plane_spec.rb @@ -0,0 +1,14 @@ +require 'airport' +require 'plane' + +describe Plane do + + let(:plane) { Plane.new } + + it "Instances of Plane should be true" do + + expect(plane).to eq plane + + end + +end