-
Notifications
You must be signed in to change notification settings - Fork 0
feat: fuel-injection을 통한 상속 이해 #2
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
Open
wapago
wants to merge
1
commit into
feature/coffee-shop
Choose a base branch
from
feature/fuel-injection
base: feature/coffee-shop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| package fuel; | ||
|
|
||
| public class Avante extends Car{ | ||
| private final int DISTANCE_PER_LITER = 15; | ||
|
|
||
| public Avante(int distanceToTravel) { | ||
| super(distanceToTravel); | ||
| } | ||
|
|
||
| @Override | ||
| double getDistancePerLiter() { | ||
| return DISTANCE_PER_LITER; | ||
| } | ||
|
|
||
| @Override | ||
| double getTripDistance() { | ||
| return super.distanceToTravel; | ||
| } | ||
|
|
||
| @Override | ||
| public String getName() { | ||
| return "Avante"; | ||
| } | ||
| } | ||
This file contains hidden or 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| package fuel; | ||
|
|
||
| public abstract class Car { | ||
| protected final int distanceToTravel; | ||
|
|
||
| protected Car(int distanceToTravel) { | ||
| this.distanceToTravel = distanceToTravel; | ||
| } | ||
|
|
||
| /** | ||
| * 리터당 이동 거리. 즉, 연비 | ||
| */ | ||
| abstract double getDistancePerLiter(); | ||
|
|
||
| /** | ||
| * 여행하려는 거리 | ||
| */ | ||
| abstract double getTripDistance(); | ||
|
|
||
| /** | ||
| * 차종의 이름 | ||
| */ | ||
| public abstract String getName(); | ||
|
|
||
| /** | ||
| * 주입해야할 연료량을 구한다. | ||
| */ | ||
| public double getChargeQuantity() { | ||
| return getTripDistance() / getDistancePerLiter(); | ||
| } | ||
| } |
This file contains hidden or 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| package fuel; | ||
|
|
||
| public class K5 extends Car{ | ||
| private final int DISTANCE_PER_LITER = 13; | ||
|
|
||
| public K5(int distanceToTravel) { | ||
| super(distanceToTravel); | ||
| } | ||
|
|
||
| @Override | ||
| double getDistancePerLiter() { | ||
|
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. 접근 제어자 붙여주세요 |
||
| return DISTANCE_PER_LITER; | ||
| } | ||
|
|
||
| @Override | ||
| double getTripDistance() { | ||
| return super.distanceToTravel; | ||
| } | ||
|
|
||
| @Override | ||
| public String getName() { | ||
| return "K5"; | ||
| } | ||
| } | ||
This file contains hidden or 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| package fuel; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
|
|
||
| public class RentCompany { | ||
| private List<Car> cars = new ArrayList<>(); | ||
|
|
||
| public static RentCompany create() { | ||
| return new RentCompany(); | ||
| } | ||
|
|
||
| public void addCar(Car car) { | ||
| cars.add(car); | ||
| } | ||
|
|
||
| public String generateReport() { | ||
| StringBuilder sb = new StringBuilder(); | ||
| for (Car car : cars) { | ||
| sb.append(car.getName()); | ||
| sb.append(" : "); | ||
| sb.append((int) car.getChargeQuantity()); | ||
| sb.append("리터"); | ||
| sb.append("\n"); | ||
| } | ||
| return sb.toString(); | ||
| } | ||
| } |
This file contains hidden or 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| package fuel; | ||
|
|
||
| public class Sonata extends Car{ | ||
| private final int DISTANCE_PER_LITER = 10; | ||
|
|
||
| public Sonata(int distanceToTravel) { | ||
| super(distanceToTravel); | ||
| } | ||
|
|
||
| @Override | ||
| double getDistancePerLiter() { | ||
| return DISTANCE_PER_LITER; | ||
| } | ||
|
|
||
| @Override | ||
| double getTripDistance() { | ||
| return super.distanceToTravel; | ||
| } | ||
|
|
||
| @Override | ||
| public String getName() { | ||
| return "Sonata"; | ||
| } | ||
| } |
This file contains hidden or 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| import fuel.Avante; | ||
| import fuel.K5; | ||
| import fuel.RentCompany; | ||
| import fuel.Sonata; | ||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| import static org.junit.jupiter.api.Assertions.*; | ||
|
|
||
| public class RentCompanyTest { | ||
| private static final String NEWLINE = "\n"; | ||
|
|
||
| @Test | ||
| public void report() throws Exception { | ||
| RentCompany company = RentCompany.create(); // factory method를 사용해 생성 | ||
| company.addCar(new Sonata(150)); | ||
| company.addCar(new K5(260)); | ||
| company.addCar(new Sonata(120)); | ||
| company.addCar(new Avante(300)); | ||
| company.addCar(new K5(390)); | ||
|
|
||
| String report = company.generateReport(); | ||
| assertEquals(report, "Sonata : 15리터" + NEWLINE + | ||
| "K5 : 20리터" + NEWLINE + | ||
| "Sonata : 12리터" + NEWLINE + | ||
| "Avante : 20리터" + NEWLINE + | ||
| "K5 : 30리터" + NEWLINE); | ||
| } | ||
|
|
||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
접근제어자가 있어야할 것 같습니다