By GokiSoft.com| 15:00 28/10/2021|
C Sharp

[Video] Cách 2: Chương trình quản lý khách sạn - Develop Hotel Project - Lập Trình C# - Lập Trình C Sharp - C2010L


Link Video Bài Giảng

Chương trình quản lý khách sạn - Develop Hotel Project - Lập Trình C# - Lập Trình C Sharp


#Utility.cs


using System;
namespace BT1460
{
    public class Utility
    {
        public static int ReadInt()
        {
            int value;

            while(true)
            {
                try
                {
                    value = int.Parse(Console.ReadLine());
                    return value;
                } catch(Exception e)
                {
                    Console.WriteLine("Nhap lai!!!");
                }
            }
        }

        public static string ConvertDateTimeToString(DateTime myDate)
        {
            return myDate.ToString("yyyy-MM-dd HH:mm:ss");
        }

        public static DateTime ConvertStringToDateTime(string str)
        {
            DateTime myDate = DateTime.ParseExact(str, "yyyy-MM-dd HH:mm:ss",
                                       System.Globalization.CultureInfo.InvariantCulture);
            return myDate;
        }
    }
}


#Room.cs


using System;
namespace BT1460
{
    public class Room
    {
        public string Name { get; set; }
        public string RoomNo { get; set; }
        public int Price { get; set; }
        public int Floor { get; set; }
        public int NumMax { get; set; }

        public Room()
        {
        }

        public Room(string name, string roomNo, int price, int floor, int numMax)
        {
            Name = name;
            RoomNo = roomNo;
            Price = price;
            Floor = floor;
            NumMax = numMax;
        }

        public void Input()
        {
            Console.WriteLine("================== NHAP ROOM ===================");
            Console.WriteLine("Nhap ma phong: ");
            RoomNo = Console.ReadLine();

            Console.WriteLine("Nhap ten phong: ");
            Name = Console.ReadLine();

            Console.WriteLine("Nhap gia: ");
            Price = Utility.ReadInt();

            Console.WriteLine("Nhap tang: ");
            Floor = Utility.ReadInt();

            Console.WriteLine("Nhap so nguoi toi da: ");
            NumMax = Utility.ReadInt();
        }

        public void Display()
        {
            Console.WriteLine("Ma phong: {0}, ten phong: {1}, gia: {2}, " +
                "tan: {3}, so nguoi: {4}", RoomNo, Name, Price, Floor, NumMax);
        }
    }
}


#Program.cs


using System;

namespace BT1460
{
    class Program
    {
        static void Main(string[] args)
        {
            DataMgr dataMgr = DataMgr.GetInstance();

            int choose;

            do
            {
                ShowMenu();
                choose = Utility.ReadInt();

                switch(choose)
                {
                    case 1:
                        dataMgr.InputHotel();
                        break;
                    case 2:
                        dataMgr.DisplayHotel();
                        break;
                    case 3:
                        dataMgr.InputBooking();
                        break;
                    case 4:
                        dataMgr.FindBookingAvaiable();
                        break;
                    case 5:
                        dataMgr.ShowProfit();
                        break;
                    case 6:
                        dataMgr.FindCustomerByCmtnd();
                        break;
                    case 7:
                        Console.WriteLine("Thoat!!!");
                        break;
                    default:
                        Console.WriteLine("Nhap sai!!!");
                        break;
                }
            } while (choose != 7);
        }

        static void ShowMenu()
        {
            Console.WriteLine("1. Nhap thong tin ks");
            Console.WriteLine("2. Hien thi thong tin ks");
            Console.WriteLine("3. Dat phong nghi");
            Console.WriteLine("4. Tim phong con trong");
            Console.WriteLine("5. Thong ke doanh thu");
            Console.WriteLine("6. Tim kiem kh");
            Console.WriteLine("7. Thoat");
            Console.WriteLine("Chon: ");
        }
    }
}


#Hotel.cs


using System;
using System.Collections.Generic;

namespace BT1460
{
    public class Hotel
    {
        public string No { get; set; }
        public string Name { get; set; }
        public string Address { get; set; }
        public string Type { get; set; }
        public List<Room> RoomList { get; set; }

        public Hotel()
        {
            RoomList = new List<Room>();
        }

        public Hotel(string no, string name, string address, string type)
        {
            No = no;
            Name = name;
            Address = address;
            Type = type;
            RoomList = new List<Room>();
        }

        public void Input()
        {
            Console.WriteLine("============== NHAP KS ==================");
            Console.WriteLine("Nhap ma ks: ");
            No = Console.ReadLine();

            Console.WriteLine("Nhap ten ks: ");
            Name = Console.ReadLine();

            Console.WriteLine("Nhap dia chi: ");
            Address = Console.ReadLine();

            Console.WriteLine("Nhap loai ks: ");
            Type = Console.ReadLine();

            Console.WriteLine("Nhap so phong can them: ");
            int N = Utility.ReadInt();

            for(int i=0;i<N;i++)
            {
                Room room = new Room();
                room.Input();

                RoomList.Add(room);
            }
        }

