Skip to content
Merged
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
1 change: 1 addition & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ repositories {
}

dependencies {
implementation 'org.jetbrains:annotations:20.1.0'
testImplementation "org.junit.jupiter:junit-jupiter:5.7.2"
testImplementation "org.assertj:assertj-core:3.19.0"
}
Expand Down
41 changes: 41 additions & 0 deletions src/main/java/rentcompany/Avante.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package rentcompany;

import java.util.Objects;

public class Avante extends Car {

private static final double fuelEfficiency = 15;
private final double distance;

public Avante(final double distance) {
this.distance = distance;
}

@Override
double getDistancePerLiter() {
return fuelEfficiency;
}

@Override
double getTripDistance() {
return distance;
}

@Override
String getName() {
return "Avante";
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Avante avante = (Avante) o;
return Double.compare(avante.distance, distance) == 0;
}

@Override
public int hashCode() {
return Objects.hash(distance);
}
}
31 changes: 31 additions & 0 deletions src/main/java/rentcompany/Car.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package rentcompany;

public abstract class Car implements Vehicle{

/**
* 리터당 이동 거리. 즉, 연비
*/
abstract double getDistancePerLiter();

/**
* 여행하려는 거리
*/
abstract double getTripDistance();

/**
* 차종의 이름
*/
abstract String getName();

/**
* 주입해야할 연료량을 구한다.
*/
double getChargeQuantity() {
return getTripDistance() / getDistancePerLiter();
}

@Override
public String report() {
return getName() + " : " + (int)getChargeQuantity() + "리터";
}
}
27 changes: 27 additions & 0 deletions src/main/java/rentcompany/K5.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package rentcompany;

public class K5 extends Car {

private static final double fuelEfficiency = 13;
private final double distance;

public K5(final double distance) {
this.distance = distance;

}

@Override
double getDistancePerLiter() {
return fuelEfficiency;
}

@Override
double getTripDistance() {
return distance;
}

@Override
String getName() {
return "K5";
}
}
25 changes: 25 additions & 0 deletions src/main/java/rentcompany/RentCompany.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package rentcompany;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

public class RentCompany {

private static final String NEWLINE = System.getProperty("line.separator");
private final List<Vehicle> vehicleList = new ArrayList<>();

public static RentCompany create() {
return new RentCompany();
}

public void addCar(final Vehicle vehicle) {
vehicleList.add(vehicle);
}

public String generateReport() {
return vehicleList.stream()
.map(Vehicle::report)
.collect(Collectors.joining(NEWLINE)) + NEWLINE;
}
}
27 changes: 27 additions & 0 deletions src/main/java/rentcompany/Sonata.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package rentcompany;

public class Sonata extends Car {

private static final double fuelEfficiency = 10;
private final double distance;

public Sonata(final double distance) {
this.distance = distance;

}

@Override
double getDistancePerLiter() {
return fuelEfficiency;
}

@Override
double getTripDistance() {
return distance;
}

@Override
String getName() {
return "Sonata";
}
}
5 changes: 5 additions & 0 deletions src/main/java/rentcompany/Vehicle.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package rentcompany;

public interface Vehicle {
String report();
}
28 changes: 28 additions & 0 deletions src/test/java/rentcompany/RentCompanyTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package rentcompany;

import org.junit.jupiter.api.Test;

import static org.assertj.core.api.AssertionsForClassTypes.assertThat;

public class RentCompanyTest {
private static final String NEWLINE = System.getProperty("line.separator");

@Test
public void report() throws Exception {
RentCompany company = RentCompany.create();
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();
assertThat(report).isEqualTo(
"Sonata : 15리터" + NEWLINE +
"K5 : 20리터" + NEWLINE +
"Sonata : 12리터" + NEWLINE +
"Avante : 20리터" + NEWLINE +
"K5 : 30리터" + NEWLINE
);
}
}