0% found this document useful (0 votes)
4 views15 pages

Code OOP

The document outlines the creation of a hotel management database, including tables for Rooms, Customers, Bookings, Services, and ServiceUsage. It also provides Java code for managing room additions, customer registrations, booking processes, displaying bookings, billing calculations, and cancellation of bookings. The code includes SQL statements for database interactions and methods for handling various hotel management functionalities.

Uploaded by

cunloser17
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views15 pages

Code OOP

The document outlines the creation of a hotel management database, including tables for Rooms, Customers, Bookings, Services, and ServiceUsage. It also provides Java code for managing room additions, customer registrations, booking processes, displaying bookings, billing calculations, and cancellation of bookings. The code includes SQL statements for database interactions and methods for handling various hotel management functionalities.

Uploaded by

cunloser17
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 15

---Bước 1: Tạo database

IF DB_ID ('HotelManagement') IS NOT NULL

BEGIN

DROP DATABASE HotelManagement;

END

GO

CREATE DATABASE HotelManagement;

GO

USE HotelManagement;

GO

---Bước 2: Tạo bảng Room

CREATE TABLE Room (

roomId INT PRIMARY KEY IDENTITY(1,1),

roomNumber VARCHAR(20) NOT NULL,

type VARCHAR(50),

price FLOAT CHECK (price >= 0),

status VARCHAR(20)

);

---Bước 3: Tạo bảng Customer

CREATE TABLE Customer (

customerId INT PRIMARY KEY IDENTITY(1,1),

name NVARCHAR(100) NOT NULL,

phone VARCHAR(15),

checkInDate DATE,

checkOutDate DATE

);
---Bước 4: Tạo bảng Booking

CREATE TABLE Booking (

bookingId INT PRIMARY KEY IDENTITY(1,1),

customerId INT,

roomId INT,

bookingDate DATE DEFAULT GETDATE(),

status VARCHAR(20),

FOREIGN KEY (customerId) REFERENCES Customer(customerId),

FOREIGN KEY (roomId) REFERENCES Room(roomId)

);

---Bước 5: Tạo bảng Service

CREATE TABLE Service (

serviceId INT PRIMARY KEY,

name NVARCHAR(100),

price DECIMAL(10, 2)

);

---Bước 6: Tạo bảng ServiceUsage

CREATE TABLE ServiceUsage (

usageId INT PRIMARY KEY,

bookingId INT,

serviceId INT,

quantity INT,

FOREIGN KEY (bookingId) REFERENCES Booking(bookingId),

FOREIGN KEY (serviceId) REFERENCES Service(serviceId)

);
///ServiceUsage

