ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • Power JAVA 10장 ) 배열 - Programming_1
    IT/솔루션) Power Java 2019. 5. 12. 17:47

    <2016. 10. 3. 19:02>

     

    Power JAVA 10장 ) 배열 - Programming_1

     

    /* 
     * 배열을 이용하여 간단한 극장 예약 시스템을 작성하여 보자.
     * 아주 작은 극장이라서 좌석이 10개밖에 안 된다.
     * 사용자가 예약을 하려고 하면 먼저 좌석 배치표를 보여준다.
     * 즉 예약이 끝난 좌석은 1로, 예약이 안된 좌석은 0으로 나타낸다.
     */
    import java.util.*;
    class Seat {
    	private int empty = 0;
    	void bookSeat() {
    		empty = 1;
    	}
    	int getEmpty() {
    		return empty;
    	}
    }
    public class Programming_1 {
    	public static void main(String[] args) {
    		// 객체, 변수 생성
    		Scanner input = new Scanner(System.in);
    		int check;
    		Seat[] seats = new Seat[10];
    		for (int i=0; i < seats.length; i++ )
    		   seats[i] = new Seat();
    		//프로그램 시작
    		while(true) {
    			System.out.print("좌석을 예약 하시겠습니까? (0 또는 1)  : ");
    			check = input.nextInt();
    			if (check == 0) break;
    			//나타나는 양식
    			System.out.println("==============================");
    			for (int i=0; i < seats.length; i++)
    			    System.out.print((i+1)+"  ");
    			System.out.println();
    			System.out.println("==============================");
    			for (int i=0; i < seats.length; i++)
    			    System.out.print(seats[i].getEmpty()+"  ");
    			System.out.println();
    			//좌석 예약
    			System.out.print("몇 번째 좌석을 예약하시겠습니까? ");
    			check = input.nextInt();
    			check--;
    			if (check < 1 || check > 10)
    			    System.out.println("유효하지 않은 좌석입니다. 처음부터 다시 진행하여 주십시오."); else {
    				seats[check].bookSeat();
    				System.out.println("예약되었습니다.");
    			}
    		}
    	}
    }

    댓글

다치지 말고 운동하자.