        public void ShowProfit()
        {
            int totalMoney = 0;
            foreach (Booking booking in DataMgr.GetInstance().BookingList)
            {
                if (booking.HotelNo == No)
                {
                    totalMoney += _GetPriceByRoomNo(booking.RoomNo);
                }
            }
            Console.WriteLine("Doanh thu ks: {0} = {1}", Name, totalMoney);
        }

        int _GetPriceByRoomNo(string roomNo)
        {
            foreach(Room room in RoomList)
            {
                if(room.RoomNo == roomNo)
                {
                    return room.Price;
                }
            }
            return 0;
        }

        public void Display()
        {
            Console.WriteLine("Ma KS: {0}, ten ks: {1}, dia chi: {2}, loai: {3}",
                No, Name, Address, Type);

            foreach(Room room in RoomList)
            {
                room.Display();
            }
        }

        public void ShowRoomAvaile(DateTime checkIn, DateTime checkOut)
        {
            Console.WriteLine("===== Ma KS: {0}, ten ks: {1}, dia chi: {2}, loai: {3}",
                No, Name, Address, Type);
            Console.WriteLine("*Danh sach phong con trong:*");

            List<string> unavailableList = _GetRoomNoUnavailable(checkIn, checkOut);
            foreach(Room room in RoomList)
            {
                if(!unavailableList.Contains(room.RoomNo))
                {
                    room.Display();
                }
            }
        }

        List<string> _GetRoomNoUnavailable(DateTime checkIn, DateTime checkOut)
        {
            List<string> dataList = new List<string>();

            foreach(Booking booking in DataMgr.GetInstance().BookingList)
            {
                if(booking.HotelNo == No && !dataList.Contains(booking.RoomNo))
                {
                    //Room -> booking.CheckIn >= CheckIn & booking.CheckIn <= CheckOut
                    //Room -> booking.CheckOut >= CheckIn & booking.CheckOut <= CheckOut
                    if((booking.CheckIn >= checkIn & booking.CheckIn <= checkOut) ||
                        (booking.CheckOut >= checkIn & booking.CheckOut <= checkOut))
                    {
                        dataList.Add(booking.RoomNo);
                    }
                }
            }

            return dataList;
        }
    }
}


#DataMgr.cs


using System;
using System.Collections.Generic;

namespace BT1460
{
    public class DataMgr
    {
        public List<Customer> CustomerList { get; set; }
        public List<Hotel> HotelList { get; set; }
        public List<Booking> BookingList { get; set; }

        private static DataMgr instance = null;

        private DataMgr()
        {
            CustomerList = new List<Customer>();
            HotelList = new List<Hotel>();
            BookingList = new List<Booking>();
        }

        public static DataMgr GetInstance()
        {
            if(instance == null)
            {
                instance = new DataMgr();
            }
            return instance;
        }

        internal void InputBooking()
        {
            Booking booking = new Booking();
            booking.Input();

            BookingList.Add(booking);
        }

        internal void ShowProfit()
        {
            foreach (Hotel hotel in HotelList)
            {
                hotel.ShowProfit();
            }
        }

        internal void FindCustomerByCmtnd()
        {
            Console.WriteLine("Nhap CMTND can tim: ");
            string cmtnd = Console.ReadLine();

            foreach(Booking booking in BookingList)
            {
                if(booking.Cmtnd == cmtnd)
                {
                    Console.WriteLine("Khach san da toi: {0}", booking.HotelNo);
                }
            }
        }

        internal void FindBookingAvaiable()
        {
            Console.WriteLine("Nhap ngay dat phong (yyyy-MM-dd HH:ii:ss): ");
            String checkIn = Console.ReadLine();
            DateTime CheckIn = Utility.ConvertStringToDateTime(checkIn);

            Console.WriteLine("Nhap ngay tra phong (yyyy-MM-dd HH:ii:ss): ");
            string checkOut = Console.ReadLine();
            DateTime CheckOut = Utility.ConvertStringToDateTime(checkOut);

            foreach(Hotel hotel in HotelList)
            {
                hotel.ShowRoomAvaile(CheckIn, CheckOut);
            }
        }

        internal void DisplayHotel()
        {
            foreach(Hotel hotel in HotelList)
            {
                hotel.Display();
            }
        }

        public void InputHotel()
        {
            Console.WriteLine("Nhap so ks can them: ");
            int N = Utility.ReadInt();

            for(int i=0;i<N;i++)
            {
                Hotel hotel = new Hotel();
                hotel.Input();

                HotelList.Add(hotel);
            }
        }
    }
}


#Customer.cs