public class ServiceUsage {

private int usageId;

private int bookingId;

private int serviceId;

private int quantity;

public ServiceUsage() {

public ServiceUsage(int usageId, int bookingId, int serviceId, int quantity) {

this.usageId = usageId;

this.bookingId = bookingId;

this.serviceId = serviceId;

this.quantity = quantity;

public int getUsageId() {

return usageId;

public void setUsageId(int usageId) {

this.usageId = usageId;

public int getBookingId() {

return bookingId;

public void setBookingId(int bookingId) {

this.bookingId = bookingId;

}
public int getServiceId() {

return serviceId;

public void setServiceId(int serviceId) {

this.serviceId = serviceId;

public int getQuantity() {

return quantity;

public void setQuantity(int quantity) {

this.quantity = quantity;

@Override

public String toString() {

// Định dạng lại ngày giờ cho dễ đọc

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

String formattedDate = (usageDate != null) ? sdf.format(usageDate) : "N/A";

return String.format("ServiceUsage [UsageID=%d, BookingID=%d, ServiceID=%d, SL=%d, Ngày


dùng=%s]", usageId, bookingId, serviceId, quantity, formattedDate);

///Service

public class Service {

private int serviceId;

private String name;

private double price;

public Service() {
}

public Service(int serviceId, String name, double price) {

this.serviceId = serviceId;

this.name = name;

this.price = price;

public int getServiceId() {

return serviceId;

public void setServiceId(int serviceId) {

this.serviceId = serviceId;

public String getName() {

return name;

public void setName(String name) {

this.name = name;

public double getPrice() {

return price;

public void setPrice(double price) {

this.price = price;

@Override

public String toString() {

return String.format("Service [ID=%d, Tên DV=%s, Giá=%,.0f VND]", serviceId, name, price);

}
//a - Thêm phòng mới

package vanggg;

import java.sql.Connection;

import java.sql.PreparedStatement;

import java.sql.SQLException;

public class HotelManagement {

public void addRoom(Room room) {

String sql = "INSERT INTO Rooms (roomId, roomNumber, type, price, status) VALUES
(?, ?, ?, ?, ?)";

try (Connection conn = DatabaseConnection.getConnection();

PreparedStatement ps = conn.prepareStatement(sql)) {

ps.setInt(1, room.getRoomId());

ps.setString(2, room.getRoomNumber());

ps.setString(3, room.getType());

ps.setDouble(4, room.getPrice());

ps.setString(5, room.getStatus());

ps.executeUpdate();

System.out.println("Phòng đã được thêm thành công!");

} catch (SQLException e) {

e.printStackTrace();

public static void main(String[] args) {

HotelManagement hotelManagement = new HotelManagement();

// Tạo đối tượng phòng và thêm vào hệ thống

Room newRoom = new Room(101, "A101", "Single", 500000, "trống");


hotelManagement.addRoom(newRoom);

//b Hiển thị danh sách đặt phòng

import java.sql.Connection;

import java.sql.PreparedStatement;

import java.sql.ResultSet;

import java.sql.SQLException;

public class HotelManagement {

public void showRooms() {

String sql = "SELECT * FROM Rooms";

try (Connection conn = DatabaseConnection.getConnection();

PreparedStatement ps = conn.prepareStatement(sql);

ResultSet rs = ps.executeQuery()) {

System.out.println("Danh sách phòng:");

while (rs.next()) {

int roomId = rs.getInt("roomId");

String roomNumber = rs.getString("roomNumber");

String type = rs.getString("type");

double price = rs.getDouble("price");

String status = rs.getString("status");

System.out.println("Phòng " + roomNumber + " - Loại: " + type + " - Giá: " + price + " - Trạng
thái: " + status);

} catch (SQLException e) {

e.printStackTrace();
}

public static void main(String[] args) {

HotelManagement hotelManagement = new HotelManagement();

// Hiển thị danh sách phòng

hotelManagement.showRooms();

//c - Thêm khách hàng mới

import java.sql.Connection;

import java.sql.PreparedStatement;

import java.sql.SQLException;

public class HotelManagement {

public void addCustomer(Customer customer) {

String sql = "INSERT INTO Customers (customerId, name, phone, checkInDate, checkOutDate)
VALUES (?, ?, ?, ?, ?)";

try (Connection conn = DatabaseConnection.getConnection();

PreparedStatement ps = conn.prepareStatement(sql)) {

ps.setInt(1, customer.getCustomerId());

ps.setString(2, customer.getName());

ps.setString(3, customer.getPhone());

ps.setString(4, customer.getCheckInDate());

ps.setString(5, customer.getCheckOutDate());

ps.executeUpdate();

System.out.println("Khách hàng đã được thêm thành công!");


} catch (SQLException e) {

e.printStackTrace();

public static void main(String[] args) {

HotelManagement hotelManagement = new HotelManagement();

// Tạo đối tượng khách hàng và thêm vào hệ thống

Customer newCustomer = new Customer(1, "Nguyễn Văn A", "0987654321", "2025-04-20",


"2025-04-22");

hotelManagement.addCustomer(newCustomer);

 Đặt phòng cho khách

import java.sql.*;

public class BookingService {

public boolean bookRoom(int customerId, int roomId) {

try (Connection conn = DBConnection.getConnection()) {

// 1. Kiểm tra trạng thái phòng

String checkRoom = "SELECT status FROM Rooms WHERE roomId = ?";

PreparedStatement psCheck = conn.prepareStatement(checkRoom);

psCheck.setInt(1, roomId);

ResultSet rs = psCheck.executeQuery();

if (rs.next()) {
String status = rs.getString("status");

if (!status.equalsIgnoreCase("trống")) {

System.out.println("Phòng đã được đặt!");

return false;

// 2. Tạo bản ghi trong Bookings

String insertBooking = "INSERT INTO Bookings (customerId, roomId, bookingDate, status)


VALUES (?, ?, GETDATE(), 'đang ở')";

PreparedStatement psInsert = conn.prepareStatement(insertBooking);

psInsert.setInt(1, customerId);

psInsert.setInt(2, roomId);

psInsert.executeUpdate();

// 3. Cập nhật trạng thái phòng

String updateRoom = "UPDATE Rooms SET status = 'đã đặt' WHERE roomId = ?";

PreparedStatement psUpdate = conn.prepareStatement(updateRoom);

psUpdate.setInt(1, roomId);

psUpdate.executeUpdate();

System.out.println("Đặt phòng thành công!");

return true;

} catch (SQLException e) {

e.printStackTrace();

return false;

}
 Hiển thị danh sách đặt phòng

import java.sql.*;

public class BookingViewer {

public void displayBookings() {

String sql = "SELECT b.bookingId, c.name, r.roomNumber, b.bookingDate, b.status " +

"FROM Bookings b " +

"JOIN Customers c ON b.customerId = c.customerId " +

"JOIN Rooms r ON b.roomId = r.roomId";

try (Connection conn = DBConnection.getConnection();

Statement stmt = conn.createStatement();

ResultSet rs = stmt.executeQuery(sql)) {

System.out.println("Danh sách đặt phòng:");

while (rs.next()) {

System.out.printf("Mã đặt: %d | Khách: %s | Phòng: %s | Ngày đặt: %s | Trạng thái: %s%n",

rs.getInt("bookingId"),

rs.getString("name"),

rs.getString("roomNumber"),

rs.getDate("bookingDate"),

rs.getString("status"));

} catch (SQLException e) {

e.printStackTrace();

 Tính hóa đơn khi trả phòng


java

Sao chép

Chỉnh sửa

import java.sql.*;

public class BillingService {

public double calculateBill(int customerId) {

double bill = 0;

String sql = "SELECT DATEDIFF(DAY, checkInDate, checkOutDate) AS stayDays, r.price, r.roomId " +

"FROM Customers c " +

"JOIN Bookings b ON c.customerId = b.customerId " +

"JOIN Rooms r ON b.roomId = r.roomId " +

"WHERE c.customerId = ? AND b.status = 'đang ở'";

try (Connection conn = DBConnection.getConnection();

PreparedStatement ps = conn.prepareStatement(sql)) {

ps.setInt(1, customerId);

ResultSet rs = ps.executeQuery();

if (rs.next()) {

int days = rs.getInt("stayDays");

double price = rs.getDouble("price");

int roomId = rs.getInt("roomId");

bill = days * price;

System.out.printf("Số ngày ở: %d, Giá mỗi ngày: %.2f, Tổng hóa đơn: %.2f%n", days, price,
bill);
// Cập nhật trạng thái booking và phòng

String updateBooking = "UPDATE Bookings SET status = 'đã trả' WHERE customerId = ?";

String updateRoom = "UPDATE Rooms SET status = 'trống' WHERE roomId = ?";

PreparedStatement psUpdate1 = conn.prepareStatement(updateBooking);

psUpdate1.setInt(1, customerId);

psUpdate1.executeUpdate();

PreparedStatement psUpdate2 = conn.prepareStatement(updateRoom);

psUpdate2.setInt(1, roomId);

psUpdate2.executeUpdate();

} else {

System.out.println("Không tìm thấy thông tin đặt phòng đang hoạt động.");

} catch (SQLException e) {

e.printStackTrace();

return bill;

 Hủy đặt phòng

import java.sql.Connection;

import java.sql.PreparedStatement;

import java.sql.ResultSet;
public class CancelBookingService {

public void huyDatPhong(int bookingId) {

Connection conn = DBConnection.getConnection();

try {

// Kiểm tra booking có tồn tại không

String checkBookingSql = "SELECT roomId FROM Bookings WHERE bookingId


= ?";

PreparedStatement checkStmt = conn.prepareStatement(checkBookingSql);

checkStmt.setInt(1, bookingId);

ResultSet rs = checkStmt.executeQuery();

if (!rs.next()) {

System.out.println(" Không tìm thấy đặt phòng với bookingId = " +


bookingId);

return;

int roomId = rs.getInt("roomId");

// Xóa các dịch vụ đã sử dụng (nếu có)

String deleteServiceUsageSql = "DELETE FROM ServiceUsage WHERE


bookingId = ?";

PreparedStatement deleteSUStmt =
conn.prepareStatement(deleteServiceUsageSql);

deleteSUStmt.setInt(1, bookingId);

deleteSUStmt.executeUpdate();

// Xóa đặt phòng trong bảng Bookings

String deleteBookingSql = "DELETE FROM Bookings WHERE bookingId = ?";

PreparedStatement deleteBookingStmt =
conn.prepareStatement(deleteBookingSql);

deleteBookingStmt.setInt(1, bookingId);

deleteBookingStmt.executeUpdate();

// Cập nhật trạng thái phòng về "trống"

String updateRoomSql = "UPDATE Rooms SET status = 'trống' WHERE roomId


= ?";
PreparedStatement updateRoomStmt =
conn.prepareStatement(updateRoomSql);

updateRoomStmt.setInt(1, roomId);

updateRoomStmt.executeUpdate();

System.out.println("Đặt phòng đã được hủy thành công.");

System.out.println("Phòng số " + roomId + " đã được cập nhật về trạng thái


'trống'.");

} catch (Exception e) {

System.out.println("Lỗi khi hủy đặt phòng: " + e.getMessage());

///gọi hàm

CancelBookingService cancelService = new CancelBookingService();

cancelService.huyDatPhong(5); // Hủy đặt phòng có mã bookingId = 5

You might also like