두번째 총복습 TMS
조건
*기능
1. pay() : 요금을 정산할수 있는 메서드
2. showDestMsg(String Destination) : 도착시 "도착지에 도착했습니다" 메세지 출력하는 메서드
*자식클래스 : 버스, 지하철
*View
이용하실 대중교통을 선택하세요 단, 출발지는 랜덤
도착지는 버튼으로 구현(optionDialog())
출발지와 도착지가 같지 않도록 구현
버스는 정방향만 가능
지하철은 역방향 가능
교대> 강남> 역삼> 선릉
(실습예제)
기능 - 추상메서드
package tms;
public abstract class Public {
String[] arStation = {"교대","강남","역삼","선릉"};
abstract int pay(int money);
abstract void showDestMsg(String destination);
abstract String go(int btnIndex, int money);
**먼저 추상메서드로 기능 구현할 수 있도록 틀을 잡는다.
버스 기능 구현
package tms;
import java.util.Random;
import javax.swing.JOptionPane;
public class Bus extends Public {
int fee = 1200;
@Override
int pay(int money) {
money-= fee;
return money;
}
//기능 먼저 구현한다음에 view단을 만들어야 덜헷갈림
@Override
java.lang.String go(int btnIndex, int money) {
//출발지와 도착지가 같은지 확인
//정방향인지 역방향인지 생각해서 for문을 돌아야한다.
int cnt = 0;
int index = 0;
String result = "";
//출발지 랜덤으로 설정
Random r = new Random();
while(true) {
index = r.nextInt(arStation.length);
if(btnIndex > index) break;
}
cnt = btnIndex - index;
if(money-fee<0) {
result = "잔액부족,";
}else{
result =""+pay(money)+",";
result += arStation[index]+ ">";
for (int i = 0; i< cnt; i++) {
result += i == cnt-1 ? arStation[index+i+1] : arStation[index+i+1]+">";
}
showDestMsg(arStation[btnIndex]);
}
return result;
}
//버스는 정방향 btnIndex > index 해야함
@Override
void showDestMsg(String destination) {
JOptionPane.showMessageDialog(null, destination+"에 도착했습니다.");
} }
VIEW단
package tms;
import javax.swing.JOptionPane;
public class Road {
void view() {
int money = 20000;
Bus bus360 = new Bus();
//BUS 기능 불러오기!!!!!
Subway line2 = new Subway();
String [] arName = {"버스","지하철"};
String [] result = new String[2];
while(true) {
int choice = JOptionPane.showOptionDialog(null, "대중교통을 선택하세요", "TMS", JOptionPane.DEFAULT_OPTION,
JOptionPane.PLAIN_MESSAGE, null, arName, null);
if(choice ==-1)break;
int btnIndex = JOptionPane.showOptionDialog(null, "도착지를 선택하세요", "TMS", JOptionPane.DEFAULT_OPTION,
JOptionPane.PLAIN_MESSAGE, null, bus360.arStation, null);
if(btnIndex==0 && choice ==0) {
JOptionPane.showMessageDialog(null, "도착지는 교대 다음 정류장부터 가능합니다.");
continue;
}
switch(choice) {
case 0:
result = bus360.go(btnIndex, money).split(",");
if(result[0].equals("잔액부족")) {
JOptionPane.showMessageDialog(null, "잔액이 부족합니다.");
}else {
money = Integer.parseInt(result[0]);
JOptionPane.showMessageDialog(null, "남은 잔액 : "+ money+"원\n"+result[1]);
}
break;
case 1:
result = line2.go(btnIndex, money).split(",");
if(result[0].equals("잔액부족")) {
JOptionPane.showMessageDialog(null, "잔액이 부족합니다.");
}else {
money = Integer.parseInt(result[0]);
JOptionPane.showMessageDialog(null, "남은 잔액 : "+ money+"원\n"+result[1]);
}
break;
}}
}
//실제 실무 뷰단에서는 아래가 끝!!
public static void main(String[] args) {
new Road().view();
} }
}
비슷한 원리를 차용하여, 역방향도 가능한 지하철도 만들어보자
Author And Source
이 문제에 관하여(두번째 총복습 TMS), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://velog.io/@jamiview/두번째-총복습-TMS저자 귀속: 원작자 정보가 원작자 URL에 포함되어 있으며 저작권은 원작자 소유입니다.
우수한 개발자 콘텐츠 발견에 전념 (Collection and Share based on the CC Protocol.)