🍥구현 기능
🍥구현하기
TransactionInfo 클래스
data class TransactionInfo (val name:String, val info:String, val money:Int)
ReservationInfo 클래스
data class ReservationInfo(val name: String, var roomNum:Int, var checkIn:String, var checkOut:String)
ReservationInfoCollection 클래스
class ReservationInfoCollection {
var reservationInfoList = arrayOf<ReservationInfo>()
fun addReservationInfo(newReservationInfo: ReservationInfo){
reservationInfoList += newReservationInfo
}
private fun printReservationInfoList(){
for(i in 0 until reservationInfoList.size){
val name = reservationInfoList[i].name
val roomNum = reservationInfoList[i].roomNum
var checkIn = reservationInfoList[i].checkIn
var checkOut = reservationInfoList[i].checkOut
checkIn = checkIn.substring(0,4)+"-"+checkIn.substring(4,6)+"-"+checkIn.substring(6)
checkOut = checkOut.substring(0,4)+"-"+checkOut.substring(4,6)+"-"+checkOut.substring(6)
println("${i+1}. 예약자: ${name}, 방 번호: ${roomNum}, 체크인: ${checkIn}, 체크아웃:${checkOut}")
}
}
fun printReservationInfoCollection(){
printReservationInfoList()
}
fun printSortedReservationInfoCollection(){
reservationInfoList.sortBy{it.checkIn}
printReservationInfoList()
}
fun isRoomAvailable(inputRoomNum:Int, inputCheckIn:String, inputCheckOut:String):Boolean{
for(i in 0 until reservationInfoList.size){
if(reservationInfoList[i].roomNum != inputRoomNum) continue
if(reservationInfoList[i].checkIn <= inputCheckIn && inputCheckIn <= reservationInfoList[i].checkOut) return false
if(reservationInfoList[i].checkIn <= inputCheckOut && inputCheckOut <= reservationInfoList[i].checkOut) return false
}
return true
}
}
TransactionInfoCollection 클래스
class TransactionInfoCollection {
var transactionInfoList = arrayOf<TransactionInfo>()
fun addNewTransactionInfo(newTransactionInfo: TransactionInfo){
transactionInfoList += newTransactionInfo
}
fun printTransactionCollection(inputName: String) {
var index = 0
for(i in 0 until transactionInfoList.size){
if(transactionInfoList[i].name != inputName) continue
println("${index+1}. ${transactionInfoList[i].info} ${transactionInfoList[i].money}원")
index++
}
if(index==0) println("해당 예약자의 입출금 내역을 찾을 수 없습니다.")
}
}
HotelReservation 클래스
package com.sunjoolee.sparta_assignment_hotelreservation
import android.util.Log
import java.time.LocalDate
import java.time.format.DateTimeFormatter
class HotelReservation {
init{
println("호텔 예약 프로그램입니다.")
}
private val menus = arrayOf(
"방 예약",
"예약 목록 출력",
"예약 목록 (정렬) 출력",
"시스템 종료",
"금액 입금-출금 내역 목록 출력",
"예약 변경/취소")
private var reservationInfoCollection= ReservationInfoCollection()
private var transactionInfoCollection = TransactionInfoCollection()
fun start(){
while(true) {
printMenu()
var input = readLine()!!.toInt()
when (input) {
1 -> runMenu1()
2 -> runMenu2()
3 -> runMenu3()
4 -> {
runMenu4()
return
}
5 -> runMenu5()
6 -> runMenu6()
else -> println("존재하지 않는 메뉴입니다.")
}
}
}
private fun printMenu(){
println("[메뉴]")
for(i in 0 until menus.size){
print("${i+1}. ${menus[i]}.\t")
}
println()
}
private fun runMenu1(){
println("예약자의 성함을 입력해주세요.")
val name = readLine()!!
var roomNum = 0
while(roomNum < 100 || 999< roomNum) {
println("예약할 방 번호를 입력해주세요.")
roomNum = readLine()!!.toInt()
if(roomNum < 100 || 999< roomNum) println("올바르지 않은 방 번호입니다. 방 번호는 100부터 999까지 입니다.")
}
var nowDate = LocalDate.now()
val formatter = DateTimeFormatter.ofPattern("yyyyMMdd")
val nowDateStr = nowDate.format(formatter)
var checkIn:String
var checkOut:String
while(true) {
checkIn = "00000000"
while (checkIn.toInt() < nowDateStr.toInt()) {
println("체크인 날짜를 입력해주세요. (표기 방식: 20230631)")
checkIn = readLine()!!
if (checkIn.toInt() < nowDateStr.toInt()) println("체크인 날짜로 지난 날짜를 선책할 수 없습니다.")
}
checkOut = "00000000"
while (checkOut.toInt() <= checkIn.toInt()) {
println("체크아웃 날짜를 입력해주세요. (표기 방식: 20230631)")
checkOut = readLine()!!
if (checkOut.toInt() <= checkIn.toInt()) println("체크아웃 날짜로 체크인 날짜보다 전이거나 같은 날짜를 선택할 수 없습니다.")
}
if(reservationInfoCollection.isRoomAvailable(roomNum, checkIn, checkOut)) break;
else println("해당 기간에 이미 방을 사용 중입니다. 다른 날짜를 선택해주세요.")
}
transactionInfoCollection.addNewTransactionInfo(TransactionInfo(name, "예약비 입금", 50000))
transactionInfoCollection.addNewTransactionInfo(TransactionInfo(name, "예약비 출금", -50000))
val newReservationInfo = ReservationInfo(name, roomNum, checkIn, checkOut)
reservationInfoCollection.addReservationInfo(newReservationInfo)
println("호텔 예약이 완료되었습니다.")
}
private fun runMenu2(){
println("호텔 예약 목록입니다.")
reservationInfoCollection.printReservationInfoCollection()
}
private fun runMenu3(){
println("호텔 예약 목록입니다. (정렬 완료)")
reservationInfoCollection.printSortedReservationInfoCollection()
}
private fun runMenu4(){
println("시스템을 종료합니다.")
}
private fun runMenu5(){
println("조회할 예약자의 성함을 입력하세요")
val name = readLine()!!
transactionInfoCollection.printTransactionCollection(name)
}
private fun runMenu6(){
}
}
MyHotelReservation.kt
fun main(){
val hotelReservation = HotelReservation()
hotelReservation.start()
}