import java.util.Scanner;
class Concert{
private String name;
Concert(){
name = null;
}
public String getName() {
return name;
}
public void cancel() {
name = null;
}
public void reserve(String name) {
this.name = name;
}
public boolean isOccupied() {
if(name == null) {
return false;
}
else {
return true;
}
}
public boolean match(String name) {
return(name.equals(this.name));
}
}
public class ConcertReservation{
private char type;
private Concert[] c;
ConcertReservation(char type,int num){
this.type = type;
c = new Concert[num];
for(int i=0;i<c.length;i++) {
c[i] = new Concert();
}
}
public boolean reserve() {
Scanner sc = new Scanner(System.in);
int seatNum;
String name;
show();
System.out.print("이름>>");
name = sc.next();
System.out.print("번호>>");
seatNum = sc.nextInt();
if(seatNum<1||seatNum>=c.length) {
System.out.println("잘못된 좌석 번호입니다.");
return false;
}
//이미 예약된 좌석 번호
if(c[seatNum-1].isOccupied()) {
System.out.println("이미 예약된 좌석입니다.");
return false;
}
c[seatNum-1].reserve(name);
return true;
}
public boolean cancel() {
Scanner sc = new Scanner(System.in);
String name;
show();
System.out.print("이름>>");
name = sc.next();
if(name != null) {
for(int i=0;i<c.length;i++) {
if(c[i].match(name)) {
c[i].cancel();
return true;
}
}
System.out.println("예약자 이름을 찾지 못함");
}
return false;
}
public void show() {
System.out.print(type+">>");
for(int i=0;i<c.length;i++) {
if(c[i].isOccupied()) {
System.out.print(c[i].getName());
}
else {
System.out.print("---");
}
System.out.print(" ");
}
System.out.println();
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc = new Scanner(System.in);
ConcertReservation s = new ConcertReservation('S',10);
ConcertReservation a = new ConcertReservation('A',10);
ConcertReservation b = new ConcertReservation('B',10);
System.out.println("명품예약 콘서트홀 예약 시스템입니다.");
while(true) {
System.out.print("예약:1, 조회:2, 취소:3, 끝내기:4>>");
int num = sc.nextInt();
switch(num) {
case 1:
System.out.print("좌석 구분 S(1), A(2), B(3)>>");
int typeNum = sc.nextInt();
if(typeNum<1||typeNum>3) {
System.out.println("잘못 입력하셨습니다.");
System.out.print("좌석 구분 S(1), A(2), B(3)>>");
typeNum = sc.nextInt();
}
if(typeNum==1) {
s.reserve();
}
else if(typeNum==2) {
a.reserve();
}
else if(typeNum==3) {
b.reserve();
}
break;
case 2:
s.show();
a.show();
b.show();
System.out.println("<<<조회를 완료하였습니다.>>>");
break;
case 3:
System.out.print("좌석 구분 S(1), A(2), B(3)>>");
typeNum = sc.nextInt();
if(typeNum<1||typeNum>3) {
System.out.println("잘못 입력하셨습니다.");
System.out.print("좌석 구분 S(1), A(2), B(3)>>");
typeNum = sc.nextInt();
}
if(typeNum==1) {
s.cancel();
}
else if(typeNum==2) {
a.cancel();
}
else if(typeNum==3) {
b.cancel();
}
break;
case 4:
return;
default:
System.out.println("없는 번호입니다.");
System.out.println("다시 번호를 입력해주세요");
}
}
}
}
결과
명품예약 콘서트홀 예약 시스템입니다.
예약:1, 조회:2, 취소:3, 끝내기:4>>1
좌석 구분 S(1), A(2), B(3)>>1
S>>--- --- --- --- --- --- --- --- --- ---
이름>>황기태
번호>>1
예약:1, 조회:2, 취소:3, 끝내기:4>>1
좌석 구분 S(1), A(2), B(3)>>2
A>>--- --- --- --- --- --- --- --- --- ---
이름>>김효수
번호>>5
예약:1, 조회:2, 취소:3, 끝내기:4>>2
S>>황기태 --- --- --- --- --- --- --- --- ---
A>>--- --- --- --- 김효수 --- --- --- --- ---
B>>--- --- --- --- --- --- --- --- --- ---
<<<조회를 완료하였습니다.>>>
예약:1, 조회:2, 취소:3, 끝내기:4>>3
좌석 구분 S(1), A(2), B(3)>>2
A>>--- --- --- --- 김효수 --- --- --- --- ---
이름>>김효수
예약:1, 조회:2, 취소:3, 끝내기:4>>2
S>>황기태 --- --- --- --- --- --- --- --- ---
A>>--- --- --- --- --- --- --- --- --- ---
B>>--- --- --- --- --- --- --- --- --- ---
<<<조회를 완료하였습니다.>>>
예약:1, 조회:2, 취소:3, 끝내기:4>>4
더보기
너무 어려워서 인터넷에서 정답지 찾아서 보강했다
3번 더 풀어봐야겠다
'명품JAVA프로그래밍 > 4장 클래스와 객체' 카테고리의 다른 글
[명품JAVA프로그래밍] 4장 실습문제 11번 (0) | 2021.12.27 |
---|---|
[명품JAVA프로그래밍] 4장 실습문제 10번 (0) | 2021.12.27 |
[명품JAVA프로그래밍] 4장 실습문제 9번 (0) | 2021.12.27 |
[명품JAVA프로그래밍] 4장 실습문제 8번 (0) | 2021.12.27 |
[명품JAVA프로그래밍] 4장 실습문제 7번 (0) | 2021.12.27 |