개발/JAVA

[JAVA]자바 - 배열을 이용하여 아주 간단한 영화 예매하기 만들기

윤_ve 2021. 2. 4. 01:09

오늘 해볼건 아주 간단한 영화 예매 프로그램이다(프로그램이라 하기에도 그렇다;ㅋㅋ)

 

우선 배열을 이용할건데 좌석을 보여주고 원하는 자리를 선택하게 한 뒤 그 자리가 예약이 되었다는 의미로 숫자를 0에서 1로 바꾸고 원래 처음 질문인 예매를 할건지 물어보게 반복되는 프로그램을 만들 생각이다.

 

이렇게 일단은 코드를 적었는데... 자꾸 한바퀴돌고 리셋이 되면 예약되었던 자리가 다시 0으로 돌아간다..

이런 기본적인 것도 한번에 못하다니 너무 암울하다... 바로 떠오르지 않아 한 두시간 혼자서 머리를 굴려본 결과

약간의 수정을 거쳐 원하는 코드를 만들 수 있었다.

 

import java.util.Scanner;

public class Reservation {
	public static void main(String[] args) {
		int seat[] = new int[10];
		int reserved;
		Scanner input = new Scanner(System.in);
		while (true) {
			System.out.print("좌석을 예약하시겠습니까? (1 또는 0 )");
			int res = input.nextInt();
			if (res == 1) {
				System.out.println("현재의 예약상태는 다음과 같습니다.");
				System.out.println("=============================");
				for (int i = 0; i < 10; i++) {      // 좌석 번호를 반복문을 통해 표현
					System.out.print((i + 1) + " ");
				}
				System.out.println();
				for (int i : seat) {              // => for(int i = 0; i < seat.length; i++) {
					System.out.print(i + " ");    //       System.out.println(seat[i] + " ");
				}                                 //    } 
				System.out.println();
				System.out.print("몇 번째 좌석을 예약하시겠습니까?");
				reserved = input.nextInt();
				if (seat[reserved - 1] == 0) {
					seat[reserved - 1] = 1;
					System.out.println("예약되었습니다.");
				} else if (seat[reserved - 1] == 1) {
					System.out.println("이미 예약된 좌석입니다.");
				}
			} else if (res == 0) {
				System.out.println("프로그램을 종료합니다.");
				break;
			} else {
				System.out.println("1 또는 0을 선택해주요");
			}
		}
	}
}

이런 식으로 표현 해보았다.

(2시간이나 걸렸다.. 코린이는 슬퍼서 웁니다...ㅠㅠ)