Skip to content
Open
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
28 changes: 28 additions & 0 deletions .eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{
"env": {
"browser": true,
"es2021": true,
"node": true
},
"extends": [
"eslint:recommended",
"airbnb-base",
"prettier"
],
"parserOptions": {
"ecmaVersion": 2015,
"sourceType": "module"
},
"rules": {
"no-console": "off",
"no-plusplus": "off",
"class-methods-use-this": "off",
"radix": "off",
"no-restricted-properties": "off",
"max-depth" : ["error", 2],
"no-return-assign" : "off",
"no-continue" : "off",
"no-param-reassign" : "off",
"import/extensions" : "off"
}
}
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
.ides
.DS_Store
8 changes: 8 additions & 0 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"singleQuote": true,
"printWidth": 100,
"tabWidth": 2,
"useTabs": false,
"semi": true,
"arrowParens": "always"
}
130 changes: 129 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,129 @@
# javascript-subway-final
# 🚇 지하철 노선도 경로 조회 미션
- 등록된 지하철 노선도에서 경로를 조회하는 기능을 구현한다.

## 🚀 기능 구현 목록
### 초기 설정
0. 공통조건 : 정상적으로 프로그램이 수행되지 않은 경우 `alert`으로 에러를 출력한다.
1. 사용자가 프로그램을 시작하면 역, 노선, 구간 데이터가 초기화된다.
- [조건] 소요 시간과 거리는 양의 정수이다.
```
1. 지하철역으로 교대, 강남, 역삼, 남부터미널, 양재, 양재시민의숲, 매봉 역 정보가 등록되어 있다.
2. 지하철 노선으로 2호선, 3호선, 신분당선이 등록되어 있다.
3. 노선에 역이 아래와 같이 등록되어 있다.(왼쪽 끝이 상행 종점)
- 2호선: 교대 - ( 2km / 3분 ) - 강남 - ( 2km / 3분 ) - 역삼
- 3호선: 교대 - ( 3km / 2분 ) - 남부터미널 - ( 6km / 5분 ) - 양재 - ( 1km / 1분 ) - 매봉
- 신분당선: 강남 - ( 2km / 8분 ) - 양재 - ( 10km / 3분 ) - 양재시민의숲
```
2. 사용자는 출발역과 도착역을 입력할 수 있다.
- [예외처리] 등록되어 있지 않은 역을 입력한건 아닌지 검증한다.
- [예외처리] 역의 이름은 두 글자 이상으로 이루어져 있다.
- [예외처리] 출발역과 도착역이 다른 역인지 검증해야 한다.
3. 사용자가 길찾기 버튼을 누르면 `최단 거리` 또는 `최소 시간` 옵션을 선택할 수 있다.
4. 최단거리, 최소시간의 선택에 따라 총 거리 및 총 소요시간을 함께 출력한다.
- [예외처리] 출발역과 도착역이 연결되었는지 검증한다.

## ✅ 프로그래밍 요구사항
### 길찾기 관련 기능
- 출발역을 입력하는 input 태그는 `departure-station-name-input` id 속성값을 가진다.
- 도착역을 입력하는 input 태그는 `arrival-station-name-input` id 속성값을 가진다.
- 최단거리, 최소시간을 선택하는 radio는 `search-type` name 속성값을 가진다.
- **radio option의 default 값은 최단거리이다.**
- 길찾기 버튼은 `search-button` id 속성값을 가진다.
- 📝 결과는 `table`을 이용하여 보여준다.

## ❗️힌트
## 데이터 초기화
자바스크립트에서 데이터를 초기화하는 방법 중에 하나는 아래와 같이 data를 `export`하고, `import`하는 것이다.

```javascript
export const users = [
{
name: 'Alt'
},
{
name: 'Jamie'
},
{
name: 'Sony'
}
]

export const courses = [
{
name: 'frontend',
},
{
name: 'backend',
},
{
name: 'iOS',
},
{
name: 'Android',
}
]

```
위와 같이 데이터를 `export`하면 아래와 같이 데이터를 `import` 하여 사용할 수 있다.
```javascript
import { users, courses } from './data.js'

function App() {
this.users = users
this.courses = courses
}
```

## 최단 경로 라이브러리
- `utils/Dijkstra.js` 라이브러리를 활용하면 간편하게 최단거리를 조회할 수 있다.
- 정점(Vertex)과 간선(Edge), 그리고 가중치 개념을 이용
- 정점: 지하철역
- 간선: 지하철역 연결정보
- 가중치: 거리 or 소요 시간
- 최단 거리 기준 조회 시 가중치를 거리로 설정
- 최소 시간 기준 조회 시 가중치를 시간으로 설정

