[KOSTA] Spring 기반 Cloud 서비스 개발자 양성 과정 15일차 - 객체 모델링 실습

JUNBEOM PARK·2022년 2월 18일
0
post-thumbnail

🎲 객체 모델링

Usecase

Class Diagram

📃 코드

Service.java

package kosta.hotel;

public interface Service {
   public void show();
   public String getName();
}

Hotel.java

package kosta.hotel;

public class Hotel implements Service {
   private String roomNo;
   private String roomClass;
   private int price;
   
   public Hotel() {}
   
   public Hotel(String roomNo, String roomClass, int price) {
      super();
      this.roomNo = roomNo;
      this.roomClass = roomClass;
      this.price = price;
   }

   @Override
   public void show() {
      System.out.println("방번호 : "+roomNo);
      System.out.println("방등급 : "+roomClass);
      System.out.println("1박당 가격 : "+price);
   }

   @Override
   public String getName() {
      // TODO Auto-generated method stub
      return null;
   }

   public String getRoomNo() {
      return roomNo;
   }

   public void setRoomNo(String roomNo) {
      this.roomNo = roomNo;
   }

   public String getRoomClass() {
      return roomClass;
   }

   public void setRoomClass(String roomClass) {
      this.roomClass = roomClass;
   }

   public int getPrice() {
      return price;
   }

   public void setPrice(int price) {
      this.price = price;
   }

}

User.java

package kosta.hotel;

public class User extends Hotel implements Service {
   private String name;
   private String people;
   private int days;
   
   public User() {}

   public User(String roomNo, String roomClass, int price) {
      super();
      setRoomNo(roomNo);
      setRoomClass(roomClass);
      setPrice(price);
   }

   public User(String roomNo, String roomClass, int price, String name, String people, int days) {
      super(roomNo, roomClass, price);
      this.name = name;
      this.people = people;
      this.days = days;
   }

   @Override
   public void show() {
      // TODO Auto-generated method stub
      super.show();
      System.out.println("예약자 이름 : " + name);
      System.out.println("인원수  : " + people);
      System.out.println("숙박일 : " + days);
   }
   
   public void cancel() {
      setName(null);
      setPeople(null);
      setDays(0);
   }
   
   public void reservation(String name, String People, int Days) {
      setName(name);
      setPeople(People);
      setDays(Days);
   }

   public String getName() {
      return name;
   }

   public void setName(String name) {
      this.name = name;
   }

   public String getPeople() {
      return people;
   }

   public void setPeople(String people) {
      this.people = people;
   }

   public int getDays() {
      return days;
   }

   public void setDays(int days) {
      this.days = days;
   }
   
   
}

Stay.java

package kosta.hotel;

public class Stay extends User implements Service {
   private boolean checkIn;
   
   public Stay() {}
   

   public Stay(String name, String people, int days) {
      super(name, people, days);
   }


   public Stay(String roomNo, String roomClass, int price, String name, String people, int days, boolean checkIn) {
      super(roomNo, roomClass, price, name, people, days);
      this.checkIn = checkIn;
   }

   @Override
   public void show() {
      // TODO Auto-generated method stub
      super.show();
   }
   
   public void checkOut() {
      System.out.println("-----계산서-----");
      show();
      System.out.println("금액 : " + getPrice()*getDays());
      System.out.println("결제수단 : 1.현금 2.신용카드 3.수표");
   }

   public boolean isCheckIn() {
      return checkIn;
   }

   public void setCheckIn(boolean checkIn) {
      this.checkIn = checkIn;
   }
   
   
}

Main.java


package kosta.hotel;

import java.util.Scanner;

public class Main {

   public static void main(String[] args) {
      Scanner sc = new Scanner(System.in);
      Hotel h[] = {
            new Hotel("1","Twin room",10),
            new Hotel("2","Triple room",20),
            new Hotel("3","Suite room",40)
      };
      User u[] = new User[10];
      int count = 0;
      
      while(true) {
         System.out.println("1.객실조회 2.예약 3.예약조회 4.예약취소 5.CheckIn 6.종료");
         System.out.print("메뉴 : ");
         String menu = sc.nextLine();
         switch (menu) {
         case "1":
            for (int i = 0; i < h.length; i++) {
               h[i].show();
            }
            break;
         case "2":
            System.out.println("예약하려는 방 선택 1.Twin room(2명이하), 2.Triple room(3명이하), 3.Suite room(4명이상)");
            System.out.print("선택 : ");
            String room = sc.nextLine();
            String room2 = "";
            switch (room) {
            case "1":
               room2 = "Twin room";
               break;
            case "2":
               room2 = "Triple room";
               break;
            case "3":
               room2 = "Suite room";
               break;
            }
            System.out.print("예약자이름 : ");
            String name = sc.nextLine();
            System.out.print("인원수 : ");
            String people = sc.nextLine();
            System.out.print("숙박일 : ");
            int days = sc.nextInt();
            sc.nextLine();
            for (int i = 0; i < h.length; i++) {
               if (h[i].getRoomClass().equals(room2)) {
                  u[count++] = new User(h[i].getRoomNo(),h[i].getRoomClass(),h[i].getPrice(),name,people,days);
               }
            }
            System.out.println("예약완료.");
            break;
         case "3":
            System.out.print("예약자이름 : ");
            String name2 = sc.nextLine();
            for (int i = 0; i < count; i++) {
               if ((u[i]).getName().equals(name2)) {
                  (u[i]).show();
               }
            }
            
            break;
         case "4":
            System.out.print("예약자이름 : ");
            String name3 = sc.nextLine();
            for (int i = 0; i < count; i++) {
               if (u[i].getName().equals(name3)) {
                  System.out.println("예약취소 완료");
                  u[i].cancel();
                  for (int j = i; j < count-1;j++) {
                     u[j] = u[j+1];
                  }
                  count--;
               }
            }
            
            break;
         case "5":
            System.out.print("예약자이름 : ");
            String name4 = sc.nextLine();
            System.out.println("CheakIn 하겠습니까? 1.yes 2.no");
            String cheak = sc.nextLine();
            switch (cheak) {
            case "1":
               for (int i = 0; i<count; i++) {
                  if (u[i].getName().equals(name4)) {
                     
                     System.out.println("---------------계산서----------");
                     System.out.println("총금액 : " + u[i].getPrice() * u[i].getDays());
                     System.out.println("결제수단 : 1.현금 2.신용카드 3.수표");
                     String pay = sc.nextLine();
                     switch(pay) {
                     case "1":
                        System.out.println("결제수단 : 현금");
                        break;
                     case "2":
                        System.out.println("결제수단 : 신용카드");
                        break;
                     case "3":
                        System.out.println("결제수단 : 수표");
                        break;
                     }
                     System.out.println("결제금액 : " + u[i].getPrice() * u[i].getDays());
                     System.out.println("이용해주셔서 감사합니다.");
                  }
               }
               break;
            case "2":
               for (int i = 0; i<count; i++) {
                  if (u[i].getName().equals(name4)) {
                     u[i].cancel();
                  }
               }
               System.out.println("예약이 취소되었습니다.");
               break;
            }
            break;
         case "6":
            return;
         }
      }
   }
}
profile
DB 엔지니어👍

0개의 댓글