H tên: Nguyn Ngọc Trang Đài
MSSV: 21110050
KIM TH PHN MM _ LAB 02 _24/11/2024
HotelBooking
public class HotelBooking {
private int userAge;
private String roomType; // "Standard", "Deluxe", "Suite"
private int nights;
private int numberOfRooms;
private int peoplePerRoom;
private int availableRoomsStandard;
private int availableRoomsDeluxe;
private int availableRoomsSuite;
private double balance;
private double roomPrice;
public int getAvailableRoomsStandard() {
return availableRoomsStandard;
}
public void setAvailableRoomsStandard(int availableRoomsStandard) {
this.availableRoomsStandard = availableRoomsStandard;
}
public HotelBooking(int userAge, String roomType, int nights, int
numberOfRooms, int peoplePerRoom, double balance) {
this.userAge = userAge;
this.roomType = roomType.trim(); // Loại bỏ khoảng trắng dư thừa
this.nights = nights;
this.numberOfRooms = numberOfRooms;
this.peoplePerRoom = peoplePerRoom;
this.balance = balance;
this.roomPrice = 100;
// Room availability
this.availableRoomsStandard = 5;
this.availableRoomsDeluxe = 3;
this.availableRoomsSuite = 2;
}
public String bookRoom() {
if (userAge < 18) {
return "You must be at least 18 years old.";
}
if (nights < 1) { // Sửa điều kiện
return "You must book at least 1 night.";
}
if (!roomType.equals("Standard") && !roomType.equals("Deluxe") &&
!roomType.equals("Suite")) {
return "Invalid room type.";
}
if (numberOfRooms > 3) {
return "You can only book up to 3 rooms.";
}
// Kiểm tra số phòng còn lại
if (roomType.equals("Standard") && availableRoomsStandard <
numberOfRooms) {
return "Not enough Standard rooms available.";
}
if (roomType.equals("Deluxe") && availableRoomsDeluxe <
numberOfRooms) {
return "Not enough Deluxe rooms available.";
}
if (roomType.equals("Suite") && availableRoomsSuite < numberOfRooms)
{
return "Not enough Suite rooms available.";
}
// Kiểm tra sức chứa của phòng
if ((roomType.equals("Standard") && peoplePerRoom > 2) ||
(roomType.equals("Deluxe") && peoplePerRoom > 4) ||
(roomType.equals("Suite") && peoplePerRoom > 6)) {
return "Too many people for the room.";
}
double totalAmount = numberOfRooms * nights * roomPrice;
if (balance < totalAmount) {
return "Insufficient balance.";
}
// Xử lý giảm số lượng phòng
if (roomType.equals("Standard")) availableRoomsStandard -=
numberOfRooms;
if (roomType.equals("Deluxe")) availableRoomsDeluxe -= numberOfRooms;
if (roomType.equals("Suite")) availableRoomsSuite -= numberOfRooms;
// Trừ tiền từ số dư
balance -= totalAmount;
return "Booking successful!";
}
public String cancelRoom(int roomsToCancel) {
if (roomsToCancel > numberOfRooms) {
return "Invalid cancellation request.";
}
// Khôi phục phòng
if (roomType.equals("Standard")) availableRoomsStandard +=
roomsToCancel;
if (roomType.equals("Deluxe")) availableRoomsDeluxe += roomsToCancel;
if (roomType.equals("Suite")) availableRoomsSuite += roomsToCancel;
numberOfRooms -= roomsToCancel;
return "Room canceled successfully!";
}
}
HotelBookingTest
import org.junit.Test;
import static org.junit.Assert.assertEquals;
public class HotelBookingTest {
@Test
public void testUnderageBooking() {
HotelBooking booking = new HotelBooking(17, "Standard", 2, 1, 2,
500);
assertEquals("You must be at least 18 years old.",
booking.bookRoom());
}
@Test
public void testInvalidRoomType() {
HotelBooking booking = new HotelBooking(25, "Luxury", 2, 1, 2, 500);
assertEquals("Invalid room type.", booking.bookRoom());
}
@Test
public void testNotEnoughRoomsStandard() {
HotelBooking booking = new HotelBooking(25, "Standard", 2, 3, 2,
500);
booking.setAvailableRoomsStandard(2); // Sử dụng setter thay vì truy
cập trực tiếp
assertEquals("Not enough Standard rooms available.",
booking.bookRoom());
}
@Test
public void testExceedMaxPeopleStandard() {
HotelBooking booking = new HotelBooking(25, "Standard", 2, 1, 3,
500);
assertEquals("Too many people for the room.", booking.bookRoom());
}
@Test
public void testInsufficientBalance() {
HotelBooking booking = new HotelBooking(25, "Standard", 2, 2, 2,
100);
assertEquals("Insufficient balance.", booking.bookRoom());
}
@Test
public void testSuccessfulBooking() {
HotelBooking booking = new HotelBooking(25, "Suite", 2, 1, 4, 1000);
assertEquals("Booking successful!", booking.bookRoom());
}
@Test
public void testCancelRoomSuccess() {
HotelBooking booking = new HotelBooking(25, "Standard", 2, 2, 2,
500);
booking.bookRoom(); // Đặt phòng trước
assertEquals("Room canceled successfully!", booking.cancelRoom(1));
}
@Test
public void testCancelRoomInvalid() {
HotelBooking booking = new HotelBooking(25, "Standard", 2, 2, 2,
500);
booking.bookRoom(); // Đặt phòng trước
assertEquals("Invalid cancellation request.", booking.cancelRoom(3));
}
}
Kết qu
FoodOrder
import java.util.List;
public class FoodOrder {
private List<String> items;
private List<Integer> quantities;
private double totalAmount;
private boolean isPaid;
private String paymentMethod;
private boolean hasMainDish;
private List<Boolean> itemAvailability; // Kiểm tra món nào còn hàng
private int maxItemsPerOrder = 5;
public FoodOrder(List<String> items, List<Integer> quantities, double
totalAmount, boolean isPaid, String paymentMethod, boolean hasMainDish,
List<Boolean> itemAvailability) {
this.items = items;
this.quantities = quantities;
this.totalAmount = totalAmount;
this.isPaid = isPaid;
this.paymentMethod = paymentMethod;
this.hasMainDish = hasMainDish;
this.itemAvailability = itemAvailability;
}
public String placeOrder() {
// Giới hạn số món
if (items.size() > maxItemsPerOrder) {
return "You can only order up to 5 items.";
}
// Tổng giá trị đơn hàng
if (totalAmount < 100000) {
return "Total order amount must be at least 100,000 VNĐ.";
}
// Kiểm tra tình trạng từng món
for (Boolean availability : itemAvailability) {
if (!availability) {
return "One or more items are out of stock.";
}
}
// Giới hạn số lượng món
for (int quantity : quantities) {
if (quantity > 10) {
return "You can only order a maximum of 10 portions of each
item.";
}
}
// Kiểm tra món chính
if (!hasMainDish) {
return "You must have at least one main dish in your order.";
}
// Kiểm tra mã giảm giá cho đơn hàng trên 500.000 VNĐ
if (totalAmount > 500000) {
boolean isPromoCodeApplied = false; // Thêm biến tạm để kiểm tra
mã giảm giá
if (!isPromoCodeApplied) {
return "Promo code must be applied for orders over 500,000
VNĐ.";
}
}
// Kiểm tra thanh toán qua thẻ tín dụng
if (paymentMethod.equals("credit_card") && totalAmount < 200000) {
return "Credit card payment is only available for orders above
200,000 VNĐ.";
}
return "Order placed successfully!";
}
public String processPayment() {
if (!isPaid) {
return "Payment is required before processing the order.";
}
return "Payment processed successfully.";
}
}
FoodOrderTest
import org.junit.Test;
import static org.junit.Assert.*;
import java.util.Arrays;
public class FoodOrderTest {
@Test
public void testMaxItemsPerOrder() {
FoodOrder order = new FoodOrder(
Arrays.asList("A", "B", "C", "D", "E", "F"),
Arrays.asList(1, 1, 1, 1, 1, 1),
300000,
true,
"cash",
true,
Arrays.asList(true, true, true, true, true, true)
);
assertEquals("You can only order up to 5 items.",
order.placeOrder());
}
@Test
public void testMinOrderValue() {
FoodOrder order = new FoodOrder(
Arrays.asList("A"),
Arrays.asList(1),
90000,
true,
"cash",
true,
Arrays.asList(true)
);
assertEquals("Total order amount must be at least 100,000 VNĐ.",
order.placeOrder());
}
@Test
public void testItemOutOfStock() {
FoodOrder order = new FoodOrder(
Arrays.asList("A", "B"),
Arrays.asList(1, 1),
300000,
true,
"cash",
true,
Arrays.asList(true, false)
);
assertEquals("One or more items are out of stock.",
order.placeOrder());
}
@Test
public void testMaxQuantityPerItem() {
FoodOrder order = new FoodOrder(
Arrays.asList("A", "B"),
Arrays.asList(11, 5),
300000,
true,
"cash",
true,
Arrays.asList(true, true)
);
assertEquals("You can only order a maximum of 10 portions of each
item.", order.placeOrder());
}
@Test
public void testMainDishRequired() {
FoodOrder order = new FoodOrder(
Arrays.asList("Dessert"),
Arrays.asList(1),
300000,
true,
"cash",
false,
Arrays.asList(true)
);
assertEquals("You must have at least one main dish in your order.",
order.placeOrder());
}
@Test
public void testCreditCardLimit() {
FoodOrder order = new FoodOrder(
Arrays.asList("A"),
Arrays.asList(1),
150000,
true,
"credit_card",
true,
Arrays.asList(true)
);
assertEquals("Credit card payment is only available for orders above
200,000 VNĐ.", order.placeOrder());
}
@Test
public void testPaymentNotProcessed() {
FoodOrder order = new FoodOrder(
Arrays.asList("A", "B"),
Arrays.asList(1, 1),
300000,
false,
"cash",
true,
Arrays.asList(true, true)
);
assertEquals("Payment is required before processing the order.",
order.processPayment());
}
@Test
public void testPromoCodeRequired() {
FoodOrder order = new FoodOrder(
Arrays.asList("A", "B", "C"),
Arrays.asList(2, 2, 2),
600000, // Tổng giá trị đơn hàng trên 500.000 VNĐ
true, // Thanh toán đã hoàn thành
"cash", // Phương thức thanh toán
true, // Có món chính
Arrays.asList(true, true, true) // Tất cả món còn hàng
);
// Kỳ vọng kết quả là thông báo yêu cầu áp dụng mã giảm giá
assertEquals("Promo code must be applied for orders over 500,000
VNĐ.", order.placeOrder());
}
@Test
public void testValidOrder() {
FoodOrder order = new FoodOrder(
Arrays.asList("A", "B"),
Arrays.asList(2, 3),
300000,
true,
"cash",
true,
Arrays.asList(true, true)
);
assertEquals("Order placed successfully!", order.placeOrder());
}
}
Kết qu

Preview text:

Họ tên: Nguyễn Ngọc Trang Đài MSSV: 21110050
KIỂM THỬ PHẦN MỀM _ LAB 02 _24/11/2024 HotelBooking public class HotelBooking { private int userAge;
private String roomType; // "Standard", "Deluxe", "Suite" private int nights; private int numberOfRooms; private int peoplePerRoom;
private int availableRoomsStandard;
private int availableRoomsDeluxe;
private int availableRoomsSuite; private double balance; private double roomPrice;
public int getAvailableRoomsStandard() {
return availableRoomsStandard; }
public void setAvailableRoomsStandard(int availableRoomsStandard) {
this.availableRoomsStandard = availableRoomsStandard; }
public HotelBooking(int userAge, String roomType, int nights, int
numberOfRooms, int peoplePerRoom, double balance) { this.userAge = userAge;
this.roomType = roomType.trim(); // Loại bỏ khoảng trắng dư thừa this.nights = nights;
this.numberOfRooms = numberOfRooms;
this.peoplePerRoom = peoplePerRoom; this.balance = balance; this.roomPrice = 100; // Room availability
this.availableRoomsStandard = 5;
this.availableRoomsDeluxe = 3;
this.availableRoomsSuite = 2; } public String bookRoom() { if (userAge < 18) {
return "You must be at least 18 years old."; }
if (nights < 1) { // Sửa điều kiện
return "You must book at least 1 night."; }
if (!roomType.equals("Standard") && !roomType.equals("Deluxe") && !roomType.equals("Suite")) { return "Invalid room type."; } if (numberOfRooms > 3) {
return "You can only book up to 3 rooms."; }
// Kiểm tra số phòng còn lại
if (roomType.equals("Standard") && availableRoomsStandard < numberOfRooms) {
return "Not enough Standard rooms available."; }
if (roomType.equals("Deluxe") && availableRoomsDeluxe < numberOfRooms) {
return "Not enough Deluxe rooms available."; }
if (roomType.equals("Suite") && availableRoomsSuite < numberOfRooms) {
return "Not enough Suite rooms available."; }
// Kiểm tra sức chứa của phòng
if ((roomType.equals("Standard") && peoplePerRoom > 2) ||
(roomType.equals("Deluxe") && peoplePerRoom > 4) ||
(roomType.equals("Suite") && peoplePerRoom > 6)) {
return "Too many people for the room."; }
double totalAmount = numberOfRooms * nights * roomPrice;
if (balance < totalAmount) {
return "Insufficient balance."; }
// Xử lý giảm số lượng phòng
if (roomType.equals("Standard")) availableRoomsStandard -= numberOfRooms;
if (roomType.equals("Deluxe")) availableRoomsDeluxe -= numberOfRooms;
if (roomType.equals("Suite")) availableRoomsSuite -= numberOfRooms;
// Trừ tiền từ số dư balance -= totalAmount;
return "Booking successful!"; }
public String cancelRoom(int roomsToCancel) {
if (roomsToCancel > numberOfRooms) {
return "Invalid cancellation request."; } // Khôi phục phòng
if (roomType.equals("Standard")) availableRoomsStandard += roomsToCancel;
if (roomType.equals("Deluxe")) availableRoomsDeluxe += roomsToCancel;
if (roomType.equals("Suite")) availableRoomsSuite += roomsToCancel;
numberOfRooms -= roomsToCancel;
return "Room canceled successfully!"; } } HotelBookingTest import org.junit.Test;
import static org.junit.Assert.assertEquals;
public class HotelBookingTest { @Test
public void testUnderageBooking() {
HotelBooking booking = new HotelBooking(17, "Standard", 2, 1, 2, 500);
assertEquals("You must be at least 18 years old.", booking.bookRoom()); } @Test
public void testInvalidRoomType() {
HotelBooking booking = new HotelBooking(25, "Luxury", 2, 1, 2, 500);
assertEquals("Invalid room type.", booking.bookRoom()); } @Test
public void testNotEnoughRoomsStandard() {
HotelBooking booking = new HotelBooking(25, "Standard", 2, 3, 2, 500);
booking.setAvailableRoomsStandard(2); // Sử dụng setter thay vì truy cập trực tiếp
assertEquals("Not enough Standard rooms available.", booking.bookRoom()); } @Test
public void testExceedMaxPeopleStandard() {
HotelBooking booking = new HotelBooking(25, "Standard", 2, 1, 3, 500);
assertEquals("Too many people for the room.", booking.bookRoom()); } @Test
public void testInsufficientBalance() {
HotelBooking booking = new HotelBooking(25, "Standard", 2, 2, 2, 100);
assertEquals("Insufficient balance.", booking.bookRoom()); } @Test
public void testSuccessfulBooking() {
HotelBooking booking = new HotelBooking(25, "Suite", 2, 1, 4, 1000);
assertEquals("Booking successful!", booking.bookRoom()); } @Test
public void testCancelRoomSuccess() {
HotelBooking booking = new HotelBooking(25, "Standard", 2, 2, 2, 500);
booking.bookRoom(); // Đặt phòng trước
assertEquals("Room canceled successfully!", booking.cancelRoom(1)); } @Test
public void testCancelRoomInvalid() {
HotelBooking booking = new HotelBooking(25, "Standard", 2, 2, 2, 500);
booking.bookRoom(); // Đặt phòng trước
assertEquals("Invalid cancellation request.", booking.cancelRoom(3)); } } Kết quả FoodOrder import java.util.List; public class FoodOrder { private List items; private List quantities; private double totalAmount; private boolean isPaid;
private String paymentMethod; private boolean hasMainDish;
private List itemAvailability; // Kiểm tra món nào còn hàng
private int maxItemsPerOrder = 5;
public FoodOrder(List items, List quantities, double
totalAmount, boolean isPaid, String paymentMethod, boolean hasMainDish, List itemAvailability) { this.items = items;
this.quantities = quantities;
this.totalAmount = totalAmount; this.isPaid = isPaid;
this.paymentMethod = paymentMethod;
this.hasMainDish = hasMainDish;
this.itemAvailability = itemAvailability; } public String placeOrder() { // Giới hạn số món
if (items.size() > maxItemsPerOrder) {
return "You can only order up to 5 items."; }
// Tổng giá trị đơn hàng
if (totalAmount < 100000) {
return "Total order amount must be at least 100,000 VNĐ."; }
// Kiểm tra tình trạng từng món
for (Boolean availability : itemAvailability) { if (!availability) {
return "One or more items are out of stock."; } }
// Giới hạn số lượng món
for (int quantity : quantities) { if (quantity > 10) {
return "You can only order a maximum of 10 portions of each item."; } } // Kiểm tra món chính if (!hasMainDish) {
return "You must have at least one main dish in your order."; }
// Kiểm tra mã giảm giá cho đơn hàng trên 500.000 VNĐ
if (totalAmount > 500000) {
boolean isPromoCodeApplied = false; // Thêm biến tạm để kiểm tra mã giảm giá if (!isPromoCodeApplied) {
return "Promo code must be applied for orders over 500,000 VNĐ."; } }
// Kiểm tra thanh toán qua thẻ tín dụng
if (paymentMethod.equals("credit_card") && totalAmount < 200000) {
return "Credit card payment is only available for orders above 200,000 VNĐ."; }
return "Order placed successfully!"; }
public String processPayment() { if (!isPaid) {
return "Payment is required before processing the order."; }
return "Payment processed successfully."; } } FoodOrderTest import org.junit.Test;
import static org.junit.Assert.*; import java.util.Arrays; public class FoodOrderTest { @Test
public void testMaxItemsPerOrder() {
FoodOrder order = new FoodOrder(
Arrays.asList("A", "B", "C", "D", "E", "F"),
Arrays.asList(1, 1, 1, 1, 1, 1), 300000, true, "cash", true,
Arrays.asList(true, true, true, true, true, true) );
assertEquals("You can only order up to 5 items.", order.placeOrder()); } @Test
public void testMinOrderValue() {
FoodOrder order = new FoodOrder( Arrays.asList("A"), Arrays.asList(1), 90000, true, "cash", true, Arrays.asList(true) );
assertEquals("Total order amount must be at least 100,000 VNĐ.", order.placeOrder()); } @Test
public void testItemOutOfStock() {
FoodOrder order = new FoodOrder(
Arrays.asList("A", "B"), Arrays.asList(1, 1), 300000, true, "cash", true,
Arrays.asList(true, false) );
assertEquals("One or more items are out of stock.", order.placeOrder()); } @Test
public void testMaxQuantityPerItem() {
FoodOrder order = new FoodOrder(
Arrays.asList("A", "B"), Arrays.asList(11, 5), 300000, true, "cash", true,
Arrays.asList(true, true) );
assertEquals("You can only order a maximum of 10 portions of each item.", order.placeOrder()); } @Test
public void testMainDishRequired() {
FoodOrder order = new FoodOrder(
Arrays.asList("Dessert"), Arrays.asList(1), 300000, true, "cash", false, Arrays.asList(true) );
assertEquals("You must have at least one main dish in your order.", order.placeOrder()); } @Test
public void testCreditCardLimit() {
FoodOrder order = new FoodOrder( Arrays.asList("A"), Arrays.asList(1), 150000, true, "credit_card", true, Arrays.asList(true) );
assertEquals("Credit card payment is only available for orders above
200,000 VNĐ.", order.placeOrder()); } @Test
public void testPaymentNotProcessed() {
FoodOrder order = new FoodOrder(
Arrays.asList("A", "B"), Arrays.asList(1, 1), 300000, false, "cash", true,
Arrays.asList(true, true) );
assertEquals("Payment is required before processing the order.", order.processPayment()); } @Test
public void testPromoCodeRequired() {
FoodOrder order = new FoodOrder(
Arrays.asList("A", "B", "C"),
Arrays.asList(2, 2, 2),
600000, // Tổng giá trị đơn hàng trên 500.000 VNĐ
true, // Thanh toán đã hoàn thành
"cash", // Phương thức thanh toán true, // Có món chính
Arrays.asList(true, true, true) // Tất cả món còn hàng );
// Kỳ vọng kết quả là thông báo yêu cầu áp dụng mã giảm giá
assertEquals("Promo code must be applied for orders over 500,000 VNĐ.", order.placeOrder()); } @Test
public void testValidOrder() {
FoodOrder order = new FoodOrder(
Arrays.asList("A", "B"), Arrays.asList(2, 3), 300000, true, "cash", true,
Arrays.asList(true, true) );
assertEquals("Order placed successfully!", order.placeOrder()); } } Kết quả