From 105bbfe160274661d440e35f3e46aa7c6541337f Mon Sep 17 00:00:00 2001 From: mesheikbrown Date: Sat, 7 Mar 2026 11:51:21 -0500 Subject: [PATCH 1/5] UML Done --- Layout.puml | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 Layout.puml diff --git a/Layout.puml b/Layout.puml new file mode 100644 index 0000000..d9ad679 --- /dev/null +++ b/Layout.puml @@ -0,0 +1,35 @@ +@startuml + +class Book { + - title : String + - author : String + - isAvailable : boolean + + Book(title : String, author : String) + + getTitle() : String + + setTitle(title : String) : void + + getAuthor() : String + + setAuthor(author : String) : void + + isAvailable() : boolean + + setAvailable(available : boolean) : void + + toString() : String +} + +class Library { + - books : ArrayList + + Library() + + addBook(book : Book) : void + + displayAllBooks() : void + + borrowBook(title : String) : void + + returnBook(title : String) : void + + searchBook(title : String) : void +} + +class Main { + + main(args : String[]) : void +} + +Library "1" --> "*" Book : manages +Main --> Library : uses +Main --> Book : creates + +@enduml \ No newline at end of file From 0c6f599bc53bd8e09cbb3cd8abdfebf553b9ba64 Mon Sep 17 00:00:00 2001 From: whblue261 Date: Sat, 7 Mar 2026 13:17:02 -0500 Subject: [PATCH 2/5] Book complete --- .../org/codedifferently/maxxblue/Book.java | 56 +++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 src/main/java/org/codedifferently/maxxblue/Book.java diff --git a/src/main/java/org/codedifferently/maxxblue/Book.java b/src/main/java/org/codedifferently/maxxblue/Book.java new file mode 100644 index 0000000..52551c6 --- /dev/null +++ b/src/main/java/org/codedifferently/maxxblue/Book.java @@ -0,0 +1,56 @@ +package org.codedifferently.maxxblue; + +public class Book { + + + // Private fields + private String title; + private String author; + private boolean isAvailable; + + // Constructor + public Book(String title, String author) { + this.title = title; + this.author = author; + this.isAvailable = true; // default value + } + + // Getter for title + public String getTitle() { + return title; + } + + // Setter for title + public void setTitle(String title) { + this.title = title; + } + + // Getter for author + public String getAuthor() { + return author; + } + + // Setter for author + public void setAuthor(String author) { + this.author = author; + } + + // Check availability + public boolean isAvailable() { + return isAvailable; + } + + // Set availability + public void setAvailable(boolean available) { + this.isAvailable = available; + } + + // toString method + @Override + public String toString() { + return "Book Title: " + title + + ", Author: " + author + + ", Available: " + isAvailable; + } + } + From c95c2b0ec02ef5ea87bf6a22c8f0b0e134e49115 Mon Sep 17 00:00:00 2001 From: mesheikbrown Date: Sat, 7 Mar 2026 13:50:01 -0500 Subject: [PATCH 3/5] Library Done --- src/main/java/LibraryBookTracker/Book.java | 56 +++++++++++++ src/main/java/LibraryBookTracker/Library.java | 82 +++++++++++++++++++ src/main/java/LibraryBookTracker/Main.java | 8 ++ 3 files changed, 146 insertions(+) create mode 100644 src/main/java/LibraryBookTracker/Book.java create mode 100644 src/main/java/LibraryBookTracker/Library.java create mode 100644 src/main/java/LibraryBookTracker/Main.java diff --git a/src/main/java/LibraryBookTracker/Book.java b/src/main/java/LibraryBookTracker/Book.java new file mode 100644 index 0000000..7cd6afa --- /dev/null +++ b/src/main/java/LibraryBookTracker/Book.java @@ -0,0 +1,56 @@ +package LibraryBookTracker; + +public class Book { + + + // Private fields + private String title; + private String author; + private boolean isAvailable; + + // Constructor + public Book(String title, String author) { + this.title = title; + this.author = author; + this.isAvailable = true; // default value + } + + // Getter for title + public String getTitle() { + return title; + } + + // Setter for title + public void setTitle(String title) { + this.title = title; + } + + // Getter for author + public String getAuthor() { + return author; + } + + // Setter for author + public void setAuthor(String author) { + this.author = author; + } + + // Check availability + public boolean isAvailable() { + return isAvailable; + } + + // Set availability + public void setAvailable(boolean available) { + this.isAvailable = available; + } + + // toString method + @Override + public String toString() { + return "Book Title: " + title + + ", Author: " + author + + ", Available: " + isAvailable; + } + } + diff --git a/src/main/java/LibraryBookTracker/Library.java b/src/main/java/LibraryBookTracker/Library.java new file mode 100644 index 0000000..b3eca26 --- /dev/null +++ b/src/main/java/LibraryBookTracker/Library.java @@ -0,0 +1,82 @@ +package LibraryBookTracker; + +import java.util.ArrayList; + +public class Library { + + // Using ArrayList because the number of books can grow over time + private ArrayList books; + + // Constructor creates an empty list when the library starts + public Library() { + books = new ArrayList<>(); + } + + // Adds a new book object into the library + public void addBook(Book book) { + books.add(book); + System.out.println("Book added successfully."); + } + + // Displays every book in the library + public void displayAllBooks() { + // This condition helps prevent printing an empty list with no explanation + if (books.isEmpty()) { + System.out.println("No books in the library."); + return; + } + + // Loop goes through every book in the collection and prints it + for (Book book : books) { + System.out.println(book); + } + } + + // Lets a user borrow a book by title + public void borrowBook(String title) { + for (Book book : books) { + // equalsIgnoreCase lets the user type upper/lowercase without breaking the search + if (book.getTitle().equalsIgnoreCase(title)) { + if (book.isAvailable()) { + book.setAvailable(false); + System.out.println("You borrowed the book: " + book.getTitle()); + } else { + System.out.println("That book is already borrowed."); + } + return; + } + } + + System.out.println("Book not found."); + } + + // Lets a user return a book by title + public void returnBook(String title) { + for (Book book : books) { + if (book.getTitle().equalsIgnoreCase(title)) { + if (!book.isAvailable()) { + book.setAvailable(true); + System.out.println("You returned the book: " + book.getTitle()); + } else { + System.out.println("That book was already available."); + } + return; + } + } + + System.out.println("Book not found."); + } + + // Searches for one book and prints its information + public void searchBook(String title) { + for (Book book : books) { + if (book.getTitle().equalsIgnoreCase(title)) { + System.out.println("Book found:"); + System.out.println(book); + return; + } + } + + System.out.println("Book not found."); + } +} diff --git a/src/main/java/LibraryBookTracker/Main.java b/src/main/java/LibraryBookTracker/Main.java new file mode 100644 index 0000000..757f477 --- /dev/null +++ b/src/main/java/LibraryBookTracker/Main.java @@ -0,0 +1,8 @@ +package LibraryBookTracker; + +public class Main { + + public static void main(String[] args) { + + } +} From 4a1baa413efb617189664be2a1d2465884bd01f2 Mon Sep 17 00:00:00 2001 From: mesheikbrown Date: Sat, 7 Mar 2026 14:53:43 -0500 Subject: [PATCH 4/5] Main Done With a few changes to the classes --- Layout.puml | 29 ++- src/main/java/LibraryBookTracker/Book.java | 29 ++- src/main/java/LibraryBookTracker/Library.java | 146 ++++++++++---- src/main/java/LibraryBookTracker/Main.java | 184 +++++++++++++++++- 4 files changed, 336 insertions(+), 52 deletions(-) diff --git a/Layout.puml b/Layout.puml index d9ad679..24dc0b9 100644 --- a/Layout.puml +++ b/Layout.puml @@ -1,10 +1,13 @@ @startuml class Book { + - id : int - title : String - author : String - isAvailable : boolean - + Book(title : String, author : String) + + Book(id : int, title : String, author : String) + + getId() : int + + setId(id : int) : void + getTitle() : String + setTitle(title : String) : void + getAuthor() : String @@ -16,20 +19,34 @@ class Book { class Library { - books : ArrayList + - nextBookId : int + Library() - + addBook(book : Book) : void + + addBook(title : String, author : String) : void + displayAllBooks() : void - + borrowBook(title : String) : void - + returnBook(title : String) : void - + searchBook(title : String) : void + + displayAvailableBooks() : void + + displayBorrowedBooks() : void + + borrowBookById(id : int) : void + + returnBookById(id : int) : void + + searchBookById(id : int) : void + + searchBookByTitle(title : String) : void + - findBookById(id : int) : Book } class Main { + main(args : String[]) : void + + startApplication(scanner : Scanner, library : Library) : void + + printMenu() : void + + getValidatedMenuChoice(scanner : Scanner) : int + + addStarterBooks(library : Library) : void + + addBook(scanner : Scanner, library : Library) : void + + borrowBook(scanner : Scanner, library : Library) : void + + returnBook(scanner : Scanner, library : Library) : void + + searchBook(scanner : Scanner, library : Library) : void + + getValidatedSearchChoice(scanner : Scanner) : int + + getValidatedPositiveNumber(scanner : Scanner) : int } Library "1" --> "*" Book : manages Main --> Library : uses -Main --> Book : creates @enduml \ No newline at end of file diff --git a/src/main/java/LibraryBookTracker/Book.java b/src/main/java/LibraryBookTracker/Book.java index 7cd6afa..b536adb 100644 --- a/src/main/java/LibraryBookTracker/Book.java +++ b/src/main/java/LibraryBookTracker/Book.java @@ -7,16 +7,23 @@ public class Book { private String title; private String author; private boolean isAvailable; + private int id; // Constructor - public Book(String title, String author) { + public Book(int id, String title, String author) { this.title = title; this.author = author; this.isAvailable = true; // default value + this.id = id; } + // Getter for title - public String getTitle() { + public int getId() { + return id; + } + + public String getTitle() { return title; } @@ -46,11 +53,19 @@ public void setAvailable(boolean available) { } // toString method - @Override - public String toString() { - return "Book Title: " + title + - ", Author: " + author + - ", Available: " + isAvailable; + + @Override + public String toString() { + String status; + + if (isAvailable) { + status = "Available"; + } else { + status = "Borrowed"; + } + + return "ID: " + id + " | Title: " + title + " | Author: " + author + " | Status: " + status; + } } diff --git a/src/main/java/LibraryBookTracker/Library.java b/src/main/java/LibraryBookTracker/Library.java index b3eca26..bcff8d1 100644 --- a/src/main/java/LibraryBookTracker/Library.java +++ b/src/main/java/LibraryBookTracker/Library.java @@ -4,79 +4,149 @@ public class Library { - // Using ArrayList because the number of books can grow over time + // Using ArrayList because the number of books can grow as users add more private ArrayList books; - // Constructor creates an empty list when the library starts + // This keeps track of the next ID to assign to a new book + private int nextBookId; + + // Constructor creates the book list and starts IDs at 1 public Library() { books = new ArrayList<>(); + nextBookId = 1; } - // Adds a new book object into the library - public void addBook(Book book) { - books.add(book); + // Creates and adds a new book using the next available ID + public void addBook(String title, String author) { + Book newBook = new Book(nextBookId, title, author); + books.add(newBook); + nextBookId++; + System.out.println("Book added successfully."); + System.out.println(newBook); } - // Displays every book in the library + // Displays every book in the system public void displayAllBooks() { - // This condition helps prevent printing an empty list with no explanation if (books.isEmpty()) { System.out.println("No books in the library."); return; } - // Loop goes through every book in the collection and prints it + System.out.println("\n--- All Books ---"); for (Book book : books) { System.out.println(book); } } - // Lets a user borrow a book by title - public void borrowBook(String title) { + // Displays only books that are currently available + public void displayAvailableBooks() { + boolean foundAvailable = false; + + System.out.println("\n--- Available Books ---"); for (Book book : books) { - // equalsIgnoreCase lets the user type upper/lowercase without breaking the search - if (book.getTitle().equalsIgnoreCase(title)) { - if (book.isAvailable()) { - book.setAvailable(false); - System.out.println("You borrowed the book: " + book.getTitle()); - } else { - System.out.println("That book is already borrowed."); - } - return; + if (book.isAvailable()) { + System.out.println(book); + foundAvailable = true; } } - System.out.println("Book not found."); + if (!foundAvailable) { + System.out.println("No available books right now."); + } } - // Lets a user return a book by title - public void returnBook(String title) { + // Displays only books that are currently borrowed + public void displayBorrowedBooks() { + boolean foundBorrowed = false; + + System.out.println("\n--- Borrowed Books ---"); for (Book book : books) { - if (book.getTitle().equalsIgnoreCase(title)) { - if (!book.isAvailable()) { - book.setAvailable(true); - System.out.println("You returned the book: " + book.getTitle()); - } else { - System.out.println("That book was already available."); - } - return; + if (!book.isAvailable()) { + System.out.println(book); + foundBorrowed = true; } } - System.out.println("Book not found."); + if (!foundBorrowed) { + System.out.println("No borrowed books right now."); + } + } + + // Lets the user borrow a book using its unique ID + public void borrowBookById(int id) { + Book book = findBookById(id); + + if (book == null) { + System.out.println("Book not found."); + return; + } + + if (!book.isAvailable()) { + System.out.println("That book is already borrowed."); + return; + } + + book.setAvailable(false); + System.out.println("You borrowed: " + book.getTitle()); + } + + // Lets the user return a book using its unique ID + public void returnBookById(int id) { + Book book = findBookById(id); + + if (book == null) { + System.out.println("Book not found."); + return; + } + + if (book.isAvailable()) { + System.out.println("That book is already available."); + return; + } + + book.setAvailable(true); + System.out.println("You returned: " + book.getTitle()); } - // Searches for one book and prints its information - public void searchBook(String title) { + // Searches for a book by ID + public void searchBookById(int id) { + Book book = findBookById(id); + + if (book == null) { + System.out.println("Book not found."); + } else { + System.out.println("Book found:"); + System.out.println(book); + } + } + + // Searches for books by title + // Using contains() makes searching more flexible than exact matching + public void searchBookByTitle(String title) { + boolean found = false; + + System.out.println("\n--- Search Results ---"); for (Book book : books) { - if (book.getTitle().equalsIgnoreCase(title)) { - System.out.println("Book found:"); + if (book.getTitle().toLowerCase().contains(title.toLowerCase())) { System.out.println(book); - return; + found = true; } } - System.out.println("Book not found."); + if (!found) { + System.out.println("No books matched that title."); + } + } + + // Helper method used to find one book by its unique ID + // Keeping this logic in one place avoids repeated code + private Book findBookById(int id) { + for (Book book : books) { + if (book.getId() == id) { + return book; + } + } + return null; } -} +} \ No newline at end of file diff --git a/src/main/java/LibraryBookTracker/Main.java b/src/main/java/LibraryBookTracker/Main.java index 757f477..02bbf6c 100644 --- a/src/main/java/LibraryBookTracker/Main.java +++ b/src/main/java/LibraryBookTracker/Main.java @@ -1,8 +1,190 @@ package LibraryBookTracker; +import java.util.Scanner; + public class Main { public static void main(String[] args) { + // Scanner reads user input from the keyboard + Scanner scanner = new Scanner(System.in); + + // One Library object manages the whole system + Library library = new Library(); + + // Add starter books so the app has data at launch + addStarterBooks(library); + + // Start the menu loop + startApplication(scanner, library); + + // Close scanner at the end of the program + scanner.close(); + } + + // Controls the main loop of the application + public static void startApplication(Scanner scanner, Library library) { + boolean running = true; + + while (running) { + printMenu(); + int choice = getValidatedMenuChoice(scanner); + + switch (choice) { + case 1: + addBook(scanner, library); + break; + case 2: + library.displayAllBooks(); + break; + case 3: + library.displayAvailableBooks(); + break; + case 4: + library.displayBorrowedBooks(); + break; + case 5: + borrowBook(scanner, library); + break; + case 6: + returnBook(scanner, library); + break; + case 7: + searchBook(scanner, library); + break; + case 8: + System.out.println("Thank you for visiting MW Library !"); + running = false; + break; + } + } + } + + // Prints the main menu for the user + public static void printMenu() { + System.out.println("\n===== Library Book Tracker ====="); + System.out.println("1. Add a book"); + System.out.println("2. View all books"); + System.out.println("3. View available books"); + System.out.println("4. View borrowed books"); + System.out.println("5. Borrow a book by ID"); + System.out.println("6. Return a book by ID"); + System.out.println("7. Search for a book"); + System.out.println("8. Exit"); + System.out.print("Choose an option (1-8): "); + } + + // Validates menu input so the program does not crash on bad input + public static int getValidatedMenuChoice(Scanner scanner) { + while (true) { + if (!scanner.hasNextInt()) { + System.out.println("Invalid input. Please enter a number."); + scanner.next(); + continue; + } + + int choice = scanner.nextInt(); + scanner.nextLine(); + + if (choice >= 1 && choice <= 8) { + return choice; + } + + System.out.println("Invalid menu option. Please choose 1-8."); + } + } + + // Adds starter books to make the program easier to test + public static void addStarterBooks(Library library) { + library.addBook("The Hobbit", "J.R.R. Tolkien"); + library.addBook("Maze Runner", "James Dashner"); + library.addBook("To Kill a Mockingbird", "Harper Lee"); + library.addBook("The Godfather", "Mario Puzo"); + library.addBook("Harry Potter", "J. K. Rowling"); + } + + // Collects input for a new book and sends it to the Library + public static void addBook(Scanner scanner, Library library) { + System.out.print("Enter book title: "); + String title = scanner.nextLine(); + + System.out.print("Enter author name: "); + String author = scanner.nextLine(); + + library.addBook(title, author); + } + + // Lets the user borrow a book by entering the book's ID + public static void borrowBook(Scanner scanner, Library library) { + System.out.print("Enter the ID of the book to borrow: "); + int id = getValidatedPositiveNumber(scanner); + library.borrowBookById(id); + } + + // Lets the user return a book by entering the book's ID + public static void returnBook(Scanner scanner, Library library) { + System.out.print("Enter the ID of the book to return: "); + int id = getValidatedPositiveNumber(scanner); + library.returnBookById(id); + } + + // Gives the user two search options: by ID or by title + public static void searchBook(Scanner scanner, Library library) { + System.out.println("Search by:"); + System.out.println("1. ID"); + System.out.println("2. Title"); + System.out.print("Choose an option: "); + + int searchChoice = getValidatedSearchChoice(scanner); + + if (searchChoice == 1) { + System.out.print("Enter book ID: "); + int id = getValidatedPositiveNumber(scanner); + library.searchBookById(id); + } else { + System.out.print("Enter book title: "); + String title = scanner.nextLine(); + library.searchBookByTitle(title); + } + } + + // Validates search menu options + public static int getValidatedSearchChoice(Scanner scanner) { + while (true) { + if (!scanner.hasNextInt()) { + System.out.println("Invalid input. Please enter a number."); + scanner.next(); + continue; + } + + int choice = scanner.nextInt(); + scanner.nextLine(); + + if (choice == 1 || choice == 2) { + return choice; + } + + System.out.println("Please choose 1 or 2."); + } + } + + // Validates positive number input for book IDs + public static int getValidatedPositiveNumber(Scanner scanner) { + while (true) { + if (!scanner.hasNextInt()) { + System.out.println("Invalid input. Please enter a number."); + scanner.next(); + continue; + } + + int number = scanner.nextInt(); + scanner.nextLine(); + + if (number > 0) { + return number; + } + + System.out.println("Please enter a number greater than 0."); + } } -} +} \ No newline at end of file From 2390ecdd5604294c9bbbb920454cbfbc9c1bbd39 Mon Sep 17 00:00:00 2001 From: mesheikbrown Date: Sat, 7 Mar 2026 14:57:46 -0500 Subject: [PATCH 5/5] Updated main --- src/main/java/LibraryBookTracker/Main.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/java/LibraryBookTracker/Main.java b/src/main/java/LibraryBookTracker/Main.java index 02bbf6c..21335a2 100644 --- a/src/main/java/LibraryBookTracker/Main.java +++ b/src/main/java/LibraryBookTracker/Main.java @@ -53,7 +53,7 @@ public static void startApplication(Scanner scanner, Library library) { searchBook(scanner, library); break; case 8: - System.out.println("Thank you for visiting MW Library !"); + System.out.println("Thank you for visiting M&W Library !"); running = false; break; } @@ -62,7 +62,7 @@ public static void startApplication(Scanner scanner, Library library) { // Prints the main menu for the user public static void printMenu() { - System.out.println("\n===== Library Book Tracker ====="); + System.out.println("\n===== M&W Library Book Tracker ====="); System.out.println("1. Add a book"); System.out.println("2. View all books"); System.out.println("3. View available books");