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
19 changes: 19 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
target/
!.mvn/wrapper/maven-wrapper.jar
!**/src/main/**/target/
!**/src/test/**/target/

.idea/
*.iml
*.ipr
*.iws

.classpath
.project
.settings/

data/
*.log

.DS_Store
Thumbs.db
51 changes: 51 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.2.5</version>
<relativePath/>
</parent>

<groupId>com.ironhack</groupId>
<artifactId>ironlibrary</artifactId>
<version>1.0.0</version>
<name>IronLibrary</name>
<description>Library Management System - IronHack Unit 3 Homework</description>

<properties>
<java.version>17</java.version>
</properties>

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>

<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
12 changes: 12 additions & 0 deletions src/main/java/com/ironhack/Application.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.ironhack;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class Application {

public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
243 changes: 243 additions & 0 deletions src/main/java/com/ironhack/controller/LibraryMenuController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,243 @@
package com.ironhack.controller;

import com.ironhack.dto.request.AddBookRequest;
import com.ironhack.dto.request.IssueBookRequest;
import com.ironhack.dto.response.BookResponse;
import com.ironhack.dto.response.IssueResponse;
import com.ironhack.model.MenuOption;
import com.ironhack.service.LibraryService;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Scanner;

@Component
@org.springframework.boot.autoconfigure.condition.ConditionalOnProperty(
name = "ironlibrary.menu.enabled",
havingValue = "true",
matchIfMissing = true
)
public class LibraryMenuController implements CommandLineRunner {

private final LibraryService libraryService;
private final Scanner scanner;

public LibraryMenuController(LibraryService libraryService) {
this.libraryService = libraryService;
this.scanner = new Scanner(System.in);
}

@Override
public void run(String... args) {
boolean running = true;

while (running) {
displayMenu();
try {
int choice = readMenuChoice();
MenuOption option = MenuOption.fromValue(choice);

if (option == null) {
System.out.println("Invalid choice. Please select a number from the menu.");
continue;
}

running = handleMenuOption(option);
} catch (Exception exception) {
System.out.println("Error: " + exception.getMessage());
}
}

System.out.println("Thank you for using IronLibrary. Goodbye!");
}

public void displayMenu() {
System.out.println();
System.out.println("===== IronLibrary Menu =====");
System.out.println("1. Add a book");
System.out.println("2. Search book by title");
System.out.println("3. Search book by category");
System.out.println("4. Search book by Author");
System.out.println("5. List all books along with author");
System.out.println("6. Issue book to student");
System.out.println("7. List books by usn");
System.out.println("8. List books to be returned today");
System.out.println("9. Exit");
System.out.print("Enter your choice: ");
}

public int readMenuChoice() {
while (!scanner.hasNextInt()) {
scanner.next();
System.out.print("Invalid input. Please enter a number: ");
}
int choice = scanner.nextInt();
scanner.nextLine();
return choice;
}

public boolean handleMenuOption(MenuOption option) {
return switch (option) {
case ADD_BOOK -> {
handleAddBook();
yield true;
}
case SEARCH_BY_TITLE -> {
handleSearchByTitle();
yield true;
}
case SEARCH_BY_CATEGORY -> {
handleSearchByCategory();
yield true;
}
case SEARCH_BY_AUTHOR -> {
handleSearchByAuthor();
yield true;
}
case LIST_ALL_BOOKS -> {
handleListAllBooks();
yield true;
}
case ISSUE_BOOK -> {
handleIssueBook();
yield true;
}
case LIST_BOOKS_BY_USN -> {
handleListBooksByUsn();
yield true;
}
case LIST_BOOKS_DUE_TODAY -> {
handleListBooksDueToday();
yield true;
}
case EXIT -> false;
};
}

private void handleAddBook() {
System.out.print("Enter isbn : ");
String isbn = scanner.nextLine();
System.out.print("Enter title : ");
String title = scanner.nextLine();
System.out.print("Enter category : ");
String category = scanner.nextLine();
System.out.print("Enter Author name : ");
String authorName = scanner.nextLine();
System.out.print("Enter Author mail : ");
String authorEmail = scanner.nextLine();
System.out.print("Enter number of books : ");
int quantity = readPositiveInteger();

libraryService.addBook(new AddBookRequest(isbn, title, category, authorName, authorEmail, quantity));
System.out.println("Book added successfully.");
}

private void handleSearchByTitle() {
System.out.print("Enter title : ");
String title = scanner.nextLine();
printBookTable(libraryService.searchBooksByTitle(title), false);
}

private void handleSearchByCategory() {
System.out.print("Enter category : ");
String category = scanner.nextLine();
printBookTable(libraryService.searchBooksByCategory(category), false);
}

private void handleSearchByAuthor() {
System.out.print("Enter name : ");
String authorName = scanner.nextLine();
printBookTable(libraryService.searchBooksByAuthor(authorName), false);
}

private void handleListAllBooks() {
printBookTable(libraryService.listAllBooksWithAuthors(), true);
}

private void handleIssueBook() {
System.out.print("Enter usn : ");
String usn = scanner.nextLine();
System.out.print("Enter name : ");
String name = scanner.nextLine();
System.out.print("Enter book ISBN : ");
String isbn = scanner.nextLine();

IssueResponse response = libraryService.issueBookToStudent(new IssueBookRequest(usn, name, isbn));
System.out.println();
System.out.println(response.getIssueReturnMessage());
}

private void handleListBooksByUsn() {
System.out.print("Enter usn : ");
String usn = scanner.nextLine();
printIssueTable(libraryService.listBooksByUsn(usn));
}

private void handleListBooksDueToday() {
printIssueTable(libraryService.listBooksDueToday());
}

public void printBookTable(List<BookResponse> books, boolean includeAuthor) {
System.out.println();
if (includeAuthor) {
System.out.printf("%-24s%-20s%-12s%-16s%-20s%s%n",
"Book ISBN", "Book Title", "Category", "No of Books", "Author name", "Author mail");
} else {
System.out.printf("%-24s%-20s%-12s%s%n",
"Book ISBN", "Book Title", "Category", "No of Books");
}

for (BookResponse book : books) {
if (includeAuthor) {
System.out.printf("%-24s%-20s%-12s%-16d%-20s%s%n",
book.getIsbn(),
book.getTitle(),
book.getCategory(),
book.getQuantity(),
book.getAuthorName(),
book.getAuthorEmail());
} else {
System.out.printf("%-24s%-20s%-12s%d%n",
book.getIsbn(),
book.getTitle(),
book.getCategory(),
book.getQuantity());
}
}
}

public void printIssueTable(List<IssueResponse> issues) {
System.out.println();
System.out.printf("%-20s%-16s%s%n", "Book Title", "Student Name", "Return date");

for (IssueResponse issue : issues) {
System.out.printf("%-20s%-16s%s%n",
issue.getBookTitle(),
issue.getStudentName(),
issue.getReturnDate());
}
}

private int readPositiveInteger() {
while (true) {
try {
if (!scanner.hasNextInt()) {
scanner.next();
System.out.print("Invalid number. Enter number of books : ");
continue;
}
int value = scanner.nextInt();
scanner.nextLine();
if (value <= 0) {
System.out.print("Number must be greater than zero. Enter number of books : ");
continue;
}
return value;
} catch (Exception exception) {
scanner.nextLine();
System.out.print("Invalid number. Enter number of books : ");
}
}
}
}
Loading