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

Mostly complete but no Rspec for weather #2497

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
35 changes: 35 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,26 +38,61 @@ We have a request from a client to write the software to control the flow of pla
As an air traffic controller
So I can get passengers to a destination
I want to instruct a plane to land at an airport
```
| Objects | Messages |
| ---------- | ------------- |
| Air Traffic Controller | |
| Plane | |
| Airport | land |

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

| Objects | Messages |
| ---------- | ------------- |
| Air Traffic Controller | |
| Plane | |
| Airport | take_off |

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

| Objects | Messages |
| ---------- | ------------- |
| Air Traffic Controller | |
| Plane | |
| Airport | land, 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

| Objects | Messages |
| ---------- | ------------- |
| System Designer | |
| Airport | full |

As an air traffic controller
To ensure safety
I want to prevent takeoff when weather is stormy

| Objects | Messages |
| ---------- | ------------- |
| Air Traffic Controller | |
| Plane | |
| Airport | take_off, stormy? |

As an air traffic controller
To ensure safety
I want to prevent landing when weather is stormy

| Objects | Messages |
| ---------- | ------------- |
| Air Traffic Controller | |
| Plane | |
| Airport | land, 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 Down
53 changes: 53 additions & 0 deletions lib/airport.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
require_relative 'plane'

class Airport
attr_reader :planes, :capacity, :weather
DEFAULT_CAPACITY = 50

def initialize(capacity = DEFAULT_CAPACITY, weather = false)
@planes = []
@capacity = capacity
@weather = weather
end

def land(plane)
fail "The airport is already full" if full?
if @planes.include? plane
"That plane is already in the airport"
else
fail "The weather is too dangerous" if stormy?
@planes.push(plane)
"#{plane} has successfully landed."
end
end

def take_off(plane)
if @planes.include? plane
fail "The weather is too dangerous" if stormy?
@planes.delete(plane)
"#{plane} has successfully departed."
else
"That plane is not in the airport"
end
end


def full?
@planes.length >= @capacity
end

def stormy?
@random = random
if (1..19).include?(@random)
@weather = false
else
@weather = true
end
end

private

def random
rand(1..20)
end
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



end
88 changes: 88 additions & 0 deletions spec/airport_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
require_relative '../lib/airport.rb'

describe Airport do

let(:plane) { double :plane }
let(:plane2) { double :plane }


it "is an instance of airport" do
expect(subject).to be_an_instance_of(Airport)
end

it "can receive the land command" do
expect(subject).to respond_to(:land)
end

it "can receive the take off command" do
expect(subject).to respond_to(:take_off)
end

it "can receive the full? command" do
expect(subject).to respond_to(:full?)
end

it "can receive the stormy? command" do
expect(subject).to respond_to(:stormy?)
end

it "should be possible to declare the capacity of the airport" do
a = Airport.new(100)
expect(a.capacity).to eq 100
end

it "will prevent planes from landing when it is at full capacity" do
a = Airport.new(1)
a.land(plane2)
expect{ a.land(plane) }.to raise_error("The airport is already full")
end


it "will successfully land planes when not stormy" do
end

it "will successfully take off planes when not stormy" do
end

it "will not land planes when stormy" do
end

it "will not take off planes when stormy" do
end

describe "after an instance of Plane has been instructed to land when capacity has not yet been reached" do
before do
subject.land(plane)
end

it "will store the plane in the airport" do
expect(subject.planes.size).to eq 1
end

it "will report the plane's landing" do
expect("#{plane} has successfully landed.")
end

it "will not allow the plane to land again" do
expect(subject.land(plane)).to eq "That plane is already in the airport"
end

describe "after that instance of Plane has been instructed to take off again" do
before do
subject.take_off(plane)
end

it "will remove the plane from storage" do
expect(subject.planes.size).to eq 0
end

it "will report the plane's departure" do
expect("#{plane} has successfully departed.")
end

it "will not allow the plane to take off again" do
expect(subject.take_off(plane)).to eq "That plane is not in the airport"
end
end
end
end
10 changes: 10 additions & 0 deletions spec/plane_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
require_relative '../lib/plane.rb'

describe Plane do

it "is an instance of Plane" do
expect(subject).to be_an_instance_of(Plane)
end

end