프로그래밍 언어 복습/JAVA
#27 JAVA 비행기 예약 시스템 클래스화
사재원 교수
2022. 5. 20. 18:51
위 문제를 함수화하시오.
정답공개 !
package hihihi;
import java.util.Arrays;
import java.util.Random;
import java.util.Scanner;
//좌석은 총 18자리 좌석은 9행 2열로 이루어져있다.
class Making{
int[][] array = new int[9][2];
}
//어느 좌석 예약할건지 행과 열 입력받기
class Where{
Scanner sc = new Scanner(System.in);
public int[] where() {
int[] where = new int[2];
while(true) {
System.out.print("행 입력 : ");
where[0] = sc.nextInt();
System.out.print("열 입력 : ");
where[1] = sc.nextInt();
if(where[0]>8 || where[0]<0){
System.out.println("없는 좌석입니다.");
continue;
}
if(where[1]>1 || where[1]<0) {
System.out.println("없는 좌석입니다.");
continue;
}
break;
}
return where;
}
}
//예약된곳을 1로 체크 , 사용자가 그만하고 싶을때까지 계속 반복
class Check{
public void checkgo(Making m,Where w) {
Scanner sc = new Scanner(System.in);
m = new Making();
w = new Where();
while(true) {
System.out.println("(1) 예약 / (2) 종료 : ");
int a = sc.nextInt();
if(a==1) {
while(true) {
System.out.println("예약하실 자리를 말씀해주세요.");
int[] where = w.where();
if(m.array[where[0]][where[1]]==1) {
System.out.println("이미 예약 되어 있는 자리입니다.");
continue;
}
m.array[where[0]][where[1]] = 1;
for(int i=0;i<m.array.length;i++) {
for(int j=0;j<m.array[i].length;j++) {
System.out.print(m.array[i][j]+"\t");
}
System.out.println();
}
break;
}
}else if(a==2) {
System.out.println("프로그램 종료");
break;
}else{
System.out.println("다시 입력해주세요.");
}
}
}
}
public class gta {
public static void main(String[] args) {
Making m = new Making();
Where w = new Where();
Check c = new Check();
c.checkgo(m, w);
}
}
감사합니다.