-
Notifications
You must be signed in to change notification settings - Fork 2.2k
airport challenge #2516
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
base: main
Are you sure you want to change the base?
airport challenge #2516
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,46 @@ | ||
| require_relative 'plane' | ||
|
|
||
| class Airport | ||
| DEFAULT_CAPACITY = 3 | ||
|
|
||
| # makes capacity variable | ||
| attr_accessor :capacity | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| attr_accessor :planes_at_airport | ||
|
|
||
| def initialize(capacity = DEFAULT_CAPACITY) | ||
| @planes_at_airport = [] | ||
| @capacity = capacity | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| end | ||
|
|
||
| def land(_plane) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| 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 | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good way to make weather sunny most of the time |
||
| end | ||
|
|
||
| end | ||
| 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 |
| 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 |
| 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 |
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
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