```javascript
import Dijkstra from "./utils/Dijkstra.js";
const dijkstra = new Dijkstra()

//dijkstra.addEdge("출발역", "도착역", 거리 또는 시간);
dijkstra.addEdge("V1", "V2", 2);
dijkstra.addEdge("V2", "V3", 2);
dijkstra.addEdge("V1", "V3", 100);

const result = dijkstra.findShortestPath("V1", "V3");
// result = ["V1", "V2", "V3"]
```

#### 테스트설명
<img src="/images/dijkstra_example.png" width="400">

- 역 사이의 거리를 고려하지 않는 경우 V1->V3 경로가 최단 경로
- 역 사이의 거리를 고려할 경우 V1->V3 경로의 거리는 100km, V1->V2->V3 경로의 거리는 4km이므로 최단 경로는 V1->V2->V3

<br>

### 요구사항
- 사용자가 잘못된 입력 값을 작성한 경우 `alert`을 이용해 메시지를 보여주고, 재입력할 수 있게 한다.
- 외부 라이브러리(jQuery, Lodash 등)를 사용하지 않고, 순수 Vanilla JS로만 구현한다.
- **자바스크립트 코드 컨벤션을 지키면서 프로그래밍** 한다
- [https://google.github.io/styleguide/jsguide.html](https://google.github.io/styleguide/jsguide.html)
- [https://ui.toast.com/fe-guide/ko_CODING-CONVENSION/](https://ui.toast.com/fe-guide/ko_CODING-CONVENTION)
- **indent(인덴트, 들여쓰기) depth를 3이 넘지 않도록 구현한다. 2까지만 허용**한다.
- 예를 들어 while문 안에 if문이 있으면 들여쓰기는 2이다.
- 힌트: indent(인덴트, 들여쓰기) depth를 줄이는 좋은 방법은 함수(또는 메소드)를 분리하면 된다.
- **함수(또는 메소드)의 길이가 15라인을 넘어가지 않도록 구현한다.**
- 함수(또는 메소드)가 한 가지 일만 잘 하도록 구현한다.
- 변수 선언시 [var](https://developer.mozilla.org/ko/docs/Web/JavaScript/Reference/Statements/var) 를 사용하지 않는다. [const](https://developer.mozilla.org/ko/docs/Web/JavaScript/Reference/Statements/const) 와 [let](https://developer.mozilla.org/ko/docs/Web/JavaScript/Reference/Statements/let) 을 사용한다.
- [import](https://developer.mozilla.org/ko/docs/Web/JavaScript/Reference/Statements/import) 문을 이용해 스크립트를 모듈화하고 불러올 수 있게 만든다.
- [template literal](https://developer.mozilla.org/ko/docs/Web/JavaScript/Reference/Template_literals)을 이용해 데이터와 html string을 가독성 좋게 표현한다.

<br/>

## 📝 미션 저장소 및 진행 요구사항

- 미션은 [https://github.com/woowacourse/javascript-subway-path-precourse](https://github.com/woowacourse/javascript-subway-path-precourse) 저장소를 fork/clone해 시작한다.
- **기능을 구현하기 전에 javascript-subway-path-precourse/docs/README.md 파일에 구현할 기능 목록**을 정리해 추가한다.
- **git의 commit 단위는 앞 단계에서 README.md 파일에 정리한 기능 목록 단위로 추가**한다.
- [프리코스 과제 제출](https://github.com/woowacourse/woowacourse-docs/tree/master/precourse) 문서 절차를 따라 미션을 제출한다.
Binary file added images/dijkstra_example.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/path_result.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/path_result.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
18 changes: 18 additions & 0 deletions index.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#input-search-station-container{
margin-bottom: 1rem;
}

.station-input-form-container >input {
height: 1.2rem;
margin-bottom: 1rem;
}

.path-selectors {
margin-bottom: 1rem;
}

#path-result-title {
font-size: 1.1rem;
font-weight: bold;
margin-bottom: 1rem;
}
19 changes: 19 additions & 0 deletions index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>지하철 길찾기</title>
<link rel="stylesheet" href="index.css" />
</head>
<body>
<div id="app">
<section id="title">
<h1>🚇 지하철 길찾기</h1>
</section>

<section id="input-search-station-container"></section>
<scction id="path-search-result-container"></scction>
</div>
<script type="module" src="src/index.js"></script>
</body>
</html>
109 changes: 109 additions & 0 deletions src/controllers/MainController.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
import SearchPathInputFormView from '../views/SearchPathInputFormView.js';
import ResultSearchPathView from '../views/SearchPathResultView.js';

import StationModel from '../models/StationModel.js';
import StationDistanceModel from '../models/StationDistanceModel.js';
import StationTimeModel from '../models/StationTimeModel.js';

import { Lines2, Lines3, LineSinbundang } from '../utils/data.js';
import { SEARCH_PATH_TYPE, ERROR_MESSAGE } from '../utils/constants.js';
import stationInputValidator from '../utils/stationInputValidator.js';

export default class MainController {
init() {
this.initStation();

this.searchPathInputForm = new SearchPathInputFormView()
.setup(document.querySelector('#input-search-station-container'))
.on('submitSearchInputValue', (e) => this.onSubmitSearchInputHandler(e.detail))
.on('onChangeSelector', (e) => this.onChangeSelectorHandler(e.detail));

this.resultSearchPath = new ResultSearchPathView()
.setup(document.querySelector('#path-search-result-container'))
.hide();

this.selectPathType = SEARCH_PATH_TYPE.MIN_DISTANCE;
this.stationTimeModel = new StationTimeModel();
this.stationDistanceModel = new StationDistanceModel();
}

initStation() {
this.stationmodel = new StationModel();

this.createStation(Lines2);
this.createStation(Lines3);
this.createStation(LineSinbundang);
}

createStation(Line) {
Line.forEach((station) => {
if (!this.stationmodel.getStationByName(station.stationName)) {
this.stationmodel.createStation(station.id, station.stationName, station.line);
} else {
this.stationmodel.addLIne(station.stationName, station.line);
}
});
}

findPath(departureStationName, arrivalStationName) {
this.selectPathType === SEARCH_PATH_TYPE.MIN_DISTANCE
? this.getShortestDistancePath(departureStationName, arrivalStationName)
: this.getShortestTimePath(departureStationName, arrivalStationName);
}

getShortestDistancePath(departureStationName, arrivalStationName) {
const shortestDistancePath = this.stationDistanceModel.getShortestDistancePath(
departureStationName,
arrivalStationName
);
const totalShortestDistance = this.stationDistanceModel.getTotalDistance(shortestDistancePath);
const totalTime = this.stationTimeModel.getTotalTime(shortestDistancePath);
this.renderResultView(shortestDistancePath, totalShortestDistance, totalTime);
}

getShortestTimePath(departureStationName, arrivalStationName) {
const shortestTimePath = this.stationTimeModel.getShortestTimePath(
departureStationName,
arrivalStationName
);
console.log(this.stationTimeModel.getTotalTime(shortestTimePath));
const totalShortestTime = this.stationTimeModel.getTotalTime(shortestTimePath);
const totalDistance = this.stationDistanceModel.getTotalDistance(shortestTimePath);
this.renderResultView(shortestTimePath, totalDistance, totalShortestTime);
}

renderResultView(path, totalDistance, totalTime) {
this.resultSearchPath.renderSearchPathResult(
path,
totalDistance,
totalTime,
this.selectPathType
);
this.resultSearchPath.show();
}

isValidStation(departureStationName, arrivalStationName) {
this.departureStationName = departureStationName.trim();
this.arrivalStationName = arrivalStationName.trim();

stationInputValidator(
this.departureStationName,
this.arrivalStationName,
this.stationmodel.getStationNames()
)
? this.findPath(departureStationName, arrivalStationName)
: this.searchPathInputForm.resetInputForm();
}

onSubmitSearchInputHandler({ departureStationName, arrivalStationName }) {
this.isValidStation(departureStationName, arrivalStationName);
}

onChangeSelectorHandler(targetId) {
if (!this.departureStationName || !this.arrivalStationName) {
return alert(ERROR_MESSAGE.IS_NOT_INPUT);
}
this.selectPathType = targetId;
this.findPath(this.departureStationName, this.arrivalStationName);
}
}
5 changes: 5 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import MainController from './controllers/MainController.js';

document.addEventListener('DOMContentLoaded', () => {
new MainController().init();
});
11 changes: 11 additions & 0 deletions src/models/Station.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
export default class Station {
constructor(id, name) {
this.id = id;
this.name = name;
this.lineNumber = [];
}

setLine(lineNumber) {
this.lineNumber.push(lineNumber);
}
}
40 changes: 40 additions & 0 deletions src/models/StationDistanceModel.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { stationDistance } from '../utils/data.js';
import Dijkstra from '../utils/Dijkstra.js';

export default class StationDistanceModel {
constructor() {
this.distances = stationDistance;
this.dijkstra = new Dijkstra();
}

setDistance() {
this.distances.forEach((station) => {
this.dijkstra.addEdge(station.departureStation, station.endStation, station.distance);
});
}

getShortestDistancePath(departureStation, endStation) {
this.setDistance();
// todo 연결되지 않은 곳일 때 예외처리
return this.dijkstra.findShortestPath(departureStation, endStation);
}

getTotalDistance(path) {
return path.reduce((totalDistance, station, index) => {
if (index !== path.length - 1) {
totalDistance += this.getOneDistance(station, path[index + 1]);
}
return totalDistance;
}, 0);
}

getOneDistance(departureStation, endStation) {
return parseInt(
this.distances.find(
(time) =>
(time.departureStation === departureStation && time.endStation === endStation) ||
(time.departureStation === endStation && time.endStation === departureStation)
).distance
);
}
}
Loading