package kr.or.ddit.basic;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
import java.util.TreeMap;
public class HotelTest {
private HashMap<Integer, Room> hotelMap;
private Scanner scan;
public HotelTest() {
hotelMap = new HashMap<Integer, Room>();
scan = new Scanner(System.in);
}
public static void main(String[] args) {
HotelTest h = new HotelTest();
h.start();
}
public void start() {
initialRoom(); // 룸의 초기값 저장
System.out.println("*********************************************");
System.out.println(" 호텔문을 열었습니다. 어서오십시요.");
System.out.println("*********************************************");
while (true) {
int choice = menu();
switch (choice) {
case 1:
checkIn();
break;
case 2:
checkOut();
break;
case 3:
roomState();
break;
case 4:
System.out.println("*********************************************");
System.out.println(" 호텔문을 닫았습니다.");
System.out.println("*********************************************");
return;
default:
System.out.println("잘못된 입력입니다");
}
}
}
private void initialRoom() {
for(int i=201; i<=209; i++) {
Room room = new Room(i, "싱글룸");
hotelMap.put(i, room);
}
for(int i=301; i<=309; i++) {
Room room = new Room(i, "더블룸");
hotelMap.put(i, room);
}
for(int i=401; i<=409; i++) {
Room room = new Room(i, "스위트룸");
hotelMap.put(i, room);
}
}
private void roomState() {
System.out.println("----------------------------------------------");
System.out.println(" 현재 객실 상태");
System.out.println("----------------------------------------------");
System.out.println("방 번호\t방 종류\t투숙객 이름");
System.out.println("----------------------------------------------");
// hotelMap의 키를 정렬하여 순회하기 위한 TreeMap 생성
TreeMap<Integer, Room> sortedMap = new TreeMap<>(hotelMap);
for (Map.Entry<Integer, Room> entry : sortedMap.entrySet()) {
int num = entry.getKey();
Room room = entry.getValue();
String type = room.getType();
String guest = room.getGuest();
if (room.getGuest() == null || room.getGuest().isEmpty()) {
guest = " -";
}
System.out.println(num + "\t" + type + "\t" + guest);
}
// 순서 정렬X
// for(Map.Entry<Integer, Room> entry : hotelMap.entrySet()) {
// int num = entry.getKey();
// Room room = entry.getValue();
// String type = room.getType();
// String guest = room.getGuest();
// if(room.getGuest()==null||room.getGuest().isEmpty()) {
// guest=" -";
// }
// System.out.println(num+"\t"+type+"\t"+guest);
// }
}
private void checkOut() {
System.out.println("----------------------------------------------");
System.out.println(" 체크아웃 작업");
System.out.println("----------------------------------------------");
System.out.println("체크아웃 할 방 번호를 입력하세요.");
System.out.print("방번호 입력 >> ");
int num = scan.nextInt();
if(!hotelMap.containsKey(num)) {
System.out.println(num+"호 객실은 존재하지 않습니다.");
} else {
Room room = hotelMap.get(num);
if(room.getGuest()==null||room.getGuest().isEmpty()) {
System.out.println(num+"호 객실에는 체크인 한 사람이 없습니다.");
} else {
String name = room.getGuest();
room.setGuest("");
hotelMap.put(num, room);
System.out.println(num+"호 객실의 "+name+"님 체크아웃을 완료하였습니다.");
}
}
}
private void checkIn() {
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();
if(!hotelMap.containsKey(num)) {
System.out.println(num+"호 객실은 존재하지 않습니다.");
} else {
Room room = hotelMap.get(num);
if(room.getGuest()!=null&&!room.getGuest().isEmpty()) {
System.out.println(num+"호 객실은 이미 손님이 있습니다.");
} else {
System.out.print("이름 입력 >> ");
String name = scan.next();
room.setGuest(name);
hotelMap.put(num, room);
System.out.println("체크인이 완료되었습니다.");
}
}
}
public int menu() {
System.out.println("-----------------------------------------------------------");
System.out.println("어떤 업무를 하시겠습니까?");
System.out.println("1. 체크인 2. 체크아웃 3. 객실상태 4. 업무종료");
System.out.println("-----------------------------------------------------------");
System.out.print("선택>> ");
return scan.nextInt();
}
}
class Room {
private int num;
private String type;
private String guest;
public Room(int num, String type) {
super();
this.num = num;
this.type = type;
}
public int getNum() {
return num;
}
public void setNum(int num) {
this.num = num;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getGuest() {
return guest;
}
public void setGuest(String guest) {
this.guest = guest;
}
}