Skip to content
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
105 changes: 76 additions & 29 deletions README.md
Original file line number Diff line number Diff line change
@@ -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.

```
______
Expand All @@ -13,26 +14,79 @@ 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
-------

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
=> #<Airport:0x000000015b145e68 @capacity=3, @planes_at_airport=[]>
3.0.2 :002 > plane = Plane.new
=> #<Plane:0x000000014e027ea8>

```

**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
=> #<Airport:0x000000015b145e68 @capacity=3, @planes_at_airport=[]>
3.0.2 :002 > plane = Plane.new
=> #<Plane:0x000000014e027ea8>
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
=> #<Airport:0x000000012f026658 @capacity=3, @planes_at_airport=[]>
3.0.2 :002 > plane = Plane.new
=> #<Plane:0x000000014ea29ca8>
3.0.2 :003 > airport.planes_at_airport
=> []
3.0.2 :004 > airport.land(plane)
=> "#<Plane:0x000000014eb22998> has landed"
3.0.2 :005 > airport.land(plane)
=> "#<Plane:0x000000014ea2b5f8> has landed"
3.0.2 :006 > airport.take_off(plane)
=> "#<Plane:0x000000014ea2b5f8> has taken off"
3.0.2 :007 > airport.land(plane)
=> "#<Plane:0x000000012f82d568> has landed"
3.0.2 :008 > airport.planes_at_airport
=> [#<Plane:0x000000014eb22998>, #<Plane:0x000000012f82d568>]

```

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

class Airport
DEFAULT_CAPACITY = 3

@AKCDNG AKCDNG Apr 25, 2022

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Good use of a constant variable - omits the risk of using magic numbers


# makes capacity variable
attr_accessor :capacity

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

attr_accessor can be changed to attr_reader

attr_accessor :planes_at_airport

def initialize(capacity = DEFAULT_CAPACITY)
@planes_at_airport = []
@capacity = capacity

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

@capacity = capacity can be changed to @capacity

end

def land(_plane)

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

_plane argument is not used within method, potentially can be omitted with caution, (I think).

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

@AKCDNG AKCDNG Apr 25, 2022

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Good way to make weather sunny most of the time

end

end
18 changes: 18 additions & 0 deletions lib/plane.rb
Original file line number Diff line number Diff line change
@@ -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
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'
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
8 changes: 8 additions & 0 deletions spec/plane_spec.rb
Original file line number Diff line number Diff line change
@@ -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