Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tim's Airport Challenge #2500

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 20 additions & 43 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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.
56 changes: 56 additions & 0 deletions lib/airport.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
require 'airport'
require 'plane'

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You could add a Constant variable for capacity to reduce use of magic numbers.

class Airport

attr_accessor :hanger

def initialize(capacity = 5)

@capacity = capacity
@hanger = []

end

def random_weather

rand(10)

end

def can_take_off?
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like how you made two separate methods for 'can_take_off?' and 'can_land?' you've made it easy to understand.


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
6 changes: 6 additions & 0 deletions lib/plane.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
require 'airport'
require 'plane'

class Plane

end
71 changes: 71 additions & 0 deletions spec/airport_spec.rb
Original file line number Diff line number Diff line change
@@ -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
14 changes: 14 additions & 0 deletions spec/plane_spec.rb
Original file line number Diff line number Diff line change
@@ -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