Skip to content
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
71 changes: 58 additions & 13 deletions src/main/java/org/example/App.java
Original file line number Diff line number Diff line change
@@ -1,27 +1,72 @@
package org.example;

import jakarta.persistence.*;
import org.example.EMFactory;
import org.example.user.SessionManager;
import org.example.user.UserCLI;
import org.example.user.UserService;
import org.example.user.repository.JpaUserRepository;
import org.example.user.repository.UserRepository;

import java.util.Scanner;

public class App {
static void main(String[] args) {
initialize(); // "Warm up" the factory

final UserRepository userRepository = new JpaUserRepository();
final UserService userService = new UserService(userRepository);
Scanner scanner = new Scanner(System.in);
final UserCLI userCli = new UserCLI(userService, scanner);
SearchCli searchCli = new SearchCli(scanner, EMFactory.getEntityManager());

try (EntityManagerFactory emf = Persistence.createEntityManagerFactory("library_system");
EntityManager em = emf.createEntityManager()) {
// #######################
// Application starts here
// #######################
boolean appRunning = true;
while (appRunning) {
System.out.println("\n\nBIBLIOTEKSSYSTEMET");
System.out.println("Startsida Inloggad som: " + SessionManager.loggedInDisplayName());
System.out.println("========================================================");

em.getTransaction().begin();
if (SessionManager.isLoggedIn()) {
System.out.println("1. Sök bok | 2. Hantera användare | 3. Logga ut | 0. Avsluta");
} else {
System.out.println("1. Sök bok | 2. Logga in/Skapa konto | 0. Avsluta");
}

User user = new User();
System.out.print("Menyval: ");
String choice = scanner.nextLine();
switch (choice) {
case "1" -> {
searchCli.bookSearchCli();
}
case "2" -> {
if (SessionManager.isLoggedIn()) userCli.manageUserMenu();
else userCli.userMenu();
}
case "3" -> {
if (SessionManager.isLoggedIn()) userCli.logout();
}
case "0" -> appRunning = false;
default -> System.out.println("Ogiltigt val.");
}
}
terminate();
}

user.setUsername("testUser");
user.setEmail("testMail");
user.setPassword("test");
user.setFirstName("test");
user.setLastName("test");
private static void terminate() {
System.out.println("Bibliotekssystemet avslutat. Välkommen åter!");
EMFactory.close();
}

em.persist(user);
public static void initialize() {
// Turn off Hibernate logging
System.setProperty("org.jboss.logging.provider", "jdk");
java.util.logging.Logger hibernateLogger = java.util.logging.Logger.getLogger("org.hibernate");
hibernateLogger.setLevel(java.util.logging.Level.SEVERE);

em.getTransaction().commit();
}
// Initialize EntityManagerFactory
EMFactory.init();
}
}

Expand Down
91 changes: 45 additions & 46 deletions src/main/java/org/example/SearchCli.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,56 +9,55 @@
import java.util.stream.Collectors;

public class SearchCli {
static EntityManager em;
static BookSearch searchService = new BookSearch();
static Scanner sc;

// Constructor?
public SearchCli(Scanner sc, EntityManager em) {
this.sc = sc;
this.em = em;
}

public static void main(String[] args) {
EntityManagerFactory emf = Persistence.createEntityManagerFactory("library_system");
EntityManager em = emf.createEntityManager();

BookSearch searchService = new BookSearch();
Scanner sc = new Scanner(System.in);

boolean running = true;

while (running) {
System.out.println("\n Bibliotekssystem – Sök");
System.out.println("1) Sök på titel");
System.out.println("2) Sök på författare");
System.out.println("3) Sök på genre");
System.out.println("0) Avsluta");
System.out.print("Välj: ");

String choice = sc.nextLine().trim();

switch (choice) {
case "1" -> {
System.out.print("Skriv titel (eller del av titel): ");
String q = sc.nextLine();
List<Book> results = searchService.searchByTitle(em, q);
handleSearchFlow(sc, results);
}
case "2" -> {
System.out.print("Skriv författare (för- eller efternamn): ");
String q = sc.nextLine();
List<Book> results = searchService.searchByAuthor(em, q);
handleSearchFlow(sc, results);
public static void bookSearchCli() {
boolean running = true;

while (running) {
System.out.println("\n Bibliotekssystem – Sök");
System.out.println("1) Sök på titel");
System.out.println("2) Sök på författare");
System.out.println("3) Sök på genre");
System.out.println("0) Tillbaka");
System.out.print("Välj: ");

String choice = sc.nextLine().trim();

switch (choice) {
case "1" -> {
System.out.print("Skriv titel (eller del av titel): ");
String q = sc.nextLine();
List<Book> results = searchService.searchByTitle(em, q);
handleSearchFlow(sc, results);
}
case "2" -> {
System.out.print("Skriv författare (för- eller efternamn): ");
String q = sc.nextLine();
List<Book> results = searchService.searchByAuthor(em, q);
handleSearchFlow(sc, results);
}
case "3" -> {
System.out.print("Skriv genre: ");
String q = sc.nextLine();
List<Book> results = searchService.searchByGenre(em, q);
handleSearchFlow(sc, results);
}
case "0" -> running = false;
default -> System.out.println("Ogiltigt val, försök igen.");
}
case "3" -> {
System.out.print("Skriv genre: ");
String q = sc.nextLine();
List<Book> results = searchService.searchByGenre(em, q);
handleSearchFlow(sc, results);
}
case "0" -> running = false;
default -> System.out.println("Ogiltigt val, försök igen.");
}
}

sc.close();
em.close();
emf.close();

System.out.println("Hej då!");
}

// Flöde: lista -> välj -> detalj

Expand Down Expand Up @@ -105,15 +104,15 @@ private static void chooseAndShowBookDetails(Scanner sc, List<Book> results) {
Book selected = results.get(n - 1);
printBookDetails(selected);

System.out.print("Tryck Enter för att fortsätta...");
System.out.print("Tryck Enter för att gå tillbaka til sök");
sc.nextLine();
return;
}
}

private static void printBookDetails(Book b) {
System.out.println("\n==============================");
System.out.println("📖 " + b.getTitle());
System.out.println(" " + b.getTitle());
System.out.println("------------------------------");
System.out.println("Författare: " + formatAuthors(b));
System.out.println("Genre: " + formatGenres(b));
Expand Down