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

๐Ÿš€ 2๋‹จ๊ณ„ - ์š”๊ธˆ ์กฐํšŒ #492

Open
wants to merge 7 commits into
base: leejuoh
Choose a base branch
from
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
import nextstep.member.domain.MemberDetailCustom;
import nextstep.member.domain.MemberRepository;
import nextstep.subway.application.service.StationService;
import nextstep.subway.domain.entity.PathFinder;
import nextstep.subway.domain.entity.Station;
import nextstep.subway.domain.entity.SubwayMap;
import nextstep.subway.domain.repository.LineRepository;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
Expand All @@ -36,8 +36,8 @@ public Long createFavorite(MemberDetailCustom memberDetailCustom, FavoriteReques
Station source = stationService.getStationById(request.getSource());
Station target = stationService.getStationById(request.getTarget());
Member member = getMember(memberDetailCustom);
PathFinder pathFinder = new PathFinder(lineRepository.findAll());
if (!pathFinder.isValidPath(source, target)) {
SubwayMap subwayMap = new SubwayMap(lineRepository.findAll());
if (!subwayMap.isValidPath(source, target)) {
throw new BadRequestException("invalid favorite info");
}
Favorite favorite = favoriteRepository.save(Favorite.of(member.getId(), source, target));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@
public class AddSectionRequest {

private final Long upStationId;

private final Long downStationId;

private final Long distance;
private final Long duration;

public AddSectionRequest(Long upStationId, Long downStationId, Long distance) {
public AddSectionRequest(Long upStationId, Long downStationId, Long distance, Long duration) {
this.upStationId = upStationId;
this.downStationId = downStationId;
this.distance = distance;
this.duration = duration;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,15 @@ public class LineCreateRequest {
private final Long downStationId;

private final Long distance;
private final Long duration;

public LineCreateRequest(String name, String color, Long upStationId, Long downStationId, Long distance) {
public LineCreateRequest(String name, String color, Long upStationId, Long downStationId, Long distance,
Long duration) {
this.name = name;
this.color = color;
this.upStationId = upStationId;
this.downStationId = downStationId;
this.distance = distance;
this.duration = duration;
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
package nextstep.subway.application.dto.response;

import java.util.List;
import java.util.stream.Collectors;
import lombok.Getter;
import lombok.NoArgsConstructor;
import nextstep.subway.domain.entity.Path;
import nextstep.subway.domain.entity.Station;

@NoArgsConstructor
Expand All @@ -28,14 +26,13 @@ public StationDto(Station station) {

private List<StationDto> stations;
private long distance;

public PathResponse(Path shortestPath) {
this.stations = shortestPath.getStations().stream()
.map(StationDto::new)
.collect(Collectors.toList());
this.distance = (long) shortestPath.getDistance();

private long duration;
private int fare;

public PathResponse(List<StationDto> stations, long distance, long duration, int fare) {
this.stations = stations;
this.distance = distance;
this.duration = duration;
this.fare = fare;
}


}
18 changes: 16 additions & 2 deletions src/main/java/nextstep/subway/application/service/LineService.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,15 @@ public LineResponse saveLine(LineCreateRequest lineCreateRequest) {
lineCreateRequest.getName(),
lineCreateRequest.getColor()
));
line.addSection(Section.of(line, upStation, downStation, lineCreateRequest.getDistance()));
line.addSection(
Section.of(
line,
upStation,
downStation,
lineCreateRequest.getDistance(),
lineCreateRequest.getDuration()
)
);
return new LineResponse(line);
}

Expand Down Expand Up @@ -67,7 +75,13 @@ public LineResponse addSection(Long lineId, AddSectionRequest addSectionRequest)
Station upStation = stationService.getStationById(addSectionRequest.getUpStationId());
Station downStation = stationService.getStationById(addSectionRequest.getDownStationId());
line.addSection(
Section.of(line, upStation, downStation, addSectionRequest.getDistance())
Section.of(
line,
upStation,
downStation,
addSectionRequest.getDistance(),
addSectionRequest.getDuration()
)
);
return new LineResponse(line);
}
Expand Down
18 changes: 13 additions & 5 deletions src/main/java/nextstep/subway/application/service/PathService.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@
import nextstep.subway.application.exception.CanNotFindPathException;
import nextstep.subway.domain.entity.Line;
import nextstep.subway.domain.entity.Path;
import nextstep.subway.domain.entity.PathFinder;
import nextstep.subway.domain.entity.Station;
import nextstep.subway.domain.entity.SubWayFare;
import nextstep.subway.domain.entity.SubwayMap;
import nextstep.subway.domain.enums.PathSearchType;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

Expand All @@ -19,14 +21,20 @@ public class PathService {
private final StationService stationService;
private final LineService lineService;

public PathResponse findPath(Long sourceStationId, Long targetStationId) {
public PathResponse findPath(Long sourceStationId, Long targetStationId, PathSearchType type) {
Station source = stationService.getStationById(sourceStationId);
Station target = stationService.getStationById(targetStationId);
List<Line> lines = lineService.getLines();
PathFinder pathFinder = new PathFinder(lines);
Path shortestPath = pathFinder.findShortestPath(source, target)
SubwayMap subwayMap = new SubwayMap(lines);
Path shortestPath = subwayMap.findShortestPath(source, target, type)
.orElseThrow(() -> new CanNotFindPathException("Unable to find the shortest path."));
return new PathResponse(shortestPath);
SubWayFare subWayFare = new SubWayFare(shortestPath);
return new PathResponse(
shortestPath.getStationDtoOfPath(target),
shortestPath.getDistance(),
shortestPath.getDuration(),
subWayFare.calculateFare()
);
}


Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package nextstep.subway.domain.entity;

public class DistanceFarePolicy implements FarePolicy {


public static final long DEFAULT_DISTANCE = 10L;
public static final int OVER_10KM_DISTANCE = 10;
public static final int OVER_50KM_DISTANCE = 50;
public static final int OVER_10KM_FARE_DISTANCE = 5;
public static final int OVER_50KM_FARE_DISTANCE = 8;
public static final int OVER_10KM_FARE_AMOUNT = 100;
public static final int OVER_50KM_FARE_AMOUNT = 100;
private final long distance;

public DistanceFarePolicy(long distance) {
this.distance = distance;
}

@Override
public int getAdditionalFare() {
if (distance > DEFAULT_DISTANCE) {
return calculateOverFare(distance);
}
return 0;
}

private int calculateOverFare(long distance) {
if (distance > OVER_50KM_DISTANCE) {
return (int) ((Math.ceil((distance - DEFAULT_DISTANCE - 1) / OVER_50KM_FARE_DISTANCE) + 1)
* OVER_50KM_FARE_AMOUNT);
}
if (distance > OVER_10KM_DISTANCE) {
return (int) ((Math.ceil((distance - DEFAULT_DISTANCE - 1) / OVER_10KM_FARE_DISTANCE) + 1)
* OVER_10KM_FARE_AMOUNT);
}
return 0;
}

}
6 changes: 6 additions & 0 deletions src/main/java/nextstep/subway/domain/entity/FarePolicy.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package nextstep.subway.domain.entity;

public interface FarePolicy {

int getAdditionalFare();
}
38 changes: 33 additions & 5 deletions src/main/java/nextstep/subway/domain/entity/Path.java
Original file line number Diff line number Diff line change
@@ -1,16 +1,44 @@
package nextstep.subway.domain.entity;

import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;
import lombok.Getter;
import nextstep.subway.application.dto.response.PathResponse.StationDto;

@Getter
public class Path {

private List<Station> stations;
private double distance;
private final Sections sections;

public Path(List<Station> stations, double distance) {
this.stations = stations;
this.distance = distance;
public Path(Sections sections) {
this.sections = sections;
}

public List<StationDto> getStationDtoOfPath(Station target) {
List<Station> stations = this.getStationsOfPath();
if (!stations.isEmpty()) {
Station sourceStation = stations.get(0);
if (Objects.equals(sourceStation.getId(), target.getId())) {
Collections.reverse(stations);
}
}
return stations.stream()
.map(StationDto::new)
.collect(Collectors.toList());
}

public List<Station> getStationsOfPath() {
return this.sections.getSortedStationsByUpDirection(true);
}


public long getDistance() {
return this.sections.getDistance();
}

public long getDuration() {
return this.sections.getDuration();
}
}
64 changes: 0 additions & 64 deletions src/main/java/nextstep/subway/domain/entity/PathFinder.java

This file was deleted.

9 changes: 6 additions & 3 deletions src/main/java/nextstep/subway/domain/entity/Section.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,18 @@ public class Section implements Comparable<Section> {

private long distance;

public Section(Line line, Station upStation, Station downStation, long distance) {
private long duration;

public Section(Line line, Station upStation, Station downStation, long distance, long duration) {
this.line = line;
this.upStation = upStation;
this.downStation = downStation;
this.distance = distance;
this.duration = duration;
}

public static Section of(Line line, Station upStation, Station downStation, long distance) {
return new Section(line, upStation, downStation, distance);
public static Section of(Line line, Station upStation, Station downStation, long distance, long duration) {
return new Section(line, upStation, downStation, distance, duration);
}

@Override
Expand Down
8 changes: 8 additions & 0 deletions src/main/java/nextstep/subway/domain/entity/Sections.java
Original file line number Diff line number Diff line change
Expand Up @@ -140,4 +140,12 @@ private Optional<Section> getSectionByDownStation(Station station) {
public List<Section> getAllSections() {
return this.sections;
}

public long getDistance() {
return this.sections.stream().mapToLong(Section::getDistance).sum();
}

public long getDuration() {
return this.sections.stream().mapToLong(Section::getDuration).sum();
}
}
20 changes: 20 additions & 0 deletions src/main/java/nextstep/subway/domain/entity/SubWayFare.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package nextstep.subway.domain.entity;

import java.util.List;

public class SubWayFare {

private static final int DEFAULT_FARE = 1250;
private final List<FarePolicy> policies;
private int fare;

public SubWayFare(Path path) {
this.policies = List.of(new DistanceFarePolicy(path.getDistance()));
this.fare = DEFAULT_FARE;
}

public int calculateFare() {
policies.forEach(policy -> fare += policy.getAdditionalFare());
return fare;
}
}
Loading