참치코더의 꿈 메모장

2021-08-25 Java - 호텔 방 예약 프로그램 CRUD 과제 (Map 사용) 본문

JAVA

2021-08-25 Java - 호텔 방 예약 프로그램 CRUD 과제 (Map 사용)

참치깡 2021. 8. 25. 20:29
728x90

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
 
public class Hotel{
 
    public static void main(String[] args) {
        
        System.out.println("**************************");
        System.out.println("호텔 문을 열었습니다.");
        System.out.println("**************************");
        System.out.println();
        System.out.println();
        
        Map<Integer, Object> hotel = new HashMap<>();
        
        while(true) {
            System.out.println("*******************************************");
            System.out.println("어떤 업무를 하시겠습니까?");
            System.out.println("1.체크인  2.체크아웃 3.객실상태 4.업무종료");
            System.out.println("*******************************************");
            
            System.out.print("메뉴선택 =>");
            int num = ScanUtil.nextInt();
            System.out.println();
            
            switch(num) {
                case 1: checkIn(hotel); break;
                case 2: checkOut(hotel); break;
                case 3: status(hotel); break;
                case 4
                    System.out.println();
                    System.out.println("**************************");
                    System.out.println("호텔 문을 닫았습니다.");
                    System.out.println("**************************");
                    System.exit(0);
                    break;
            }
        }
 
    }
 
    private static void status(Map<Integer, Object> hotel) {
        
        /*List<Entry<Integer, Object>> list = new ArrayList<>();
        
        //Map.Entry 인터페이스의 메서드를 사용하면  getKey()로 key 객체를 가져오거나 getValue()를 사용하면  value를 가져올수 있다.
                
        Collections.sort(list, new Comparator<Entry<Integer, Object>>() {
 
            @Override
            public int compare(Entry<Integer, Object> name1, Entry<Integer, Object> name2) {
                return ((String) name1.getValue()).compareTo((String) name2.getValue());  //내림차순으로 정렬 
            }  // 번호 순으로 정렬하려고 했는데 실패했다....
        });*/
        
        
        Set<Integer> keyset = hotel.keySet();
        //Set<Integer> keyset = ((Map<Integer, Object>) list).keySet();  -> 왜 안될까?
        
        Iterator<Integer> iterator = keyset.iterator();
        
        while(iterator.hasNext()) {
            
            int str = iterator.next();
            
            String name = (String)hotel.get(str);
            
            System.out.println("방번호 : " + str + ", 투숙객 : " + name );
        }
        System.out.println();
    }
 
    private static int checkOut(Map<Integer, Object> hotel) {
        
        System.out.println("어느방을 체크아웃 하시겠습니까?");
        System.out.print("방번호 입력=>");
        int num = ScanUtil.nextInt();
        
        if(hotel.get(num) == null) {
            System.out.println(num +"방에는 체크인한 사람이 없습니다.");
            System.out.println();
            return 0;
        }
        
        hotel.remove(num);
        System.out.println("체크아웃 되었습니다.");
        System.out.println();
        return 0;
        
    }
 
    private static int checkIn(Map<Integer, Object> hotel) {
        
        System.out.println("어느방에 체크인 하시겠습니까?");
        System.out.print("방번호 입력 =>");
    
        int num = ScanUtil.nextInt();
        System.out.println();
        
        System.out.println("누구를 체크인 하시겠습니까?");
        System.out.print("이름 입력=>");
        
        String name = ScanUtil.nextLine();
        
        if(hotel.get(num) != null) {
            System.out.println(num +"방에는 이미 사람이 있습니다.");
            System.out.println();
            return 0;
        }
        
        hotel.put(num, name);
        
        System.out.println("체크인 되었습니다.");
        System.out.println();
        return 0;
        
    }
 
}
 
cs

*  Map 컬렉션 프레임 워크를 이용한

   방을 예약하고 예약 상태를 살펴보고 체크아웃을 하는 간단한 CRUD 과제를 진행하였다.

 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
import java.util.Scanner;
 
public class ScanUtil{
    
    private static Scanner s = new Scanner(System.in);
    
    public static String nextLine() {
        return s.nextLine();
    }
    
    public static int nextInt() {
        return Integer.parseInt(s.nextLine());
    }
 
}
 
 
cs

* 이건 키보드로 이클립스에서 입력받을 때 static 선언을 하여 객체 선언을 하지 않아도 한 문장으로

받을수 있게 만든 것이다.

 

* 빨리 git 사용법을 알아야 깃에 올릴텐데 아직 공부중이다. git 마스터 하면 한번 깃에다가 올려보자!  

728x90
Comments