Untitled

mail@pastecode.io avatar
unknown
java
a month ago
24 kB
3
Indexable
Never
import java.util.Scanner;
import java.util.ArrayList;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeParseException;

public class ClassroomBookingApp {

    // Scanner object
    private static final Scanner INPUT = new Scanner(System.in);

    // Three array list
    private static final ArrayList<Classroom> CLASSROOMS = new ArrayList<>();
    private static final ArrayList<Lecturer> LECTURERS = new ArrayList<>();
    private static final ArrayList<Booking> BOOKINGS = new ArrayList<>();

    //===============================================================
    // PRINTING OUTPUT
    //===============================================================

    private static void printMainMenu() {
        System.out.println("*".repeat(50));
        System.out.println("Welcome to Classroom Booking Application");
        System.out.println("*** Main Menu ***");
        System.out.println("1. Manage Classrooms");
        System.out.println("2. Manage Lecturers");
        System.out.println("3. Manage Bookings");
        System.out.println("4. Generate Reports");
        System.out.println("0. Exit");
        System.out.println("*".repeat(50));
    }

    private static void printClassroomsMenu() {
        System.out.println("*".repeat(50));
        System.out.println("*** Classrooms Menu ***");
        System.out.println("1. Create Classroom");
        System.out.println("2. Update Classroom");
        System.out.println("3. Delete Classroom");
        System.out.println("4. View All Classrooms");
        System.out.println("0. Back to Main Menu");
        System.out.println("*".repeat(50));
    }

    private static void printLecturersMenu() {
        System.out.println("*".repeat(50));
        System.out.println("*** Lecturers Menu ***");
        System.out.println("1. Add Lecturer");
        System.out.println("2. Edit Lecturer");
        System.out.println("3. Remove Lecturer");
        System.out.println("4. View All Lecturers");
        System.out.println("0. Back to Main Menu");
        System.out.println("*".repeat(50));
    }

    private static void printBookingsMenu() {
        System.out.println("*".repeat(50));
        System.out.println("*** Bookings Menu ***");
        System.out.println("1. Make a Booking");
        System.out.println("2. Cancel a Booking");
        System.out.println("3. View All Bookings");
        System.out.println("0. Back to Main Menu");
        System.out.println("*".repeat(50));
    }

    private static void printReportsMenu() {
        System.out.println("*".repeat(50));
        System.out.println("*** Reports Menu ***");
        System.out.println("1. Generate Bookings Report by Date");
        System.out.println("2. Generate Bookings Report by Classroom");
        System.out.println("3. Generate Bookings Report by Lecturer");
        System.out.println("4. Generate Total Number of Bookings");
        System.out.println("0. Back to Main Menu");
        System.out.println("*".repeat(50));
    }

    private static void printClassrooms() {
        System.out.println("*".repeat(50));
        System.out.println("** Classroom List **");

        // Print rooms
        for (Classroom eachRoom : CLASSROOMS) {
            System.out.printf("Classroom Number: %d, Name: %s, Location: %s%n"
            , eachRoom.getClassroomNum(), eachRoom.getClassroomName(), eachRoom.getClassroomLocation());
        }

        System.out.println("*".repeat(50));
    }

    private static void printLecturers() {
        System.out.println("*".repeat(50));
        System.out.println("** Lecturer List **");

        // Print lecturers
        for (Lecturer eachLecturer : LECTURERS) {
            System.out.printf("Lecturer ID: %d, Name: %s, Contact: %d, Email: %s%n"
            , eachLecturer.getLecturerId(), eachLecturer.getFullName(), eachLecturer.getContactNumber(), eachLecturer.getEmailAddress());
        }

        System.out.println("*".repeat(50));
    }

    private static void printBookings() {
        System.out.println("*".repeat(50));
        System.out.println("** Booking List **");

        // Print bookings
        for (Booking eachBooking : BOOKINGS) {
            System.out.println("Booking ID: " + eachBooking.getBookingId() + ", Date: " + eachBooking.getBookingDate() +
            ", Classroom Number: " + eachBooking.getClassroom().getClassroomNum() + ", Lecturer ID: " + eachBooking.getLecturer().getLecturerId());
        }

        System.out.println("*".repeat(50));
    }

    private static void printMsg(String prompt) {
        System.out.println("*".repeat(50));
        System.out.println(prompt);
        System.out.println("*".repeat(50));
    }

    //===============================================================
    // GET VALID INPUT
    //===============================================================

    private static String getNonEmptyInput(String prompt) {
        String input = "";

        while (true) {

            // Get input
            System.out.print(prompt);
            input = INPUT.nextLine().trim();

            // Is input empty
            if (!input.isEmpty()) {
                break;
            } else {
                printMsg("Error: Input can't be EMPTY!");
            }

        }

        return input;
    }

