Untitled
unknown
plain_text
a year ago
22 kB
14
Indexable
// @author Group 3 import java.util.InputMismatchException; 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 private static final Scanner INPUT = new Scanner(System.in); // Define the ArrayLists as static fields (so can be accessed by all methods instead of main method) private static ArrayList<Classroom> classrooms = new ArrayList<>(); private static ArrayList<Lecturer> lecturers = new ArrayList<>(); private static ArrayList<Booking> bookings = new ArrayList<>(); // Display Main Menu 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)); } // Display Classrooms Menu private static void printClassroomsMenu() { System.out.println("*".repeat(50)); System.out.println("Welcome to Classroom Booking Application"); 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("5. Back to Main Menu"); System.out.println("*".repeat(50)); } private static void printLecturersMenu() { System.out.println("*".repeat(50)); System.out.println("Welcome to Classroom Booking Application"); 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("5. Back to Main Menu"); System.out.println("*".repeat(50)); } private static void printBookingsMenu() { System.out.println("*".repeat(50)); System.out.println("Welcome to Classroom Booking Application"); 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("4. Back to Main Menu"); System.out.println("*".repeat(50)); } private static void printReportsMenu() { System.out.println("*".repeat(50)); System.out.println("Welcome to Classroom Booking Application"); 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("5. Back to Main Menu"); System.out.println("*".repeat(50)); } // Get User's Menu Option Input private static int getOptionInput() { boolean validInput = false; int menuOption = -1; // First Prompt to Obtain Input System.out.print("Please select an option to continue: "); while (!validInput) { try { // Obtain Input menuOption = INPUT.nextInt(); // Check Input Range switch (menuOption) { case 0: case 1: case 2: case 3: case 4: break; default: System.out.println("Hey! You entered an invalid option!"); System.out.print("Please reselect a VALID option: "); continue; } validInput = true; // If Input Isn't Integer } catch (InputMismatchException e) { System.out.println("Hey! You entered an invalid option!"); INPUT.next(); // Clear Invalid Input System.out.print("Please reselect a VALID option: "); } } return menuOption; // Return Valid Input } private static void manageClassroomsMenu() { //Appendix C boolean backToMainMenu = false; while (!backToMainMenu) { printClassroomsMenu(); //prints classroom menu int choice = getOptionInput(); switch (choice) { case 1: createClassroom(); break; case 2: updateClassroom(); break; case 3: deleteClassroom(); break; case 4: viewAllClassrooms(); break; case 5: backToMainMenu = true; break; default: System.out.println("Invalid option, please try again!"); } } } private static void createClassroom() { //create classroom method System.out.print("Enter classroom number: "); int classroomNum = INPUT.nextInt(); INPUT.nextLine(); // this is required as the integer is read and the newline character remains in the buffer. ( not required for the strings input) System.out.print("Enter classroom name: "); String classroomName = INPUT.nextLine(); System.out.print("Enter classroom location: "); String classroomLocation = INPUT.nextLine(); Classroom newRoom = new Classroom(classroomNum, classroomName, classroomLocation); classrooms.add(newRoom); System.out.println("New Classroom created"); } private static void updateClassroom() { //update classroom method System.out.print("Enter classroom number to update: "); int classroomNum = INPUT.nextInt(); INPUT.nextLine(); Classroom roomToUpdate = null; for (Classroom existingClassroom : classrooms) { if (existingClassroom.getClassroomNumber() == classroomNum) { roomToUpdate = existingClassroom; break; } } if (roomToUpdate == null) { System.out.println("Error: Invalid classroom number!"); return; } System.out.print("Update classroom name: "); // get user input for new details String newClassroomName = INPUT.nextLine(); System.out.print("Update classroom location: "); String newClassroomLocation = INPUT.nextLine(); roomToUpdate.setClassroomName(newClassroomName); //updates the ClassroomName var created in Classroom file roomToUpdate.setClassroomLocation(newClassroomLocation); System.out.println("Classroom '" + classroomNum + "' updated!"); } private static void deleteClassroom() { //delete classroom method System.out.print("Enter classroom number: "); int classroomNum = INPUT.nextInt(); INPUT.nextLine(); Classroom roomToDelete = null; for (Classroom existingClassroom : classrooms) { if (existingClassroom.getClassroomNumber() == classroomNum) { roomToDelete = existingClassroom; break; } } if (roomToDelete == null) { System.out.println("Error: classroom number '" + classroomNum + "' does not exist!"); } else { classrooms.remove(roomToDelete); //remove method System.out.println("Classroom '" + classroomNum + "' deleted!"); } } private static void viewAllClassrooms() { if (classrooms.isEmpty()) { //helps to check if list is empty System.out.println("No classrooms created."); } else { System.out.println("** Classroom List **"); for (Classroom viewClassroom : classrooms) { System.out.println("Classroom Number: " + viewClassroom.getClassroomNumber() + ", Name: " + viewClassroom.getClassroomName() + ", Location: " + viewClassroom.getClassroomLocation()); } } } private static void manageLecturersMenu() { //Appendix D boolean backToMainMenu = false; while (!backToMainMenu) { printLecturersMenu(); //prints lecturers menu int choice = getOptionInput(); switch (choice) { case 1: addLecturer(); break; case 2: editLecturer(); break; case 3: removeLecturer(); break; case 4: viewAllLecturers(); break; case 5: backToMainMenu = true; break; default: System.out.println("Invalid option, please try again!"); } } } private static void addLecturer() { //create lecturer method System.out.print("Enter lecturer ID: "); int lecturerId = INPUT.nextInt(); INPUT.nextLine(); System.out.print("Enter lecturer full name: "); String fullName = INPUT.nextLine(); System.out.print("Enter lecturer contact number: "); String contactNumber = INPUT.nextLine(); System.out.print("Enter lecturer email: "); String emailAddress = INPUT.nextLine(); Lecturer newLecturer = new Lecturer(lecturerId, fullName, contactNumber, emailAddress); lecturers.add(newLecturer); System.out.println("New lecturer added!"); } private static void editLecturer() { //edit lecturer method System.out.print("Enter lecturer ID to edit: "); int lecturerId = INPUT.nextInt(); INPUT.nextLine(); Lecturer lecturerToEdit = null; for (Lecturer existingLecturer : lecturers) { if (existingLecturer.getLecturerId() == lecturerId) { lecturerToEdit = existingLecturer; break; } } if (lecturerToEdit == null) { System.out.println("Invalid lecturer ID!"); return; } System.out.print("Edit lecturer full name: "); String newFullName = INPUT.nextLine(); System.out.print("Edit lecturer contact number: "); String newContactNumber = INPUT.nextLine(); System.out.print("Edit lecturer email: "); String newEmailAddress = INPUT.nextLine(); lecturerToEdit.setFullName(newFullName); //update lecturer details lecturerToEdit.setContactNumber(newContactNumber); lecturerToEdit.setEmailAddress(newEmailAddress); System.out.println("Lecturer '" + lecturerId + "' edited!"); } private static void removeLecturer() { //remove lecturer method System.out.print("Enter lecturer ID to remove: "); int lecturerId = INPUT.nextInt(); INPUT.nextLine(); Lecturer lecturerToRemove = null; //finding lecturer in the list for (Lecturer existingLecturer : lecturers) { if (existingLecturer.getLecturerId() == lecturerId) { lecturerToRemove = existingLecturer; break; } } if (lecturerToRemove != null) { lecturers.remove(lecturerToRemove); //remove lecturer System.out.println("Lecturer '" + lecturerId + "' removed!"); } else { System.out.println("Error: Lecturer ID '" + lecturerId + "' does not exist!"); } } private static void viewAllLecturers() { //view lecturer method if (lecturers.isEmpty()) { System.out.println("No lecturers added!"); } else { System.out.println("** Lecturer List **"); for (Lecturer viewLecturer : lecturers) { System.out.println("Lecturer ID: " + viewLecturer.getLecturerId() + ", Name: " + viewLecturer.getFullName() + ", Contact: " + viewLecturer.getContactNumber() + ", Email: " + viewLecturer.getEmailAddress()); } } } private static void manageBookingsMenu() { //Appendix E boolean backToMainMenu = false; while (!backToMainMenu) { printBookingsMenu(); //prints booking menu int choice = getOptionInput(); switch (choice) { case 1: makeBooking(); break; case 2: cancelBooking(); break; case 3: viewAllBookings(); break; case 4: backToMainMenu = true; break; default: System.out.println("Invalid option, please try again!"); } } } private static void makeBooking() { //make a booking method System.out.print("Enter booking date (dd/mm/yyyy): "); String inputDate = INPUT.nextLine(); LocalDate bookingDate = LocalDate.parse(inputDate, DateTimeFormatter.ofPattern("dd/MM/yyyy")); //converts inputDate from string format to LocalDate System.out.print("Enter lecturer id: "); int lecturerId = INPUT.nextInt(); INPUT.nextLine(); Lecturer selectedLecturer = null; // Finds if lecturer exists for (Lecturer existingLecturer : lecturers) { if (existingLecturer.getLecturerId() == lecturerId) { selectedLecturer = existingLecturer; break; } } if (selectedLecturer == null) { System.out.println("Error: Lecturer ID '" + lecturerId + "' not found!"); return; } Classroom availableClassroom = null; //method to check if have available classes for (Classroom reservedClass : classrooms) { boolean isBooked = false; for (Booking reservedBooking : bookings) { if (reservedBooking.getClassroom().equals(reservedClass) && reservedBooking.getBookingDate().equals(bookingDate)) { isBooked = true; break; } } if (!isBooked) { availableClassroom = reservedClass; break; } } if (availableClassroom == null) { System.out.println("No available classroom on " + bookingDate); return; } int bookingId = generateBookingId(); Booking newBooking = new Booking(bookingId, bookingDate, availableClassroom, selectedLecturer); bookings.add(newBooking); System.out.println("Classroom '" + availableClassroom.getClassroomName() + "' booked, booking ID is " + bookingId); } private static int nextBookingId = 50001; // Static variable to keep track of the next booking ID ( following appendix number, else should start from 1) private static int generateBookingId() { return nextBookingId++; // Return the current ID and increment the counter } private static void cancelBooking() { //cancel booking method System.out.print("Enter booking ID: "); int bookingId = INPUT.nextInt(); INPUT.nextLine(); boolean found = false; //Iterator<Booking> iterator = bookings.iterator(); for (Booking booked : bookings) { //finds the booking then cancels it if (booked.getBookingId() == bookingId) { bookings.remove(booked); // or can use iterator.remove(); // Remove the booking using the iterator safer option System.out.println("Booking '" + bookingId + "' cancelled!"); found = true; break; } } if (!found) { System.out.println("Booking ID '" + bookingId + "' not found."); } } private static void viewAllBookings() { // view bookings method if (bookings.isEmpty()) { System.out.println("No bookings found."); } else { System.out.println("All Bookings:"); for (Booking booked : bookings) { System.out.println("Booking ID: " + booked.getBookingId() + ", Date: " + booked.getBookingDate() + ", Classroom Number: " + booked.getClassroom().getClassroomNumber() + ", Lecturer ID: " + booked.getLecturer().getLecturerId()); } } } private static void manageReportsMenu() { //Appendix F boolean backToMainMenu = false; while (!backToMainMenu) { printReportsMenu(); //prints reports menu int choice = getOptionInput(); switch (choice) { case 1: generateBookingsByDate(); break; case 2: generateBookingsByClassroom(); break; case 3: generateBookingsByLecturer(); break; case 4: generateTotalBookings(); break; case 5: backToMainMenu = true; break; default: System.out.println("Invalid option, please try again!"); } } } private static void generateBookingsByDate() { //generate bookings by date method System.out.print("Enter date (dd/MM/yyyy): "); String inputDate = INPUT.nextLine(); LocalDate reportDate; try { reportDate = LocalDate.parse(inputDate, DateTimeFormatter.ofPattern("dd/MM/yyyy")); } catch (DateTimeParseException e) { //imported from java, use exception handling try-catch System.out.println("Invalid date format! Make sure to enter the date in dd/MM/yyyy format."); return; } boolean found = false; for (Booking bookedByDate : bookings) { if (bookedByDate.getBookingDate().equals(reportDate)) { System.out.println("Booking ID: " + bookedByDate.getBookingId() + ", Classroom number: " + bookedByDate.getClassroom().getClassroomNumber() + ", Lecturer ID: " + bookedByDate.getLecturer().getLecturerId()); found = true; } } if (!found) { System.out.println("No bookings found for " + reportDate); } } private static void generateBookingsByClassroom() { //generate bookings by classroom method System.out.print("Enter classroom number: "); int classroomNumber = INPUT.nextInt(); INPUT.nextLine(); boolean found = false; for (Booking bookedByClassroom : bookings) { if (bookedByClassroom.getClassroom().getClassroomNumber() == classroomNumber) { System.out.println("Booking ID: " + bookedByClassroom.getBookingId() + ", Date: " + bookedByClassroom.getBookingDate().format(DateTimeFormatter.ofPattern("dd/MM/yyyy")) + ", Lecturer ID: " + bookedByClassroom.getLecturer().getLecturerId()); found = true; } } if (!found) { System.out.println("No bookings found for classroom number " + classroomNumber); } } private static void generateBookingsByLecturer() { //generate bookings by lecturer method System.out.print("Enter lecturer id: "); int lecturerId = INPUT.nextInt(); INPUT.nextLine(); boolean found = false; for (Booking bookedByLecturer : bookings) { if (bookedByLecturer.getLecturer().getLecturerId() == lecturerId) { System.out.println("Booking ID: " + bookedByLecturer.getBookingId() + ", Date: " + bookedByLecturer.getBookingDate().format(DateTimeFormatter.ofPattern("dd/MM/yyyy")) + ", Classroom number: " + bookedByLecturer.getClassroom().getClassroomNumber()); found = true; } } if (!found) { System.out.println("No bookings found for lecturer ID: " + lecturerId); } } private static void generateTotalBookings() { // find total number of booking method System.out.println("Total number of bookings: " + bookings.size()); } // Main Method public static void main(String[] args) { // Display Main Menu printMainMenu(); // Obtain Main Menu Option from User int menuOption = getOptionInput(); switch (menuOption) { case 1: // Manage Classrooms manageClassroomsMenu(); break; case 2: // Manage Lecturers manageLecturersMenu(); break; case 3: // Manage Bookings manageBookingsMenu(); break; case 4: // Generate Reports manageReportsMenu(); break; case 0: // Exit System.out.println("Thank you for using the Classroom Booking Application!"); System.exit(0); } } }
Editor is loading...
Leave a Comment