Java Programming
package workshop.subway.domain;
/*
* 카드 정보를 담는 엔티티
*/
public class CardDTO {
private int balance;
public CardDTO() {
super();
}
public CardDTO(int balance) {
super();
this.balance = balance;
}
public int getBalance() {
return balance;
}
public void setBalance(int balance) {
this.balance = balance;
}
@Override
public String toString() {
return "CardDTO [balance=" + balance + "]";
}
}
package workshop.subway.domain;
/*
* 역 정보를 담는 엔티티
*/
public class StationDTO {
private String id;
private String name;
private int location;
public StationDTO() {
super();
// TODO Auto-generated constructor stub
}
public StationDTO(String id, String name, int location) {
super();
this.id = id;
this.name = name;
this.location = location;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getLocation() {
return location;
}
public void setLocation(int location) {
this.location = location;
}
@Override
public String toString() {
return "StationDTO [id=" + id + ", name=" + name + ", location=" + location + "]";
}
}
package workshop.subway.service;
import workshop.subway.domain.CardDTO;
import workshop.subway.domain.StationDTO;
public interface SubwayService {
// 1. constance
// public int x = 10 ;
// 운행요금을 반환하는 메서드(잔액 부족할 경우 -1 )
// CardDTO card : 소유카드(충전), StationDTO start : 출발역 , StationDTO end : 종착역
public int chargeFree(CardDTO card, StationDTO start, StationDTO end);
// 카드요금을 차감하는 메서드
// amount : 차감 금액
// 차감이 성공하면 true , 실패하면 false
public boolean subtractBalance(CardDTO card, int amount);
// 출발역과 종착역 사이의 거리 계산
// 역간거리를 반환하는 메서드
public int getDistance(StationDTO start, StationDTO end);
}
package workshop.subway.service;
import workshop.subway.domain.CardDTO;
import workshop.subway.domain.StationDTO;
public class SubwayServiceImpl implements SubwayService {
public static final int BASIC_FEE = 800;
@Override
public int chargeFree(CardDTO card, StationDTO start, StationDTO end) {
int fee = 0;
int distance = getDistance(start, end);
System.out.println("distance : " + distance);
// 거리 계산
if (distance <= 12) {
fee = BASIC_FEE;
} else if (distance > 12 && distance <= 42) {
fee = BASIC_FEE + (((distance - 12 - 1) / 6) + 1) * 100;
} else {
fee = BASIC_FEE + (5 * 100) + (((distance - 42 - 1) / 12) + 1) * 100;
}
// 요금 차감
if (subtractBalance(card, fee)) {
return fee;
} else {
return -1;
}
}
@Override
public boolean subtractBalance(CardDTO card, int amount) {
int currentBalance = card.getBalance() - amount;
if (currentBalance >= 0) {
card.setBalance(currentBalance);
return true;
}
return false;
}
@Override
public int getDistance(StationDTO start, StationDTO end) {
return Math.abs(start.getLocation() - end.getLocation());
}
}
package workshop.subway.view;
import java.util.Scanner;
import workshop.subway.domain.CardDTO;
import workshop.subway.domain.StationDTO;
import workshop.subway.service.SubwayService;
import workshop.subway.service.SubwayServiceImpl;
public class SubwayView {
private SubwayService service;
private CardDTO card;
///////////////////////////////////////////
private StationDTO[] stations;
public SubwayView() {
stations = new StationDTO[]{
new StationDTO("A1105", "합정", 0),
new StationDTO("A1106", "홍대입구", 10),
new StationDTO("A1107", "신촌", 15),
new StationDTO("A1108", "이대", 20),
new StationDTO("A1109", "아현", 30),
new StationDTO("A1110", "충정로", 35),
new StationDTO("A1111", "시청", 40),
new StationDTO("A1112", "을지로입구", 50),
new StationDTO("A1113", "을지로3가", 58),
new StationDTO("A1114", "을지로4가", 76),
new StationDTO("A1115", "동대문운동장", 77)
};
service = new SubwayServiceImpl();
}
///////////////////////////////////////////
public void menu() {
System.out.print("[교통카드에 충전금액을 입력하세요 : ] : ");
card = new CardDTO(getUserInput());
// 승차역 정보 출력 및 선택
printStationList(stations);
StationDTO startStation = stations[selectNumber(stations, "승차")];
// 하차역 정보 출력 및 선택
printStationList(stations);
StationDTO endStation = stations[selectNumber(stations, "하차")];
int chargedFee = service.chargeFree(card, startStation, endStation);
System.out.println("chargedFee : " + chargedFee);
if (chargedFee >= 0) {
printSuccess(card, startStation, endStation, chargedFee);
} else {
printFail();
}
}
// 역 정보 출력
public void printStationList(StationDTO[] stations) {
System.out.println("=======================================");
for (int idx = 0; idx < stations.length; idx++) {
System.out.println(idx + 1 + ". " + stations[idx].getName());
}
System.out.println("=======================================");
}
/**
* @ params card : 소유카드
* @ params start : 출발역
* @ params end : 종착역
* @ params chargedFee : 요금
* 요금 정산 성공 메시지를 출력
*/
public void printSuccess(CardDTO card, StationDTO start, StationDTO end, int chargedFee) {
System.out.println("정상 처리되었습니다.");
System.out.println("승차역은 " + start.getName() + "역이고 하차역은 " + end.getName());
System.out.println("요금은 " + chargedFee + "원이며 , 잔액은 " + card.getBalance() + "원 입니다.");
}
// 요금 정산 실패 메시지 출력
public void printFail() {
System.out.println("잔액이 부족합니다.");
System.out.println("직원에게 문의하세요.");
}
/**
* 역 선택 메서드
*
* @ params stations 역 정보를 담는 배열
* @ params msg 출력메시지
*/
public int selectNumber(StationDTO[] stations, String msg) {
System.out.print(msg + "역을 선택하세요. 1 ~ " + stations.length + " : ");
int stationNum = getUserInput();
return stationNum - 1;
}
/**
* Scanner 이용해서 입력받은 값을 처리하는 메서드
*/
public int getUserInput() {
Scanner scan = new Scanner(System.in);
return scan.nextInt();
}
}
package workshop.subway;
import workshop.subway.service.SubwayService;
import workshop.subway.service.SubwayServiceImpl;
import workshop.subway.view.SubwayView;
public class SubwayMain {
public static void main(String[] args) {
SubwayView view = new SubwayView();
view.menu() ;
}
}
'한화시스템 BEYOND SW캠프 > TIL' 카테고리의 다른 글
[5주차] 24.02.08 목요일 (0) | 2024.02.08 |
---|---|
[5주차] 24.02.07 수요일 (0) | 2024.02.07 |
[5주차] 24.02.05 월요일 (0) | 2024.02.05 |
[4주차] 24.02.02 금요일 (0) | 2024.02.02 |
[4주차] 24.02.01 목요일 (0) | 2024.02.01 |