import java.time.LocalDate;
import java.time.format.DateTimeParseException;
import java.time.temporal.ChronoUnit;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

class Room {
    int number;
    String type;
    double price;
    boolean available = true;

    Room(int number, String type, double price) {
        this.number = number;
        this.type = type;
        this.price = price;
    }

    @Override
    public String toString() {
        return String.format("Room %-4d | %-7s | $%-8.2f | %s",
                number, type, price, available ? "Available" : "Booked");
    }
}

class Booking {
    int id;
    Room room;
    String customerName, phone, email;
    LocalDate checkIn, checkOut;
    long days;
    double totalCost;

    Booking(int id, Room room, String name, String phone, String email,
            LocalDate in, LocalDate out) {
        this.id = id;
        this.room = room;
        this.customerName = name;
        this.phone = phone;
        this.email = email;
        this.checkIn = in;
        this.checkOut = out;
        this.days = ChronoUnit.DAYS.between(in, out); // number of days
        this.totalCost = days * room.price; // total room cost
    }

    @Override
    public String toString() {
        return "Booking ID : " + id +
               "\nCustomer : " + customerName + " (" + phone + ", " + email + ")" +
               "\nRoom : " + room.number + " (" + room.type + ")" +
               "\nCheck-in : " + checkIn +
               "\nCheck-out : " + checkOut +
               "\nDays : " + days +
               String.format("%nTotal cost : $%.2f%n", totalCost);
    }
}

public class HotelRoomBookingSystem {
    private static final List<Room> rooms = new ArrayList<>();
    private static final List<Booking> bookings = new ArrayList<>();
    private static final Scanner sc = new Scanner(System.in);
    private static int nextBookingId = 1001;

    public static void main(String[] args) {
        loadRooms();
        int choice;
        do {
            printMenu();
            choice = readInt("Choose an option: ");
            switch (choice) {
                case 1 -> displayAvailableRooms();
                case 2 -> searchByType();
                case 3 -> bookRoom();
                case 4 -> System.out.println("Customer details are entered while booking (option 3).");
                case 5, 6 -> System.out.println("Check-in/check-out dates are selected while booking (option 3).");
                case 7, 8 -> System.out.println("Days and total cost are calculated automatically when booking.");
                case 9 -> cancelBooking();
                case 10 -> searchBooking();
                case 11 -> displayAllBookings();
                case 0 -> System.out.println("Goodbye!");
                default -> System.out.println("Invalid option.");
            }
        } while (choice != 0);
    }

    private static void loadRooms() {
        rooms.add(new Room(101, "Single", 50));
        rooms.add(new Room(102, "Single", 50));
        rooms.add(new Room(201, "Double", 80));
        rooms.add(new Room(202, "Double", 80));
        rooms.add(new Room(301, "Deluxe", 120));
        rooms.add(new Room(302, "Deluxe", 120));
        rooms.add(new Room(401, "Suite", 200));
    }

    private static void printMenu() {
        System.out.println("\n===== HOTEL ROOM BOOKING SYSTEM =====");
        System.out.println("1. Display available rooms");
        System.out.println("2. Search rooms by type");
        System.out.println("3. Book a room (details, dates, days, cost)");
        System.out.println("4. Enter customer details (info)");
        System.out.println("5. Select check-in date (info)");
        System.out.println("6. Select check-out date (info)");
        System.out.println("7. Calculate number of days (info)");
        System.out.println("8. Calculate total room cost (info)");
        System.out.println("9. Cancel a booking");
        System.out.println("10. Search a booking");
        System.out.println("11. Display all bookings");
        System.out.println("0. Exit");
    }

    // 1
    private static void displayAvailableRooms() {
        boolean found = false;
        for (Room r : rooms) {
            if (r.available) {
                System.out.println(r);
                found = true;
            }
        }
        if (!found) System.out.println("No rooms available.");
    }

    // 2
    private static void searchByType() {
        System.out.print("Enter room type (Single/Double/Deluxe/Suite): ");
        String type = sc.nextLine().trim();
        boolean found = false;
        for (Room r : rooms) {
            if (r.type.equalsIgnoreCase(type)) {
                System.out.println(r);
                found = true;
            }
        }
        if (!found) System.out.println("No rooms of that type.");
    }

    // 3, 4, 5, 6, 7, 8
    private static void bookRoom() {
        displayAvailableRooms();
        int number = readInt("Enter room number to book: ");
        Room room = findRoom(number);
        if (room == null) { System.out.println("Room not found."); return; }
        if (!room.available) { System.out.println("Room is already booked."); return; }

        System.out.print("Customer name: ");
        String name = sc.nextLine().trim();
        System.out.print("Phone: ");
        String phone = sc.nextLine().trim();
        System.out.print("Email: ");
        String email = sc.nextLine().trim();
        if (name.isEmpty()) { System.out.println("Name is required."); return; }

        LocalDate in = readDate("Check-in date (yyyy-MM-dd): ");
        LocalDate out = readDate("Check-out date (yyyy-MM-dd): ");
        if (in.isBefore(LocalDate.now())) {
            System.out.println("Check-in cannot be in the past."); return;
        }
        if (!out.isAfter(in)) {
            System.out.println("Check-out must be after check-in."); return;
        }

        Booking b = new Booking(nextBookingId++, room, name, phone, email, in, out);
        bookings.add(b);
        room.available = false;
        System.out.println("\nBooking successful!\n" + b);
    }

    // 9
    private static void cancelBooking() {
        int id = readInt("Enter booking ID to cancel: ");
        Booking b = findBooking(id);
        if (b == null) { System.out.println("Booking not found."); return; }
        b.room.available = true;
        bookings.remove(b);
        System.out.println("Booking " + id + " cancelled. Room " + b.room.number + " is available again.");
    }

    // 10
    private static void searchBooking() {
        System.out.print("Search by (1) Booking ID or (2) Customer name: ");
        int opt = readInt("");
        boolean found = false;
        if (opt == 1) {
            Booking b = findBooking(readInt("Booking ID: "));
            if (b != null) { System.out.println(b); found = true; }
        } else {
            System.out.print("Customer name: ");
            String name = sc.nextLine().trim().toLowerCase();
            for (Booking b : bookings) {
                if (b.customerName.toLowerCase().contains(name)) {
                    System.out.println(b);
                    found = true;
                }
            }
        }
        if (!found) System.out.println("No booking found.");
    }

    // 11
    private static void displayAllBookings() {
        if (bookings.isEmpty()) { System.out.println("No bookings yet."); return; }
        for (Booking b : bookings) System.out.println(b);
    }

    // ---------- helpers ----------
    private static Room findRoom(int number) {
        for (Room r : rooms) if (r.number == number) return r;
        return null;
    }

    private static Booking findBooking(int id) {
        for (Booking b : bookings) if (b.id == id) return b;
        return null;
    }

    private static int readInt(String prompt) {
        while (true) {
            System.out.print(prompt);
            try {
                return Integer.parseInt(sc.nextLine().trim());
            } catch (NumberFormatException e) {
                System.out.println("Please enter a valid number.");
            }
        }
    }

    private static LocalDate readDate(String prompt) {
        while (true) {
            System.out.print(prompt);
            try {
                return LocalDate.parse(sc.nextLine().trim());
            } catch (DateTimeParseException e) {
                System.out.println("Invalid date. Use yyyy-MM-dd (e.g. 2026-10-15).");
            }
        }
    }
}

ウェブサイトに埋め込む

このプロジェクトをあなたのウェブサイトに埋め込むには、以下のコードをコピーしてウェブサイトのHTMLに貼り付けてください。