Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 52 additions & 0 deletions Layout.puml
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
@startuml

class Book {
- id : int
- title : String
- author : String
- isAvailable : boolean
+ Book(id : int, title : String, author : String)
+ getId() : int
+ setId(id : int) : void
+ getTitle() : String
+ setTitle(title : String) : void
+ getAuthor() : String
+ setAuthor(author : String) : void
+ isAvailable() : boolean
+ setAvailable(available : boolean) : void
+ toString() : String
}

class Library {
- books : ArrayList<Book>
- nextBookId : int
+ Library()
+ addBook(title : String, author : String) : void
+ displayAllBooks() : 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

@enduml
71 changes: 71 additions & 0 deletions src/main/java/LibraryBookTracker/Book.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package LibraryBookTracker;

public class Book {


// Private fields
private String title;
private String author;
private boolean isAvailable;
private int id;

// Constructor
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 int getId() {
return id;
}

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() {
String status;

if (isAvailable) {
status = "Available";
} else {
status = "Borrowed";
}

return "ID: " + id + " | Title: " + title + " | Author: " + author + " | Status: " + status;

}
}

152 changes: 152 additions & 0 deletions src/main/java/LibraryBookTracker/Library.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
package LibraryBookTracker;

import java.util.ArrayList;

public class Library {

// Using ArrayList because the number of books can grow as users add more
private ArrayList<Book> books;

// 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;
}

// 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 system
public void displayAllBooks() {
if (books.isEmpty()) {
System.out.println("No books in the library.");
return;
}

System.out.println("\n--- All Books ---");
for (Book book : books) {
System.out.println(book);
}
}

// Displays only books that are currently available
public void displayAvailableBooks() {
boolean foundAvailable = false;

System.out.println("\n--- Available Books ---");
for (Book book : books) {
if (book.isAvailable()) {
System.out.println(book);
foundAvailable = true;
}
}

if (!foundAvailable) {
System.out.println("No available books right now.");
}
}

// 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.isAvailable()) {
System.out.println(book);
foundBorrowed = true;
}
}

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 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().toLowerCase().contains(title.toLowerCase())) {
System.out.println(book);
found = true;
}
}

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;
}
}
Loading