From a01fca3541b60d8d1e515b409cceb242293d1260 Mon Sep 17 00:00:00 2001 From: Reza Date: Thu, 29 May 2025 12:51:58 +0330 Subject: [PATCH 01/15] refactor: add readIntInRange function --- src/view/UserInterface.java | 79 +++++++++++++++---------------------- 1 file changed, 32 insertions(+), 47 deletions(-) diff --git a/src/view/UserInterface.java b/src/view/UserInterface.java index 7286753..efd2cec 100644 --- a/src/view/UserInterface.java +++ b/src/view/UserInterface.java @@ -123,16 +123,7 @@ public void printReportsMenu(){ public void displayMainMenu() { while (true) { printMainMenu();//Menu print - Scanner MainMenuInput = new Scanner(System.in);//Get input - int mainmenuinput = 0; - try { - mainmenuinput = MainMenuInput.nextInt(); - } - catch(Exception e) { - System.out.println("░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░"); - System.out.println("░░░░░░░░░░░░░░░░░░░░░░░░░░<<<⚠⃨Please enter valid character⚠⃨>>>░░░░░░░░░░░░░░░░░░░░░░░░░░░"); - System.out.println("░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░"); - } + int mainmenuinput = readIntInRange("Enter a number", 0, 4); switch (mainmenuinput) { case 1: displayMemberMenu(); @@ -161,15 +152,7 @@ public void displayMemberMenu() { while (true) { printManageUsersMenu(); Scanner MemberMenuInput = new Scanner(System.in); - int membermenuinput = 0; - try { - membermenuinput = MemberMenuInput.nextInt(); - } - catch(Exception e) { - System.out.println("░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░"); - System.out.println("░░░░░░░░░░░░░░░░░░░░░░░░░░<<<⚠⃨Please enter valid character⚠⃨>>>░░░░░░░░░░░░░░░░░░░░░░░░░░░"); - System.out.println("░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░"); - } + int membermenuinput = readIntInRange("Enter a number", 0, 5); switch (membermenuinput) { case 1: System.out.print("░░░░░░░░░░░░░░░░░░░░░░░░░░║Enter The Student ID║══> "); @@ -233,15 +216,7 @@ public void displayBookMenu() { while (true) { printManageBooksMenu(); Scanner BooksMenuInput = new Scanner(System.in); - int booksmenuinput = 0; - try { - booksmenuinput = BooksMenuInput.nextInt(); - } - catch(Exception e) { - System.out.println("░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░"); - System.out.println("░░░░░░░░░░░░░░░░░░░░░░░░░░<<<⚠⃨Please enter valid character⚠⃨>>>░░░░░░░░░░░░░░░░░░░░░░░░░░░"); - System.out.println("░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░"); - } + int booksmenuinput = readIntInRange("Enter a number", 0, 4); switch (booksmenuinput) { case 1: System.out.print("░░░░░░░░░░░░░░░░░░░░░░░░░░║Enter The Title║══> "); @@ -303,15 +278,7 @@ public void displayLoanMenu() { while (true) { printLoanMenu(); Scanner loanMenuInput = new Scanner(System.in); - int loanmenuinput = 0; - try { - loanmenuinput = loanMenuInput.nextInt(); - } - catch(Exception e) { - System.out.println("░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░"); - System.out.println("░░░░░░░░░░░░░░░░░░░░░░░░░░<<<⚠⃨Please enter valid character⚠⃨>>>░░░░░░░░░░░░░░░░░░░░░░░░░░░"); - System.out.println("░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░"); - } + int loanmenuinput = readIntInRange("Enter a number", 0, 2); switch (loanmenuinput) { case 1: Book isbn4; @@ -339,16 +306,7 @@ public void displayLoanMenu() { public void displayReportMenu() { while (true) { printReportsMenu(); - Scanner reportsMenuInput = new Scanner(System.in); - int reportsmenuinput = 0; - try { - reportsmenuinput = reportsMenuInput.nextInt(); - } - catch(Exception e) { - System.out.println("░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░"); - System.out.println("░░░░░░░░░░░░░░░░░░░░░░░░░░<<<⚠⃨Please enter valid character⚠⃨>>>░░░░░░░░░░░░░░░░░░░░░░░░░░░"); - System.out.println("░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░"); - } + int reportsmenuinput = readIntInRange("Enter a number", 0, 2); switch (reportsmenuinput) { case 1: break; @@ -425,4 +383,31 @@ public void displayAllBooks(List books) { public void displayReport(List results) { } + + private void printInvalidInput() { + System.out.println("░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░"); + System.out.println("░░░░░░░░░░░░░░░░░░░░░░░░░░<<<⚠⃨Please enter valid characters⚠⃨>>>░░░░░░░░░░░░░░░░░░░░░░░░░░░"); + System.out.println("░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░"); + } + + private int readIntInRange(String prompt, int min, int max) { + while (true) { + System.out.print(String.format("░░░░░░░░░░░░░░░░░░░░░░░░░░║%s║══> ", prompt)); + System.out.flush(); + + Scanner sc = new Scanner(System.in); + try { + int input = sc.nextInt(); + if (input >= min && input <= max) + return input; + else { + printInvalidInput(); + continue; + } + } catch (Exception e) { + printInvalidInput(); + continue; + } + } + } } From 089fa01102a8d1b8bd1a50aa53d209d7fb539094 Mon Sep 17 00:00:00 2001 From: Reza Date: Thu, 29 May 2025 12:56:02 +0330 Subject: [PATCH 02/15] refactor: only create one scanner --- src/view/UserInterface.java | 52 +++++++++++++++++-------------------- 1 file changed, 24 insertions(+), 28 deletions(-) diff --git a/src/view/UserInterface.java b/src/view/UserInterface.java index efd2cec..159c238 100644 --- a/src/view/UserInterface.java +++ b/src/view/UserInterface.java @@ -7,9 +7,11 @@ public class UserInterface { private LibraryController libraryController; + private Scanner scanner; public UserInterface() { libraryController = new LibraryController(); + scanner = new Scanner(System.in); } public void printMainMenu() { @@ -151,22 +153,21 @@ public void displayMainMenu() { public void displayMemberMenu() { while (true) { printManageUsersMenu(); - Scanner MemberMenuInput = new Scanner(System.in); int membermenuinput = readIntInRange("Enter a number", 0, 5); switch (membermenuinput) { case 1: System.out.print("░░░░░░░░░░░░░░░░░░░░░░░░░░║Enter The Student ID║══> "); - String ugstudentId = MemberMenuInput.nextLine(); + String ugstudentId = scanner.nextLine(); System.out.print("░░░░░░░░░░░░░░░░░░░░░░░░░░║Enter The Student First Name║══> "); - String ugfirstName = MemberMenuInput.nextLine(); + String ugfirstName = scanner.nextLine(); System.out.print("░░░░░░░░░░░░░░░░░░░░░░░░░░║Enter The Student LastName║══> "); - String uglastName = MemberMenuInput.nextLine(); + String uglastName = scanner.nextLine(); System.out.print("░░░░░░░░░░░░░░░░░░░░░░░░░░║Enter The Student Major║══> "); - String ugmajor = MemberMenuInput.nextLine(); + String ugmajor = scanner.nextLine(); System.out.print("░░░░░░░░░░░░░░░░░░░░░░░░░░║Enter The Student Enrollment Year║══> "); int ugenrollmentyear = 0; try{ - ugenrollmentyear = MemberMenuInput.nextInt(); + ugenrollmentyear = scanner.nextInt(); } catch (Exception e){ System.out.println("░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░"); @@ -177,17 +178,17 @@ public void displayMemberMenu() { break; case 2: System.out.print("░░░░░░░░░░░░░░░░░░░░░░░░░░║Enter The Student ID║══> "); - String gstudentId = MemberMenuInput.nextLine(); + String gstudentId = scanner.nextLine(); System.out.print("░░░░░░░░░░░░░░░░░░░░░░░░░░║Enter The Student First Name║══> "); - String gfirstName = MemberMenuInput.nextLine(); + String gfirstName = scanner.nextLine(); System.out.print("░░░░░░░░░░░░░░░░░░░░░░░░░░║Enter The Student LastName║══> "); - String glastName = MemberMenuInput.nextLine(); + String glastName = scanner.nextLine(); System.out.print("░░░░░░░░░░░░░░░░░░░░░░░░░░║Enter The Student Major║══> "); - String gmajor = MemberMenuInput.nextLine(); + String gmajor = scanner.nextLine(); System.out.print("░░░░░░░░░░░░░░░░░░░░░░░░░░║Enter The Student Supervisor║══> "); - String gsupervisor = MemberMenuInput.nextLine(); + String gsupervisor = scanner.nextLine(); System.out.print("░░░░░░░░░░░░░░░░░░░░░░░░░░║Enter The Student Thesis Title║══> "); - String gthesisTitle = MemberMenuInput.nextLine(); + String gthesisTitle = scanner.nextLine(); libraryController.addGraduateStudent(gstudentId, gfirstName, glastName, gmajor, gsupervisor, gthesisTitle); break; case 3: @@ -195,7 +196,7 @@ public void displayMemberMenu() { break; case 4: System.out.print("░░░░░░░░░░░░░░░░░░░░░░░░░░║Enter The Student ID║══> "); - String rstudentId = MemberMenuInput.nextLine(); + String rstudentId = scanner.nextLine(); libraryController.removeStudent(rstudentId); break; case 5: @@ -215,31 +216,30 @@ public void displayMemberMenu() { public void displayBookMenu() { while (true) { printManageBooksMenu(); - Scanner BooksMenuInput = new Scanner(System.in); int booksmenuinput = readIntInRange("Enter a number", 0, 4); switch (booksmenuinput) { case 1: System.out.print("░░░░░░░░░░░░░░░░░░░░░░░░░░║Enter The Title║══> "); - String title = BooksMenuInput.nextLine(); + String title = scanner.nextLine(); System.out.print("░░░░░░░░░░░░░░░░░░░░░░░░░░║Enter The Student Author║══> "); - String author = BooksMenuInput.nextLine(); + String author = scanner.nextLine(); System.out.print("░░░░░░░░░░░░░░░░░░░░░░░░░░║Enter The Student ISBN║══> "); - String isbn1 = BooksMenuInput.nextLine(); + String isbn1 = scanner.nextLine(); System.out.print("░░░░░░░░░░░░░░░░░░░░░░░░░░║Enter The Student Publication Year║══> "); - int publicationYear = BooksMenuInput.nextInt(); + int publicationYear = scanner.nextInt(); // TODO: print list of available categories and let the user choose one or define one libraryController.addBook(title, author, isbn1, null, publicationYear); break; case 2: System.out.print("░░░░░░░░░░░░░░░░░░░░░░░░░░║Enter The Student ISBN║══> "); - String isbn2 = BooksMenuInput.nextLine(); + String isbn2 = scanner.nextLine(); libraryController.removeBook(isbn2); break; case 3: printSearchBookMenu(); int searchbookmenu = 0; try { - searchbookmenu = BooksMenuInput.nextInt(); + searchbookmenu = scanner.nextInt(); } catch(Exception e) { System.out.println("░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░"); @@ -253,7 +253,7 @@ public void displayBookMenu() { case 2: String isbn3; System.out.print("░░░░░░░░░░░░░░░░░░░░░░░░░░║Enter The Book ISBN║══> "); - isbn3 = BooksMenuInput.nextLine(); + isbn3 = scanner.nextLine(); libraryController.searchBookByISBN(isbn3); break; case 0: @@ -277,7 +277,6 @@ public void displayBookMenu() { public void displayLoanMenu() { while (true) { printLoanMenu(); - Scanner loanMenuInput = new Scanner(System.in); int loanmenuinput = readIntInRange("Enter a number", 0, 2); switch (loanmenuinput) { case 1: @@ -324,9 +323,8 @@ public void displayReportMenu() { } public String getStudentIdForSearch() { - Scanner gSIFS = new Scanner(System.in); System.out.print("░░░░░░░░░░░░░░░░░░░░░░░░░░║Enter The Student ID║══> "); - String sstudentId = gSIFS.nextLine(); + String sstudentId = scanner.nextLine(); return sstudentId; } @@ -335,10 +333,9 @@ public void getBookISBNForOperation() { } public String getBookTitleForSearch() { - Scanner gBTFS = new Scanner(System.in); String T; System.out.print("░░░░░░░░░░░░░░░░░░░░░░░░░░║Enter The Book Title║══> "); - T = gBTFS.nextLine(); + T = scanner.nextLine(); return T; } @@ -395,9 +392,8 @@ private int readIntInRange(String prompt, int min, int max) { System.out.print(String.format("░░░░░░░░░░░░░░░░░░░░░░░░░░║%s║══> ", prompt)); System.out.flush(); - Scanner sc = new Scanner(System.in); try { - int input = sc.nextInt(); + int input = scanner.nextInt(); if (input >= min && input <= max) return input; else { From 577faa34999907a1eaf4d221aa745cad2d39a0ba Mon Sep 17 00:00:00 2001 From: Reza Date: Thu, 29 May 2025 13:52:45 +0330 Subject: [PATCH 03/15] refactor: implement readString & printPrompt --- 1q | 376 ++++++++++++++++++++++++++++++++++++ src/view/UserInterface.java | 215 +++++++++------------ 2 files changed, 466 insertions(+), 125 deletions(-) create mode 100644 1q diff --git a/1q b/1q new file mode 100644 index 0000000..65d0a24 --- /dev/null +++ b/1q @@ -0,0 +1,376 @@ +package view; + +import java.util.List; +import model.*; +import java.util.Scanner; +import controller.LibraryController; + +public class UserInterface { + private LibraryController libraryController; + private Scanner scanner; + + public UserInterface() { + libraryController = new LibraryController(); + scanner = new Scanner(System.in); + } + + public void printMainMenu() { + System.out.println( + " ██╗ ██╗ ██████╗██████╗ ██╗ ██╗██████╗ \n"+ + "██████╗██████╗██████╗ ██║ ██║██╔════╝██╔══██╗ ██║ ██║██╔══██╗ ██████╗██████╗██████╗\n"+ + "╚═════╝╚═════╝╚═════╝ ██║ ██║╚█████╗ ██████╦╝ ██║ ██║██████╦╝ ╚═════╝╚═════╝╚═════╝\n"+ + "██████╗██████╗██████╗ ██║ ██║ ╚═══██╗██╔══██╗ ██║ ██║██╔══██╗ ██████╗██████╗██████╗\n"+ + "╚═════╝╚═════╝╚═════╝ ║██████╔╝██████╔╝██████╦╝ ███████╗██║██████╦╝ ╚═════╝╚═════╝╚═════╝\n"+ + " ╚══════╝╚══════╝ ╚═════╝ ╚══════╝╚═╝╚═════╝ \n"+ + "███╗ ███╗ █████╗ ███╗ ██╗ █████╗ ██████╗ ███████╗███╗ ███╗███████╗███╗ ██╗████████╗\n"+ + "████╗ ████║██╔══██╗████╗ ██║██╔══██╗██╔════╝ ██╔════╝████╗ ████║██╔════╝████╗ ██║╚══██╔══╝\n"+ + "██╔████╔██║███████║██╔██╗██║███████║██║ ██╗ █████╗ ██╔████╔██║█████╗ ██╔██╗██║ ██║ \n"+ + "██║╚██╔╝██║██╔══██║██║╚████║██╔══██║██║ ╚██╗██╔══╝ ██║╚██╔╝██║██╔══╝ ██║╚████║ ██║ \n"+ + "██║ ╚═╝ ██║██║ ██║██║ ╚███║██║ ██║╚██████╔╝███████╗██║ ╚═╝ ██║███████╗██║ ╚███║ ██║ \n"+ + "╚═╝ ╚═╝╚═╝ ╚═╝╚═╝ ╚══╝╚═╝ ╚═╝ ╚═════╝ ╚══════╝╚═╝ ╚═╝╚══════╝╚═╝ ╚══╝ ╚═╝ \n"+ + "░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░(➊)══> Manage Users░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░\n"+ + "░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░(➋)══> Manage Books░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░\n"+ + "░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░(➌)══> Loans ░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░\n"+ + "░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░(➍)══> Reports ░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░\n"+ + "░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░(⓿)══> Exit ░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░"); + } + + public void printManageUsersMenu(){ + System.out.println( + " ███╗ ███╗ █████╗ ███╗ ██╗ █████╗ ██████╗ ███████╗ \n"+ + "██████╗██████╗██████╗ ████╗ ████║██╔══██╗████╗ ██║██╔══██╗██╔════╝ ██╔════╝ ██████╗██████╗██████╗\n"+ + "╚═════╝╚═════╝╚═════╝ ██╔████╔██║███████║██╔██╗██║███████║██║ ██╗ █████╗ ╚═════╝╚═════╝╚═════╝\n"+ + "██████╗██████╗██████╗ ██║╚██╔╝██║██╔══██║██║╚████║██╔══██║██║ ╚██╗██╔══╝ ██████╗██████╗██████╗\n"+ + "╚═════╝╚═════╝╚═════╝ ██║ ╚═╝ ██║██║ ██║██║ ╚███║██║ ██║╚██████╔╝███████╗ ╚═════╝╚═════╝╚═════╝\n"+ + " ╚═╝ ╚═╝╚═╝ ╚═╝╚═╝ ╚══╝╚═╝ ╚═╝ ╚═════╝ ╚══════╝ \n"+ + " ██╗ ██╗ ██████╗███████╗██████╗ ██████╗ \n"+ + "██████╗██████╗██████╗██████╗██║ ██║██╔════╝██╔════╝██╔══██╗██╔════╝██████╗██████╗██████╗██████╗\n"+ + "╚═════╝╚═════╝╚═════╝╚═════╝██║ ██║╚█████╗ █████╗ ██████╔╝╚█████╗ ╚═════╝╚═════╝╚═════╝╚═════╝\n"+ + "██████╗██████╗██████╗██████╗██║ ██║ ╚═══██╗██╔══╝ ██╔══██╗ ╚═══██╗██████╗██████╗██████╗██████╗\n"+ + "╚═════╝╚═════╝╚═════╝╚═════╝╚██████╔╝██████╔╝███████╗██║ ██║██████╔╝╚═════╝╚═════╝╚═════╝╚═════╝\n"+ + " ╚═════╝ ╚═════╝ ╚══════╝╚═╝ ╚═╝╚═════╝ \n"+ + "░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░(➊)══> Add Undergraduate Student░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░\n"+ + "░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░(➋)══> Add Graduate Student ░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░\n"+ + "░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░(➌)══> Search Student by ID ░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░\n"+ + "░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░(➍)══> Remove Student by ID ░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░\n"+ + "░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░(➎)══> List All Students ░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░\n"+ + "░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░(⓿)══> Back to Main Menu ░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░"); + } + + public void printManageBooksMenu() { + System.out.println( + " ███╗ ███╗ █████╗ ███╗ ██╗ █████╗ ██████╗ ███████╗ \n"+ + "██████╗██████╗██████╗ ████╗ ████║██╔══██╗████╗ ██║██╔══██╗██╔════╝ ██╔════╝ ██████╗██████╗██████╗\n"+ + "╚═════╝╚═════╝╚═════╝ ██╔████╔██║███████║██╔██╗██║███████║██║ ██╗ █████╗ ╚═════╝╚═════╝╚═════╝\n"+ + "██████╗██████╗██████╗ ██║╚██╔╝██║██╔══██║██║╚████║██╔══██║██║ ╚██╗██╔══╝ ██████╗██████╗██████╗\n"+ + "╚═════╝╚═════╝╚═════╝ ██║ ╚═╝ ██║██║ ██║██║ ╚███║██║ ██║╚██████╔╝███████╗ ╚═════╝╚═════╝╚═════╝\n"+ + " ╚═╝ ╚═╝╚═╝ ╚═╝╚═╝ ╚══╝╚═╝ ╚═╝ ╚═════╝ ╚══════╝ \n"+ + " ██████╗ █████╗ █████╗ ██╗ ██╗ ██████╗ \n"+ + "██████╗██████╗██████╗██████╗██╔══██╗██╔══██╗██╔══██╗██║ ██╔╝██╔════╝ ██████╗██████╗██████╗██████╗\n"+ + "╚═════╝╚═════╝╚═════╝╚═════╝██████╦╝██║ ██║██║ ██║█████═╝ ╚█████╗ ╚═════╝╚═════╝╚═════╝╚═════╝\n"+ + "██████╗██████╗██████╗██████╗██╔══██╗██║ ██║██║ ██║██╔═██╗ ╚═══██╗ ██████╗██████╗██████╗██████╗\n"+ + "╚═════╝╚═════╝╚═════╝╚═════╝██████╦╝╚█████╔╝╚█████╔╝██║ ╚██╗██████╔╝ ╚═════╝╚═════╝╚═════╝╚═════╝\n"+ + " ╚═════╝ ╚════╝ ╚════╝ ╚═╝ ╚═╝╚═════╝ \n"+ + "░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░(➊)══> Add Book ░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░\n"+ + "░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░(➋)══> Remove Book by ISBN ░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░\n"+ + "░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░(➌)══> Search Book ░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░\n"+ + "░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░(➍)══> List All Available Books ░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░\n"+ + "░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░(⓿)══> Back to Main Menu ░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░\n"); + } + + public void printSearchBookMenu(){ + System.out.println( + " ██████╗███████╗ █████╗ ██████╗ █████╗ ██╗ ██╗ \n"+ + "██████╗██████╗██████╗ ██╔════╝██╔════╝██╔══██╗██╔══██╗██╔══██╗██║ ██║ ██████╗██████╗██████╗\n"+ + "╚═════╝╚═════╝╚═════╝ ╚█████╗ █████╗ ███████║██████╔╝██║ ╚═╝███████║ ╚═════╝╚═════╝╚═════╝\n"+ + "██████╗██████╗██████╗ ╚═══██╗██╔══╝ ██╔══██║██╔══██╗██║ ██╗██╔══██║ ██████╗██████╗██████╗\n"+ + "╚═════╝╚═════╝╚═════╝ ██████╔╝███████╗██║ ██║██║ ██║╚█████╔╝██║ ██║ ╚═════╝╚═════╝╚═════╝\n"+ + " ╚═════╝ ╚══════╝╚═╝ ╚═╝╚═╝ ╚═╝ ╚════╝ ╚═╝ ╚═╝ \n"+ + " ██████╗ █████╗ █████╗ ██╗ ██╗ \n"+ + "██████╗██████╗██████╗██████╗ ██╔══██╗██╔══██╗██╔══██╗██║ ██╔╝ ██████╗██████╗██████╗██████╗\n"+ + "╚═════╝╚═════╝╚═════╝╚═════╝ ██████╦╝██║ ██║██║ ██║█████═╝ ╚═════╝╚═════╝╚═════╝╚═════╝\n"+ + "██████╗██████╗██████╗██████╗ ██╔══██╗██║ ██║██║ ██║██╔═██╗ ██████╗██████╗██████╗██████╗\n"+ + "╚═════╝╚═════╝╚═════╝╚═════╝ ██████╦╝╚█████╔╝╚█████╔╝██║ ╚██╗ ╚═════╝╚═════╝╚═════╝╚═════╝\n"+ + " ╚═════╝ ╚════╝ ╚════╝ ╚═╝ ╚═╝ \n"+ + "░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░(➊)══> Search by Title ░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░\n"+ + "░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░(➋)══> Search by ISBN ░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░\n"+ + "░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░(⓿)══> Back to Book Management ░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░\n" ); + } + + public void printLoanMenu(){ + System.out.println( + "██████╗██████╗██████╗██████╗ ██║ ██╗ ██╗██╗ ██╗████╗ ██╗ ██████╗██████╗██████╗██████╗\n"+ + "╚═════╝╚═════╝╚═════╝╚═════╝ ██║ ██║ ██║███████║██╔██╗██║ ╚═════╝╚═════╝╚═════╝╚═════╝\n"+ + " ██║ ██║ ██║██╔══██║██║╚████║ \n"+ + "██████╗██████╗██████╗██████╗ ███████╗╚█████╔╝██║ ██║██║ ╚███║ ██████╗██████╗██████╗██████╗\n"+ + "╚═════╝╚═════╝╚═════╝╚═════╝ ╚══════╝ ╚════╝ ╚═╝ ╚═╝╚═╝ ╚══╝ ╚═════╝╚═════╝╚═════╝╚═════╝\n"+ + "░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░(➊)══> Check Out Books ░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░\n"+ + "░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░(➋)══> Return Books ░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░\n"+ + "░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░(⓿)══> Back to Loan Menu ░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░\n"); + } + + public void printReportsMenu(){ + System.out.println( + " ██████╗ ███████╗██████╗ █████╗ ██████╗ ████████╗ ██████╗ \n"+ + "██████╗██████╗ ██╔══██╗██╔════╝██╔══██╗██╔══██╗██╔══██╗╚══██╔══╝██╔════╝ ██████╗██████╗\n"+ + "╚═════╝╚═════╝ ██████╔╝█████╗ ██████╔╝██║ ██║██████╔╝ ██║ ╚█████╗ ╚═════╝╚═════╝\n"+ + " ██╔══██╗██╔══╝ ██╔═══╝ ██║ ██║██╔══██╗ ██║ ╚═══██╗ \n"+ + "██████╗██████╗ ██║ ██║███████╗██║ ╚█████╔╝██║ ██║ ██║ ██████╔╝ ██████╗██████╗\n"+ + "╚═════╝╚═════╝ ╚═╝ ╚═╝╚══════╝╚═╝ ╚════╝ ╚═╝ ╚═╝ ╚═╝ ╚═════╝ ╚═════╝╚═════╝\n"+ + "░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░(➊)══> Check Out Books ░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░\n"+ + "░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░(➋)══> Return Books ░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░\n"+ + "░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░(⓿)══> Back to Loan Menu ░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░\n"); + } + + public void displayMainMenu() { + while (true) { + printMainMenu(); + int mainmenuinput = readIntInRange("Enter a number", 0, 4); + switch (mainmenuinput) { + case 1: + displayMemberMenu(); + break; + case 2: + displayBookMenu(); + break; + case 3: + displayLoanMenu(); + break; + case 4: + displayReportMenu(); + break; + case 0: + return; + default: + assert false: "UNREACHABLE"; + } + } + } + + public void displayMemberMenu() { + while (true) { + printManageUsersMenu(); + int choice = readIntInRange("Enter a number", 0, 5); + switch (choice) { + case 1: { + String studentID = readString("Enter the Student ID"); + String firstName = readString("Enter the Student First Name"); + String lastName = readString("Enter the Student Last Name"); + String major = readString("Enter the Student Major"); + int enrollmentYear = readIntInRange("Enter the Student Enrollment Year", 1000, 3000); + libraryController.addUndergraduateStudent(studentID, firstName, lastName, major, enrollmentYear); + break; + } + case 2: { + String studentID = readString("Enter the Student ID"); + String firstName = readString("Enter the Student First Name"); + String lastName = readString("Enter the Student Last Name"); + String major = readString("Enter the Student Major"); + String supervisor = readString("Enter the Student Supervisor"); + String thesisTitle = readString("Enter the Student Thesis Title"); + libraryController.addGraduateStudent(studentID, firstName, lastName, major, supervisor, thesisTitle); + break; + } + case 3: + libraryController.searchStudent(getStudentIdForSearch()); + break; + case 4: { + String studentID = readString("Enter the Student ID"); + libraryController.removeStudent(studentID); + break; + } + case 5: + libraryController.getAllStudents(); + break; + case 0: + return; + default: + assert false: "UNREACHABLE"; + } + } + } + + public void displayBookMenu() { + while (true) { + printManageBooksMenu(); + int choice = readIntInRange("Enter a number", 0, 4); + switch (choice) { + case 1: { + String title = readString("Enter the title"); + String author = readString("Enter the author"); + String isbn = readString("Enter the ISBN"); + int publicationYear = readIntInRange("Enter the publication year", 1000, 3000); + // TODO: print list of available categories and let the user choose one or define one + libraryController.addBook(title, author, isbn, null, publicationYear); + break; + } + case 2: { + String isbn = readString("Enter the ISBN"); + libraryController.removeBook(isbn); + break; + } + case 3: + displaySearchBookMenu(); + break; + case 4: + libraryController.getAvailableBooks(); + break; + case 0: + return; + default: + assert false: "UNREACHABLE"; + } + } + } + + public void displaySearchBookMenu() { + printSearchBookMenu(); + int choice = readIntInRange("Enter a number", 0, 2); + switch (choice){ + case 1: + libraryController.searchBookByTitle(getBookTitleForSearch()); + break; + case 2: + String isbn = readString("Enter the book ISBN"); + libraryController.searchBookByISBN(isbn); + break; + case 0: + return; + } + + } + + public void displayLoanMenu() { + while (true) { + printLoanMenu(); + int choice = readIntInRange("Enter a number", 0, 2); + switch (choice) { + case 1: + // TODO: implement check out books + break; + case 2: + // TODO: implement return books + break; + case 0: + return; + default: + assert false: "UNREACHABLE"; + } + } + } + + public void displayReportMenu() { + while (true) { + printReportsMenu(); + int choice = readIntInRange("Enter a number", 0, 2); + switch (choice) { + case 1: + break; + case 2: + break; + case 0: + return; + default: + assert false: "UNREACHABLE"; + } + } + } + + public String getStudentIdForSearch() { + return readString("Enter Student ID"); + } + + public void getBookISBNForOperation() { + + } + + public String getBookTitleForSearch() { + return readString("Enter Book Title"); + } + + public void getStudentIdForLoan() { + // TODO: Implement method 'getStudentIdForLoan'. + throw new UnsupportedOperationException("Unimplemented method 'getStudentIdForLoan'"); + } + + public void getBookISBNForLoan() { + // TODO: Implement method 'getBookISBNForLoan'. + throw new UnsupportedOperationException("Unimplemented method 'getBookISBNForLoan'"); + } + + public void displayStudentDetails(Student student) { + // TODO: Implement method 'displayStudentDetails'. + throw new UnsupportedOperationException("Unimplemented method 'displayStudentDetails'"); + } + + public void displayBookDetails(Book book) { + + } + + public void getBookDetailsForCreation() { + // TODO: Implement method 'getBookDetailsForCreation'. + throw new UnsupportedOperationException("Unimplemented method 'getBookDetailsForCreation'"); + } + + public void displayLoanDetails(Loan loan) { + // TODO: Implement method 'displayLoanDetails'. + throw new UnsupportedOperationException("Unimplemented method 'displayLoanDetails'"); + } + + public void displayAllStudents(List students) { + System.out.println(); + } + + public void displayAllBooks(List books) { + // TODO: Implement method 'displayAllBooks'. + throw new UnsupportedOperationException("Unimplemented method 'displayAllBooks'"); + } + + public void displayReport(List results) { + + } + + private void printPrompt(String prompt) { + System.out.print(String.format("░░░░░░░░░░░░░░░░░░░░░░░░░░║%s║══> ", prompt)); + System.out.flush(); + } + + private void printInvalidInput() { + System.out.println("░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░"); + System.out.println("░░░░░░░░░░░░░░░░░░░░░░░░░░<<<⚠⃨Please enter valid characters⚠⃨>>>░░░░░░░░░░░░░░░░░░░░░░░░░░░"); + System.out.println("░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░"); + } + + private void clearScannerBuffer() { + while (scanner.hasNext()) { + System.out.println(scanner.next()); + System.out.println("here"); + } + } + + private int readIntInRange(String prompt, int min, int max) { + while (true) { + clearScannerBuffer(); + printPrompt(prompt); + try { + int input = scanner.nextInt(); + if (input >= min && input <= max) + return input; + else { + printInvalidInput(); + continue; + } + } catch (Exception e) { + printInvalidInput(); + continue; + } + } + } + + private String readString(String prompt) { + clearScannerBuffer(); + printPrompt(prompt); + // TODO: validiate input + return scanner.nextLine(); + } +} diff --git a/src/view/UserInterface.java b/src/view/UserInterface.java index 159c238..5b212c6 100644 --- a/src/view/UserInterface.java +++ b/src/view/UserInterface.java @@ -124,9 +124,9 @@ public void printReportsMenu(){ public void displayMainMenu() { while (true) { - printMainMenu();//Menu print - int mainmenuinput = readIntInRange("Enter a number", 0, 4); - switch (mainmenuinput) { + printMainMenu(); + int choice = readIntInRange("Enter a number", 0, 4); + switch (choice) { case 1: displayMemberMenu(); break; @@ -142,10 +142,7 @@ public void displayMainMenu() { case 0: return; default: - System.out.println("░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░"); - System.out.println("░░░░░░░░░░░░░░░░░░░░░░░░<<<⚠⃨Please select the correct option⚠⃨>>>░░░░░░░░░░░░░░░░░░░░░░░░░"); - System.out.println("░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░"); - break; + assert false: "UNREACHABLE"; } } } @@ -153,62 +150,42 @@ public void displayMainMenu() { public void displayMemberMenu() { while (true) { printManageUsersMenu(); - int membermenuinput = readIntInRange("Enter a number", 0, 5); - switch (membermenuinput) { - case 1: - System.out.print("░░░░░░░░░░░░░░░░░░░░░░░░░░║Enter The Student ID║══> "); - String ugstudentId = scanner.nextLine(); - System.out.print("░░░░░░░░░░░░░░░░░░░░░░░░░░║Enter The Student First Name║══> "); - String ugfirstName = scanner.nextLine(); - System.out.print("░░░░░░░░░░░░░░░░░░░░░░░░░░║Enter The Student LastName║══> "); - String uglastName = scanner.nextLine(); - System.out.print("░░░░░░░░░░░░░░░░░░░░░░░░░░║Enter The Student Major║══> "); - String ugmajor = scanner.nextLine(); - System.out.print("░░░░░░░░░░░░░░░░░░░░░░░░░░║Enter The Student Enrollment Year║══> "); - int ugenrollmentyear = 0; - try{ - ugenrollmentyear = scanner.nextInt(); - } - catch (Exception e){ - System.out.println("░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░"); - System.out.println("░░░░░░░░░░░░░░░░░░░░░░░░░░<<<⚠⃨Please enter valid character⚠⃨>>>░░░░░░░░░░░░░░░░░░░░░░░░░░░"); - System.out.println("░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░"); - } - libraryController.addUndergraduateStudent(ugstudentId, ugfirstName, uglastName, ugmajor, ugenrollmentyear); + int choice = readIntInRange("Enter a number", 0, 5); + switch (choice) { + case 1: { + String studentID = readString("Enter the Student ID"); + String firstName = readString("Enter the Student First Name"); + String lastName = readString("Enter the Student Last Name"); + String major = readString("Enter the Student Major"); + int enrollmentYear = readIntInRange("Enter the Student Enrollment Year", 1000, 3000); + libraryController.addUndergraduateStudent(studentID, firstName, lastName, major, enrollmentYear); break; - case 2: - System.out.print("░░░░░░░░░░░░░░░░░░░░░░░░░░║Enter The Student ID║══> "); - String gstudentId = scanner.nextLine(); - System.out.print("░░░░░░░░░░░░░░░░░░░░░░░░░░║Enter The Student First Name║══> "); - String gfirstName = scanner.nextLine(); - System.out.print("░░░░░░░░░░░░░░░░░░░░░░░░░░║Enter The Student LastName║══> "); - String glastName = scanner.nextLine(); - System.out.print("░░░░░░░░░░░░░░░░░░░░░░░░░░║Enter The Student Major║══> "); - String gmajor = scanner.nextLine(); - System.out.print("░░░░░░░░░░░░░░░░░░░░░░░░░░║Enter The Student Supervisor║══> "); - String gsupervisor = scanner.nextLine(); - System.out.print("░░░░░░░░░░░░░░░░░░░░░░░░░░║Enter The Student Thesis Title║══> "); - String gthesisTitle = scanner.nextLine(); - libraryController.addGraduateStudent(gstudentId, gfirstName, glastName, gmajor, gsupervisor, gthesisTitle); + } + case 2: { + String studentID = readString("Enter the Student ID"); + String firstName = readString("Enter the Student First Name"); + String lastName = readString("Enter the Student Last Name"); + String major = readString("Enter the Student Major"); + String supervisor = readString("Enter the Student Supervisor"); + String thesisTitle = readString("Enter the Student Thesis Title"); + libraryController.addGraduateStudent(studentID, firstName, lastName, major, supervisor, thesisTitle); break; + } case 3: libraryController.searchStudent(getStudentIdForSearch()); break; - case 4: - System.out.print("░░░░░░░░░░░░░░░░░░░░░░░░░░║Enter The Student ID║══> "); - String rstudentId = scanner.nextLine(); - libraryController.removeStudent(rstudentId); + case 4: { + String studentID = readString("Enter the Student ID"); + libraryController.removeStudent(studentID); break; + } case 5: libraryController.getAllStudents(); break; case 0: return; default: - System.out.println("░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░"); - System.out.println("░░░░░░░░░░░░░░░░░░░░░░░░<<<⚠⃨Please select the correct option⚠⃨>>>░░░░░░░░░░░░░░░░░░░░░░░░░"); - System.out.println("░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░"); - break; + assert false: "UNREACHABLE"; } } } @@ -216,49 +193,24 @@ public void displayMemberMenu() { public void displayBookMenu() { while (true) { printManageBooksMenu(); - int booksmenuinput = readIntInRange("Enter a number", 0, 4); - switch (booksmenuinput) { - case 1: - System.out.print("░░░░░░░░░░░░░░░░░░░░░░░░░░║Enter The Title║══> "); - String title = scanner.nextLine(); - System.out.print("░░░░░░░░░░░░░░░░░░░░░░░░░░║Enter The Student Author║══> "); - String author = scanner.nextLine(); - System.out.print("░░░░░░░░░░░░░░░░░░░░░░░░░░║Enter The Student ISBN║══> "); - String isbn1 = scanner.nextLine(); - System.out.print("░░░░░░░░░░░░░░░░░░░░░░░░░░║Enter The Student Publication Year║══> "); - int publicationYear = scanner.nextInt(); + int choice = readIntInRange("Enter a number", 0, 4); + switch (choice) { + case 1: { + String title = readString("Enter the title"); + String author = readString("Enter the author"); + String isbn = readString("Enter the ISBN"); + int publicationYear = readIntInRange("Enter the publication year", 1000, 3000); // TODO: print list of available categories and let the user choose one or define one - libraryController.addBook(title, author, isbn1, null, publicationYear); + libraryController.addBook(title, author, isbn, null, publicationYear); break; - case 2: - System.out.print("░░░░░░░░░░░░░░░░░░░░░░░░░░║Enter The Student ISBN║══> "); - String isbn2 = scanner.nextLine(); - libraryController.removeBook(isbn2); + } + case 2: { + String isbn = readString("Enter the ISBN"); + libraryController.removeBook(isbn); break; - case 3: - printSearchBookMenu(); - int searchbookmenu = 0; - try { - searchbookmenu = scanner.nextInt(); - } - catch(Exception e) { - System.out.println("░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░"); - System.out.println("░░░░░░░░░░░░░░░░░░░░░░░░░░<<<⚠⃨Please enter valid character⚠⃨>>>░░░░░░░░░░░░░░░░░░░░░░░░░░░"); - System.out.println("░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░"); - } - switch (searchbookmenu){ - case 1: - libraryController.searchBookByTitle(getBookTitleForSearch()); - break; - case 2: - String isbn3; - System.out.print("░░░░░░░░░░░░░░░░░░░░░░░░░░║Enter The Book ISBN║══> "); - isbn3 = scanner.nextLine(); - libraryController.searchBookByISBN(isbn3); - break; - case 0: - return; - } + } + case 3: + displaySearchBookMenu(); break; case 4: libraryController.getAvailableBooks(); @@ -266,38 +218,43 @@ public void displayBookMenu() { case 0: return; default: - System.out.println("░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░"); - System.out.println("░░░░░░░░░░░░░░░░░░░░░░░░<<<⚠⃨Please select the correct option⚠⃨>>>░░░░░░░░░░░░░░░░░░░░░░░░░"); - System.out.println("░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░"); - break; + assert false: "UNREACHABLE"; } } } + public void displaySearchBookMenu() { + printSearchBookMenu(); + int choice = readIntInRange("Enter a number", 0, 2); + switch (choice){ + case 1: + libraryController.searchBookByTitle(getBookTitleForSearch()); + break; + case 2: + String isbn = readString("Enter the book ISBN"); + libraryController.searchBookByISBN(isbn); + break; + case 0: + return; + } + + } + public void displayLoanMenu() { while (true) { printLoanMenu(); - int loanmenuinput = readIntInRange("Enter a number", 0, 2); - switch (loanmenuinput) { + int choice = readIntInRange("Enter a number", 0, 2); + switch (choice) { case 1: - Book isbn4; - System.out.print("░░░░░░░░░░░░░░░░░░░░░░░░░░║Enter The Book ISBN║══> "); - Student SID; - System.out.print("░░░░░░░░░░░░░░░░░░░░░░░░░░║Enter The Book Student ID║══> "); - //libraryController.borrowBook(isbn4, SID); + // TODO: implement check out books break; case 2: - Book isbn5; - //isbn5 = loanMenuInput.nextLine(); - //libraryController.returnBook(isbn5); + // TODO: implement return books break; case 0: return; default: - System.out.println("░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░"); - System.out.println("░░░░░░░░░░░░░░░░░░░░░░░░<<<⚠⃨Please select the correct option⚠⃨>>>░░░░░░░░░░░░░░░░░░░░░░░░░"); - System.out.println("░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░"); - break; + assert false: "UNREACHABLE"; } } } @@ -305,8 +262,8 @@ public void displayLoanMenu() { public void displayReportMenu() { while (true) { printReportsMenu(); - int reportsmenuinput = readIntInRange("Enter a number", 0, 2); - switch (reportsmenuinput) { + int choice = readIntInRange("Enter a number", 0, 2); + switch (choice) { case 1: break; case 2: @@ -314,18 +271,13 @@ public void displayReportMenu() { case 0: return; default: - System.out.println("░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░"); - System.out.println("░░░░░░░░░░░░░░░░░░░░░░░░<<<⚠⃨Please select the correct option⚠⃨>>>░░░░░░░░░░░░░░░░░░░░░░░░░"); - System.out.println("░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░"); - break; + assert false: "UNREACHABLE"; } } } public String getStudentIdForSearch() { - System.out.print("░░░░░░░░░░░░░░░░░░░░░░░░░░║Enter The Student ID║══> "); - String sstudentId = scanner.nextLine(); - return sstudentId; + return readString("Enter Student ID"); } public void getBookISBNForOperation() { @@ -333,10 +285,7 @@ public void getBookISBNForOperation() { } public String getBookTitleForSearch() { - String T; - System.out.print("░░░░░░░░░░░░░░░░░░░░░░░░░░║Enter The Book Title║══> "); - T = scanner.nextLine(); - return T; + return readString("Enter Book Title"); } public void getStudentIdForLoan() { @@ -381,19 +330,28 @@ public void displayReport(List results) { } + private void printPrompt(String prompt) { + System.out.print(String.format("░░░░░░░░░░░░░░░░░░░░░░░░░░║%s║══> ", prompt)); + System.out.flush(); + } + private void printInvalidInput() { System.out.println("░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░"); System.out.println("░░░░░░░░░░░░░░░░░░░░░░░░░░<<<⚠⃨Please enter valid characters⚠⃨>>>░░░░░░░░░░░░░░░░░░░░░░░░░░░"); System.out.println("░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░"); } + private void clearScannerBuffer() { + if (scanner.hasNextLine()) + scanner.nextLine(); + } + private int readIntInRange(String prompt, int min, int max) { while (true) { - System.out.print(String.format("░░░░░░░░░░░░░░░░░░░░░░░░░░║%s║══> ", prompt)); - System.out.flush(); - + printPrompt(prompt); try { int input = scanner.nextInt(); + clearScannerBuffer(); if (input >= min && input <= max) return input; else { @@ -402,8 +360,15 @@ private int readIntInRange(String prompt, int min, int max) { } } catch (Exception e) { printInvalidInput(); + clearScannerBuffer(); continue; } } } + + private String readString(String prompt) { + printPrompt(prompt); + // TODO: validiate input + return scanner.nextLine(); + } } From db44d95b05b5bc9b7de634ecbe6461da3ff1e20e Mon Sep 17 00:00:00 2001 From: Reza Date: Thu, 29 May 2025 14:02:56 +0330 Subject: [PATCH 04/15] Implement displayAllStudents --- src/model/GraduateStudent.java | 6 ++++++ src/model/UndergraduateStudent.java | 6 ++++++ src/view/UserInterface.java | 6 ++++-- 3 files changed, 16 insertions(+), 2 deletions(-) diff --git a/src/model/GraduateStudent.java b/src/model/GraduateStudent.java index c94f859..e9a1194 100644 --- a/src/model/GraduateStudent.java +++ b/src/model/GraduateStudent.java @@ -38,6 +38,12 @@ public void setThesisTitle(String thesisTitle) { this.thesisTitle = thesisTitle; } + @Override + public String toString() { + return String.format("%s %s\n\tID: %s\n\tMajor: %s\n\tSupervisor: %s\n\tThesis Title: %s\n", firstName, lastName, + studentId, major, supervisor, thesisTitle); + } + @Override public JSONObject serialize() { JSONDict result = new JSONDict(); diff --git a/src/model/UndergraduateStudent.java b/src/model/UndergraduateStudent.java index 4fa8990..29ab57f 100644 --- a/src/model/UndergraduateStudent.java +++ b/src/model/UndergraduateStudent.java @@ -27,6 +27,12 @@ public void setEnrollmentYear(int enrollmentYear) { this.enrollmentYear = enrollmentYear; } + @Override + public String toString() { + return String.format("%s %s\n\tID: %s\n\tMajor: %s\n\tEnrollment Year: %d\n", firstName, lastName, studentId, major, + enrollmentYear); + } + @Override public JSONObject serialize() { JSONDict result = new JSONDict(); diff --git a/src/view/UserInterface.java b/src/view/UserInterface.java index 5b212c6..d29dafc 100644 --- a/src/view/UserInterface.java +++ b/src/view/UserInterface.java @@ -180,7 +180,7 @@ public void displayMemberMenu() { break; } case 5: - libraryController.getAllStudents(); + displayAllStudents(libraryController.getAllStudents()); break; case 0: return; @@ -318,7 +318,9 @@ public void displayLoanDetails(Loan loan) { } public void displayAllStudents(List students) { - System.out.println(); + for (Student student : students) { + System.out.println(student); + } } public void displayAllBooks(List books) { From a18ea1cc0cf198ede00e4c193de0e724404a990e Mon Sep 17 00:00:00 2001 From: Reza Date: Thu, 29 May 2025 14:05:39 +0330 Subject: [PATCH 05/15] Fix: searchStudentByID in view --- src/view/UserInterface.java | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/src/view/UserInterface.java b/src/view/UserInterface.java index d29dafc..264b126 100644 --- a/src/view/UserInterface.java +++ b/src/view/UserInterface.java @@ -171,9 +171,14 @@ public void displayMemberMenu() { libraryController.addGraduateStudent(studentID, firstName, lastName, major, supervisor, thesisTitle); break; } - case 3: - libraryController.searchStudent(getStudentIdForSearch()); + case 3: { + Student student = libraryController.searchStudent(getStudentIdForSearch()); + if (student == null) + System.out.println("Student not found!"); + else + System.out.println(student); break; + } case 4: { String studentID = readString("Enter the Student ID"); libraryController.removeStudent(studentID); From e70fc6181e82e7403d874705b6017eaf51a7d99f Mon Sep 17 00:00:00 2001 From: Reza Date: Thu, 29 May 2025 14:14:29 +0330 Subject: [PATCH 06/15] Implement DisplayAllBooks() --- src/controller/LibraryController.java | 8 +++----- src/model/Book.java | 23 ++++++++++++++--------- src/view/UserInterface.java | 7 ++++--- 3 files changed, 21 insertions(+), 17 deletions(-) diff --git a/src/controller/LibraryController.java b/src/controller/LibraryController.java index a89e894..978239d 100644 --- a/src/controller/LibraryController.java +++ b/src/controller/LibraryController.java @@ -38,10 +38,9 @@ public List getAllStudents() { return library.getAllStudents(); } - - public void addBook(String title, String author, String isbn,Category category, int publicationYear) { - Book book=new Book(title, author, isbn,category, publicationYear, false); - library.addBook(book); + public void addBook(String title, String author, String isbn, Category category, int publicationYear) { + Book book = new Book(title, author, isbn, category, publicationYear, true); + library.addBook(book); } @@ -89,7 +88,6 @@ public void saveData() { fileDataModel.saveStudents(library.getAllStudents()); } - public void loadData() { library.setBooks(fileDataModel.loadBooks()); library.setCategories(fileDataModel.loadCategories()); diff --git a/src/model/Book.java b/src/model/Book.java index efe639a..8d817d5 100644 --- a/src/model/Book.java +++ b/src/model/Book.java @@ -13,15 +13,14 @@ public class Book implements JSONSerializable { private int publicationYear; private boolean isBorrowed; - - -//Constructor - public Book(String title,String author,String isbn,Category category,int publicationYear,boolean isBorrowed){ - this.title=title; - this.author=author; - this.category=category; - this.isbn=isbn;this.publicationYear=publicationYear; - this.isBorrowed=isBorrowed; + // Constructor + public Book(String title, String author, String isbn, Category category, int publicationYear, boolean isBorrowed) { + this.title = title; + this.author = author; + this.category = category; + this.isbn = isbn; + this.publicationYear = publicationYear; + this.isBorrowed = isBorrowed; } // Setter @@ -59,6 +58,12 @@ public boolean getIsBorrowed() { return this.isBorrowed; } + @Override + public String toString() { + return String.format("%s\n\tAuthor: %s\n\tCategory: %s\n\tISBN: %s\n\tPublication Year: %d\n\tAvailable: %s\n", + title, author, category, isbn, publicationYear, isBorrowed ? "Yes" : "No"); + } + @Override public JSONObject serialize() { JSONDict result = new JSONDict(); diff --git a/src/view/UserInterface.java b/src/view/UserInterface.java index 264b126..6b137f9 100644 --- a/src/view/UserInterface.java +++ b/src/view/UserInterface.java @@ -218,7 +218,7 @@ public void displayBookMenu() { displaySearchBookMenu(); break; case 4: - libraryController.getAvailableBooks(); + displayAllBooks(libraryController.getAvailableBooks()); break; case 0: return; @@ -329,8 +329,9 @@ public void displayAllStudents(List students) { } public void displayAllBooks(List books) { - // TODO: Implement method 'displayAllBooks'. - throw new UnsupportedOperationException("Unimplemented method 'displayAllBooks'"); + for (Book book : books) { + System.out.println(book); + } } public void displayReport(List results) { From e8bc88af6f810ab0a96f1f9a79ae3f585711e730 Mon Sep 17 00:00:00 2001 From: Reza Date: Thu, 29 May 2025 14:22:28 +0330 Subject: [PATCH 07/15] Implement searchBook in view and fix Book toString --- src/controller/LibraryController.java | 2 +- src/model/Book.java | 3 +-- src/view/UserInterface.java | 18 ++++++++++++++---- 3 files changed, 16 insertions(+), 7 deletions(-) diff --git a/src/controller/LibraryController.java b/src/controller/LibraryController.java index 978239d..0f3b8a3 100644 --- a/src/controller/LibraryController.java +++ b/src/controller/LibraryController.java @@ -39,7 +39,7 @@ public List getAllStudents() { } public void addBook(String title, String author, String isbn, Category category, int publicationYear) { - Book book = new Book(title, author, isbn, category, publicationYear, true); + Book book = new Book(title, author, isbn, category, publicationYear, false); library.addBook(book); } diff --git a/src/model/Book.java b/src/model/Book.java index 8d817d5..5467098 100644 --- a/src/model/Book.java +++ b/src/model/Book.java @@ -29,7 +29,6 @@ public void borrowBook() { } public void returnBook() { - this.isBorrowed = false; } @@ -61,7 +60,7 @@ public boolean getIsBorrowed() { @Override public String toString() { return String.format("%s\n\tAuthor: %s\n\tCategory: %s\n\tISBN: %s\n\tPublication Year: %d\n\tAvailable: %s\n", - title, author, category, isbn, publicationYear, isBorrowed ? "Yes" : "No"); + title, author, category, isbn, publicationYear, isBorrowed ? "No" : "Yes"); } @Override diff --git a/src/view/UserInterface.java b/src/view/UserInterface.java index 6b137f9..82a2774 100644 --- a/src/view/UserInterface.java +++ b/src/view/UserInterface.java @@ -232,13 +232,23 @@ public void displaySearchBookMenu() { printSearchBookMenu(); int choice = readIntInRange("Enter a number", 0, 2); switch (choice){ - case 1: - libraryController.searchBookByTitle(getBookTitleForSearch()); + case 1: { + Book book = libraryController.searchBookByTitle(getBookTitleForSearch()); + if (book == null) + System.out.println("Book not found!"); + else + System.out.println(book); break; - case 2: + } + case 2: { String isbn = readString("Enter the book ISBN"); - libraryController.searchBookByISBN(isbn); + Book book = libraryController.searchBookByISBN(isbn); + if (book == null) + System.out.println("Book not found!"); + else + System.out.println(book); break; + } case 0: return; } From 67467b7cc5463f72a50c9f36c79acc0d2f4da25e Mon Sep 17 00:00:00 2001 From: Reza Date: Thu, 29 May 2025 14:38:10 +0330 Subject: [PATCH 08/15] Implement reading and storing data --- .gitignore | 5 + 1q | 376 -------------------------- src/controller/LibraryController.java | 10 +- src/data/FileDataModel.java | 6 +- src/view/UserInterface.java | 46 +--- 5 files changed, 22 insertions(+), 421 deletions(-) delete mode 100644 1q diff --git a/.gitignore b/.gitignore index a6bd609..04d233c 100644 --- a/.gitignore +++ b/.gitignore @@ -13,3 +13,8 @@ out/ .vscode/ .code-workspace + +students.json +books.json +categories.json +loans.json diff --git a/1q b/1q deleted file mode 100644 index 65d0a24..0000000 --- a/1q +++ /dev/null @@ -1,376 +0,0 @@ -package view; - -import java.util.List; -import model.*; -import java.util.Scanner; -import controller.LibraryController; - -public class UserInterface { - private LibraryController libraryController; - private Scanner scanner; - - public UserInterface() { - libraryController = new LibraryController(); - scanner = new Scanner(System.in); - } - - public void printMainMenu() { - System.out.println( - " ██╗ ██╗ ██████╗██████╗ ██╗ ██╗██████╗ \n"+ - "██████╗██████╗██████╗ ██║ ██║██╔════╝██╔══██╗ ██║ ██║██╔══██╗ ██████╗██████╗██████╗\n"+ - "╚═════╝╚═════╝╚═════╝ ██║ ██║╚█████╗ ██████╦╝ ██║ ██║██████╦╝ ╚═════╝╚═════╝╚═════╝\n"+ - "██████╗██████╗██████╗ ██║ ██║ ╚═══██╗██╔══██╗ ██║ ██║██╔══██╗ ██████╗██████╗██████╗\n"+ - "╚═════╝╚═════╝╚═════╝ ║██████╔╝██████╔╝██████╦╝ ███████╗██║██████╦╝ ╚═════╝╚═════╝╚═════╝\n"+ - " ╚══════╝╚══════╝ ╚═════╝ ╚══════╝╚═╝╚═════╝ \n"+ - "███╗ ███╗ █████╗ ███╗ ██╗ █████╗ ██████╗ ███████╗███╗ ███╗███████╗███╗ ██╗████████╗\n"+ - "████╗ ████║██╔══██╗████╗ ██║██╔══██╗██╔════╝ ██╔════╝████╗ ████║██╔════╝████╗ ██║╚══██╔══╝\n"+ - "██╔████╔██║███████║██╔██╗██║███████║██║ ██╗ █████╗ ██╔████╔██║█████╗ ██╔██╗██║ ██║ \n"+ - "██║╚██╔╝██║██╔══██║██║╚████║██╔══██║██║ ╚██╗██╔══╝ ██║╚██╔╝██║██╔══╝ ██║╚████║ ██║ \n"+ - "██║ ╚═╝ ██║██║ ██║██║ ╚███║██║ ██║╚██████╔╝███████╗██║ ╚═╝ ██║███████╗██║ ╚███║ ██║ \n"+ - "╚═╝ ╚═╝╚═╝ ╚═╝╚═╝ ╚══╝╚═╝ ╚═╝ ╚═════╝ ╚══════╝╚═╝ ╚═╝╚══════╝╚═╝ ╚══╝ ╚═╝ \n"+ - "░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░(➊)══> Manage Users░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░\n"+ - "░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░(➋)══> Manage Books░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░\n"+ - "░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░(➌)══> Loans ░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░\n"+ - "░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░(➍)══> Reports ░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░\n"+ - "░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░(⓿)══> Exit ░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░"); - } - - public void printManageUsersMenu(){ - System.out.println( - " ███╗ ███╗ █████╗ ███╗ ██╗ █████╗ ██████╗ ███████╗ \n"+ - "██████╗██████╗██████╗ ████╗ ████║██╔══██╗████╗ ██║██╔══██╗██╔════╝ ██╔════╝ ██████╗██████╗██████╗\n"+ - "╚═════╝╚═════╝╚═════╝ ██╔████╔██║███████║██╔██╗██║███████║██║ ██╗ █████╗ ╚═════╝╚═════╝╚═════╝\n"+ - "██████╗██████╗██████╗ ██║╚██╔╝██║██╔══██║██║╚████║██╔══██║██║ ╚██╗██╔══╝ ██████╗██████╗██████╗\n"+ - "╚═════╝╚═════╝╚═════╝ ██║ ╚═╝ ██║██║ ██║██║ ╚███║██║ ██║╚██████╔╝███████╗ ╚═════╝╚═════╝╚═════╝\n"+ - " ╚═╝ ╚═╝╚═╝ ╚═╝╚═╝ ╚══╝╚═╝ ╚═╝ ╚═════╝ ╚══════╝ \n"+ - " ██╗ ██╗ ██████╗███████╗██████╗ ██████╗ \n"+ - "██████╗██████╗██████╗██████╗██║ ██║██╔════╝██╔════╝██╔══██╗██╔════╝██████╗██████╗██████╗██████╗\n"+ - "╚═════╝╚═════╝╚═════╝╚═════╝██║ ██║╚█████╗ █████╗ ██████╔╝╚█████╗ ╚═════╝╚═════╝╚═════╝╚═════╝\n"+ - "██████╗██████╗██████╗██████╗██║ ██║ ╚═══██╗██╔══╝ ██╔══██╗ ╚═══██╗██████╗██████╗██████╗██████╗\n"+ - "╚═════╝╚═════╝╚═════╝╚═════╝╚██████╔╝██████╔╝███████╗██║ ██║██████╔╝╚═════╝╚═════╝╚═════╝╚═════╝\n"+ - " ╚═════╝ ╚═════╝ ╚══════╝╚═╝ ╚═╝╚═════╝ \n"+ - "░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░(➊)══> Add Undergraduate Student░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░\n"+ - "░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░(➋)══> Add Graduate Student ░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░\n"+ - "░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░(➌)══> Search Student by ID ░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░\n"+ - "░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░(➍)══> Remove Student by ID ░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░\n"+ - "░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░(➎)══> List All Students ░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░\n"+ - "░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░(⓿)══> Back to Main Menu ░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░"); - } - - public void printManageBooksMenu() { - System.out.println( - " ███╗ ███╗ █████╗ ███╗ ██╗ █████╗ ██████╗ ███████╗ \n"+ - "██████╗██████╗██████╗ ████╗ ████║██╔══██╗████╗ ██║██╔══██╗██╔════╝ ██╔════╝ ██████╗██████╗██████╗\n"+ - "╚═════╝╚═════╝╚═════╝ ██╔████╔██║███████║██╔██╗██║███████║██║ ██╗ █████╗ ╚═════╝╚═════╝╚═════╝\n"+ - "██████╗██████╗██████╗ ██║╚██╔╝██║██╔══██║██║╚████║██╔══██║██║ ╚██╗██╔══╝ ██████╗██████╗██████╗\n"+ - "╚═════╝╚═════╝╚═════╝ ██║ ╚═╝ ██║██║ ██║██║ ╚███║██║ ██║╚██████╔╝███████╗ ╚═════╝╚═════╝╚═════╝\n"+ - " ╚═╝ ╚═╝╚═╝ ╚═╝╚═╝ ╚══╝╚═╝ ╚═╝ ╚═════╝ ╚══════╝ \n"+ - " ██████╗ █████╗ █████╗ ██╗ ██╗ ██████╗ \n"+ - "██████╗██████╗██████╗██████╗██╔══██╗██╔══██╗██╔══██╗██║ ██╔╝██╔════╝ ██████╗██████╗██████╗██████╗\n"+ - "╚═════╝╚═════╝╚═════╝╚═════╝██████╦╝██║ ██║██║ ██║█████═╝ ╚█████╗ ╚═════╝╚═════╝╚═════╝╚═════╝\n"+ - "██████╗██████╗██████╗██████╗██╔══██╗██║ ██║██║ ██║██╔═██╗ ╚═══██╗ ██████╗██████╗██████╗██████╗\n"+ - "╚═════╝╚═════╝╚═════╝╚═════╝██████╦╝╚█████╔╝╚█████╔╝██║ ╚██╗██████╔╝ ╚═════╝╚═════╝╚═════╝╚═════╝\n"+ - " ╚═════╝ ╚════╝ ╚════╝ ╚═╝ ╚═╝╚═════╝ \n"+ - "░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░(➊)══> Add Book ░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░\n"+ - "░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░(➋)══> Remove Book by ISBN ░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░\n"+ - "░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░(➌)══> Search Book ░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░\n"+ - "░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░(➍)══> List All Available Books ░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░\n"+ - "░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░(⓿)══> Back to Main Menu ░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░\n"); - } - - public void printSearchBookMenu(){ - System.out.println( - " ██████╗███████╗ █████╗ ██████╗ █████╗ ██╗ ██╗ \n"+ - "██████╗██████╗██████╗ ██╔════╝██╔════╝██╔══██╗██╔══██╗██╔══██╗██║ ██║ ██████╗██████╗██████╗\n"+ - "╚═════╝╚═════╝╚═════╝ ╚█████╗ █████╗ ███████║██████╔╝██║ ╚═╝███████║ ╚═════╝╚═════╝╚═════╝\n"+ - "██████╗██████╗██████╗ ╚═══██╗██╔══╝ ██╔══██║██╔══██╗██║ ██╗██╔══██║ ██████╗██████╗██████╗\n"+ - "╚═════╝╚═════╝╚═════╝ ██████╔╝███████╗██║ ██║██║ ██║╚█████╔╝██║ ██║ ╚═════╝╚═════╝╚═════╝\n"+ - " ╚═════╝ ╚══════╝╚═╝ ╚═╝╚═╝ ╚═╝ ╚════╝ ╚═╝ ╚═╝ \n"+ - " ██████╗ █████╗ █████╗ ██╗ ██╗ \n"+ - "██████╗██████╗██████╗██████╗ ██╔══██╗██╔══██╗██╔══██╗██║ ██╔╝ ██████╗██████╗██████╗██████╗\n"+ - "╚═════╝╚═════╝╚═════╝╚═════╝ ██████╦╝██║ ██║██║ ██║█████═╝ ╚═════╝╚═════╝╚═════╝╚═════╝\n"+ - "██████╗██████╗██████╗██████╗ ██╔══██╗██║ ██║██║ ██║██╔═██╗ ██████╗██████╗██████╗██████╗\n"+ - "╚═════╝╚═════╝╚═════╝╚═════╝ ██████╦╝╚█████╔╝╚█████╔╝██║ ╚██╗ ╚═════╝╚═════╝╚═════╝╚═════╝\n"+ - " ╚═════╝ ╚════╝ ╚════╝ ╚═╝ ╚═╝ \n"+ - "░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░(➊)══> Search by Title ░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░\n"+ - "░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░(➋)══> Search by ISBN ░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░\n"+ - "░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░(⓿)══> Back to Book Management ░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░\n" ); - } - - public void printLoanMenu(){ - System.out.println( - "██████╗██████╗██████╗██████╗ ██║ ██╗ ██╗██╗ ██╗████╗ ██╗ ██████╗██████╗██████╗██████╗\n"+ - "╚═════╝╚═════╝╚═════╝╚═════╝ ██║ ██║ ██║███████║██╔██╗██║ ╚═════╝╚═════╝╚═════╝╚═════╝\n"+ - " ██║ ██║ ██║██╔══██║██║╚████║ \n"+ - "██████╗██████╗██████╗██████╗ ███████╗╚█████╔╝██║ ██║██║ ╚███║ ██████╗██████╗██████╗██████╗\n"+ - "╚═════╝╚═════╝╚═════╝╚═════╝ ╚══════╝ ╚════╝ ╚═╝ ╚═╝╚═╝ ╚══╝ ╚═════╝╚═════╝╚═════╝╚═════╝\n"+ - "░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░(➊)══> Check Out Books ░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░\n"+ - "░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░(➋)══> Return Books ░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░\n"+ - "░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░(⓿)══> Back to Loan Menu ░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░\n"); - } - - public void printReportsMenu(){ - System.out.println( - " ██████╗ ███████╗██████╗ █████╗ ██████╗ ████████╗ ██████╗ \n"+ - "██████╗██████╗ ██╔══██╗██╔════╝██╔══██╗██╔══██╗██╔══██╗╚══██╔══╝██╔════╝ ██████╗██████╗\n"+ - "╚═════╝╚═════╝ ██████╔╝█████╗ ██████╔╝██║ ██║██████╔╝ ██║ ╚█████╗ ╚═════╝╚═════╝\n"+ - " ██╔══██╗██╔══╝ ██╔═══╝ ██║ ██║██╔══██╗ ██║ ╚═══██╗ \n"+ - "██████╗██████╗ ██║ ██║███████╗██║ ╚█████╔╝██║ ██║ ██║ ██████╔╝ ██████╗██████╗\n"+ - "╚═════╝╚═════╝ ╚═╝ ╚═╝╚══════╝╚═╝ ╚════╝ ╚═╝ ╚═╝ ╚═╝ ╚═════╝ ╚═════╝╚═════╝\n"+ - "░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░(➊)══> Check Out Books ░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░\n"+ - "░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░(➋)══> Return Books ░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░\n"+ - "░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░(⓿)══> Back to Loan Menu ░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░\n"); - } - - public void displayMainMenu() { - while (true) { - printMainMenu(); - int mainmenuinput = readIntInRange("Enter a number", 0, 4); - switch (mainmenuinput) { - case 1: - displayMemberMenu(); - break; - case 2: - displayBookMenu(); - break; - case 3: - displayLoanMenu(); - break; - case 4: - displayReportMenu(); - break; - case 0: - return; - default: - assert false: "UNREACHABLE"; - } - } - } - - public void displayMemberMenu() { - while (true) { - printManageUsersMenu(); - int choice = readIntInRange("Enter a number", 0, 5); - switch (choice) { - case 1: { - String studentID = readString("Enter the Student ID"); - String firstName = readString("Enter the Student First Name"); - String lastName = readString("Enter the Student Last Name"); - String major = readString("Enter the Student Major"); - int enrollmentYear = readIntInRange("Enter the Student Enrollment Year", 1000, 3000); - libraryController.addUndergraduateStudent(studentID, firstName, lastName, major, enrollmentYear); - break; - } - case 2: { - String studentID = readString("Enter the Student ID"); - String firstName = readString("Enter the Student First Name"); - String lastName = readString("Enter the Student Last Name"); - String major = readString("Enter the Student Major"); - String supervisor = readString("Enter the Student Supervisor"); - String thesisTitle = readString("Enter the Student Thesis Title"); - libraryController.addGraduateStudent(studentID, firstName, lastName, major, supervisor, thesisTitle); - break; - } - case 3: - libraryController.searchStudent(getStudentIdForSearch()); - break; - case 4: { - String studentID = readString("Enter the Student ID"); - libraryController.removeStudent(studentID); - break; - } - case 5: - libraryController.getAllStudents(); - break; - case 0: - return; - default: - assert false: "UNREACHABLE"; - } - } - } - - public void displayBookMenu() { - while (true) { - printManageBooksMenu(); - int choice = readIntInRange("Enter a number", 0, 4); - switch (choice) { - case 1: { - String title = readString("Enter the title"); - String author = readString("Enter the author"); - String isbn = readString("Enter the ISBN"); - int publicationYear = readIntInRange("Enter the publication year", 1000, 3000); - // TODO: print list of available categories and let the user choose one or define one - libraryController.addBook(title, author, isbn, null, publicationYear); - break; - } - case 2: { - String isbn = readString("Enter the ISBN"); - libraryController.removeBook(isbn); - break; - } - case 3: - displaySearchBookMenu(); - break; - case 4: - libraryController.getAvailableBooks(); - break; - case 0: - return; - default: - assert false: "UNREACHABLE"; - } - } - } - - public void displaySearchBookMenu() { - printSearchBookMenu(); - int choice = readIntInRange("Enter a number", 0, 2); - switch (choice){ - case 1: - libraryController.searchBookByTitle(getBookTitleForSearch()); - break; - case 2: - String isbn = readString("Enter the book ISBN"); - libraryController.searchBookByISBN(isbn); - break; - case 0: - return; - } - - } - - public void displayLoanMenu() { - while (true) { - printLoanMenu(); - int choice = readIntInRange("Enter a number", 0, 2); - switch (choice) { - case 1: - // TODO: implement check out books - break; - case 2: - // TODO: implement return books - break; - case 0: - return; - default: - assert false: "UNREACHABLE"; - } - } - } - - public void displayReportMenu() { - while (true) { - printReportsMenu(); - int choice = readIntInRange("Enter a number", 0, 2); - switch (choice) { - case 1: - break; - case 2: - break; - case 0: - return; - default: - assert false: "UNREACHABLE"; - } - } - } - - public String getStudentIdForSearch() { - return readString("Enter Student ID"); - } - - public void getBookISBNForOperation() { - - } - - public String getBookTitleForSearch() { - return readString("Enter Book Title"); - } - - public void getStudentIdForLoan() { - // TODO: Implement method 'getStudentIdForLoan'. - throw new UnsupportedOperationException("Unimplemented method 'getStudentIdForLoan'"); - } - - public void getBookISBNForLoan() { - // TODO: Implement method 'getBookISBNForLoan'. - throw new UnsupportedOperationException("Unimplemented method 'getBookISBNForLoan'"); - } - - public void displayStudentDetails(Student student) { - // TODO: Implement method 'displayStudentDetails'. - throw new UnsupportedOperationException("Unimplemented method 'displayStudentDetails'"); - } - - public void displayBookDetails(Book book) { - - } - - public void getBookDetailsForCreation() { - // TODO: Implement method 'getBookDetailsForCreation'. - throw new UnsupportedOperationException("Unimplemented method 'getBookDetailsForCreation'"); - } - - public void displayLoanDetails(Loan loan) { - // TODO: Implement method 'displayLoanDetails'. - throw new UnsupportedOperationException("Unimplemented method 'displayLoanDetails'"); - } - - public void displayAllStudents(List students) { - System.out.println(); - } - - public void displayAllBooks(List books) { - // TODO: Implement method 'displayAllBooks'. - throw new UnsupportedOperationException("Unimplemented method 'displayAllBooks'"); - } - - public void displayReport(List results) { - - } - - private void printPrompt(String prompt) { - System.out.print(String.format("░░░░░░░░░░░░░░░░░░░░░░░░░░║%s║══> ", prompt)); - System.out.flush(); - } - - private void printInvalidInput() { - System.out.println("░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░"); - System.out.println("░░░░░░░░░░░░░░░░░░░░░░░░░░<<<⚠⃨Please enter valid characters⚠⃨>>>░░░░░░░░░░░░░░░░░░░░░░░░░░░"); - System.out.println("░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░"); - } - - private void clearScannerBuffer() { - while (scanner.hasNext()) { - System.out.println(scanner.next()); - System.out.println("here"); - } - } - - private int readIntInRange(String prompt, int min, int max) { - while (true) { - clearScannerBuffer(); - printPrompt(prompt); - try { - int input = scanner.nextInt(); - if (input >= min && input <= max) - return input; - else { - printInvalidInput(); - continue; - } - } catch (Exception e) { - printInvalidInput(); - continue; - } - } - } - - private String readString(String prompt) { - clearScannerBuffer(); - printPrompt(prompt); - // TODO: validiate input - return scanner.nextLine(); - } -} diff --git a/src/controller/LibraryController.java b/src/controller/LibraryController.java index 0f3b8a3..e65c14b 100644 --- a/src/controller/LibraryController.java +++ b/src/controller/LibraryController.java @@ -1,6 +1,8 @@ package controller; import data.FileDataModel; +import jsonlib.JSONSerializableFactory; + import java.util.List; import model.*; @@ -12,6 +14,13 @@ public class LibraryController { public LibraryController() { library = new Library(); fileDataModel = new FileDataModel(); + JSONSerializableFactory.registerType("UndergraduateStudent", UndergraduateStudent.class); + JSONSerializableFactory.registerType("GraduateStudent", GraduateStudent.class); + JSONSerializableFactory.registerType("Book", Book.class); + JSONSerializableFactory.registerType("Category", Category.class); + JSONSerializableFactory.registerType("Loan", Loan.class); + + loadData(); } public void addUndergraduateStudent(String studentId, String firstName, String lastName, String major, @@ -93,7 +102,6 @@ public void loadData() { library.setCategories(fileDataModel.loadCategories()); library.setLoans(fileDataModel.loadLoans()); library.setStudents(fileDataModel.loadStudents()); - } } diff --git a/src/data/FileDataModel.java b/src/data/FileDataModel.java index 70cf146..07fca2a 100644 --- a/src/data/FileDataModel.java +++ b/src/data/FileDataModel.java @@ -66,7 +66,7 @@ public String readFromFile(String filePath) { try { content = String.join("\n", Files.readAllLines(Paths.get(filePath))); } catch (IOException e) { - e.printStackTrace(); + return null; } return content; @@ -74,6 +74,10 @@ public String readFromFile(String filePath) { public List loadFromFile(String filePath) { String content = readFromFile(filePath); + if (content == null) { + return new ArrayList<>(); + } + JSONParser parser = new JSONParser(content); JSONObject jsonObj = null; diff --git a/src/view/UserInterface.java b/src/view/UserInterface.java index 82a2774..801d7d3 100644 --- a/src/view/UserInterface.java +++ b/src/view/UserInterface.java @@ -140,6 +140,7 @@ public void displayMainMenu() { displayReportMenu(); break; case 0: + libraryController.saveData(); return; default: assert false: "UNREACHABLE"; @@ -172,7 +173,7 @@ public void displayMemberMenu() { break; } case 3: { - Student student = libraryController.searchStudent(getStudentIdForSearch()); + Student student = libraryController.searchStudent(readString("Enter Student ID")); if (student == null) System.out.println("Student not found!"); else @@ -233,7 +234,7 @@ public void displaySearchBookMenu() { int choice = readIntInRange("Enter a number", 0, 2); switch (choice){ case 1: { - Book book = libraryController.searchBookByTitle(getBookTitleForSearch()); + Book book = libraryController.searchBookByTitle(readString("Enter Book Title")); if (book == null) System.out.println("Book not found!"); else @@ -291,47 +292,6 @@ public void displayReportMenu() { } } - public String getStudentIdForSearch() { - return readString("Enter Student ID"); - } - - public void getBookISBNForOperation() { - - } - - public String getBookTitleForSearch() { - return readString("Enter Book Title"); - } - - public void getStudentIdForLoan() { - // TODO: Implement method 'getStudentIdForLoan'. - throw new UnsupportedOperationException("Unimplemented method 'getStudentIdForLoan'"); - } - - public void getBookISBNForLoan() { - // TODO: Implement method 'getBookISBNForLoan'. - throw new UnsupportedOperationException("Unimplemented method 'getBookISBNForLoan'"); - } - - public void displayStudentDetails(Student student) { - // TODO: Implement method 'displayStudentDetails'. - throw new UnsupportedOperationException("Unimplemented method 'displayStudentDetails'"); - } - - public void displayBookDetails(Book book) { - - } - - public void getBookDetailsForCreation() { - // TODO: Implement method 'getBookDetailsForCreation'. - throw new UnsupportedOperationException("Unimplemented method 'getBookDetailsForCreation'"); - } - - public void displayLoanDetails(Loan loan) { - // TODO: Implement method 'displayLoanDetails'. - throw new UnsupportedOperationException("Unimplemented method 'displayLoanDetails'"); - } - public void displayAllStudents(List students) { for (Student student : students) { System.out.println(student); From 4d3d7b1d092e02089d40374ce01c9b7972049fc7 Mon Sep 17 00:00:00 2001 From: Reza Date: Fri, 30 May 2025 10:56:37 +0330 Subject: [PATCH 09/15] Implement readSelection & fix bugs - let the user choose a category when adding a book. - all models now extend BaseModel and should implement getDisplayName() which acts like toString but returns a compact one-line string version of the model. - fix bug in Category serialization. --- src/controller/LibraryController.java | 4 ++++ src/model/BaseModel.java | 7 +++++++ src/model/Book.java | 7 ++++++- src/model/Category.java | 15 ++++++++++++--- src/model/Loan.java | 8 ++++++-- src/model/Student.java | 8 +++++--- src/view/UserInterface.java | 15 +++++++++++++-- 7 files changed, 53 insertions(+), 11 deletions(-) create mode 100644 src/model/BaseModel.java diff --git a/src/controller/LibraryController.java b/src/controller/LibraryController.java index e65c14b..4dad8ab 100644 --- a/src/controller/LibraryController.java +++ b/src/controller/LibraryController.java @@ -90,6 +90,10 @@ public List getStudentsByMajor(String major) { return library.getStudentsByMajor(major); } + public List getCategories() { + return library.getCategories(); + } + public void saveData() { fileDataModel.saveBooks(library.getBooks()); fileDataModel.saveCategories(library.getCategories()); diff --git a/src/model/BaseModel.java b/src/model/BaseModel.java new file mode 100644 index 0000000..029b7ae --- /dev/null +++ b/src/model/BaseModel.java @@ -0,0 +1,7 @@ +package model; + +import jsonlib.JSONSerializable; + +public abstract class BaseModel implements JSONSerializable { + public abstract String getDisplayName(); +} diff --git a/src/model/Book.java b/src/model/Book.java index 5467098..e552340 100644 --- a/src/model/Book.java +++ b/src/model/Book.java @@ -5,7 +5,7 @@ import jsonlib.types.JSONObject; import jsonlib.types.JSONDict; -public class Book implements JSONSerializable { +public class Book extends BaseModel { private String title; private String author; private String isbn; @@ -63,6 +63,11 @@ public String toString() { title, author, category, isbn, publicationYear, isBorrowed ? "No" : "Yes"); } + @Override + public String getDisplayName() { + return String.format("%s by %s (%s)", title, author, isbn); + } + @Override public JSONObject serialize() { JSONDict result = new JSONDict(); diff --git a/src/model/Category.java b/src/model/Category.java index 3640c68..ffbb41c 100644 --- a/src/model/Category.java +++ b/src/model/Category.java @@ -1,10 +1,9 @@ package model; -import jsonlib.JSONSerializable; import jsonlib.types.JSONObject; import jsonlib.types.JSONDict; -public class Category implements JSONSerializable { +public class Category extends BaseModel { private String name; private String description; @@ -23,11 +22,21 @@ public String getDescription() { return this.description; } + @Override + public String toString() { + return String.format("%s (%s)", name, description); + } + + @Override + public String getDisplayName() { + return name; + } + @Override public JSONObject serialize() { JSONDict result = new JSONDict(); - result.put("class", JSONObject.fromString("Book")); + result.put("class", JSONObject.fromString("Category")); result.put("name", JSONObject.fromString(name)); result.put("description", JSONObject.fromString(description)); diff --git a/src/model/Loan.java b/src/model/Loan.java index e654c3d..3b8ec47 100644 --- a/src/model/Loan.java +++ b/src/model/Loan.java @@ -8,7 +8,7 @@ import jsonlib.types.JSONDict; import jsonlib.types.JSONNumber; -public class Loan implements JSONSerializable { +public class Loan extends BaseModel { private Book book; private Student student; private Date loanDate; @@ -55,6 +55,11 @@ public Date getDueDate() { return this.dueDate; } + @Override + public String getDisplayName() { + return String.format("%s borrowed %s", student.getDisplayName(), book.getDisplayName()); + } + @Override public JSONObject serialize() { JSONDict result = new JSONDict(); @@ -77,5 +82,4 @@ public static Loan deserialize(JSONDict json) { return new Loan(book, student, new Date(loanDateValue.getValue()), new Date(dueDateValue.getValue())); } - } diff --git a/src/model/Student.java b/src/model/Student.java index 6a2e2ad..d35a7a7 100644 --- a/src/model/Student.java +++ b/src/model/Student.java @@ -1,8 +1,6 @@ package model; -import jsonlib.JSONSerializable; - -public abstract class Student implements JSONSerializable { +public abstract class Student extends BaseModel { protected String studentId; protected String firstName; protected String lastName; @@ -51,4 +49,8 @@ public String getMajor() { return this.major; } + @Override + public String getDisplayName() { + return String.format("%s %s (%s)", firstName, lastName, studentId); + } } diff --git a/src/view/UserInterface.java b/src/view/UserInterface.java index 801d7d3..6b1624f 100644 --- a/src/view/UserInterface.java +++ b/src/view/UserInterface.java @@ -206,8 +206,8 @@ public void displayBookMenu() { String author = readString("Enter the author"); String isbn = readString("Enter the ISBN"); int publicationYear = readIntInRange("Enter the publication year", 1000, 3000); - // TODO: print list of available categories and let the user choose one or define one - libraryController.addBook(title, author, isbn, null, publicationYear); + Category category = readSelection("Select a category", libraryController.getCategories()); + libraryController.addBook(title, author, isbn, category, publicationYear); break; } case 2: { @@ -348,5 +348,16 @@ private String readString(String prompt) { printPrompt(prompt); // TODO: validiate input return scanner.nextLine(); + } + + private T readSelection(String prompt, List options) { + if (options == null || options.size() == 0) + return null; + + for (int i = 0; i < options.size(); i++) { + System.out.println(String.format("%d. %s", i, options.get(i).getDisplayName())); + } + + return options.get(readIntInRange(prompt, 0, options.size()-1)); } } From f962351c0acabe227fe215c6bd73553643bd610e Mon Sep 17 00:00:00 2001 From: Reza Date: Fri, 30 May 2025 11:10:27 +0330 Subject: [PATCH 10/15] Implement Borrowing and Returning books --- src/view/UserInterface.java | 34 ++++++++++++++++++++++++++++++---- 1 file changed, 30 insertions(+), 4 deletions(-) diff --git a/src/view/UserInterface.java b/src/view/UserInterface.java index 6b1624f..609da5c 100644 --- a/src/view/UserInterface.java +++ b/src/view/UserInterface.java @@ -261,12 +261,38 @@ public void displayLoanMenu() { printLoanMenu(); int choice = readIntInRange("Enter a number", 0, 2); switch (choice) { - case 1: - // TODO: implement check out books + case 1: { + String studentID = readString("Enter you Student ID"); + Student student = libraryController.searchStudent(studentID); + if (student == null) { + System.out.println("Student not found!"); + continue; + } + List availableBooks = libraryController.getAvailableBooks(); + if (availableBooks == null || availableBooks.size() == 0) { + System.out.println("No books available right now!"); + continue; + } + + Book selectedBook = readSelection("Which book do you want to borrow?", availableBooks); + libraryController.borrowBook(selectedBook, student); break; - case 2: - // TODO: implement return books + } + case 2: { + String studentID = readString("Enter you Student ID"); + if (libraryController.searchStudent(studentID) == null) { + System.out.println("Student not found!"); + continue; + } + List books = libraryController.getBorrowedBooksByStudent(studentID); + if (books == null || books.size() == 0) { + System.out.println("You haven't borrowed any book!"); + continue; + } + Book selectedBook = readSelection("Which book do you want to return?", books); + libraryController.returnBook(selectedBook); break; + } case 0: return; default: From 40b9701445f525b4ecfe6bf7595943d8be22b390 Mon Sep 17 00:00:00 2001 From: Reza Date: Fri, 30 May 2025 11:26:29 +0330 Subject: [PATCH 11/15] Implement Reports menu --- src/controller/LibraryController.java | 4 +- src/model/Library.java | 4 +- src/view/UserInterface.java | 56 ++++++++++++++++++++++----- 3 files changed, 51 insertions(+), 13 deletions(-) diff --git a/src/controller/LibraryController.java b/src/controller/LibraryController.java index 4dad8ab..eab6010 100644 --- a/src/controller/LibraryController.java +++ b/src/controller/LibraryController.java @@ -82,8 +82,8 @@ public List getBorrowedBooksByStudent(String studentId) { return library.getBorrowedBooksByStudent(studentId); } - public List getBooksByCategory(String categoryName) { - return library.getBooksByCategory(categoryName); + public List getBooksByCategory(Category category) { + return library.getBooksByCategory(category); } public List getStudentsByMajor(String major) { diff --git a/src/model/Library.java b/src/model/Library.java index 93a090c..61dc379 100644 --- a/src/model/Library.java +++ b/src/model/Library.java @@ -149,10 +149,10 @@ public List getCategories() { return this.categories; } - public List getBooksByCategory(String categoryName) { + public List getBooksByCategory(Category category) { List booksByCategory=new ArrayList<>(); for(Book book : booksByCategory){ - if(book.getCategory().getName().equals(categoryName)){ + if(book.getCategory().getName().equals(category.getName())){ booksByCategory.add(book); } } diff --git a/src/view/UserInterface.java b/src/view/UserInterface.java index 609da5c..ecc0fcd 100644 --- a/src/view/UserInterface.java +++ b/src/view/UserInterface.java @@ -117,9 +117,10 @@ public void printReportsMenu(){ " ██╔══██╗██╔══╝ ██╔═══╝ ██║ ██║██╔══██╗ ██║ ╚═══██╗ \n"+ "██████╗██████╗ ██║ ██║███████╗██║ ╚█████╔╝██║ ██║ ██║ ██████╔╝ ██████╗██████╗\n"+ "╚═════╝╚═════╝ ╚═╝ ╚═╝╚══════╝╚═╝ ╚════╝ ╚═╝ ╚═╝ ╚═╝ ╚═════╝ ╚═════╝╚═════╝\n"+ - "░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░(➊)══> Check Out Books ░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░\n"+ - "░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░(➋)══> Return Books ░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░\n"+ - "░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░(⓿)══> Back to Loan Menu ░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░\n"); + "░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░(➊)══> List Books by Category ░░░░░░░░░░░░░░░░░░░░░░░░░░\n"+ + "░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░(➋)══> List Books Borrowed by a Student ░░░░░░░░░░░░░░░░░░░░░░░░░░\n"+ + "░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░(⓿)══> List Students by Major ░░░░░░░░░░░░░░░░░░░░░░░░░░\n"+ + "░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░(⓿)══> Back to Main Menu ░░░░░░░░░░░░░░░░░░░░░░░░░░\n"); } public void displayMainMenu() { @@ -279,7 +280,7 @@ public void displayLoanMenu() { break; } case 2: { - String studentID = readString("Enter you Student ID"); + String studentID = readString("Enter your Student ID"); if (libraryController.searchStudent(studentID) == null) { System.out.println("Student not found!"); continue; @@ -304,12 +305,48 @@ public void displayLoanMenu() { public void displayReportMenu() { while (true) { printReportsMenu(); - int choice = readIntInRange("Enter a number", 0, 2); + int choice = readIntInRange("Enter a number", 0, 3); switch (choice) { - case 1: + case 1: { + List categories = libraryController.getCategories(); + if (categories == null || categories.size() == 0) { + System.out.println("No categories available rightt now!"); + continue; + } + Category category = readSelection("Choose a category", categories); + List books = libraryController.getBooksByCategory(category); + if (books == null || books.size() == 0) { + System.out.println("No books available in this category"); + continue; + } + displayReport(books); break; - case 2: + } + case 2: { + String studentID = readString("Enter your Student ID"); + if (libraryController.searchStudent(studentID) == null) { + System.out.println("Student not found!"); + continue; + } + List books = libraryController.getBorrowedBooksByStudent(studentID); + if (books == null || books.size() == 0) { + System.out.println("You haven't borrowed any book!"); + continue; + } + displayReport(books); break; + } + case 3: { + String major = readString("Enter a major"); + List students = libraryController.getStudentsByMajor(major); + if (students == null || students.size() == 0) { + System.out.println("No students found in this major"); + continue; + } + + displayReport(students); + break; + } case 0: return; default: @@ -330,8 +367,9 @@ public void displayAllBooks(List books) { } } - public void displayReport(List results) { - + public void displayReport(List results) { + for (int i = 0; i < results.size(); i++) + System.out.printf("%d. %s\n", i, results.get(i).getDisplayName()); } private void printPrompt(String prompt) { From 3f0c4959d884b1b23bd2a52013faf9672ffd4539 Mon Sep 17 00:00:00 2001 From: Reza Date: Fri, 30 May 2025 11:32:37 +0330 Subject: [PATCH 12/15] reformat code --- src/view/UserInterface.java | 739 ++++++++++++++++++------------------ 1 file changed, 372 insertions(+), 367 deletions(-) diff --git a/src/view/UserInterface.java b/src/view/UserInterface.java index ecc0fcd..e64fd2c 100644 --- a/src/view/UserInterface.java +++ b/src/view/UserInterface.java @@ -6,422 +6,427 @@ import controller.LibraryController; public class UserInterface { - private LibraryController libraryController; - private Scanner scanner; + private LibraryController libraryController; + private Scanner scanner; - public UserInterface() { - libraryController = new LibraryController(); - scanner = new Scanner(System.in); - } + public UserInterface() { + libraryController = new LibraryController(); + scanner = new Scanner(System.in); + } - public void printMainMenu() { - System.out.println( - " ██╗ ██╗ ██████╗██████╗ ██╗ ██╗██████╗ \n"+ - "██████╗██████╗██████╗ ██║ ██║██╔════╝██╔══██╗ ██║ ██║██╔══██╗ ██████╗██████╗██████╗\n"+ - "╚═════╝╚═════╝╚═════╝ ██║ ██║╚█████╗ ██████╦╝ ██║ ██║██████╦╝ ╚═════╝╚═════╝╚═════╝\n"+ - "██████╗██████╗██████╗ ██║ ██║ ╚═══██╗██╔══██╗ ██║ ██║██╔══██╗ ██████╗██████╗██████╗\n"+ - "╚═════╝╚═════╝╚═════╝ ║██████╔╝██████╔╝██████╦╝ ███████╗██║██████╦╝ ╚═════╝╚═════╝╚═════╝\n"+ - " ╚══════╝╚══════╝ ╚═════╝ ╚══════╝╚═╝╚═════╝ \n"+ - "███╗ ███╗ █████╗ ███╗ ██╗ █████╗ ██████╗ ███████╗███╗ ███╗███████╗███╗ ██╗████████╗\n"+ - "████╗ ████║██╔══██╗████╗ ██║██╔══██╗██╔════╝ ██╔════╝████╗ ████║██╔════╝████╗ ██║╚══██╔══╝\n"+ - "██╔████╔██║███████║██╔██╗██║███████║██║ ██╗ █████╗ ██╔████╔██║█████╗ ██╔██╗██║ ██║ \n"+ - "██║╚██╔╝██║██╔══██║██║╚████║██╔══██║██║ ╚██╗██╔══╝ ██║╚██╔╝██║██╔══╝ ██║╚████║ ██║ \n"+ - "██║ ╚═╝ ██║██║ ██║██║ ╚███║██║ ██║╚██████╔╝███████╗██║ ╚═╝ ██║███████╗██║ ╚███║ ██║ \n"+ - "╚═╝ ╚═╝╚═╝ ╚═╝╚═╝ ╚══╝╚═╝ ╚═╝ ╚═════╝ ╚══════╝╚═╝ ╚═╝╚══════╝╚═╝ ╚══╝ ╚═╝ \n"+ - "░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░(➊)══> Manage Users░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░\n"+ - "░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░(➋)══> Manage Books░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░\n"+ - "░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░(➌)══> Loans ░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░\n"+ - "░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░(➍)══> Reports ░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░\n"+ - "░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░(⓿)══> Exit ░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░"); - } + public void printMainMenu() { + System.out.println( + " ██╗ ██╗ ██████╗██████╗ ██╗ ██╗██████╗ \n" + + "██████╗██████╗██████╗ ██║ ██║██╔════╝██╔══██╗ ██║ ██║██╔══██╗ ██████╗██████╗██████╗\n" + + "╚═════╝╚═════╝╚═════╝ ██║ ██║╚█████╗ ██████╦╝ ██║ ██║██████╦╝ ╚═════╝╚═════╝╚═════╝\n" + + "██████╗██████╗██████╗ ██║ ██║ ╚═══██╗██╔══██╗ ██║ ██║██╔══██╗ ██████╗██████╗██████╗\n" + + "╚═════╝╚═════╝╚═════╝ ║██████╔╝██████╔╝██████╦╝ ███████╗██║██████╦╝ ╚═════╝╚═════╝╚═════╝\n" + + " ╚══════╝╚══════╝ ╚═════╝ ╚══════╝╚═╝╚═════╝ \n" + + "███╗ ███╗ █████╗ ███╗ ██╗ █████╗ ██████╗ ███████╗███╗ ███╗███████╗███╗ ██╗████████╗\n" + + "████╗ ████║██╔══██╗████╗ ██║██╔══██╗██╔════╝ ██╔════╝████╗ ████║██╔════╝████╗ ██║╚══██╔══╝\n" + + "██╔████╔██║███████║██╔██╗██║███████║██║ ██╗ █████╗ ██╔████╔██║█████╗ ██╔██╗██║ ██║ \n" + + "██║╚██╔╝██║██╔══██║██║╚████║██╔══██║██║ ╚██╗██╔══╝ ██║╚██╔╝██║██╔══╝ ██║╚████║ ██║ \n" + + "██║ ╚═╝ ██║██║ ██║██║ ╚███║██║ ██║╚██████╔╝███████╗██║ ╚═╝ ██║███████╗██║ ╚███║ ██║ \n" + + "╚═╝ ╚═╝╚═╝ ╚═╝╚═╝ ╚══╝╚═╝ ╚═╝ ╚═════╝ ╚══════╝╚═╝ ╚═╝╚══════╝╚═╝ ╚══╝ ╚═╝ \n" + + "░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░(1)══> Manage Users░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░\n" + + "░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░(2)══> Manage Books░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░\n" + + "░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░(3)══> Loans ░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░\n" + + "░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░(4)══> Reports ░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░\n" + + "░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░(0)══> Exit ░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░\n" + + "░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░"); + } - public void printManageUsersMenu(){ - System.out.println( - " ███╗ ███╗ █████╗ ███╗ ██╗ █████╗ ██████╗ ███████╗ \n"+ - "██████╗██████╗██████╗ ████╗ ████║██╔══██╗████╗ ██║██╔══██╗██╔════╝ ██╔════╝ ██████╗██████╗██████╗\n"+ - "╚═════╝╚═════╝╚═════╝ ██╔████╔██║███████║██╔██╗██║███████║██║ ██╗ █████╗ ╚═════╝╚═════╝╚═════╝\n"+ - "██████╗██████╗██████╗ ██║╚██╔╝██║██╔══██║██║╚████║██╔══██║██║ ╚██╗██╔══╝ ██████╗██████╗██████╗\n"+ - "╚═════╝╚═════╝╚═════╝ ██║ ╚═╝ ██║██║ ██║██║ ╚███║██║ ██║╚██████╔╝███████╗ ╚═════╝╚═════╝╚═════╝\n"+ - " ╚═╝ ╚═╝╚═╝ ╚═╝╚═╝ ╚══╝╚═╝ ╚═╝ ╚═════╝ ╚══════╝ \n"+ - " ██╗ ██╗ ██████╗███████╗██████╗ ██████╗ \n"+ - "██████╗██████╗██████╗██████╗██║ ██║██╔════╝██╔════╝██╔══██╗██╔════╝██████╗██████╗██████╗██████╗\n"+ - "╚═════╝╚═════╝╚═════╝╚═════╝██║ ██║╚█████╗ █████╗ ██████╔╝╚█████╗ ╚═════╝╚═════╝╚═════╝╚═════╝\n"+ - "██████╗██████╗██████╗██████╗██║ ██║ ╚═══██╗██╔══╝ ██╔══██╗ ╚═══██╗██████╗██████╗██████╗██████╗\n"+ - "╚═════╝╚═════╝╚═════╝╚═════╝╚██████╔╝██████╔╝███████╗██║ ██║██████╔╝╚═════╝╚═════╝╚═════╝╚═════╝\n"+ - " ╚═════╝ ╚═════╝ ╚══════╝╚═╝ ╚═╝╚═════╝ \n"+ - "░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░(➊)══> Add Undergraduate Student░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░\n"+ - "░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░(➋)══> Add Graduate Student ░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░\n"+ - "░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░(➌)══> Search Student by ID ░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░\n"+ - "░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░(➍)══> Remove Student by ID ░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░\n"+ - "░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░(➎)══> List All Students ░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░\n"+ - "░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░(⓿)══> Back to Main Menu ░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░"); - } + public void printManageUsersMenu() { + System.out.println( + " ███╗ ███╗ █████╗ ███╗ ██╗ █████╗ ██████╗ ███████╗ \n" + + "██████╗██████╗██████╗ ████╗ ████║██╔══██╗████╗ ██║██╔══██╗██╔════╝ ██╔════╝ ██████╗██████╗██████╗\n" + + "╚═════╝╚═════╝╚═════╝ ██╔████╔██║███████║██╔██╗██║███████║██║ ██╗ █████╗ ╚═════╝╚═════╝╚═════╝\n" + + "██████╗██████╗██████╗ ██║╚██╔╝██║██╔══██║██║╚████║██╔══██║██║ ╚██╗██╔══╝ ██████╗██████╗██████╗\n" + + "╚═════╝╚═════╝╚═════╝ ██║ ╚═╝ ██║██║ ██║██║ ╚███║██║ ██║╚██████╔╝███████╗ ╚═════╝╚═════╝╚═════╝\n" + + " ╚═╝ ╚═╝╚═╝ ╚═╝╚═╝ ╚══╝╚═╝ ╚═╝ ╚═════╝ ╚══════╝ \n" + + " ██╗ ██╗ ██████╗███████╗██████╗ ██████╗ \n" + + "██████╗██████╗██████╗██████╗██║ ██║██╔════╝██╔════╝██╔══██╗██╔════╝██████╗██████╗██████╗██████╗\n" + + "╚═════╝╚═════╝╚═════╝╚═════╝██║ ██║╚█████╗ █████╗ ██████╔╝╚█████╗ ╚═════╝╚═════╝╚═════╝╚═════╝\n" + + "██████╗██████╗██████╗██████╗██║ ██║ ╚═══██╗██╔══╝ ██╔══██╗ ╚═══██╗██████╗██████╗██████╗██████╗\n" + + "╚═════╝╚═════╝╚═════╝╚═════╝╚██████╔╝██████╔╝███████╗██║ ██║██████╔╝╚═════╝╚═════╝╚═════╝╚═════╝\n" + + " ╚═════╝ ╚═════╝ ╚══════╝╚═╝ ╚═╝╚═════╝ \n" + + "░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░(1)══> Add Undergraduate Student ░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░\n" + + "░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░(2)══> Add Graduate Student ░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░\n" + + "░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░(3)══> Search Student by ID ░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░\n" + + "░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░(4)══> Remove Student by ID ░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░\n" + + "░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░(5)══> List All Students ░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░\n" + + "░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░(0)══> Back to Main Menu ░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░\n" + + "░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░"); + } - public void printManageBooksMenu() { - System.out.println( - " ███╗ ███╗ █████╗ ███╗ ██╗ █████╗ ██████╗ ███████╗ \n"+ - "██████╗██████╗██████╗ ████╗ ████║██╔══██╗████╗ ██║██╔══██╗██╔════╝ ██╔════╝ ██████╗██████╗██████╗\n"+ - "╚═════╝╚═════╝╚═════╝ ██╔████╔██║███████║██╔██╗██║███████║██║ ██╗ █████╗ ╚═════╝╚═════╝╚═════╝\n"+ - "██████╗██████╗██████╗ ██║╚██╔╝██║██╔══██║██║╚████║██╔══██║██║ ╚██╗██╔══╝ ██████╗██████╗██████╗\n"+ - "╚═════╝╚═════╝╚═════╝ ██║ ╚═╝ ██║██║ ██║██║ ╚███║██║ ██║╚██████╔╝███████╗ ╚═════╝╚═════╝╚═════╝\n"+ - " ╚═╝ ╚═╝╚═╝ ╚═╝╚═╝ ╚══╝╚═╝ ╚═╝ ╚═════╝ ╚══════╝ \n"+ - " ██████╗ █████╗ █████╗ ██╗ ██╗ ██████╗ \n"+ - "██████╗██████╗██████╗██████╗██╔══██╗██╔══██╗██╔══██╗██║ ██╔╝██╔════╝ ██████╗██████╗██████╗██████╗\n"+ - "╚═════╝╚═════╝╚═════╝╚═════╝██████╦╝██║ ██║██║ ██║█████═╝ ╚█████╗ ╚═════╝╚═════╝╚═════╝╚═════╝\n"+ - "██████╗██████╗██████╗██████╗██╔══██╗██║ ██║██║ ██║██╔═██╗ ╚═══██╗ ██████╗██████╗██████╗██████╗\n"+ - "╚═════╝╚═════╝╚═════╝╚═════╝██████╦╝╚█████╔╝╚█████╔╝██║ ╚██╗██████╔╝ ╚═════╝╚═════╝╚═════╝╚═════╝\n"+ - " ╚═════╝ ╚════╝ ╚════╝ ╚═╝ ╚═╝╚═════╝ \n"+ - "░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░(➊)══> Add Book ░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░\n"+ - "░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░(➋)══> Remove Book by ISBN ░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░\n"+ - "░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░(➌)══> Search Book ░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░\n"+ - "░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░(➍)══> List All Available Books ░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░\n"+ - "░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░(⓿)══> Back to Main Menu ░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░\n"); - } + public void printManageBooksMenu() { + System.out.println( + " ███╗ ███╗ █████╗ ███╗ ██╗ █████╗ ██████╗ ███████╗ \n" + + "██████╗██████╗██████╗ ████╗ ████║██╔══██╗████╗ ██║██╔══██╗██╔════╝ ██╔════╝ ██████╗██████╗██████╗\n" + + "╚═════╝╚═════╝╚═════╝ ██╔████╔██║███████║██╔██╗██║███████║██║ ██╗ █████╗ ╚═════╝╚═════╝╚═════╝\n" + + "██████╗██████╗██████╗ ██║╚██╔╝██║██╔══██║██║╚████║██╔══██║██║ ╚██╗██╔══╝ ██████╗██████╗██████╗\n" + + "╚═════╝╚═════╝╚═════╝ ██║ ╚═╝ ██║██║ ██║██║ ╚███║██║ ██║╚██████╔╝███████╗ ╚═════╝╚═════╝╚═════╝\n" + + " ╚═╝ ╚═╝╚═╝ ╚═╝╚═╝ ╚══╝╚═╝ ╚═╝ ╚═════╝ ╚══════╝ \n" + + " ██████╗ █████╗ █████╗ ██╗ ██╗ ██████╗ \n" + + "██████╗██████╗██████╗██████╗██╔══██╗██╔══██╗██╔══██╗██║ ██╔╝██╔════╝ ██████╗██████╗██████╗██████╗\n" + + "╚═════╝╚═════╝╚═════╝╚═════╝██████╦╝██║ ██║██║ ██║█████═╝ ╚█████╗ ╚═════╝╚═════╝╚═════╝╚═════╝\n" + + "██████╗██████╗██████╗██████╗██╔══██╗██║ ██║██║ ██║██╔═██╗ ╚═══██╗ ██████╗██████╗██████╗██████╗\n" + + "╚═════╝╚═════╝╚═════╝╚═════╝██████╦╝╚█████╔╝╚█████╔╝██║ ╚██╗██████╔╝ ╚═════╝╚═════╝╚═════╝╚═════╝\n" + + " ╚═════╝ ╚════╝ ╚════╝ ╚═╝ ╚═╝╚═════╝ \n" + + "░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░(1)══> Add Book ░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░\n" + + "░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░(2)══> Remove Book by ISBN ░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░\n" + + "░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░(3)══> Search Book ░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░\n" + + "░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░(4)══> List All Available Books ░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░\n" + + "░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░(0)══> Back to Main Menu ░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░\n" + + "░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░"); + } - public void printSearchBookMenu(){ - System.out.println( - " ██████╗███████╗ █████╗ ██████╗ █████╗ ██╗ ██╗ \n"+ - "██████╗██████╗██████╗ ██╔════╝██╔════╝██╔══██╗██╔══██╗██╔══██╗██║ ██║ ██████╗██████╗██████╗\n"+ - "╚═════╝╚═════╝╚═════╝ ╚█████╗ █████╗ ███████║██████╔╝██║ ╚═╝███████║ ╚═════╝╚═════╝╚═════╝\n"+ - "██████╗██████╗██████╗ ╚═══██╗██╔══╝ ██╔══██║██╔══██╗██║ ██╗██╔══██║ ██████╗██████╗██████╗\n"+ - "╚═════╝╚═════╝╚═════╝ ██████╔╝███████╗██║ ██║██║ ██║╚█████╔╝██║ ██║ ╚═════╝╚═════╝╚═════╝\n"+ - " ╚═════╝ ╚══════╝╚═╝ ╚═╝╚═╝ ╚═╝ ╚════╝ ╚═╝ ╚═╝ \n"+ - " ██████╗ █████╗ █████╗ ██╗ ██╗ \n"+ - "██████╗██████╗██████╗██████╗ ██╔══██╗██╔══██╗██╔══██╗██║ ██╔╝ ██████╗██████╗██████╗██████╗\n"+ - "╚═════╝╚═════╝╚═════╝╚═════╝ ██████╦╝██║ ██║██║ ██║█████═╝ ╚═════╝╚═════╝╚═════╝╚═════╝\n"+ - "██████╗██████╗██████╗██████╗ ██╔══██╗██║ ██║██║ ██║██╔═██╗ ██████╗██████╗██████╗██████╗\n"+ - "╚═════╝╚═════╝╚═════╝╚═════╝ ██████╦╝╚█████╔╝╚█████╔╝██║ ╚██╗ ╚═════╝╚═════╝╚═════╝╚═════╝\n"+ - " ╚═════╝ ╚════╝ ╚════╝ ╚═╝ ╚═╝ \n"+ - "░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░(➊)══> Search by Title ░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░\n"+ - "░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░(➋)══> Search by ISBN ░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░\n"+ - "░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░(⓿)══> Back to Book Management ░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░\n" ); - } + public void printSearchBookMenu() { + System.out.println( + " ██████╗███████╗ █████╗ ██████╗ █████╗ ██╗ ██╗ \n" + + "██████╗██████╗██████╗ ██╔════╝██╔════╝██╔══██╗██╔══██╗██╔══██╗██║ ██║ ██████╗██████╗██████╗\n" + + "╚═════╝╚═════╝╚═════╝ ╚█████╗ █████╗ ███████║██████╔╝██║ ╚═╝███████║ ╚═════╝╚═════╝╚═════╝\n" + + "██████╗██████╗██████╗ ╚═══██╗██╔══╝ ██╔══██║██╔══██╗██║ ██╗██╔══██║ ██████╗██████╗██████╗\n" + + "╚═════╝╚═════╝╚═════╝ ██████╔╝███████╗██║ ██║██║ ██║╚█████╔╝██║ ██║ ╚═════╝╚═════╝╚═════╝\n" + + " ╚═════╝ ╚══════╝╚═╝ ╚═╝╚═╝ ╚═╝ ╚════╝ ╚═╝ ╚═╝ \n" + + " ██████╗ █████╗ █████╗ ██╗ ██╗ \n" + + "██████╗██████╗██████╗██████╗ ██╔══██╗██╔══██╗██╔══██╗██║ ██╔╝ ██████╗██████╗██████╗██████╗\n" + + "╚═════╝╚═════╝╚═════╝╚═════╝ ██████╦╝██║ ██║██║ ██║█████═╝ ╚═════╝╚═════╝╚═════╝╚═════╝\n" + + "██████╗██████╗██████╗██████╗ ██╔══██╗██║ ██║██║ ██║██╔═██╗ ██████╗██████╗██████╗██████╗\n" + + "╚═════╝╚═════╝╚═════╝╚═════╝ ██████╦╝╚█████╔╝╚█████╔╝██║ ╚██╗ ╚═════╝╚═════╝╚═════╝╚═════╝\n" + + " ╚═════╝ ╚════╝ ╚════╝ ╚═╝ ╚═╝ \n" + + "░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░(1)══> Search by Title ░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░\n" + + "░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░(2)══> Search by ISBN ░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░\n" + + "░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░(0)══> Back to Book Management ░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░\n" + + "░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░"); + } - public void printLoanMenu(){ - System.out.println( - "██████╗██████╗██████╗██████╗ ██║ ██╗ ██╗██╗ ██╗████╗ ██╗ ██████╗██████╗██████╗██████╗\n"+ - "╚═════╝╚═════╝╚═════╝╚═════╝ ██║ ██║ ██║███████║██╔██╗██║ ╚═════╝╚═════╝╚═════╝╚═════╝\n"+ - " ██║ ██║ ██║██╔══██║██║╚████║ \n"+ - "██████╗██████╗██████╗██████╗ ███████╗╚█████╔╝██║ ██║██║ ╚███║ ██████╗██████╗██████╗██████╗\n"+ - "╚═════╝╚═════╝╚═════╝╚═════╝ ╚══════╝ ╚════╝ ╚═╝ ╚═╝╚═╝ ╚══╝ ╚═════╝╚═════╝╚═════╝╚═════╝\n"+ - "░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░(➊)══> Check Out Books ░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░\n"+ - "░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░(➋)══> Return Books ░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░\n"+ - "░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░(⓿)══> Back to Loan Menu ░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░\n"); - } + public void printLoanMenu() { + System.out.println( + "██████╗██████╗██████╗██████╗ ██║ ██╗ ██╗██╗ ██╗████╗ ██╗ ██████╗██████╗██████╗██████╗\n" + + "╚═════╝╚═════╝╚═════╝╚═════╝ ██║ ██║ ██║███████║██╔██╗██║ ╚═════╝╚═════╝╚═════╝╚═════╝\n" + + " ██║ ██║ ██║██╔══██║██║╚████║ \n" + + "██████╗██████╗██████╗██████╗ ███████╗╚█████╔╝██║ ██║██║ ╚███║ ██████╗██████╗██████╗██████╗\n" + + "╚═════╝╚═════╝╚═════╝╚═════╝ ╚══════╝ ╚════╝ ╚═╝ ╚═╝╚═╝ ╚══╝ ╚═════╝╚═════╝╚═════╝╚═════╝\n" + + "░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░(1)══> Check Out Books ░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░\n" + + "░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░(2)══> Return Books ░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░\n" + + "░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░(0)══> Back to Loan Menu ░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░\n" + + "░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░"); + } - public void printReportsMenu(){ - System.out.println( - " ██████╗ ███████╗██████╗ █████╗ ██████╗ ████████╗ ██████╗ \n"+ - "██████╗██████╗ ██╔══██╗██╔════╝██╔══██╗██╔══██╗██╔══██╗╚══██╔══╝██╔════╝ ██████╗██████╗\n"+ - "╚═════╝╚═════╝ ██████╔╝█████╗ ██████╔╝██║ ██║██████╔╝ ██║ ╚█████╗ ╚═════╝╚═════╝\n"+ - " ██╔══██╗██╔══╝ ██╔═══╝ ██║ ██║██╔══██╗ ██║ ╚═══██╗ \n"+ - "██████╗██████╗ ██║ ██║███████╗██║ ╚█████╔╝██║ ██║ ██║ ██████╔╝ ██████╗██████╗\n"+ - "╚═════╝╚═════╝ ╚═╝ ╚═╝╚══════╝╚═╝ ╚════╝ ╚═╝ ╚═╝ ╚═╝ ╚═════╝ ╚═════╝╚═════╝\n"+ - "░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░(➊)══> List Books by Category ░░░░░░░░░░░░░░░░░░░░░░░░░░\n"+ - "░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░(➋)══> List Books Borrowed by a Student ░░░░░░░░░░░░░░░░░░░░░░░░░░\n"+ - "░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░(⓿)══> List Students by Major ░░░░░░░░░░░░░░░░░░░░░░░░░░\n"+ - "░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░(⓿)══> Back to Main Menu ░░░░░░░░░░░░░░░░░░░░░░░░░░\n"); - } + public void printReportsMenu() { + System.out.println( + " ██████╗ ███████╗██████╗ █████╗ ██████╗ ████████╗ ██████╗ \n" + + "██████╗██████╗ ██╔══██╗██╔════╝██╔══██╗██╔══██╗██╔══██╗╚══██╔══╝██╔════╝ ██████╗██████╗\n" + + "╚═════╝╚═════╝ ██████╔╝█████╗ ██████╔╝██║ ██║██████╔╝ ██║ ╚█████╗ ╚═════╝╚═════╝\n" + + " ██╔══██╗██╔══╝ ██╔═══╝ ██║ ██║██╔══██╗ ██║ ╚═══██╗ \n" + + "██████╗██████╗ ██║ ██║███████╗██║ ╚█████╔╝██║ ██║ ██║ ██████╔╝ ██████╗██████╗\n" + + "╚═════╝╚═════╝ ╚═╝ ╚═╝╚══════╝╚═╝ ╚════╝ ╚═╝ ╚═╝ ╚═╝ ╚═════╝ ╚═════╝╚═════╝\n" + + "░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░(1)══> List Books by Category ░░░░░░░░░░░░░░░░░░░░░░░░░░\n" + + "░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░(2)══> List Books Borrowed by a Student ░░░░░░░░░░░░░░░░░░░░░░░░░░\n" + + "░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░(3)══> List Students by Major ░░░░░░░░░░░░░░░░░░░░░░░░░░\n" + + "░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░(0)══> Back to Main Menu ░░░░░░░░░░░░░░░░░░░░░░░░░░\n"); + } - public void displayMainMenu() { - while (true) { - printMainMenu(); - int choice = readIntInRange("Enter a number", 0, 4); - switch (choice) { - case 1: - displayMemberMenu(); - break; - case 2: - displayBookMenu(); - break; - case 3: - displayLoanMenu(); - break; - case 4: - displayReportMenu(); - break; - case 0: - libraryController.saveData(); - return; - default: - assert false: "UNREACHABLE"; - } - } + public void displayMainMenu() { + while (true) { + printMainMenu(); + int choice = readIntInRange("Enter a number", 0, 4); + switch (choice) { + case 1: + displayMemberMenu(); + break; + case 2: + displayBookMenu(); + break; + case 3: + displayLoanMenu(); + break; + case 4: + displayReportMenu(); + break; + case 0: + libraryController.saveData(); + return; + default: + assert false : "UNREACHABLE"; + } } + } - public void displayMemberMenu() { - while (true) { - printManageUsersMenu(); - int choice = readIntInRange("Enter a number", 0, 5); - switch (choice) { - case 1: { - String studentID = readString("Enter the Student ID"); - String firstName = readString("Enter the Student First Name"); - String lastName = readString("Enter the Student Last Name"); - String major = readString("Enter the Student Major"); - int enrollmentYear = readIntInRange("Enter the Student Enrollment Year", 1000, 3000); - libraryController.addUndergraduateStudent(studentID, firstName, lastName, major, enrollmentYear); - break; - } - case 2: { - String studentID = readString("Enter the Student ID"); - String firstName = readString("Enter the Student First Name"); - String lastName = readString("Enter the Student Last Name"); - String major = readString("Enter the Student Major"); - String supervisor = readString("Enter the Student Supervisor"); - String thesisTitle = readString("Enter the Student Thesis Title"); - libraryController.addGraduateStudent(studentID, firstName, lastName, major, supervisor, thesisTitle); - break; - } - case 3: { - Student student = libraryController.searchStudent(readString("Enter Student ID")); - if (student == null) - System.out.println("Student not found!"); - else - System.out.println(student); - break; - } - case 4: { - String studentID = readString("Enter the Student ID"); - libraryController.removeStudent(studentID); - break; - } - case 5: - displayAllStudents(libraryController.getAllStudents()); - break; - case 0: - return; - default: - assert false: "UNREACHABLE"; - } + public void displayMemberMenu() { + while (true) { + printManageUsersMenu(); + int choice = readIntInRange("Enter a number", 0, 5); + switch (choice) { + case 1: { + String studentID = readString("Enter the Student ID"); + String firstName = readString("Enter the Student First Name"); + String lastName = readString("Enter the Student Last Name"); + String major = readString("Enter the Student Major"); + int enrollmentYear = readIntInRange("Enter the Student Enrollment Year", 1000, 3000); + libraryController.addUndergraduateStudent(studentID, firstName, lastName, major, enrollmentYear); + break; } - } - - public void displayBookMenu() { - while (true) { - printManageBooksMenu(); - int choice = readIntInRange("Enter a number", 0, 4); - switch (choice) { - case 1: { - String title = readString("Enter the title"); - String author = readString("Enter the author"); - String isbn = readString("Enter the ISBN"); - int publicationYear = readIntInRange("Enter the publication year", 1000, 3000); - Category category = readSelection("Select a category", libraryController.getCategories()); - libraryController.addBook(title, author, isbn, category, publicationYear); - break; - } - case 2: { - String isbn = readString("Enter the ISBN"); - libraryController.removeBook(isbn); - break; - } - case 3: - displaySearchBookMenu(); - break; - case 4: - displayAllBooks(libraryController.getAvailableBooks()); - break; - case 0: - return; - default: - assert false: "UNREACHABLE"; - } + case 2: { + String studentID = readString("Enter the Student ID"); + String firstName = readString("Enter the Student First Name"); + String lastName = readString("Enter the Student Last Name"); + String major = readString("Enter the Student Major"); + String supervisor = readString("Enter the Student Supervisor"); + String thesisTitle = readString("Enter the Student Thesis Title"); + libraryController.addGraduateStudent(studentID, firstName, lastName, major, supervisor, thesisTitle); + break; } + case 3: { + Student student = libraryController.searchStudent(readString("Enter Student ID")); + if (student == null) + System.out.println("Student not found!"); + else + System.out.println(student); + break; + } + case 4: { + String studentID = readString("Enter the Student ID"); + libraryController.removeStudent(studentID); + break; + } + case 5: + displayAllStudents(libraryController.getAllStudents()); + break; + case 0: + return; + default: + assert false : "UNREACHABLE"; + } } + } - public void displaySearchBookMenu() { - printSearchBookMenu(); - int choice = readIntInRange("Enter a number", 0, 2); - switch (choice){ + public void displayBookMenu() { + while (true) { + printManageBooksMenu(); + int choice = readIntInRange("Enter a number", 0, 4); + switch (choice) { case 1: { - Book book = libraryController.searchBookByTitle(readString("Enter Book Title")); - if (book == null) - System.out.println("Book not found!"); - else - System.out.println(book); + String title = readString("Enter the title"); + String author = readString("Enter the author"); + String isbn = readString("Enter the ISBN"); + int publicationYear = readIntInRange("Enter the publication year", 1000, 3000); + Category category = readSelection("Select a category", libraryController.getCategories()); + libraryController.addBook(title, author, isbn, category, publicationYear); break; } case 2: { - String isbn = readString("Enter the book ISBN"); - Book book = libraryController.searchBookByISBN(isbn); - if (book == null) - System.out.println("Book not found!"); - else - System.out.println(book); + String isbn = readString("Enter the ISBN"); + libraryController.removeBook(isbn); break; } + case 3: + displaySearchBookMenu(); + break; + case 4: + displayAllBooks(libraryController.getAvailableBooks()); + break; case 0: return; + default: + assert false : "UNREACHABLE"; } + } + } + public void displaySearchBookMenu() { + printSearchBookMenu(); + int choice = readIntInRange("Enter a number", 0, 2); + switch (choice) { + case 1: { + Book book = libraryController.searchBookByTitle(readString("Enter Book Title")); + if (book == null) + System.out.println("Book not found!"); + else + System.out.println(book); + break; + } + case 2: { + String isbn = readString("Enter the book ISBN"); + Book book = libraryController.searchBookByISBN(isbn); + if (book == null) + System.out.println("Book not found!"); + else + System.out.println(book); + break; + } + case 0: + return; } - public void displayLoanMenu() { - while (true) { - printLoanMenu(); - int choice = readIntInRange("Enter a number", 0, 2); - switch (choice) { - case 1: { - String studentID = readString("Enter you Student ID"); - Student student = libraryController.searchStudent(studentID); - if (student == null) { - System.out.println("Student not found!"); - continue; - } - List availableBooks = libraryController.getAvailableBooks(); - if (availableBooks == null || availableBooks.size() == 0) { - System.out.println("No books available right now!"); - continue; - } + } + + public void displayLoanMenu() { + while (true) { + printLoanMenu(); + int choice = readIntInRange("Enter a number", 0, 2); + switch (choice) { + case 1: { + String studentID = readString("Enter you Student ID"); + Student student = libraryController.searchStudent(studentID); + if (student == null) { + System.out.println("Student not found!"); + continue; + } + List availableBooks = libraryController.getAvailableBooks(); + if (availableBooks == null || availableBooks.size() == 0) { + System.out.println("No books available right now!"); + continue; + } - Book selectedBook = readSelection("Which book do you want to borrow?", availableBooks); - libraryController.borrowBook(selectedBook, student); - break; - } - case 2: { - String studentID = readString("Enter your Student ID"); - if (libraryController.searchStudent(studentID) == null) { - System.out.println("Student not found!"); - continue; - } - List books = libraryController.getBorrowedBooksByStudent(studentID); - if (books == null || books.size() == 0) { - System.out.println("You haven't borrowed any book!"); - continue; - } - Book selectedBook = readSelection("Which book do you want to return?", books); - libraryController.returnBook(selectedBook); - break; - } - case 0: - return; - default: - assert false: "UNREACHABLE"; - } + Book selectedBook = readSelection("Which book do you want to borrow?", availableBooks); + libraryController.borrowBook(selectedBook, student); + break; } + case 2: { + String studentID = readString("Enter your Student ID"); + if (libraryController.searchStudent(studentID) == null) { + System.out.println("Student not found!"); + continue; + } + List books = libraryController.getBorrowedBooksByStudent(studentID); + if (books == null || books.size() == 0) { + System.out.println("You haven't borrowed any book!"); + continue; + } + Book selectedBook = readSelection("Which book do you want to return?", books); + libraryController.returnBook(selectedBook); + break; + } + case 0: + return; + default: + assert false : "UNREACHABLE"; + } } + } - public void displayReportMenu() { - while (true) { - printReportsMenu(); - int choice = readIntInRange("Enter a number", 0, 3); - switch (choice) { - case 1: { - List categories = libraryController.getCategories(); - if (categories == null || categories.size() == 0) { - System.out.println("No categories available rightt now!"); - continue; - } - Category category = readSelection("Choose a category", categories); - List books = libraryController.getBooksByCategory(category); - if (books == null || books.size() == 0) { - System.out.println("No books available in this category"); - continue; - } - displayReport(books); - break; - } - case 2: { - String studentID = readString("Enter your Student ID"); - if (libraryController.searchStudent(studentID) == null) { - System.out.println("Student not found!"); - continue; - } - List books = libraryController.getBorrowedBooksByStudent(studentID); - if (books == null || books.size() == 0) { - System.out.println("You haven't borrowed any book!"); - continue; - } - displayReport(books); - break; - } - case 3: { - String major = readString("Enter a major"); - List students = libraryController.getStudentsByMajor(major); - if (students == null || students.size() == 0) { - System.out.println("No students found in this major"); - continue; - } - - displayReport(students); - break; - } - case 0: - return; - default: - assert false: "UNREACHABLE"; - } + public void displayReportMenu() { + while (true) { + printReportsMenu(); + int choice = readIntInRange("Enter a number", 0, 3); + switch (choice) { + case 1: { + List categories = libraryController.getCategories(); + if (categories == null || categories.size() == 0) { + System.out.println("No categories available rightt now!"); + continue; + } + Category category = readSelection("Choose a category", categories); + List books = libraryController.getBooksByCategory(category); + if (books == null || books.size() == 0) { + System.out.println("No books available in this category"); + continue; + } + displayReport(books); + break; } - } + case 2: { + String studentID = readString("Enter your Student ID"); + if (libraryController.searchStudent(studentID) == null) { + System.out.println("Student not found!"); + continue; + } + List books = libraryController.getBorrowedBooksByStudent(studentID); + if (books == null || books.size() == 0) { + System.out.println("You haven't borrowed any book!"); + continue; + } + displayReport(books); + break; + } + case 3: { + String major = readString("Enter a major"); + List students = libraryController.getStudentsByMajor(major); + if (students == null || students.size() == 0) { + System.out.println("No students found in this major"); + continue; + } - public void displayAllStudents(List students) { - for (Student student : students) { - System.out.println(student); + displayReport(students); + break; + } + case 0: + return; + default: + assert false : "UNREACHABLE"; } } + } - public void displayAllBooks(List books) { - for (Book book : books) { - System.out.println(book); - } + public void displayAllStudents(List students) { + for (Student student : students) { + System.out.println(student); } + } - public void displayReport(List results) { - for (int i = 0; i < results.size(); i++) - System.out.printf("%d. %s\n", i, results.get(i).getDisplayName()); + public void displayAllBooks(List books) { + for (Book book : books) { + System.out.println(book); } + } - private void printPrompt(String prompt) { - System.out.print(String.format("░░░░░░░░░░░░░░░░░░░░░░░░░░║%s║══> ", prompt)); - System.out.flush(); - } + public void displayReport(List results) { + for (int i = 0; i < results.size(); i++) + System.out.printf("%d. %s\n", i, results.get(i).getDisplayName()); + } - private void printInvalidInput() { - System.out.println("░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░"); - System.out.println("░░░░░░░░░░░░░░░░░░░░░░░░░░<<<⚠⃨Please enter valid characters⚠⃨>>>░░░░░░░░░░░░░░░░░░░░░░░░░░░"); - System.out.println("░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░"); - } + private void printPrompt(String prompt) { + System.out.print(String.format("░░░░░░░░░░░░░░░░░░░░░░░░░░║%s║══> ", prompt)); + System.out.flush(); + } - private void clearScannerBuffer() { - if (scanner.hasNextLine()) - scanner.nextLine(); - } + private void printInvalidInput() { + System.out.println("░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░"); + System.out.println("░░░░░░░░░░░░░░░░░░░░░░░░░░<<<⚠⃨Please enter valid characters⚠⃨>>>░░░░░░░░░░░░░░░░░░░░░░░░░░░"); + System.out.println("░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░"); + } - private int readIntInRange(String prompt, int min, int max) { - while (true) { - printPrompt(prompt); - try { - int input = scanner.nextInt(); - clearScannerBuffer(); - if (input >= min && input <= max) - return input; - else { - printInvalidInput(); - continue; - } - } catch (Exception e) { + private void clearScannerBuffer() { + if (scanner.hasNextLine()) + scanner.nextLine(); + } + + private int readIntInRange(String prompt, int min, int max) { + while (true) { + printPrompt(prompt); + try { + int input = scanner.nextInt(); + clearScannerBuffer(); + if (input >= min && input <= max) + return input; + else { printInvalidInput(); - clearScannerBuffer(); continue; } + } catch (Exception e) { + printInvalidInput(); + clearScannerBuffer(); + continue; } } + } - private String readString(String prompt) { - printPrompt(prompt); - // TODO: validiate input - return scanner.nextLine(); - } + private String readString(String prompt) { + printPrompt(prompt); + // TODO: validiate input + return scanner.nextLine(); + } - private T readSelection(String prompt, List options) { - if (options == null || options.size() == 0) - return null; + private T readSelection(String prompt, List options) { + if (options == null || options.size() == 0) + return null; - for (int i = 0; i < options.size(); i++) { - System.out.println(String.format("%d. %s", i, options.get(i).getDisplayName())); - } - - return options.get(readIntInRange(prompt, 0, options.size()-1)); + for (int i = 0; i < options.size(); i++) { + System.out.println(String.format("%d. %s", i, options.get(i).getDisplayName())); } + + return options.get(readIntInRange(prompt, 0, options.size() - 1)); + } } From 76a4ff9e8cf6d2655ce06a6f5680a932ebc86200 Mon Sep 17 00:00:00 2001 From: Reza Date: Fri, 30 May 2025 11:51:38 +0330 Subject: [PATCH 13/15] Add an option to create a new category --- src/controller/LibraryController.java | 4 ++++ src/model/Library.java | 16 +++++++++----- src/view/UserInterface.java | 32 ++++++++++++++++++++++++++- 3 files changed, 45 insertions(+), 7 deletions(-) diff --git a/src/controller/LibraryController.java b/src/controller/LibraryController.java index eab6010..11cfa59 100644 --- a/src/controller/LibraryController.java +++ b/src/controller/LibraryController.java @@ -50,7 +50,11 @@ public List getAllStudents() { public void addBook(String title, String author, String isbn, Category category, int publicationYear) { Book book = new Book(title, author, isbn, category, publicationYear, false); library.addBook(book); + } + public void addCategory(String name, String description) { + Category category = new Category(name, description); + library.addCategory(category); } public void removeBook(String isbn) { diff --git a/src/model/Library.java b/src/model/Library.java index 61dc379..131418b 100644 --- a/src/model/Library.java +++ b/src/model/Library.java @@ -31,6 +31,10 @@ public void addBook(Book book) { books.add(book); } + public void addCategory(Category category) { + categories.add(category); + } + public void removeBook(String isbn) { for (Book book : books) { if (book.getIsbn().equals(isbn)) { @@ -150,13 +154,13 @@ public List getCategories() { } public List getBooksByCategory(Category category) { - List booksByCategory=new ArrayList<>(); - for(Book book : booksByCategory){ - if(book.getCategory().getName().equals(category.getName())){ - booksByCategory.add(book); - } + List booksByCategory = new ArrayList<>(); + for (Book book : booksByCategory) { + if (book.getCategory().getName().equals(category.getName())) { + booksByCategory.add(book); } - return booksByCategory; + } + return booksByCategory; } diff --git a/src/view/UserInterface.java b/src/view/UserInterface.java index e64fd2c..be3f0d6 100644 --- a/src/view/UserInterface.java +++ b/src/view/UserInterface.java @@ -212,7 +212,8 @@ public void displayBookMenu() { String author = readString("Enter the author"); String isbn = readString("Enter the ISBN"); int publicationYear = readIntInRange("Enter the publication year", 1000, 3000); - Category category = readSelection("Select a category", libraryController.getCategories()); + List categories = libraryController.getCategories(); + Category category = readCategorySelection(categories); libraryController.addBook(title, author, isbn, category, publicationYear); break; } @@ -419,6 +420,35 @@ private String readString(String prompt) { return scanner.nextLine(); } + private Category createNewCategory() { + String name = readString("Enter Category Name"); + String description = readString("Enter Category Description"); + libraryController.addCategory(name, description); + System.out.println("New category created successfully!"); + return readCategorySelection(libraryController.getCategories()); + + } + + private Category readCategorySelection(List categories) { + if (categories == null || categories.size() == 0) { + System.out.println("No categories available"); + System.out.println("Creating a new category..."); + + return createNewCategory(); + } + + for (int i = 0; i < categories.size(); i++) + System.out.println(String.format("%d. %s", i, categories.get(i).getDisplayName())); + + System.out.println(String.format("%d. %s", categories.size(), "Add new category")); + + int choice = readIntInRange("Choose an option", 0, categories.size()); + if (choice == categories.size()) + return createNewCategory(); + else + return categories.get(choice); + } + private T readSelection(String prompt, List options) { if (options == null || options.size() == 0) return null; From cb563add7e6c6af9788be4c103fc3e4b5f0e1dae Mon Sep 17 00:00:00 2001 From: Reza Date: Fri, 30 May 2025 12:00:17 +0330 Subject: [PATCH 14/15] fix: ConcurrentModificationException when removing --- src/model/Library.java | 25 ++++++++++++------------- src/view/UserInterface.java | 3 ++- 2 files changed, 14 insertions(+), 14 deletions(-) diff --git a/src/model/Library.java b/src/model/Library.java index 131418b..4432e76 100644 --- a/src/model/Library.java +++ b/src/model/Library.java @@ -36,11 +36,11 @@ public void addCategory(Category category) { } public void removeBook(String isbn) { - for (Book book : books) { - if (book.getIsbn().equals(isbn)) { - books.remove(book); + for (int i = books.size() - 1; i >= 0; i--) + if (books.get(i).getIsbn().equals(isbn)) { + books.remove(i); + return; } - } } public Book searchBookByTitle(String title) { @@ -84,11 +84,11 @@ public void addStudent(Student student) { } public void removeStudent(String studentId) { - for (Student student : students) { - if (student.getStudentId().equals(studentId)) { - students.remove(student); + for (int i = students.size() - 1; i >= 0; i--) + if (students.get(i).getStudentId().equals(studentId)) { + students.remove(i); + return; } - } } public Student searchStudentById(String studentId) { @@ -125,13 +125,12 @@ public boolean borrowBook(Book book, Student student) { } public boolean returnBook(Book book) { - for (Loan loan : loans) { - if (loan.getBook().equals(book)) { - loans.remove(loan); - loan.getBook().returnBook(); + for (int i = loans.size() - 1; i >= 0; i--) + if (loans.get(i).getBook().equals(book)) { + loans.remove(i); return true; } - } + return false; } diff --git a/src/view/UserInterface.java b/src/view/UserInterface.java index be3f0d6..f8bdc0b 100644 --- a/src/view/UserInterface.java +++ b/src/view/UserInterface.java @@ -125,7 +125,8 @@ public void printReportsMenu() { "░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░(1)══> List Books by Category ░░░░░░░░░░░░░░░░░░░░░░░░░░\n" + "░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░(2)══> List Books Borrowed by a Student ░░░░░░░░░░░░░░░░░░░░░░░░░░\n" + "░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░(3)══> List Students by Major ░░░░░░░░░░░░░░░░░░░░░░░░░░\n" + - "░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░(0)══> Back to Main Menu ░░░░░░░░░░░░░░░░░░░░░░░░░░\n"); + "░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░(0)══> Back to Main Menu ░░░░░░░░░░░░░░░░░░░░░░░░░░\n" + + "░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░"); } public void displayMainMenu() { From 93237e8197a10d4414cfd81dc6046bd95b2420be Mon Sep 17 00:00:00 2001 From: Reza Date: Fri, 30 May 2025 12:20:47 +0330 Subject: [PATCH 15/15] Implement equals and hashCode for all models --- src/model/BaseModel.java | 6 ++++++ src/model/Book.java | 21 +++++++++++++++++++++ src/model/Category.java | 22 ++++++++++++++++++++++ src/model/GraduateStudent.java | 24 ++++++++++++++++++++++++ src/model/Loan.java | 21 ++++++++++++++++++++- src/model/UndergraduateStudent.java | 23 +++++++++++++++++++++++ 6 files changed, 116 insertions(+), 1 deletion(-) diff --git a/src/model/BaseModel.java b/src/model/BaseModel.java index 029b7ae..515f257 100644 --- a/src/model/BaseModel.java +++ b/src/model/BaseModel.java @@ -4,4 +4,10 @@ public abstract class BaseModel implements JSONSerializable { public abstract String getDisplayName(); + + @Override + public abstract int hashCode(); + + @Override + public abstract boolean equals(Object obj); } diff --git a/src/model/Book.java b/src/model/Book.java index e552340..f241c47 100644 --- a/src/model/Book.java +++ b/src/model/Book.java @@ -1,5 +1,7 @@ package model; +import java.util.Objects; + import jsonlib.JSONSerializable; import jsonlib.JSONSerializableFactory; import jsonlib.types.JSONObject; @@ -89,4 +91,23 @@ public static Book deserialize(JSONDict json) { category, json.getInteger("publicationYear"), json.getBoolean("isBorrowed")); } + @Override + public int hashCode() { + return Objects.hash(title, author, isbn, category, publicationYear, isBorrowed); + } + + @Override + public boolean equals(Object obj) { + if (this == obj) + return true; + if (obj == null) + return false; + if (!(obj instanceof Book)) + return false; + + Book other = (Book) obj; + return this.title.equals(other.title) && this.author.equals(other.author) && this.isbn.equals(other.isbn) + && this.category.equals(other.category) && this.publicationYear == other.publicationYear + && this.isBorrowed == other.isBorrowed; + } } diff --git a/src/model/Category.java b/src/model/Category.java index ffbb41c..163102a 100644 --- a/src/model/Category.java +++ b/src/model/Category.java @@ -1,6 +1,9 @@ package model; import jsonlib.types.JSONObject; + +import java.util.Objects; + import jsonlib.types.JSONDict; public class Category extends BaseModel { @@ -46,4 +49,23 @@ public JSONObject serialize() { public static Category deserialize(JSONDict json) { return new Category(json.getString("name"), json.getString("description")); } + + @Override + public int hashCode() { + return Objects.hash(name, description); + } + + @Override + public boolean equals(Object obj) { + if (this == obj) + return true; + if (obj == null) + return false; + if (!(obj instanceof Category)) + return false; + + Category other = (Category) obj; + + return this.name.equals(other.name) && this.description.equals(other.description); + } } diff --git a/src/model/GraduateStudent.java b/src/model/GraduateStudent.java index e9a1194..2303097 100644 --- a/src/model/GraduateStudent.java +++ b/src/model/GraduateStudent.java @@ -1,6 +1,9 @@ package model; import jsonlib.types.JSONObject; + +import java.util.Objects; + import jsonlib.types.JSONDict; public class GraduateStudent extends Student { @@ -65,4 +68,25 @@ public static GraduateStudent deserialize(JSONDict json) { return new GraduateStudent(json.getString("studentId"), json.getString("firstName"), json.getString("lastName"), json.getString("major"), json.getString("supervisor"), json.getString("thesisTitle")); } + + @Override + public int hashCode() { + return Objects.hash(studentId, firstName, lastName, major, supervisor, thesisTitle); + } + + @Override + public boolean equals(Object obj) { + if (this == obj) + return true; + if (obj == null) + return false; + if (!(obj instanceof GraduateStudent)) + return false; + + GraduateStudent other = (GraduateStudent) obj; + + return this.studentId.equals(other.studentId) && this.firstName.equals(other.firstName) + && this.lastName.equals(other.lastName) && this.major.equals(other.major) + && this.supervisor.equals(other.supervisor) && this.thesisTitle.equals(other.thesisTitle); + } } diff --git a/src/model/Loan.java b/src/model/Loan.java index 3b8ec47..a124f05 100644 --- a/src/model/Loan.java +++ b/src/model/Loan.java @@ -1,8 +1,8 @@ package model; import java.util.Date; +import java.util.Objects; -import jsonlib.JSONSerializable; import jsonlib.JSONSerializableFactory; import jsonlib.types.JSONObject; import jsonlib.types.JSONDict; @@ -82,4 +82,23 @@ public static Loan deserialize(JSONDict json) { return new Loan(book, student, new Date(loanDateValue.getValue()), new Date(dueDateValue.getValue())); } + + @Override + public int hashCode() { + return Objects.hash(book, student, loanDate, dueDate); + } + + @Override + public boolean equals(Object obj) { + if (this == obj) + return true; + if (obj == null) + return false; + if (!(obj instanceof Loan)) + return false; + + Loan other = (Loan) obj; + return this.student.equals(other.student) && this.book.equals(other.book) && this.loanDate.equals(other.loanDate) + && this.dueDate.equals(other.dueDate); + } } diff --git a/src/model/UndergraduateStudent.java b/src/model/UndergraduateStudent.java index 29ab57f..e01057b 100644 --- a/src/model/UndergraduateStudent.java +++ b/src/model/UndergraduateStudent.java @@ -1,6 +1,9 @@ package model; import jsonlib.types.JSONObject; + +import java.util.Objects; + import jsonlib.types.JSONDict; public class UndergraduateStudent extends Student { @@ -53,4 +56,24 @@ public static UndergraduateStudent deserialize(JSONDict json) { json.getString("lastName"), json.getString("major"), json.getInteger("enrollmentYear")); } + @Override + public int hashCode() { + return Objects.hash(studentId, firstName, lastName, major, enrollmentYear); + } + + @Override + public boolean equals(Object obj) { + if (this == obj) + return true; + if (obj == null) + return false; + if (!(obj instanceof UndergraduateStudent)) + return false; + + UndergraduateStudent other = (UndergraduateStudent) obj; + + return this.studentId.equals(other.studentId) && this.firstName.equals(other.firstName) + && this.lastName.equals(other.lastName) && this.major.equals(other.major) + && this.enrollmentYear == other.enrollmentYear; + } }