JAVA/문제풀이
컬렉션 프레임워크 연습문제4(Hotel)
inderrom
2022. 10. 28. 13:51
package kr.or.ddit.basic;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Scanner;
import java.util.Set;
public class DeadokHotel {
private Map<Integer,Room> hotelMap;
Scanner scan;
// 생성자
public DeadokHotel() {
hotelMap = new HashMap<Integer, Room>();
scan = new Scanner(System.in);
// 객실 초기화
for(int i = 2; i<=4; i++) {
String type = null; //방종류가 저장될 변수
switch(i){
case 2 : type = "싱글룸"; break;
case 3 : type = "더블룸"; break;
case 4 : type = "스위트룸"; break;
}
for(int j=1; j<=9;j++) {
int num = i * 100 + j;
hotelMap.put(num, new Room(num,type));
}
}
} // 생성자 끝..
public static void main(String[] args) {
new DeadokHotel().hotelStart();
}
// 시작 메서드
public void hotelStart() {
System.out.println("*********************************************");
System.out.println(" 호텔문을 열었습니다. 어서오십시요.");
System.out.println("*********************************************");
System.out.println();
while(true) {
int choice = displayMenu();
switch (choice) {
case 1: //체크인
checkIn(); break;
case 2: //체크아웃
checkOut();
break;
case 3: //객실상태출력
showRoom();break;
case 4:
System.out.println();
System.out.println("*********************************************");
System.out.println(" 호텔문을 닫았습니다.");
System.out.println("*********************************************");
return;
default:
System.out.println("작업 번호를 잘못 입력했습니다. 다시 입력해주세요");
break;
}
}
}
// 체크아웃을 처리하는 메서드
private void checkOut() {
System.out.println();
System.out.println("----------------------------------------------");
System.out.println(" 체크아웃 작업");
System.out.println("----------------------------------------------");
System.out.println("체크아웃 할 방 번호를 입력하세요.");
System.out.println("방번호 입력 >> ");
int num = scan.nextInt();
// 입력한 값이 Map의 Key값에 없으면 잘못 입력한 방번호이다.
if(!hotelMap.containsKey(num)) {
System.out.println(num + "호 객실은 존재하지 않습니다.");
return;
}
// 해당 객실에 손님이 있는지 검사
if(hotelMap.get(num).getGuestName()==null) {
System.out.println(num + "호 객실에는 손님이 없스니다");
return;
}
//현재의 투숙객 이름 구하기
String name = hotelMap.get(num).getGuestName();
//체크아웃 작업은 해당 객실의 투숙객 이름을 null로 변경하면 된다.
hotelMap.get(num).setGuestName(null);
System.out.println(num + "호 객실의" + name + "씨가 체크아웃을 완료했습니다...");
}
// 전체 객실 상태를 출력하는 메서드
private void showRoom() {
System.out.println();
//Map의 Key값인 방번호를 구해서 List에 넣은 후 정렬한다.
Set<Integer> roomKeySet = hotelMap.keySet();
List<Integer> roomList = new ArrayList<Integer>(roomKeySet);
Collections.sort(roomList);
System.out.println();
System.out.println("----------------------------------------------");
System.out.println(" 현재 객실 상태");
System.out.println("----------------------------------------------");
System.out.println(" 방 번호 방 종류 투숙객 이름");
System.out.println("----------------------------------------------");
//List에서 방번호를 하나씩 꺼내와 Map에서 해당 방번호와 같이 저장된 Room객체를 구해서 출력한다.
for(int num : roomList) {
Room r = hotelMap.get(num);
System.out.print(r.getRoomNum() + "\t" + r.getRoomType() + "\t");
String name = " -";
if( r.getGuestName()!=null ) {
name = r.getGuestName();
}
System.out.println(name);
}
System.out.println("-------------------------------------------------");
}
// 체크인 하는 메서드
private void checkIn() {
System.out.println();
System.out.println("----------------------------------------------");
System.out.println(" 체크인 작업");
System.out.println("----------------------------------------------");
System.out.println(" * 201~209 : 싱글룸");
System.out.println(" * 301~309 : 더블룸");
System.out.println(" * 401~409 : 스위트룸");
System.out.println("----------------------------------------------");
System.out.print("방 번호 입력 >>");
int num = scan.nextInt();
// 입력한 값이 Map의 Key값에 없으면 잘못 입력한 방번호이다.
if(!hotelMap.containsKey(num)) {
System.out.println(num + "호 객실은 존재하지 않습니다.");
return;
}
// 해당 객실에 손님이 있는지 검사
if(hotelMap.get(num).getGuestName()!=null) {
System.out.println(num + "호 객실에는 이미 손님이 있습니다.");
return;
}
System.out.println("누구를 체크인 하시겠습니까?");
System.out.print("이름 입력 >>");
String name = scan.next();
// 입력 받은 투숙객 이름을 해당 객실의 투숙객 명단에 넣는다.
hotelMap.get(num).setGuestName(name);
System.out.println(num + "호 객실에 " + name + "씨가 체크인이 완료되었습니다.");
}
// 메뉴를 출력하고 작업번호를 입력받아 반환하는 메서드
private int displayMenu(){
System.out.println("-----------------------------------------------------------");
System.out.println("어떤 업무를 하시겠습니까?");
System.out.println("1. 체크인 2. 체크아웃 3. 객실상태 4. 업무종료");
System.out.println("-----------------------------------------------------------");
System.out.println("선택>>");
return scan.nextInt();
}
}
class Room{
private int roomNum; //방번호
private String roomType; //방종류
private String guestName; //투숙객이름
public int getRoomNum() {
return roomNum;
}
public void setRoomNum(int roomNum) {
this.roomNum = roomNum;
}
public String getRoomType() {
return roomType;
}
public void setRoomType(String roomType) {
this.roomType = roomType;
}
public String getGuestName() {
return guestName;
}
public void setGuestName(String guestName) {
this.guestName = guestName;
}
public Room(int roomNum, String roomType) {
super();
this.roomNum = roomNum;
this.roomType = roomType;
}
}