Repository

명품자바 4장 실습문제 12번 (예약시스템)

dev_zephyr 2021. 1. 27. 18:35

학원에서의 과제를 저장해 놓는겸사겸사리.

몇달 전인데 기억이 안나는 부분도 있고 

플로우차트 보면 귀엽다..ㅋㅋ

 

 

 

 

import java.util.InputMismatchException;
import java.util.Scanner;

class CustomerDTO {
	private String name;
	
	public CustomerDTO() { // 기본생성자를 호출하면 객체의 필드(name)가 "---"으로 초기화된다.
		name = "---";
	}
	
	public void setName(String name) {
		this.name = name;
	}
	
	public String getName() {
		return name;
	}
	
}

class Controller {
	Scanner scan = new Scanner(System.in);
	final int BLOCK_MAX_SIZE = 10;
	CustomerDTO[] s = new CustomerDTO[BLOCK_MAX_SIZE];
	CustomerDTO[] a = new CustomerDTO[BLOCK_MAX_SIZE];
	CustomerDTO[] b = new CustomerDTO[BLOCK_MAX_SIZE];
	
	public Controller() { // 처음 기본생성자에서 좌석을 모두 ---로 초기화한다.
		for(int i=0; i<BLOCK_MAX_SIZE; i++) {
			CustomerDTO c = new CustomerDTO();
			s[i] = c;
		}
		for(int i=0; i<BLOCK_MAX_SIZE; i++) {
			CustomerDTO c = new CustomerDTO();
			a[i] = c;
		}
		for(int i=0; i<BLOCK_MAX_SIZE; i++) {
			CustomerDTO c = new CustomerDTO();
			b[i] = c;
		}
	}
	
	public int verifyInt() { // 숫자를 입력받을때마다 사용. 숫자말고 문자를 잘못입력했을때 에러방지.
		int choice;
		while(true) {
			try {
				choice = scan.nextInt();
				return choice;
			} catch(InputMismatchException e) { // 숫자말고 다른거 입력했을때 예외처리
				System.out.println("잘못된 입력입니다. 다시 입력해주세요.");
				scan.next();
			}
		}
	}
	
	public String showBlock() { // 예약,취소하기전 객체배열(좌석블럭)을 출력하는 메서드
		while(true) {			// 좌석 구분 번호를 입력받고 좌석(S, A, B)중 하나를 리턴한다.
			System.out.print("좌석 구분 S(1), A(2), B(3) >> ");
				int choice = verifyInt();
				switch(choice) {
				case 1 :
					show(s);
					return "s";
				case 2 :	
					show(a);
					return "a";
				case 3 :
					show(b);
					return "b";
				default :
					System.out.println("잘못입력하셨습니다."); // 1,2,3말고 다른거 입력받았을대
				}
		}
	}
	
	public void show(CustomerDTO[] dto) { // 파라미터로 들어온 객체배열(좌석블럭)을 출력하는 메서드
		for(int i=0; i<dto.length; i++) {
			System.out.print(dto[i].getName() + " ");
		}
		System.out.println();
	}
	
	public void showAll() { // 모든 객체를 출력하는 메서드
		for(int i=0; i<s.length; i++) {
			System.out.print(s[i].getName() + " ");
		}
		System.out.println();
		for(int i=0; i<a.length; i++) {
			System.out.print(a[i].getName() + " ");
		}
		System.out.println();
		for(int i=0; i<b.length; i++) {
			System.out.print(b[i].getName() + " ");
		}
		System.out.println();
		System.out.println("<<<조회를 완료하였습니다.>>>");
	}
	
	public void input() {
		String block = showBlock();
		//이름 입력받은걸 객체로 만들고
		//번호 입력받은걸 인덱스로 삼아서 만든 객체를 그 자리에 넣는다.
		CustomerDTO tmp = new CustomerDTO();
		System.out.print("이름>> ");
		tmp.setName(scan.next());
		System.out.print("번호>> ");
		int seatNumber = verifyInt();
		
		if(block.equals("s")) {
			s[seatNumber-1] = tmp; // 좌석은 1번부터 시작하는데 배열인덱스는 0부터 시작하므로 -1
		} else if(block.equals("a")) {
			a[seatNumber-1] = tmp;
		} else {
			b[seatNumber-1] = tmp;
		}
		
	}
	
	public void cancle() { // 취소 메서드
		String block = showBlock();      // 배열에서 삭제시킨다는 개념이 아닌
		System.out.print("이름>> ");      // 처음의 "---" 값으로 덮어버린다는 개념
		String cancleName = scan.next();
		
		if(block.equals("s")) { // 입력받은 이름을 가진 객체를 찾아서 새로운 객체(기본생성자->초기값"---")로 덮어쓴다.
			for(int i=0; i<s.length; i++) {
				if(cancleName.equals(s[i].getName())) {
					s[i] = new CustomerDTO();
					break;
				} 
			}
		} else if(block.equals("a")) {
			for(int i=0; i<a.length; i++) {
				if(cancleName.equals(a[i].getName())) {
					a[i] = new CustomerDTO();
					break;
				}
			}
		} else if(block.equals("b")){
			for(int i=0; i<b.length; i++) {
				if(cancleName.equals(b[i].getName())) {
					b[i] = new CustomerDTO();
					break;
				}
			}
		} 
	}
}

class Viewer {
	Scanner scan = new Scanner(System.in);
	Controller ctr = new Controller();
	int choice = 0;
	
	public Viewer() { // 기본생성자가 호출되면 프로그램 시작
		run();
	}
	
	public void run() {
		System.out.println("명품콘서트홀 예약 시스템입니다.");		
		
		while(true) {
			System.out.print("예약: 1, 조회: 2, 취소: 3, 끝내기: 4 >> ");
			choice = ctr.verifyInt();
			switch(choice) {
			case 1 : // input
				ctr.input();
				break;
			case 2 : // 조회(전체출력)
				ctr.showAll();
				break;
			case 3 : // 취소(해당좌석 조회 후 삭제)	
				ctr.cancle();
				break;
			case 4 : // finish
				finish();
			}
			
		}
		
	}
	
	public void finish() {
		System.out.println("<<< 프로그램 종료 >>>");
		System.exit(0);
	}
	
	
	
}

public class Exercise12 {
	public static void main(String[] args) {
		new Viewer();
	}

}

 

 

결과