지하철, 버스 TMS만들기

김재원·2021년 12월 20일
0

자바

목록 보기
29/41

public abstract class Public {
String[] arStation = {"교대","강남","역삼","선릉"};

abstract int pay(int money);
abstract void showDestMsg(String destination);
abstract String go(int btnIndex, int money);

}


버스

public class Bus extends Public{
int fee = 1200;

@Override
int pay(int money) {
	money -= fee;
	return money;
}


@Override
void showDestMsg(String destination) {
	JOptionPane.showMessageDialog(null,destination + "에 도착했습니다.");
	
}


@Override
String go(int btnIndex,int money) {
	//출발지와 도착지가 같은지 비교
	//정방향 역뱡향생각해서 for문 돌아야한다.
	//btnindex : 도착지
	//index : 출발지
	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;
}

}


지하철

public class subway extends Public{

int fee = 1200;

@Override
int pay(int money) {
	money -= fee;
	return money;
}


@Override
void showDestMsg(String destination) {
	JOptionPane.showMessageDialog(null,destination + "에 도착했습니다.");
	
}


@Override
String go(int btnIndex,int money) {
	//출발지와 도착지가 같은지 비교
	//정방향 역뱡향생각해서 for문 돌아야한다.
	//btnindex : 도착지
	//index : 출발지
	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] + ">";
		
		if(cnt > 0) {
			for (int i = 0; i < cnt; i++) {
				result += i == cnt -1 ? arStation[index+i+1] : arStation[index+i+1] + ">";
			}
			showDestMsg(arStation[btnIndex]);
			}
			else {
				cnt *= -1;
				for (int i = 0; i < cnt; i++) {
					//index : 출발지
					result += i == cnt -1 ? arStation[index-i-1] : arStation[index-i-1] + ">";
				}
				showDestMsg(arStation[btnIndex]);
		}
		
		for (int i = 0; i < cnt; i++) {
			result += i == cnt -1 ? arStation[index+i+1] : arStation[index+i+1] + ">";
		} 
		//도착지
		showDestMsg(arStation[btnIndex]);
	}
	return result;
}

}


public class Road {

void view() {
	int money = 20000;
	Bus bus360 = new 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();

}

}

profile
우당탕탕 주니어 디벨로퍼

0개의 댓글