    private static int getValidOption(String typeOfMenu) {
        int input = 0;
        int maxOption = typeOfMenu.equals("Bookings") ? 3 : 4;

        while (true) {

            try {

                // Get input
                input = Integer.parseInt(getNonEmptyInput("Please select an option to continue: "));

                // Is input in range
                if (0 <= input && input <= maxOption) {
                    break;
                } else {
                    printMsg("Error: Invalid Option!");
                }

            // Is input Integer
            } catch (NumberFormatException e) {
                printMsg("Error: Invalid Option!");
            }

        }

        return input; // Return valid input
    }

    private static int getValidInfo(String prompt) {
        int input = 0;

        while (true) {

            try {

                // Get input
                input = Integer.parseInt(getNonEmptyInput(prompt));
                return input;

            // Is input Integer
            } catch (NumberFormatException e) {
                printMsg("Error: That is not a NUMBER!");
            }

        }

    }

    private static String getValidEmail(String prompt) {

        while (true) {

            // Get email
            String email = getNonEmptyInput(prompt);

            // Email doesn't contains "0"
            if (!email.contains("@")) {
                printMsg("Error: Invalid email format!");
                continue;
            }

            // Split email into 2 parts
            String[] parts = email.split("@");

            // "@" more than 1
            if (parts.length != 2) {
                printMsg("Error: Invalid email format!");
                continue;
            }

            // Get 2 parts value
            String username = parts[0];
            String domain = parts[1];

            // Is first part empty or second part has wrong format
            if ((!username.isEmpty()) && (domain.equals("sim.edu.sg"))) {
                return email;
            }

        }

    }

    private static LocalDate getValidDate() {
        LocalDate date = null;
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd/MM/yyyy");

        while (true) {

            try {

                // Get date
                date = LocalDate.parse(getNonEmptyInput("Enter booking date(dd/MM/yyyy): "), formatter);
                return date;

            // Is format correct
            } catch (DateTimeParseException e) {
                printMsg("Error: Invalid date format!");
            }

        }

    }

    private static Classroom getValidRoom() {

        while (true) {

            // Get room number
            int roomInput = getValidInfo("Enter classroom number: ");

            // Find room
            for (Classroom existingRoom : CLASSROOMS) {

                // Is room found
                if (existingRoom.getClassroomNum() == roomInput) {
                    return existingRoom;
                }

            }

            // Room not found
            printMsg("Error: Room not found!");
        }

    }

    private static Classroom getEmptyRoom(LocalDate bookingDate) {

        // Get available room
        for (Classroom room : CLASSROOMS) {
            boolean isBooked = false;

            for (Booking reserved : BOOKINGS) {

                // Is room reserved with same booking date
                if (room.equals(reserved.getClassroom()) && reserved.getBookingDate().equals(bookingDate)) {
                    isBooked = true;
                    break;
                }

            }

            // If room available
            if (!isBooked) {
                return room;
            }

        }

        // Display unsuccessful msg
        printMsg("Error: No available classroom on '" + bookingDate + "'!");

        return null;
    }

    private static Lecturer getValidLecturer() {

        while (true) {

            // Get lecturer ID
            int lecturerInput = getValidInfo("Enter lecturer ID: ");

            // Find lecturer
            for (Lecturer existingLecturer : LECTURERS) {

                // Is lecturer found
                if (existingLecturer.getLecturerId() == lecturerInput) {
                    return existingLecturer;
                }

            }

            // Lecturer not found
            printMsg("Error: Lecturer not found!");
        }

    }

    private static Booking getValidBooking() {

        while (true) {

            // Get booking ID
            int bookingInput = getValidInfo("Enter booking ID: ");

            // Find booking
            for (Booking existingBooking : BOOKINGS) {

                // Is lecturer found
                if (existingBooking.getBookingId() == bookingInput) {
                    return existingBooking;
                }

            }

            // Booking not found
            printMsg("Error: Booking not found!");
        }

    }

    //===============================================================
    // CLASSROOM
    //===============================================================

    private static void createClassroom() {

        // Get classroom info
        int classroomNum = getValidInfo("Enter classroom number: ");
        String classroomName = getNonEmptyInput("Enter classroom name: ");
        String classroomLocation = getNonEmptyInput("Enter classroom location: ");

        // Create classroom
        Classroom newRoom = new Classroom(classroomNum, classroomName, classroomLocation);
        CLASSROOMS.add(newRoom);

        // Display successful msg
        printMsg("New classroom created!");

    }

    private static void updateClassroom() {

        // Get room number
        Classroom roomToUpdate = getValidRoom();

        // Get new classroom info
        String newName = getNonEmptyInput("Update classroom name: ");
        String newLocation = getNonEmptyInput("Update classroom location: ");

        // Update classroom info
        roomToUpdate.setClassroomName(newName);
        roomToUpdate.setClassroomLocation(newLocation);

        // Display successful msg
        printMsg("Classroom '" + roomToUpdate.getClassroomNum() + "' updated!");

    }

    private static void deleteClassroom() {

        // Get room number
        Classroom roomToDelete = getValidRoom();

        // Remove room
        CLASSROOMS.remove(roomToDelete);

        // Display successful msg
        printMsg("Classroom '" + roomToDelete.getClassroomNum() + "' deleted!");

    }

    private static void viewClassrooms() {

        // 0 room
        if (CLASSROOMS.isEmpty()) {
            printMsg("No classrooms found!");

        // Has room
        } else {
            printClassrooms();
        }
    }

    //===============================================================
    // LECTURERS
    //===============================================================

    private static void addLecturer() {

        // Get lecturer info
        int lecturerId = getValidInfo("Enter lecturer ID: ");
        String lecturerName = getNonEmptyInput("Enter lecturer full name: ");
        int lecturerContact = getValidInfo("Enter lecturer contact number: ");
        String lecturerEmail = getValidEmail("Enter lecturer email: ");

        // Add lecturer
        Lecturer newLecturer = new Lecturer(lecturerId, lecturerName, lecturerContact, lecturerEmail);
        LECTURERS.add(newLecturer);

        // Display successful msg
        printMsg("New lecturer added!");

    }

    private static void editLecturer() {

        // Get lecturer ID
        Lecturer lecturerToUpdate = getValidLecturer();

        // Get new lecturer info
        String newName = getNonEmptyInput("Edit lecturer full name: ");
        int newContact = getValidInfo("Edit lecturer contact number: ");
        String newEmail = getValidEmail("Edit lecturer email: ");

        // Update lecturer info
        lecturerToUpdate.setFullName(newName); //update lecturer details
        lecturerToUpdate.setContactNumber(newContact);
        lecturerToUpdate.setEmailAddress(newEmail);

        // Display successful msg
        printMsg("Lecturer '" + lecturerToUpdate.getLecturerId() + "' edited!");

    }

    private static void removeLecturer() {

        // Get lecturer ID
        Lecturer lecturerToRemove = getValidLecturer();

        // Remove lecturer
        LECTURERS.remove(lecturerToRemove);

        // Display successful msg
        printMsg("Lecturer '" + lecturerToRemove.getLecturerId() + "' removed!");

    }

    private static void viewLecturers() {

        // 0 lecturer
        if (LECTURERS.isEmpty()) {
            printMsg("No lecturers found!");

        // Has lecturer
        } else {
            printLecturers();
        }

    }

    //===============================================================
    // BOOKINGS
    //===============================================================

    private static void makeBooking() {

        // Get booking info
        LocalDate bookingDate = getValidDate();
        Lecturer bookingLecturer = getValidLecturer();

        // Search available room
        Classroom bookingRoom = getEmptyRoom(bookingDate);

        // Rooms unavailable (Stop booking)
        if (bookingRoom == null) {
            return;
        }

        // Make booking
        Booking newBooking = new Booking(bookingDate, bookingRoom, bookingLecturer);
        BOOKINGS.add(newBooking);

        // Display successful msg
        printMsg("Classroom '" + bookingRoom.getClassroomName() + "' booked, booking ID is " + newBooking.getBookingId());

    }

    private static void cancelBooking() {

        // Get booking ID
        Booking bookingToCancel = getValidBooking();

        // Cancel booking
        BOOKINGS.remove(bookingToCancel);

        // Display successful msg
        printMsg("Booking '" + bookingToCancel.getBookingId() + "' cancelled!");

    }

    private static void viewBookings() {

        // 0 booking
        if (BOOKINGS.isEmpty()) {
            printMsg("No bookings found!");

        // Has booking
        } else {
            printBookings();
        }

    }

    //===============================================================
    // REPORTS
    //===============================================================

    private static void generateBookingsByDate() {
        boolean isFound = false;

        // Get booking date
        LocalDate bookingDate = getValidDate();

        System.out.println("*".repeat(50));
        // Print booking found
        for (Booking eachBooking : BOOKINGS) {

            if (eachBooking.getBookingDate().equals(bookingDate)) {
                isFound = true;
                System.out.println("Booking ID: " + eachBooking.getBookingId() + ", Classroom Number: " +
                eachBooking.getClassroom().getClassroomNum() + ", Lecturer ID: " + eachBooking.getLecturer().getLecturerId());
            }

        }

        // Booking not found
        if (!isFound) printMsg("No bookings found for '" + bookingDate + "'!");

        System.out.println("*".repeat(50));
    }

    private static void generateBookingsByClassroom() {
        boolean isFound = false;

        // Get classroom number
        Classroom roomToGenerate = getValidRoom();

        System.out.println("*".repeat(50));

        // Print booking found
        for (Booking eachBooking : BOOKINGS) {

            if (eachBooking.getClassroom().getClassroomNum() == roomToGenerate.getClassroomNum()) {
                isFound = true;
                System.out.println("Booking ID: " + eachBooking.getBookingId() + ", Date: " +
                eachBooking.getBookingDate() + ", Lecturer ID: " + eachBooking.getLecturer().getLecturerId());
            }

        }

        // Booking not found
        if (!isFound) printMsg("No bookings found for classroom '" + roomToGenerate.getClassroomNum() + "'!");

        System.out.println("*".repeat(50));
    }

    private static void generateBookingsByLecturer() {
        boolean isFound = false;

        // Get lecturer ID
        Lecturer lecturerToGenerate = getValidLecturer();

        System.out.println("*".repeat(50));

        // Print booking found
        for (Booking eachBooking : BOOKINGS) {

            if (eachBooking.getLecturer().getLecturerId() == lecturerToGenerate.getLecturerId()) {
                isFound = true;
                System.out.println("Booking ID: " + eachBooking.getBookingId() + ", Date: " +
                        eachBooking.getBookingDate() + ", Classroom Number: " + eachBooking.getClassroom().getClassroomNum());
            }

        }

        System.out.println("*".repeat(50));

        // Booking not found
        if (!isFound) printMsg("No bookings found for lecturer '" + lecturerToGenerate.getLecturerId() + "'!");
    }

    private static void generateTotalBookings() {

        printMsg("Total number of bookings: " + BOOKINGS.size());

    }

    //===============================================================
    // MAIN METHOD
    //===============================================================

    public static void main(String[] args) {
        int optionInput = 0; // Variable to store user's option input
        boolean toMainMenu = true;
        boolean toClassroomsMenu = true;
        boolean toLecturersMenu = true;
        boolean toBookingsMenu = true;
        boolean toReportsMenu = true;

        while (toMainMenu) {
            toMainMenu = false;

            // Display main menu
            printMainMenu();

            // Get option choice
            optionInput = getValidOption("Main");
            switch (optionInput) {

                // Manage Classrooms
                case 1:
                    while (toClassroomsMenu) {

                        // Display classrooms menu
                        printClassroomsMenu();

                        // Get option choice
                        optionInput = getValidOption("Classrooms");
                        switch (optionInput) {

                            case 1:
                                createClassroom();
                                break;
                            case 2:
                                updateClassroom();
                                break;
                            case 3:
                                deleteClassroom();
                                break;
                            case 4:
                                viewClassrooms();
                                break;
                            case 0:
                                toClassroomsMenu = false;
                                toMainMenu = true;
                                break;
                        }

                    }
                    break;

                // Manage Lecturers
                case 2:
                    while (toLecturersMenu) {

                        // Display lecturers menu
                        printLecturersMenu();

                        // Get option choice
                        optionInput = getValidOption("Lecturers");
                        switch (optionInput) {

                            case 1:
                                addLecturer();
                                break;
                            case 2:
                                editLecturer();
                                break;
                            case 3:
                                removeLecturer();
                                break;
                            case 4:
                                viewLecturers();
                                break;
                            case 0:
                                toLecturersMenu = false;
                                toMainMenu = true;
                                break;
                        }

                    }
                    break;

                // Manage Bookings
                case 3:
                    while (toBookingsMenu) {

                        // Display bookings menu
                        printBookingsMenu();

                        // Get option choice
                        optionInput = getValidOption("Bookings");
                        switch (optionInput) {

                            case 1:
                                makeBooking();
                                break;
                            case 2:
                                cancelBooking();
                                break;
                            case 3:
                                viewBookings();
                                break;
                            case 0:
                                toBookingsMenu = false;
                                toMainMenu = true;
                                break;

                        }

                    }
                    break;

                // Manage Reports
                case 4:
                    while (toReportsMenu) {

                        // Display reports menu
                        printReportsMenu();

                        // Get option choice
                        optionInput = getValidOption("Reports");
                        switch (optionInput) {

                            case 1:
                                generateBookingsByDate();
                                break;
                            case 2:
                                generateBookingsByClassroom();
                                break;
                            case 3:
                                generateBookingsByLecturer();
                                break;
                            case 4:
                                generateTotalBookings();
                                break;
                            case 0:
                                toReportsMenu = false;
                                toMainMenu = true;
                                break;
                        }

                    }
                    break;

                // Exit
                case 0:
                    printMsg("Thank you for using the Classroom Booking Application!");
                    break;
            }
        }
    }

}
Leave a Comment