상위 클래스 대중교통은 생략함
🚌 하위 클래스 버스
public class Bus extends transportation{
int maxPass = 30; // 최대 승객 수
int currentPass = 0; // 현재 승객 수
int cost = 1000; // 요금
// 버스 번호 지정 [고유값으로 생성되어야 되기에 랜덤함수로 함]
public Bus() {
this.num = (int)(Math.random()*100+1);
// 랜덤함수는 기본형이 Double형이기에 (int)로 정수화
// 1부터 값을 뽑고 싶다면 +1 => 랜덤 함수는 0부터 나오기 때문에
System.out.println("버스 번호 : "+num);
}
// 버스 상태 변경
boolean busStatus(boolean change) {
if(change == true)
status = "운행중";
else {
status = "차고지행";
currentPass = 0;
maxPass = 30;
}
return change;
}
// 버스 현재 상태
void currentBus() {
System.out.println("상태 = "+status);
System.out.println("주유량 = "+currentGas);
if(!gasLeft())
System.out.println("주유 필요");
}
// 버스 기름에 따른 상태 변경
void drive() {
if(gasLeft()) {
System.out.println("남은 기름 : "+ currentGas );
System.out.println("운행 가능");
}
if(!gasLeft()) {
System.out.println("주유가 필요합니다");
System.out.println("운행 불가 = 차고지행");
status = "차고지행";
}
}
// 주유량
int refuel(int gas) {
currentGas += gas;
if(!gasLeft()) {
status = "차고지행";
}
return currentGas;
}
boolean available() {
//승객 탑승은 ‘최대 승객수’ 이하까지 가능
return maxPass >= currentPass;
}
// 승객 탑승
int board(int pass) {
if(pass >= (maxPass-currentPass))
System.out.println("최대 승객 수 초과");
else {
if(available()) {
currentPass += pass;
System.out.println("탑승 승객 수 = "+pass+"명");
System.out.println("잔여 승객 수 = "+(maxPass-currentPass)+"명");
System.out.println("요금 확인 = "+(cost*pass));
}
if(!available()) {
System.out.println("최대 승객 수 초과");
}
}
return currentPass;
}
// 속도변경
int changeSpeed(int acceleration) {
//주유 상태를 체크하고 주유량이 10 이상이어야 운행할 수 있음
if(gasLeft()) {
this.acceleration = acceleration;
currentSpeed += acceleration;
System.out.println("현재 속도는 "+ currentSpeed+"입니다.");
}
System.out.println("주유량을 확인해주세요."+currentGas);
return currentGas;
}
}
🚌🎬📝 버스클래스 시나리오
public class testBus {
public static void main(String[] args) {
// TODO Auto-generated method stub
// 버스 테스트
// 1번
// 1~2. 버스 2대 생성 & 출력
Bus bus1 = new Bus();
Bus bus2 = new Bus();
// 2번 (버스 1대로 진행)
// 1 ~ 2. 승객 +2 & 출력
bus1.board(2);
// 3 ~ 4. 주유량 -50
bus1.refuel(-50);
System.out.println("주유량 = "+bus1.currentGas);
// 5. 상태 변경 => 차고지행
bus1.busStatus(false);
// 6. 주유량 +10
bus1.refuel(10);
// 7. 버스 상태와 주유량 출력
bus1.currentBus();
// 8. 상태 변경 => 운행중
bus1.busStatus(true);
// 9 ~ 10. 승객 +45 => 최대 승객 수 초과
bus1.board(45);
// 11 ~ 12. 승객 +5 & 출력
bus1.board(5);
// 13. 주유량 -55
bus1.refuel(-55);
// 14. 버스 상태와 주유량 출력
bus1.currentBus();
}
}
💻 출력값
버스 번호 : 92
버스 번호 : 15
탑승 승객 수 = 2명
잔여 승객 수 = 28명
요금 확인 = 2000
주유량 = 50
상태 = 차고지행
주유량 = 60
최대 승객 수 초과
탑승 승객 수 = 5명
잔여 승객 수 = 25명
요금 확인 = 5000
상태 = 차고지행
주유량 = 5
주유 필요
🚓 하위 클래스 택시
public class Taxi extends transportation {
String destination; // 목적지
int distance; // 목적지까지 거리
int maxPass = 4; // 최대 승객수
int defaultDistance = 1; // 기본 거리
int defaultCost = 3000; // 기본 요금
int perDistance = 1000; // 거리당 요금
static String status = "일반"; // 상태
int speed = 0; // 속도
int total = 0; // 누적 금액
int cost; // 승객이 지불할 금액
// 택시 번호 지정 [고유값으로 생성되어야 되기에 랜덤함수로 함]
public Taxi() {
this.num = (int)(Math.random()*100+1);
// 랜덤함수는 기본형이 Double형이기에 (int)로 정수화
// 1부터 값을 뽑고 싶다면 +1 => 랜덤 함수는 0부터 나오기 때문에
System.out.println("택시 번호 : "+num);
Taxi.drive();
}
static boolean drive() {
if (!gasLeft()) {
status = "운행 불가";
System.out.println("주유 필요");
return false;
}
return true;
}
// 탑승 승객 목적지 거리
void board(int pass, String dest, int dis) {
if(status == "일반") {
if(pass > 4)
System.out.println("최대 승객 수 초과");
else {
if(dis==1)
cost = defaultCost+ (perDistance*dis);
else
cost = defaultCost+ (perDistance*(dis-1));
status = "운행중";
System.out.println("탑승 승객 수 = "+pass);
System.out.println("잔여 승객 수 = "+ (maxPass-pass));
System.out.println("기본 요금 확인 = "+defaultCost);
System.out.println("목적지 = "+dest);
System.out.println("목적지까지 거리 = "+ dis+"km");
System.out.println("지불할 요금 = "+cost);
total += cost;
}
}
}
// 주유량
int refuel(int gas) {
currentGas += gas;
if(!gasLeft()) {
status = "운행 불가";
}
return currentGas;
}
// 요금 지불
int pay() {
status = "일반";
maxPass = 4;
System.out.println("누적 요금 = "+ total);
if(!gasLeft())
System.out.println("주유 필요");
cost = 0;
return total;
}
void passenger(int pass) {
if(pass > 4)
System.out.println("최대 승객 수 초과");
}
// 속도변경
int changeSpeed(int acceleration) {
//주유 상태를 체크하고 주유량이 10 이상이어야 운행할 수 있음
if(gasLeft()) {
this.acceleration = acceleration;
currentSpeed += acceleration;
System.out.println("현재 속도는 "+ currentSpeed+"입니다.");
}
System.out.println("주유량을 확인해주세요."+currentGas);
return currentGas;
}
}
🚓🎬📝 택시클래스 시나리오
public class testTaxi {
public static void main(String[] args) {
// TODO Auto-generated method stub
// 택시 테스트
// 1번
// 1~2. 버스 2대 생성 & 출력
Taxi taxi1 = new Taxi();
Taxi taxi2 = new Taxi();
System.out.println("taxi1 주유량 = "+taxi1.currentGas);
System.out.println("taxi1 상태 = "+taxi1.status);
System.out.println("taxi2 주유량 = "+taxi2.currentGas);
System.out.println("taxi2 상태 = "+taxi2.status);
//2번(Taxi 1대로 진행)
// 1~2.승객+2 목적지 = 서울역 목적지까지 거리 2km & 출력
taxi1.board(2, "서울역", 2);
System.out.println("상태 = "+ taxi1.status);
// 3. 주유량 -80
taxi1.refuel(-80);
// 4~5. 요금결제 & 출력
System.out.println("주유량 = "+taxi1.currentGas);
taxi1.pay();
// 6~7. 승객+5 & 최대승객수 초과
taxi1.passenger(5);
// 8~9. 승객+3 목적지 = 구로디지털단지역 목적지까지 거리 12km & 출력
taxi1.board(3, "구로디지털단지역", 12);
// 10. 주유량 -20
taxi1.refuel(-20);
// 11~12. 요금 결제 & 출력
System.out.println("주유량 = "+taxi1.currentGas);
taxi1.pay();
}
}
💻 출력값
택시 번호 : 73
택시 번호 : 58
taxi1 주유량 = 100
taxi1 상태 = 일반
taxi2 주유량 = 100
taxi2 상태 = 일반
탑승 승객 수 = 2
잔여 승객 수 = 2
기본 요금 확인 = 3000
목적지 = 서울역
목적지까지 거리 = 2km
지불할 요금 = 4000
상태 = 운행중
주유량 = 20
누적 요금 = 4000
최대 승객 수 초과
탑승 승객 수 = 3
잔여 승객 수 = 1
기본 요금 확인 = 3000
목적지 = 구로디지털단지역
목적지까지 거리 = 12km
지불할 요금 = 14000
주유량 = 0
누적 요금 = 18000
주유 필요