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

Airport Challenge Stevie Spiegl #2510

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 5 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
57 changes: 43 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,32 @@
**Airport Challenge**

A sadly incomplete attempt at the airport challenge. Ideal for those who would like to simultaneously land their plane in two different airports at the same time.

## Getting started

- run git clone https://github.com/S-Spiegl/airport_challenge.git
- Run the command `gem install bundler` (if you don't have bundler already)

## Usage

- Run irb and require './lib/airport.rb'
- Instantiate a new airport e.g. heathrow = Airport.new
- Instantiate planes e.g. boeing_747 = plane.new
- Land and fly those planes e.g. heathrow.land(boeing_747)
- If you want to change airport capacity, do so when instantiating a new airport e.g. la_guardia = Airport.new(10)
- You can also control the weather here if you don't want to leave it to chance... e.g. jfk = Airport.new(10, 'fine')

## Running tests

run rspec

**Other notes:**

- I needed to spend some time practising on Boris Bikes to get my head around this one, and as a result I wasn't able to completely finish. Planes can take off and land, default capacity can be overridden by passing the airport an argument when instantiating it, and exceptions are raised according to the weather, but I was didn't have time to learn about stubbing to ensure regular testing, and didn't have time to work out how to defend against all edge cases. Consequently, it's possible to land the same plane into two different airports, or into the same airport twice etc. Though at least planes can only take off from airports that they're in, as far as I can tell. I'll look more into how to guard against these errors - I'm assuming some kind of exception? - but will need to submit it in its current state for now.
- If you run the rspec, you'll get different errors according to what weather you get, but the lowest number of errors is 2, which seems to be to do with a single plane being returned in an array...
- Also, I had planned to create a weather generator as a separate file but was struggling to call this so had to just use a constant.
- In terms of outside help, I took a quick look at this https://medium.com/@charlottebrf/makers-academy-day-5-8dc1c792cda5, which I stumbled upon while googling how to create a random weather generator, and which gave me the idea of using a constant for the capacity (and subsequently for the weather, as I never got onto making a generator in a separate class). Other than that, I also reached out to my mentor, Nico Cortese, Gawain Hewitt, and Jimmy Lyons (big thank-you to all three).

Airport Challenge
=================

Expand Down Expand Up @@ -35,29 +64,29 @@ Task
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:

```
As an air traffic controller
So I can get passengers to a destination
As an air traffic controller
So I can get passengers to a destination
I want to instruct a plane to land at an airport

As an air traffic controller
So I can get passengers on the way to their destination
As an air traffic controller
So I can get passengers on the way to their destination
I want to instruct a plane to take off from an airport and confirm that it is no longer in the airport

As an air traffic controller
To ensure safety
I want to prevent landing when the airport is full
As an air traffic controller
To ensure safety
I want to prevent landing when the airport is full

As the system designer
So that the software can be used for many different airports
I would like a default airport capacity that can be overridden as appropriate

As an air traffic controller
To ensure safety
I want to prevent takeoff when weather is stormy
As an air traffic controller
To ensure safety
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
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.
Expand All @@ -72,7 +101,7 @@ 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.
* 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.

Expand Down
31 changes: 31 additions & 0 deletions lib/airport.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
require_relative 'plane'

class Airport

CAPACITY = 5
WEATHER = {0 => "stormy", 1 => "fine", 2 => "fine", 3 => "fine"}


def initialize(capacity = CAPACITY, weather = WEATHER[rand(4)])
@planes = []
@capacity = capacity
@weather = weather
end

def release_plane(plane)
fail 'weather too stormy for take-off' unless @weather != "stormy"
@planes.pop
end

def land_plane(plane)

fail 'airport full' if @planes.length == @capacity
fail 'weather too stormy for landing' unless @weather != "stormy"
@planes << plane
end

attr_accessor :planes

Choose a reason for hiding this comment

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

I think that the ruby convention is to use attr_reader over attr_accessor, and that they go before the methods in the class

attr_accessor :capacity
attr_accessor :weather

end
5 changes: 5 additions & 0 deletions lib/plane.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class Plane
def working?
true
end
end
5 changes: 5 additions & 0 deletions lib/weather_generator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class WeatherGenerator
def generate_weather
weather = {0 => "sunny", 1 => "stormy"}
end
end
58 changes: 58 additions & 0 deletions spec/airport_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
require 'airport'

describe Airport do

it 'responds to planes' do
expect(subject).to respond_to(:planes)
end

it 'responds to release_plane' do
expect(subject).to respond_to(:release_plane)
end

describe '#release_plane' do
it 'releases a plane' do
plane = Plane.new
subject.land_plane(plane)
expect(subject.release_plane(plane)).to eq plane
end
end

it 'responds to land_plane with one argument' do
expect(subject).to respond_to(:land_plane).with(1).argument
end

describe '#land_plane' do
it 'prevents landing when airport is full' do
5.times {subject.land_plane Plane.new}
expect { subject.land_plane Plane.new }.to raise_error 'airport full'
end
end


it 'has a default capacity that can be overridden' do
expect(subject.capacity).to eq 5
end

it 'lands something' do
plane = Plane.new
expect(subject.land_plane(plane)).to eq plane
end

it 'returns landed planes' do
plane = Plane.new
subject.land_plane(plane)
expect(subject.planes).to eq plane
end

it 'prevents landing if weather is stormy' do
subject.weather = "stormy"
expect { subject.land_plane(Plane.new) }.to raise_error 'weather too stormy for landing'
end

it 'prevents takeoff if weather is stormy' do
subject.weather = "stormy"
expect { subject.release_plane(Plane.new) }.to raise_error 'weather too stormy for take-off'
end

end
6 changes: 6 additions & 0 deletions spec/plane_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
require 'plane'
describe Plane do
it 'responds to working' do
expect(subject).to respond_to(:working?)
end
end
7 changes: 7 additions & 0 deletions spec/weather_generator_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
require 'weather_generator'

describe WeatherGenerator do
it 'responds to generate_weather' do
expect(subject).to respond_to(:generate_weather)
end
end