Browse Source

Running the app

main
Jesus 1 year ago
parent
commit
84f0c44b14
4 changed files with 109 additions and 11 deletions
  1. +21
    -0
      src/main/java/com/jesuspinar/booksearch/App.java
  2. +0
    -10
      src/main/java/com/jesuspinar/booksearch/BooksearchApplication.java
  3. +88
    -0
      src/main/java/com/jesuspinar/booksearch/Run.java
  4. +0
    -1
      src/main/java/com/jesuspinar/booksearch/service/BookService.java

+ 21
- 0
src/main/java/com/jesuspinar/booksearch/App.java View File

@ -0,0 +1,21 @@
package com.jesuspinar.booksearch;
import com.jesuspinar.booksearch.config.AppConfig;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
public class App {
public static void main(String[] args) {
ApplicationContext appContext = new AnnotationConfigApplicationContext(AppConfig.class);
Run runApp = appContext.getBean(Run.class);
runApp.run(args);
((AnnotationConfigApplicationContext) appContext).close();
}
}

+ 0
- 10
src/main/java/com/jesuspinar/booksearch/BooksearchApplication.java View File

@ -1,10 +0,0 @@
package com.jesuspinar.booksearch;
public class BooksearchApplication {
public static void main(String[] args) {
}
}

+ 88
- 0
src/main/java/com/jesuspinar/booksearch/Run.java View File

@ -0,0 +1,88 @@
package com.jesuspinar.booksearch;
import com.jesuspinar.booksearch.model.Book;
import com.jesuspinar.booksearch.service.IBookQueryService;
import com.jesuspinar.booksearch.service.IBookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
@Component
public class Run {
@Autowired
IBookService iBookService;
@Autowired
IBookQueryService iBookQueryService;
@Autowired
Help help;
public void run(String[] args) {
if (args.length < 1) {
System.out.println("Error de sintaxis");
System.out.println(help.getHelp());
} else if (args.length == 1) {
switch (args[0].toLowerCase()) {
case "-lg":
iBookService.findAllGenres().forEach(System.out::println);
break;
case "-h":
System.out.println(help.getHelp());
break;
default:
System.out.println("Error de sintaxis");
System.out.println(help.getHelp());
}
} else if (args.length % 2 != 0) {
System.out.println("Error de sintaxis");
System.out.println(help.getHelp());
} else if (args.length > 8) {
System.out.println("Error de sintaxis");
System.out.println(help.getHelp());
} else {
List<String[]> argumentos = new ArrayList<>();
for (int i = 0; i < args.length; i += 2) {
argumentos.add(new String[]{args[i], args[i + 1]});
}
boolean error = false;
for (String[] argumento : argumentos) {
switch (argumento[0].toLowerCase()) {
case "-ag":
iBookQueryService.anyGenre(argumento[1].split(","));
break;
case "-tg":
iBookQueryService.allGenres(argumento[1].split(","));
break;
case "-t":
iBookQueryService.anyTitle(argumento[1]);
break;
default:
error = true;
System.out.println("Error de sintaxis");
System.out.println(help.getHelp());
}
}
if (!error) {
Collection<Book> result = iBookQueryService.exec();
System.out.printf("%s\t%-35s\t%-22s\t%s\n", "ID", "Título", "Autor", "Género");
if (result != null) {
result.forEach(book -> System.out.printf("%s\t%-35s\t%-22s\t%s\n",
book.getId(), book.getTitle(), book.getAuthors(),
book.getGenre()));
} else {
System.out.println("No hay libros que cumplan esos criterios.");
}
}
}
}
}

+ 0
- 1
src/main/java/com/jesuspinar/booksearch/service/BookService.java View File

@ -27,7 +27,6 @@ public class BookService implements IBookService{
result = iBookDao.findAll()
.stream()
.map(book -> book.getGenre())
//.flatMap(l -> Stream.of(l))//TODO: Check this if other java version!
.distinct()
.sorted()
.collect(Collectors.toList());


Loading…
Cancel
Save