Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Il30 solve menu issue #34

Merged
merged 11 commits into from
Apr 8, 2024
Merged
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
4 changes: 3 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,9 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<arguments>
<argument>-Dspring.main.runOnce=true</argument>
</arguments>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
Expand Down Expand Up @@ -99,5 +102,4 @@
</plugin>
</plugins>
</build>

</project>
18 changes: 14 additions & 4 deletions src/main/java/com/ironhack/ironLibrary/IronLibraryApplication.java
Original file line number Diff line number Diff line change
@@ -1,21 +1,31 @@
package com.ironhack.ironLibrary;

import com.ironhack.ironLibrary.service.IMenuService;
import com.ironhack.ironLibrary.service.MenuServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

import java.awt.*;

@SpringBootApplication
public class IronLibraryApplication {
public class IronLibraryApplication implements CommandLineRunner {

@Value("${app.menu.enabled}")
private boolean isMenuEnabled;
@Autowired
IMenuService menuService;

public static void main(String[] args) {
SpringApplication.run(IronLibraryApplication.class, args
);
}

@Override
public void run(String... args) throws Exception {
if (isMenuEnabled) {
menuService.mainManu();
}
}
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

public interface IssueRepository extends JpaRepository<Issue, String> {
@Query("SELECT b from Issue i JOIN i.issueStudent c JOIN i.issueBook b WHERE c.usn = ?1")
Optional<List<Book>> findAllBooksByUsn(String Usn);
Optional<List<Book>> findAllBooksByUsn(String usn);

@Query("SELECT b, i FROM Issue i JOIN i.issueStudent c JOIN i.issueBook b WHERE c.usn = ?1")
Optional<List<Object[]>> findAllBooksAndIssuesByUsn(String usn);
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,23 @@

import java.util.List;
public interface IMenuService {
void addBook (List<String> bookAndAuthorInformation);

void mainManu() throws Exception;

List<String> getNewBookInformation();

public List<String> getNewIssueInformation();

void addBook (List<String> bookAndAuthorInformation);

Book searchBookByAuthor(String authorName) throws NoBookFoundException;

List<Book> searchBookByCategory(String Category) throws NoBookFoundException;

void issueBookToStudent(List<String> issueInformation) throws NoBookFoundException;

List<Object[]> searchBooksByUsn(String usn) throws Exception;

List<Object[]> searchBooksAlongAuthors() throws NoBookFoundException;

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package com.ironhack.ironLibrary.service;

import com.ironhack.ironLibrary.model.Book;
import com.ironhack.ironLibrary.model.Issue;
import com.ironhack.ironLibrary.model.Student;
import com.ironhack.ironLibrary.repository.IssueRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
Expand All @@ -14,8 +17,19 @@ public class IssueServiceImpl implements IIssueService{

@Autowired
private IssueRepository issueRepository;

@Override
public Issue save(Student student, Book book) {
return issueRepository.save(new Issue(student, book));
}

@Override
public Optional<List<Object[]>> findAllBooksAndIssuesByUsn(String usn){
return issueRepository.findAllBooksAndIssuesByUsn(usn);
}

@Override
public Optional<List<Book>> findAllBooksIssuedByUsn(String usn) {
return issueRepository.findAllBooksByUsn(usn);
}
}
177 changes: 150 additions & 27 deletions src/main/java/com/ironhack/ironLibrary/service/MenuServiceImpl.java
Original file line number Diff line number Diff line change
@@ -1,19 +1,18 @@
package com.ironhack.ironLibrary.service;

import com.ironhack.ironLibrary.IronLibraryApplication;
import com.ironhack.ironLibrary.model.Author;
import com.ironhack.ironLibrary.model.Book;
import com.ironhack.ironLibrary.model.Issue;
import com.ironhack.ironLibrary.model.Student;
import com.ironhack.ironLibrary.utils.DataOutput;
import com.ironhack.ironLibrary.utils.InvalidBookInformationException;
import com.ironhack.ironLibrary.utils.NoBookFoundException;
import org.springframework.beans.factory.annotation.Autowired;



import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import java.util.Scanner;
import java.sql.SQLOutput;
import java.util.*;

import com.ironhack.ironLibrary.utils.Validator;
import org.springframework.stereotype.Service;
Expand All @@ -31,8 +30,7 @@ public class MenuServiceImpl implements IMenuService{
private IStudentService studentService;

@Autowired
private IIssueService iIssueService;

private IIssueService issueService;

/**
* TODO Testing
Expand Down Expand Up @@ -63,14 +61,27 @@ public List<String> getNewBookInformation(){
return bookAndAuthorDetails;
}

public List<String> getNewIssueInformation(){

List <String> issueDetails = new ArrayList<>();
String usn = Validator.userInput("Enter usn: ", true, "validateUsnFormat",
"USN must have 11 digits");
issueDetails.add(usn);
String studentName = Validator.userInput("Enter name: ", true, "validateStringGeneralFormat",
"The author name only accepts letters");
issueDetails.add(studentName);
String isbn = Validator.userInput("Enter book ISBN: ",true, "checkISBNFormat",
"The ISBN must follow the next format: 978-92-95055-02-5");
issueDetails.add(isbn);
return issueDetails;
}

public void addBook(List<String> bookAndAuthorInformation) {

if (bookAndAuthorInformation.size() != 6) {
throw new InvalidBookInformationException(
"The book and author information must be a six-element string list.");
}

String isbn = bookAndAuthorInformation.get(0);
String title = bookAndAuthorInformation.get(1);
String category = bookAndAuthorInformation.get(2);
Expand Down Expand Up @@ -127,39 +138,33 @@ public List<Book> searchBookByCategory(String category) throws NoBookFoundExcept
}
}


@Override
public void issueBookToStudent(List<String> issueData) throws NoBookFoundException {
String usn = issueData.get(0);
String name = issueData.get(1);
String isbn = issueData.get(2);

if (!Validator.checkISBNFormat(isbn)
|| !Validator.validateStringGeneralFormat(name)
|| !Validator.checkUsnFormat(usn)
) {
throw new InvalidBookInformationException("The provided information is invalid. Please check the format");
}

Optional<Student> optionalStudent = studentService.findStudentByUsnAndName(usn,name);
if(optionalStudent.isEmpty()){
studentService.save(usn, name);
}else{
Student student = optionalStudent.get();
Book book = bookService.findByIsbn(isbn).orElseThrow(() -> new NoBookFoundException("No books are found with that ISBN"));
if (book.getQuantity() < 1) {
throw new NoBookFoundException("Quantity unavailable");
} else {
book.setQuantity(book.getQuantity() - 1);
bookService.save(book);
iIssueService.save(student, book);
// Update book quantity

}
optionalStudent = studentService.findStudentByUsnAndName(usn,name);
}
Student student = optionalStudent.get();
Book book = bookService.findByIsbn(isbn).orElseThrow(() -> new NoBookFoundException("No books are found with that ISBN"));
if (book.getQuantity() < 1) {
throw new NoBookFoundException("Quantity unavailable");
} else {
book.setQuantity(book.getQuantity() - 1);
bookService.save(book);
issueService.save(student, book);
// Update book quantity
}
}


public List<Object[]> searchBooksByUsn(String usn) throws Exception {
Optional<Student> optionalStudent = studentService.findStudentByUsn(usn);
if(optionalStudent.isPresent()){
Expand All @@ -172,14 +177,132 @@ public List<Object[]> searchBooksByUsn(String usn) throws Exception {
}
}else{
throw new Exception("No student found for this usn: " + usn);

}
}

@Override
public List<Object[]> searchBooksAlongAuthors() throws NoBookFoundException {
return authorService.findAllBooksWithAuthors().orElseThrow(()-> new NoBookFoundException("No Books are found"));
}

@Override
public void mainManu() throws Exception {
System.out.println("Welcome to Iron Library book management system!!");
System.out.println("Please access one of the following options");
Scanner scanner = new Scanner(System.in);
int userInput = 0;
boolean errorHandling = false;
printMenuOptions();

do{
while (!scanner.hasNextInt()) {
System.out.println("Invalid input. Please enter a number from 1 to 8.");
scanner.next();
}
userInput = scanner.nextInt();

if (userInput < 1 || userInput > 8) {
System.out.println("Invalid input. Please enter a number from 1 to 8.");
} else {
errorHandling = executeMenuOption(userInput);
if (userInput != 8) {
printMenuOptions();
}
}
}while(userInput != 8 || !errorHandling);

}

private void printMenuOptions(){
System.out.println("""
Menu:
1 - Add a book
2 - Search book by title
3 - Search book by category
4 - Search book by Author
5 - List all books along with author
6 - Issue book to student
7 - List books by usn
8 - Exit
only digits between 1-8 are available""");
}

private boolean executeMenuOption(int userInput) {
boolean isError = false;
switch (userInput){
case 1:
List<String> bookAndAuthorInformation = getNewBookInformation();
addBook(bookAndAuthorInformation);
return isError;
case 2:
System.out.println("option 2");
break;
case 3:
String category = Validator.userInput("Enter category: ", true, "validateStringGeneralFormat",
"The category only accepts letters");
List<Book> books = null;
try {
books = searchBookByCategory(category);
System.out.println(DataOutput.listBookTable(books));
return isError;
} catch (NoBookFoundException e) {
System.out.println(e.getMessage());
return true;
}
case 4:
String authorName = Validator.userInput("Enter author name: ", true, "validateStringGeneralFormat",
"The author name only accepts letters");
Book book = null;
try {
book = searchBookByAuthor(authorName);
System.out.println(DataOutput.oneBookTable(book));
return isError;
} catch (NoBookFoundException e) {
System.out.println(e.getMessage());
return true;
}
case 5:
List<Object[]> objects = null;
try {
objects = searchBooksAlongAuthors();
System.out.println(DataOutput.listBookTableWithAuthor(objects));
return isError;
} catch (NoBookFoundException e) {
System.out.println(e.getMessage());
return true;
}
case 6:
List <String> issueData = getNewIssueInformation();
try {
issueBookToStudent(issueData);
return isError;
} catch (NoBookFoundException e) {
System.out.println(e.getMessage());
return true;
}
case 7:
String usn = Validator.userInput("Enter usn: ", true, "validateUsnFormat",
"USN must have 11 digits");
try {
List <Object[]> info = searchBooksByUsn(usn);
Object[] firstElement = info.get(0);
Issue issue = (Issue) firstElement[1];
Student student = issue.getIssueStudent();
System.out.println(DataOutput.listBookTableByUsn(info, student));
return isError;
} catch (Exception e) {
System.out.println(e.getMessage());
return true;
}
case 8:
System.out.println("Thanks for use our application :)");
System.exit(0);
break;
default:
System.out.println("Invalid input. Please enter a number from 1 to 8.");
}
return isError;
}
}


Expand Down
Loading
Loading