-
-
Save anonymous/3a076794f1086831c12b418903120e8f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Refactor replace @type conditional polymorphism | |
diff --git a/lib/phone_plan.rb b/lib/phone_plan.rb | |
index e43ed70..bd7429a 100644 | |
--- a/lib/phone_plan.rb | |
+++ b/lib/phone_plan.rb | |
@@ -6,22 +6,7 @@ | |
class PhonePlan | |
def cost | |
- if type == "individual" | |
- number_of_phones * price | |
- elsif type == "family" | |
- number_of_extra_phones = number_of_phones - 1 | |
- cost_per_extra_phone = 10 | |
- | |
- price + (number_of_extra_phones * cost_per_extra_phone) | |
- elsif type == "business" | |
- subtotal = number_of_phones * price | |
- | |
- if number_of_phones < 50 | |
- subtotal * 0.75 | |
- else | |
- subtotal * 0.50 | |
- end | |
- end | |
+ @type.cost | |
end | |
diff --git a/spec/lib/phone_plan_spec.rb b/spec/lib/phone_plan_spec.rb | |
index 13c27eb..ecb88a6 100644 | |
--- a/spec/lib/phone_plan_spec.rb | |
+++ b/spec/lib/phone_plan_spec.rb | |
@@ -2,50 +2,5 @@ require "spec_helper" | |
describe PhonePlan do | |
describe "#cost" do | |
- context "when it is the individual plan" do | |
- it "multiples the price by the number of phones" do | |
- phone_plan = IndividualPhonePlan.new( | |
- number_of_phones: 3, | |
- price: 40, | |
- type: "individual" | |
- ) | |
- expect(phone_plan.cost).to eq 120 | |
- end | |
- end | |
- | |
- context "when it is the family plan" do | |
- it "discounts each additional phone" do | |
- phone_plan = FamilyPhonePlan.new( | |
- number_of_phones: 3, | |
- price: 40, | |
- type: "family" | |
- ) | |
- expect(phone_plan.cost).to eq 60 | |
- end | |
- end | |
- | |
- context "when it is the business plan" do | |
- context "when there are fewer than 50 phones" do | |
- it "returns the price with a 25% discount" do | |
- phone_plan = BusinessPhonePlan.new( | |
- number_of_phones: 3, | |
- price: 40, | |
- type: "business" | |
- ) | |
- expect(phone_plan.cost).to eq 90 | |
- end | |
- end | |
- | |
- context "when there are 50 or more phones" do | |
- it "returns the price with a 50% discount" do | |
- phone_plan = BusinessPhonePlan.new( | |
- number_of_phones: 51, | |
- price: 40, | |
- type: "business" | |
- ) | |
- expect(phone_plan.cost).to eq 1020 | |
- end | |
- end | |
- end | |
end | |
end | |
diff --git a/spec/lib/business_phone_plan_spec.rb b/spec/lib/business_phone_plan_spec.rb | |
new file mode 100644 | |
index 0000000..29e92c5 | |
--- /dev/null | |
+++ b/spec/lib/business_phone_plan_spec.rb | |
@@ -0,0 +1,28 @@ | |
+require 'spec_helper' | |
+ | |
+describe BusinessPhonePlan do | |
+ describe '#cost' do | |
+ context "when there are fewer than 50 phones" do | |
+ it "returns the price with a 25% discount" do | |
+ phone_plan = BusinessPhonePlan.new( | |
+ number_of_phones: 3, | |
+ price: 40, | |
+ type: "business" | |
+ ) | |
+ expect(phone_plan.cost).to eq 90 | |
+ end | |
+ end | |
+ | |
+ context "when there are 50 or more phones" do | |
+ it "returns the price with a 50% discount" do | |
+ phone_plan = BusinessPhonePlan.new( | |
+ number_of_phones: 51, | |
+ price: 40, | |
+ type: "business" | |
+ ) | |
+ expect(phone_plan.cost).to eq 1020 | |
+ end | |
+ end | |
+ end | |
+end | |
+ | |
diff --git a/spec/lib/family_phone_plan_spec.rb b/spec/lib/family_phone_plan_spec.rb | |
new file mode 100644 | |
index 0000000..c3815a0 | |
--- /dev/null | |
+++ b/spec/lib/family_phone_plan_spec.rb | |
@@ -0,0 +1,14 @@ | |
+require 'spec_helper' | |
+ | |
+describe FamilyPhonePlan do | |
+ describe '#cost' do | |
+ it "discounts each additional phone" do | |
+ phone_plan = FamilyPhonePlan.new( | |
+ number_of_phones: 3, | |
+ price: 40, | |
+ type: "family" | |
+ ) | |
+ expect(phone_plan.cost).to eq 60 | |
+ end | |
+ end | |
+end | |
diff --git a/spec/lib/individual_phone_plan_spec.rb b/spec/lib/individual_phone_plan_spec.rb | |
new file mode 100644 | |
index 0000000..e827159 | |
--- /dev/null | |
+++ b/spec/lib/individual_phone_plan_spec.rb | |
@@ -0,0 +1,14 @@ | |
+require 'spec_helper' | |
+ | |
+describe IndividualPhonePlan do | |
+ describe '#cost' do | |
+ it "multiples the price by the number of phones" do | |
+ phone_plan = IndividualPhonePlan.new( | |
+ number_of_phones: 3, | |
+ price: 40, | |
+ type: "individual" | |
+ ) | |
+ expect(phone_plan.cost).to eq 120 | |
+ end | |
+ end | |
+end | |
diff --git a/spec/lib/phone_plan_spec.rb b/spec/lib/phone_plan_spec.rb | |
index 13c27eb..ecb88a6 100644 | |
--- a/spec/lib/phone_plan_spec.rb | |
+++ b/spec/lib/phone_plan_spec.rb | |
@@ -2,50 +2,5 @@ require "spec_helper" | |
describe PhonePlan do | |
describe "#cost" do | |
- context "when it is the individual plan" do | |
- it "multiples the price by the number of phones" do | |
- phone_plan = IndividualPhonePlan.new( | |
- number_of_phones: 3, | |
- price: 40, | |
- type: "individual" | |
- ) | |
- expect(phone_plan.cost).to eq 120 | |
- end | |
- end | |
- | |
- context "when it is the family plan" do | |
- it "discounts each additional phone" do | |
- phone_plan = FamilyPhonePlan.new( | |
- number_of_phones: 3, | |
- price: 40, | |
- type: "family" | |
- ) | |
- expect(phone_plan.cost).to eq 60 | |
- end | |
- end | |
- | |
- context "when it is the business plan" do | |
- context "when there are fewer than 50 phones" do | |
- it "returns the price with a 25% discount" do | |
- phone_plan = BusinessPhonePlan.new( | |
- number_of_phones: 3, | |
- price: 40, | |
- type: "business" | |
- ) | |
- expect(phone_plan.cost).to eq 90 | |
- end | |
- end | |
- | |
- context "when there are 50 or more phones" do | |
- it "returns the price with a 50% discount" do | |
- phone_plan = BusinessPhonePlan.new( | |
- number_of_phones: 51, | |
- price: 40, | |
- type: "business" | |
- ) | |
- expect(phone_plan.cost).to eq 1020 | |
- end | |
- end | |
- end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment