Untitled
unknown
java
a year ago
17 kB
7
Indexable
import java.util.Scanner; import java.util.ArrayList; import java.time.format.DateTimeFormatter; import java.time.LocalDate; import java.time.format.DateTimeParseException; public class ClassroomBookingApp { // Create new Scanner object to obtain input private static final Scanner INPUT = new Scanner(System.in); // Define the ArrayLists as static fields (so can be accessed by all methods instead of just main method) 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 printMsg(String prompt) { System.out.println("*".repeat(50)); System.out.println(prompt); System.out.println("*".repeat(50)); } //=============================================================== // FORMAT CHECKING //=============================================================== private static boolean isValidEmailFormat(String email) { // Email doesn't contains "0" if (!email.contains("@")) { return false; } // Split email into 2 parts String[] parts = email.split("@"); // "@" more than 1 if (parts.length != 2) { return false; } // Get 2 parts value String username = parts[0]; String domain = parts[1]; // Is first part empty or second part has wrong format return (!username.isEmpty()) && (domain.equals("sim.edu.sg")); } //=============================================================== // 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! Please try again"); } } 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)); break; // Is input Integer } catch (NumberFormatException e) { printMsg("Error: That is not a NUMBER!"); } } return input; } private static String getValidEmail(String prompt) { while (true) { // Get email String email = getNonEmptyInput(prompt); // Invalid email format if (!isValidEmailFormat(email)) { printMsg("Error: Invalid email format! Please try again!"); // Valid email format } else { return email; } } } 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: The room is not existing!"); } } private static Lecturer getValidLecturer() { while (true) { // Get room number int lecturerInput = getValidInfo("Enter lecturer ID: "); // Find room for (Lecturer existingLecturer : LECTURERS) { // Is room found if (existingLecturer.getLecturerId() == lecturerInput) { return existingLecturer; } } // Room not found printMsg("Error: The lecturer is not existing!"); } } //=============================================================== // 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 success 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 success msg printMsg("Classroom '" + roomToUpdate.getClassroomNum() + "' updated!"); } private static void deleteClassroom() { // Get room number Classroom roomToDelete = getValidRoom(); // Remove room CLASSROOMS.remove(roomToDelete); // Display success msg printMsg("Classroom '" + roomToDelete.getClassroomNum() + "' deleted!"); } private static void viewClassrooms() { // 0 room if (CLASSROOMS.isEmpty()) { printMsg("No classrooms created!"); // 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 success 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 success msg printMsg("Lecturer '" + lecturerToUpdate.getLecturerId() + "' edited!"); } private static void removeLecturer() { // Get lecturer ID Lecturer lecturerToRemove = getValidLecturer(); // Remove lecturer LECTURERS.remove(lecturerToRemove); // Display success msg printMsg("Lecturer '" + lecturerToRemove.getLecturerId() + "' removed!"); } private static void viewLecturers() { // 0 lecturer if (LECTURERS.isEmpty()) { printMsg("No lecturers added!"); // Has lecturer } else { printLecturers(); } } //=============================================================== // BOOKINGS //=============================================================== private static void makeBooking() {} private static void cancelBooking() {} private static void viewBookings() {} //=============================================================== // 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(); 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(); 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) { printReportsMenu(); optionInput = getValidOption("Reports"); } break; // Exit case 0: printMsg("Thank you for using the Classroom Booking Application!"); break; } } } }
Editor is loading...
Leave a Comment