[Video] 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
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
https://qviet.vn/chuong-trinh-quan-ly-khach-san-develop-hotel-project-lap-trinh-c-lap-trinh-c-sharp.html
🧩 I. Interface IPerson
🎯 Mục tiêu:
Khai báo 3 thuộc tính (property):
-
Skills— đọc/ghi (Read/Write) -
DateOfBirth— chỉ đọc (Read-only) -
Age— chỉ đọc (Read-only)
💡 Giải thích:
Interface chỉ khai báo, không có code xử lý bên trong.
Class nào implements interface này thì bắt buộc phải định nghĩa (override) các property trong đó.
🧠 Code:
public interface IPerson
{
string Skills { get; set; } // Read/Write
DateTime DateOfBirth { get; } // Read-only
int Age { get; } // Read-only
}
🧩 II. Abstract Class Employee
🎯 Mục tiêu:
-
Có 2 field:
_id,_name -
2 constructor
-
2 property:
ID(chỉ đọc),Name(đọc/ghi, kiểm tra độ dài) -
1 phương thức trừu tượng
ShowInfo()
🧠 Code + Giải thích:
public abstract class Employee
{
// 🔹 1. Fields - private
private int _id;
private string _name;
// 🔹 2. Constructor 1: chỉ có id
public Employee(int id)
{
_id = id;
_name = "No name"; // Giá trị mặc định
}
// 🔹 3. Constructor 2: có id + name
public Employee(int id, string name)
{
_id = id;
_name = name;
}
// 🔹 4. Property ID - chỉ đọc
public int ID
{
get { return _id; }
}
// 🔹 5. Property Name - đọc/ghi
public string Name
{
get { return _name; }
set
{
if (value.Length < 3)
throw new Exception("Name must be at least 3 characters!");
_name = value;
}
}
// 🔹 6. Abstract method - bắt buộc class con phải override
public abstract void ShowInfo();
}
🧩 III. Class Programmer (kế thừa Employee, implements IPerson)
🎯 Mục tiêu:
-
Kế thừa
Employee -
Cài đặt interface
IPerson -
Có thêm 3 field:
_skills,_DOB,_age -
2 constructor
-
Các property override từ interface
-
Override
ShowInfo()
🧠 Code + Giải thích:
public class Programmer : Employee, IPerson
{
// 🔹 1. Fields riêng
private string _skills;
private DateTime _DOB;
private int _age;
// 🔹 2. Constructor 1: id + name
public Programmer(int id, string name) : base(id, name)
{
_skills = string.Empty;
_DOB = DateTime.Now;
}
// 🔹 3. Constructor 2: id + name + skills + DOB
public Programmer(int id, string name, string skills, DateTime dob)
: base(id, name)
{
_skills = skills;
_DOB = dob;
}
// 🔹 4. Property Skills (implement từ IPerson)
public string Skills
{
get { return _skills; }
set
{
if (value.Length < 1)
throw new Exception("Skills cannot be empty!");
_skills = value;
}
}
// 🔹 5. Property DateOfBirth (read-only)
public DateTime DateOfBirth
{
get { return _DOB; }
}
// 🔹 6. Property Age (read-only, tính theo DOB)
public int Age
{
get
{
return DateTime.Now.Year - _DOB.Year;
}
}
// 🔹 7. Override method ShowInfo()
public override void ShowInfo()
{
Console.WriteLine($"Id: {ID} | Name: {Name} | Skills: {Skills} | DOB: {_DOB.ToShortDateString()} | Age: {Age}");
}
}
🧩 IV. Class HiredProgrammers
🎯 Mục tiêu:
-
Quản lý danh sách
List<Programmer> -
Có constructor nhận
capacity -
2 method:
-
AddNew(Programmer p)— thêm vào danh sách, nếu đầy thì Exception -
ShowFilterInfo(int underAge)— in ra các lập trình viên cóAge <= underAge
-
🧠 Code + Giải thích:
public class HiredProgrammers
{
private List<Programmer> HPGM;
private int capacity;
// 🔹 Constructor
public HiredProgrammers(int capacity)
{
this.capacity = capacity;
HPGM = new List<Programmer>(capacity);
}
// 🔹 AddNew method
public void AddNew(Programmer prog)
{
if (HPGM.Count >= capacity)
throw new Exception("List is full!");
HPGM.Add(prog);
}
// 🔹 ShowFilterInfo method
public int ShowFilterInfo(int underAge)
{
int count = 0;
foreach (var p in HPGM)
{
if (p.Age <= underAge)
{
p.ShowInfo();
count++;
}
}
return count;
}
}
🧩 V. Class Test (Main Program)
🎯 Mục tiêu:
-
Tạo đối tượng
HiredProgrammers -
Nhập 3 programmer (dùng
try-catch) -
Nhập giá trị
underAge -
Gọi
ShowFilterInfo()
🧠 Code + Giải thích:
class Test
{
static void Main(string[] args)
{
HiredProgrammers myEmployee = new HiredProgrammers(3);
for (int i = 0; i < 3; i++)
{
try
{
Console.WriteLine($"Enter info for programmer {i + 1}:");
Console.Write("ID: ");
int id = int.Parse(Console.ReadLine());
Console.Write("Name: ");
string name = Console.ReadLine();
Console.Write("Skills: ");
string skills = Console.ReadLine();
Console.Write("Date of Birth (yyyy-mm-dd): ");
DateTime dob = DateTime.Parse(Console.ReadLine());
Programmer p = new Programmer(id, name, skills, dob);
myEmployee.AddNew(p);
}
catch (Exception ex)
{
Console.WriteLine($"Error: {ex.Message}");
}
}
Console.Write("\nEnter underage value to filter: ");
int underAge = int.Parse(Console.ReadLine());
Console.WriteLine($"\nList of programmers age <= {underAge}:");
int count = myEmployee.ShowFilterInfo(underAge);
Console.WriteLine($"Total: {count}");
}
}
✅ Tổng kết chức năng
| Lớp | Chức năng chính |
|---|---|
IPerson | Định nghĩa khung cho thông tin cá nhân |
Employee | Lớp trừu tượng mô tả nhân viên cơ bản |
Programmer | Lớp con cụ thể cho lập trình viên |
HiredProgrammers | Quản lý danh sách lập trình viên |
Test | Chương trình chính, nhập và xử lý dữ liệu |
📁 Models/Customer.cs
using System;
namespace Hotels.Models
{
public class Customer
{
public string CMTND { get; set; }
public string Fullname { get; set; }
public int Age { get; set; }
public string Gender { get; set; }
public string Address { get; set; }
public Customer() { }
public Customer(string cmtnd, string fullname, int age, string gender, string address)
{
CMTND = cmtnd;
Fullname = fullname;
Age = age;
Gender = gender;
Address = address;
}
public void Input()
{
Console.Write("Nhap CMTND: ");
CMTND = Console.ReadLine();
InputWithoutCMTND();
}
public void InputWithoutCMTND()
{
Console.Write("Nhap ten: ");
Fullname = Console.ReadLine();
Console.Write("Nhap tuoi: ");
Age = int.Parse(Console.ReadLine());
Console.Write("Nhap gioi tinh: ");
Gender = Console.ReadLine();
Console.Write("Nhap dia chi: ");
Address = Console.ReadLine();
}
public void Display()
{
Console.WriteLine($"CMTND: {CMTND}, Ten: {Fullname}, Tuoi: {Age}, Gioi tinh: {Gender}, Dia chi: {Address}");
}
}
}
📁 Models/Room.cs
using System;
namespace Hotels.Models
{
public class Room
{
public string RoomNo { get; set; }
public string RoomName { get; set; }
public float Price { get; set; }
public int PeopleMax { get; set; }
public int Floor { get; set; }
public Room() { }
public Room(string roomNo, string roomName, float price, int peopleMax, int floor)
{
RoomNo = roomNo;
RoomName = roomName;
Price = price;
PeopleMax = peopleMax;
Floor = floor;
}
public void Input()
{
Console.Write("Nhap ma phong: ");
RoomNo = Console.ReadLine();
Console.Write("Nhap ten phong: ");
RoomName = Console.ReadLine();
Console.Write("Nhap gia: ");
Price = float.Parse(Console.ReadLine());
Console.Write("Nhap so nguoi toi da: ");
PeopleMax = int.Parse(Console.ReadLine());
Console.Write("Nhap tang: ");
Floor = int.Parse(Console.ReadLine());
}
public void Display()
{
Console.WriteLine($"Ma phong: {RoomNo}, Ten phong: {RoomName}, Gia: {Price}, So nguoi toi da: {PeopleMax}, Tang: {Floor}");
}
}
}
📁 Models/Hotel.cs
using System;
using System.Collections.Generic;
namespace Hotels.Models
{
public class Hotel
{
public string HotelCode { 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 void Input()
{
Console.Write("Nhap ma KS: ");
HotelCode = Console.ReadLine();
Console.Write("Nhap ten KS: ");
Name = Console.ReadLine();
Console.Write("Nhap dia chi: ");
Address = Console.ReadLine();
Console.Write("Nhap loai KS (VIP/Binh Dan): ");
Type = Console.ReadLine();
Console.Write("Nhap so phong cua khach san: ");
int n = int.Parse(Console.ReadLine());
for (int i = 0; i < n; i++)
{
Console.WriteLine($"\nNhap thong tin phong thu {i + 1}:");
Room room = new Room();
room.Input();
RoomList.Add(room);
}
}
public void Display()
{
Console.WriteLine($"\nMa KS: {HotelCode}, Ten KS: {Name}, Dia Chi: {Address}, Loai: {Type}");
foreach (Room room in RoomList)
{
room.Display();
}
}
}
}
📁 Models/Book.cs
using System;
using System.Collections.Generic;
namespace Hotels.Models
{
public class Book
{
public string CMTND { get; set; }
public string HotelCode { get; set; }
public string RoomNo { get; set; }
public DateTime CheckIn { get; set; }
public DateTime CheckOut { get; set; }
public Book() { }
public void Input(List<Customer> customers, List<Hotel> hotels)
{
if (hotels.Count == 0)
{
Console.WriteLine("Khong co du lieu khach san!");
return;
}
Console.Write("Nhap CMTND: ");
CMTND = Console.ReadLine();
bool exists = false;
foreach (var c in customers)
{
if (c.CMTND.Equals(CMTND))
{
exists = true;
break;
}
}
if (!exists)
{
Console.WriteLine("Khach hang moi - nhap thong tin:");
Customer newCus = new Customer();
newCus.CMTND = CMTND;
newCus.InputWithoutCMTND();
customers.Add(newCus);
}
// Chon khach san
Hotel currentHotel = null;
do
{
Console.WriteLine("Danh sach khach san:");
foreach (var h in hotels)
Console.WriteLine($"Ma KS: {h.HotelCode}, Ten KS: {h.Name}");
Console.Write("Nhap ma KS: ");
string code = Console.ReadLine();
currentHotel = hotels.Find(h => h.HotelCode.Equals(code));
if (currentHotel == null) Console.WriteLine("Khong tim thay ma KS. Vui long nhap lai!");
} while (currentHotel == null);
// Chon phong
Room currentRoom = null;
do
{
Console.WriteLine("Danh sach phong:");
foreach (var r in currentHotel.RoomList)
Console.WriteLine($"Ma Phong: {r.RoomNo}, Ten Phong: {r.RoomName}");
Console.Write("Nhap ma phong: ");
string roomNo = Console.ReadLine();
currentRoom = currentHotel.RoomList.Find(r => r.RoomNo.Equals(roomNo));
if (currentRoom == null) Console.WriteLine("Khong tim thay ma phong. Vui long nhap lai!");
else RoomNo = roomNo;
} while (currentRoom == null);
Console.Write("Nhap ngay CheckIn (dd/MM/yyyy): ");
CheckIn = ConvertStringToDate(Console.ReadLine());
Console.Write("Nhap ngay CheckOut (dd/MM/yyyy): ");
CheckOut = ConvertStringToDate(Console.ReadLine());
HotelCode = currentHotel.HotelCode;
}
private DateTime ConvertStringToDate(string value)
{
return DateTime.ParseExact(value, "dd/MM/yyyy", null);
}
}
}
📁 Program.cs
using System;
using System.Collections.Generic;
using Hotels.Models;
namespace Hotels
{
class Program
{
static void Main(string[] args)
{
List<Customer> customers = new List<Customer>();
List<Hotel> hotels = new List<Hotel>();
List<Book> books = new List<Book>();
int choose;
do
{
ShowMenu();
choose = int.Parse(Console.ReadLine());
switch (choose)
{
case 1:
InputHotels(hotels);
break;
case 2:
DisplayHotels(hotels);
break;
case 3:
Booking(customers, hotels, books);
break;
case 4:
FindAvailableRooms(hotels, books);
break;
case 5:
CalculateRevenue(hotels, books);
break;
case 6:
SearchCustomerHistory(customers, hotels, books);
break;
case 7:
Console.WriteLine("Thoat chuong trinh...");
break;
default:
Console.WriteLine("Lua chon khong hop le!");
break;
}
} while (choose != 7);
}
static void ShowMenu()
{
Console.WriteLine("\n=========== MENU ===========");
Console.WriteLine("1. Nhap thong tin khach san");
Console.WriteLine("2. Hien thi thong tin khach san");
Console.WriteLine("3. Dat phong");
Console.WriteLine("4. Tim phong con trong");
Console.WriteLine("5. Thong ke doanh thu khach san");
Console.WriteLine("6. Tim kiem thong tin khach hang");
Console.WriteLine("7. Thoat");
Console.Write("Chon chuc nang: ");
}
static void InputHotels(List<Hotel> hotels)
{
Console.Write("Nhap so luong khach san: ");
int n = int.Parse(Console.ReadLine());
for (int i = 0; i < n; i++)
{
Console.WriteLine($"\nNhap thong tin khach san thu {i + 1}:");
Hotel h = new Hotel();
h.Input();
hotels.Add(h);
}
}
static void DisplayHotels(List<Hotel> hotels)
{
if (hotels.Count == 0)
{
Console.WriteLine("Chua co du lieu!");
return;
}
foreach (var h in hotels)
h.Display();
}
static void Booking(List<Customer> customers, List<Hotel> hotels, List<Book> books)
{
Book book = new Book();
book.Input(customers, hotels);
books.Add(book);
Console.WriteLine("Dat phong thanh cong!");
}
static void FindAvailableRooms(List<Hotel> hotels, List<Book> books)
{
if (hotels.Count == 0)
{
Console.WriteLine("Chua co du lieu!");
return;
}
Console.Write("Nhap ma khach san: ");
string code = Console.ReadLine();
Hotel hotel = hotels.Find(h => h.HotelCode.Equals(code));
if (hotel == null)
{
Console.WriteLine("Khong tim thay KS!");
return;
}
Console.Write("Nhap ngay CheckIn (dd/MM/yyyy): ");
DateTime checkIn = DateTime.ParseExact(Console.ReadLine(), "dd/MM/yyyy", null);
Console.Write("Nhap ngay CheckOut (dd/MM/yyyy): ");
DateTime checkOut = DateTime.ParseExact(Console.ReadLine(), "dd/MM/yyyy", null);
Console.WriteLine("\nCac phong trong:");
foreach (var room in hotel.RoomList)
{
bool occupied = false;
foreach (var b in books)
{
if (b.HotelCode == code && b.RoomNo == room.RoomNo)
{
if (!(checkOut <= b.CheckIn || checkIn >= b.CheckOut))
{
occupied = true;
break;
}
}
}
if (!occupied)
Console.WriteLine($"Ma phong: {room.RoomNo}, Ten phong: {room.RoomName}");
}
}
static void CalculateRevenue(List<Hotel> hotels, List<Book> books)
{
foreach (var hotel in hotels)
{
float total = 0;
foreach (var b in books)
{
if (b.HotelCode == hotel.HotelCode)
{
Room room = hotel.RoomList.Find(r => r.RoomNo == b.RoomNo);
if (room != null)
{
int days = (b.CheckOut - b.CheckIn).Days;
total += days * room.Price;
}
}
}
Console.WriteLine($"Khach san {hotel.Name} thu duoc: {total} VND");
}
}
static void SearchCustomerHistory(List<Customer> customers, List<Hotel> hotels, List<Book> books)
{
Console.Write("Nhap CMTND khach hang: ");
string cmt = Console.ReadLine();
Customer customer = customers.Find(c => c.CMTND.Equals(cmt));
if (customer == null)
{
Console.WriteLine("Khong tim thay khach hang!");
return;
}
Console.WriteLine($"Khach hang {customer.Fullname} da den cac khach san:");
foreach (var b in books)
{
if (b.CMTND == cmt)
{
Hotel hotel = hotels.Find(h => h.HotelCode.Equals(b.HotelCode));
if (hotel != null)
Console.WriteLine($"- {hotel.Name}");
}
}
}
}
}
✅ Tổng kết:
-
Đã hoàn thiện đầy đủ 7 chức năng trong menu.
-
Có kiểm tra lỗi nhập, danh sách trống, nhập lại khi sai.
-
Tính doanh thu tự động theo số ngày × giá phòng.
-
Tìm kiếm khách hàng hiển thị danh sách khách sạn họ từng đến.