-
View ClassAutomotive/WPF MVVM 2023. 11. 27. 16:36
이번에는 WPF MVVM 중 View에 대해 학습하였다.
namespace Youtube_Lecture.Models { public class RoomID { public int FloorNumber { get; } public int RoomNumber { get; } public RoomID(int floorNumber, int roomNumber) { FloorNumber = floorNumber; RoomNumber = roomNumber; } public override string ToString() { return $"{FloorNumber}{RoomNumber}"; } public override bool Equals(object obj) { return obj is RoomID roomID && FloorNumber == roomID.FloorNumber && RoomNumber == roomID.RoomNumber; } public override int GetHashCode() { return HashCode.Combine(FloorNumber, RoomNumber); } public static bool operator == (RoomID roomID1, RoomID roomID2) { if(roomID1 == null && roomID2 == null) { return true; } return !(roomID1 is null) && roomID1.Equals(roomID2); } public static bool operator !=(RoomID roomID1, RoomID roomID2) { return !(roomID1 == roomID2); } } }
RoomID class는 층과 방에 대한 정보를 저장한다.
public override bool Equals(object obj) { return obj is RoomID roomID && FloorNumber == roomID.FloorNumber && RoomNumber == roomID.RoomNumber; } public override int GetHashCode() { return HashCode.Combine(FloorNumber, RoomNumber); }
Equals를 재정의하면 HashSet 또는 Dictionary에 저장 될 경우를 대비해서 GetHashCode를 재 정의해야한다.
public static bool operator == (RoomID roomID1, RoomID roomID2) { if(roomID1 == null && roomID2 == null) { return true; } return !(roomID1 is null) && roomID1.Equals(roomID2); } public static bool operator !=(RoomID roomID1, RoomID roomID2) { return !(roomID1 == roomID2); }
List에 넣기 전에 이미 예약되어있는 방 번호인지 확인하지 위해 operator == 연산자와 operator != 연산자를 재 정의했다.
public class Reservation { public RoomID RoomID { get; } public string Username { get; } public DateTime StartTime { get; } public DateTime EndTime { get; } public TimeSpan Length => EndTime.Subtract(StartTime); public Reservation(RoomID roomID, string username, DateTime startTime, DateTime endTime) { RoomID = roomID; StartTime = startTime; EndTime = EndTime; Username = username; } internal bool Conflicts(Reservation reservation) { if(reservation.RoomID == RoomID) { return false; } return reservation.StartTime < EndTime && reservation.EndTime > StartTime; } }
예약 클래스는 방 번호, 사용자 이름, 시작 끝 시간을 갖고있다.
internal bool Conflicts(Reservation reservation) { if(reservation.RoomID == RoomID) { return false; } return reservation.StartTime < EndTime && reservation.EndTime > StartTime; }
Conflicts 함수는 새로운 방이 입력되면 추가하고, 기존에 있던 방이 추가 될 경우 예약 시간과 겹치지 않을 때 추가된다.
public class ReservationBook { private readonly List<Reservation> _reservations; public ReservationBook() { _reservations = new List<Reservation>(); } /// <summary> /// /// </summary> /// <param name="username"></param> /// <returns></returns> public IEnumerable<Reservation> GetReservationsForUser(string username) { return _reservations.Where(r => r.Username == username); } /// <summary> /// /// </summary> /// <param name="reservation"></param> public void AddReservation(Reservation reservation) { foreach(Reservation existingReservation in _reservations) { if(existingReservation.Conflicts(reservation)) { throw new ReservationConfilicException(existingReservation, reservation); } } //이미 있는 방과 예약시간이 겹치지 않는다면 추가. _reservations.Add(reservation); } }
예약 내역을 List로 갖고 있고, 유저 이름으로 예약 내역을 찾는 함수와 예약 내역을 추가하는 함수를 작성했다.
public class ReservationConfilicException : Exception { public Reservation ExistingReservation { get; } public Reservation IncomingReservation { get; } public ReservationConfilicException() { } public ReservationConfilicException(string message, Reservation existingReservation, Reservation incomingReservation) : base(message) { ExistingReservation = existingReservation; IncomingReservation = incomingReservation; } public ReservationConfilicException(string message, Exception inner, Reservation existingReservation, Reservation incomingReservation) : base(message) { ExistingReservation = existingReservation; IncomingReservation = incomingReservation; } protected ReservationConfilicException( System.Runtime.Serialization.SerializationInfo info, System.Runtime.Serialization.StreamingContext context) : base(info, context) { } public ReservationConfilicException(Reservation existingReservation, Reservation incomingReservation) { ExistingReservation = existingReservation; IncomingReservation = incomingReservation; } }
중복되는 방이 있거나, 예약 시간이 겹치는 경우 예외 처리를 위해 ReservationConfilicException을 만들었다.
생성자로 Reservation 변수를 2개를 받는데, 추후에 Exception 처리를 위해 사용 될 것으로 생각된다.
public class Hotel { private readonly ReservationBook _reservationBook; public string Name { get; } public Hotel(string name) { Name = name; _reservationBook = new ReservationBook(); } /// <summary> /// /// </summary> /// <param name="username"></param> /// <returns></returns> public IEnumerable<Reservation> GetReservationsForUser(string username) { return _reservationBook.GetReservationsForUser(username); } /// <summary> /// /// </summary> /// <param name="reservation"></param> /// <exception cref="ReservationConflict" public void MakeReservation(Reservation reservation) { _reservationBook.AddReservation(reservation); } }
public class Hotel { private readonly ReservationBook _reservationBook; public string Name { get; } public Hotel(string name) { Name = name; _reservationBook = new ReservationBook(); } /// <summary> /// /// </summary> /// <param name="username"></param> /// <returns></returns> public IEnumerable<Reservation> GetReservationsForUser(string username) { return _reservationBook.GetReservationsForUser(username); } /// <summary> /// /// </summary> /// <param name="reservation"></param> /// <exception cref="ReservationConflict" public void MakeReservation(Reservation reservation) { _reservationBook.AddReservation(reservation); } }
호텔 클래스는 예약 내역 List를 가지고 있고, 예약 내역에서 사용자를 찾는 기능과 예약내역을 찾는 기능을 만들었다.