using System;
namespace BT1460
{
    public class Customer
    {
        public string Fullname { get; set; }
        public string Cmtnd { get; set; }
        public string Gender { get; set; }
        public string Address { get; set; }
        public int Age { get; set; }

        public Customer()
        {
        }

        public Customer(string cmtnd)
        {
            Cmtnd = cmtnd;
        }

        public Customer(string fullname, string cmtnd, string gender, string address, int age)
        {
            Fullname = fullname;
            Cmtnd = cmtnd;
            Gender = gender;
            Address = address;
            Age = age;
        }

        public void Input()
        {
            Console.WriteLine("Nhap CMTND: ");
            Cmtnd = Console.ReadLine();

            InputWithoutCmtnd();
        }

        public void InputWithoutCmtnd()
        {
            Console.WriteLine("Nhap ten: ");
            Fullname = Console.ReadLine();

            Console.WriteLine("Nhap gioi tinh: ");
            Gender = Console.ReadLine();

            Console.WriteLine("Nhap dia chi: ");
            Address = Console.ReadLine();

            Console.WriteLine("Nhap tuoi: ");
            Age = Utility.ReadInt();
        }

        public void Display()
        {
            Console.WriteLine("Ten: {0}, Cmtnd: {1}, gioi tinh: {2}, dia chi: {3}, tuoi: {4}",
                Fullname, Cmtnd, Gender, Address, Age);
        }
    }
}


#Booking.cs


using System;
namespace BT1460
{
    public class Booking
    {
        public string Cmtnd { get; set; }
        public string HotelNo { get; set; }
        public string RoomNo { get; set; }
        public DateTime CheckIn { get; set; }
        public DateTime CheckOut { get; set; }

        public Booking()
        {
        }

        public Booking(string cmtnd, string hotelNo, string roomNo, DateTime checkIn, DateTime checkOut)
        {
            Cmtnd = cmtnd;
            HotelNo = hotelNo;
            RoomNo = roomNo;
            CheckIn = checkIn;
            CheckOut = checkOut;
        }

        public void Input()
        {
            DataMgr dataMgr = DataMgr.GetInstance();

            if(dataMgr.HotelList.Count == 0)
            {
                Console.WriteLine("Khong co ks nao");
                return;
            }

            bool isFind = false;

            Console.WriteLine("====== BOOKING =======");
            Console.WriteLine("Nhap ma ks: ");
            Hotel currentHotel = null;
            while(true)
            {
                HotelNo = Console.ReadLine();
                isFind = false;
                foreach (Hotel hotel in dataMgr.HotelList)
                {
                    if(hotel.No == HotelNo)
                    {
                        currentHotel = hotel;
                        isFind = true;
                        break;
                    }
                }
                if(isFind)
                {
                    break;
                } else
                {
                    Console.WriteLine("Nhap lai: ");
                }
            }

            if(currentHotel.RoomList.Count == 0)
            {
                Console.WriteLine("Khong co phong");
                return;
            }

            Console.WriteLine("Nhap ma phong: ");
            while (true)
            {
                RoomNo = Console.ReadLine();
                isFind = false;
                foreach (Room room in currentHotel.RoomList)
                {
                    if (room.RoomNo == RoomNo)
                    {
                        isFind = true;
                        break;
                    }
                }
                if (isFind)
                {
                    break;
                }
                else
                {
                    Console.WriteLine("Nhap lai: ");
                }
            }

            Console.WriteLine("Nhap Cmtnd: ");
            isFind = false;
            Cmtnd = Console.ReadLine();
            foreach(Customer c in DataMgr.GetInstance().CustomerList)
            {
                if(c.Cmtnd == Cmtnd)
                {
                    isFind = true;
                    break;
                }
            }
            if(!isFind)
            {
                Customer c = new Customer(Cmtnd);
                c.InputWithoutCmtnd();

                DataMgr.GetInstance().CustomerList.Add(c);
            }

            Console.WriteLine("Nhap ngay dat phong (yyyy-MM-dd HH:ii:ss): ");
            String checkIn = Console.ReadLine();
            CheckIn = Utility.ConvertStringToDateTime(checkIn);

            Console.WriteLine("Nhap ngay tra phong (yyyy-MM-dd HH:ii:ss): ");
            string checkOut = Console.ReadLine();
            CheckOut = Utility.ConvertStringToDateTime(checkOut);
        }

        public void Display()
        {
            Console.WriteLine("Ma KS: {0}, ma phong: {1}, cmtnd: {2}, ngay dat phong {3}, " +
                "ngay tra phong: {4}", HotelNo, RoomNo, Cmtnd,
                Utility.ConvertDateTimeToString(CheckIn), Utility.ConvertDateTimeToString(CheckOut));
        }
    }
}




Phản hồi từ học viên

5

(Dựa trên đánh giá ngày hôm nay)

Đăng nhập để làm bài kiểm tra

Chưa có kết quả nào trước đó