-
Notifications
You must be signed in to change notification settings - Fork 0
Features/loan #12
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
Features/loan #12
Changes from all commits
190396f
3ceaaea
4875777
28ecdc7
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -7,6 +7,8 @@ | |
| import org.example.user.repository.JpaUserRepository; | ||
| import org.example.user.repository.UserRepository; | ||
|
|
||
| import java.time.format.DateTimeFormatter; | ||
| import java.util.List; | ||
| import java.util.Scanner; | ||
|
|
||
| public class App { | ||
|
|
@@ -18,6 +20,8 @@ static void main(String[] args) { | |
| Scanner scanner = new Scanner(System.in); | ||
| final UserCLI userCli = new UserCLI(userService, scanner); | ||
| SearchCli searchCli = new SearchCli(scanner, EMFactory.getEntityManager()); | ||
| DateTimeFormatter formatter = | ||
| DateTimeFormatter.ofPattern("yyyy-MM-dd"); | ||
|
|
||
| // ####################### | ||
| // Application starts here | ||
|
|
@@ -29,24 +33,63 @@ static void main(String[] args) { | |
| System.out.println("========================================================"); | ||
|
|
||
| if (SessionManager.isLoggedIn()) { | ||
| System.out.println("1. Sök bok | 2. Hantera användare | 3. Logga ut | 0. Avsluta"); | ||
| System.out.println("1. Sök bok | 2. Hantera användare | 3. Logga ut | 4. Mina lån | 0. Avsluta"); | ||
| } else { | ||
| System.out.println("1. Sök bok | 2. Logga in/Skapa konto | 0. Avsluta"); | ||
| } | ||
|
|
||
| System.out.print("Menyval: "); | ||
| String choice = scanner.nextLine(); | ||
| LoanServices loanServices = new LoanServices(EMFactory.getEntityManager()); | ||
| switch (choice) { | ||
| case "1" -> { | ||
| searchCli.bookSearchCli(); | ||
| } | ||
| case "2" -> { | ||
| if (SessionManager.isLoggedIn()) userCli.manageUserMenu(); | ||
| else userCli.userMenu(); | ||
| if (SessionManager.isLoggedIn()) { | ||
| userCli.manageUserMenu(); | ||
| } else { | ||
| userCli.userMenu(); | ||
| } | ||
| } | ||
| case "3" -> { | ||
| if (SessionManager.isLoggedIn()) userCli.logout(); | ||
| } | ||
| case "4" -> { | ||
| System.out.println("Dina aktiva lån:"); | ||
| List<Loan> loans = loanServices.activeLoans(SessionManager.getCurrentUser()); | ||
|
|
||
| boolean running = true; | ||
| while (running) { | ||
| if (!loans.isEmpty()) { | ||
| int counter = 0; | ||
| for (Loan loan : loans) { | ||
| counter += 1; | ||
| System.out.println(counter + ". Titel: " + loan.getBook().getTitle() + " - Åter: " + loan.getReturnDate() | ||
| .format(formatter)); | ||
| } | ||
| System.out.println("Välj nummer för den bok du vill lämna tillbaka (0 = tillbaka): "); | ||
| String inputString = scanner.nextLine(); | ||
| Integer input = Integer.parseInt(inputString); | ||
| if (input >= 1 && input <= loans.size()) { | ||
| loanServices.returnBook( | ||
| SessionManager.getCurrentUser(), | ||
| loans.get(input - 1).getBook() | ||
| ); | ||
| loans.remove(input - 1); | ||
| System.out.println("Du har lämnat tillbaka din bok!"); | ||
| } else if (input == 0) { | ||
| running = false; | ||
| } else { | ||
| System.out.println("ogiltigt val."); | ||
| } | ||
|
|
||
| } else { | ||
| System.out.println("Inga aktiva lån."); | ||
| running = false; | ||
| } | ||
| } | ||
| } | ||
|
Comment on lines
+58
to
+92
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add login check and handle Two issues:
🐛 Proposed fix case "4" -> {
+ if (!SessionManager.isLoggedIn()) {
+ System.out.println("Du måste logga in först.");
+ break;
+ }
System.out.println("Dina aktiva lån:");
List<Loan> loans = loanServices.activeLoans(SessionManager.getCurrentUser());
boolean running = true;
while (running) {
if (!loans.isEmpty()) {
int counter = 0;
for (Loan loan : loans) {
counter += 1;
System.out.println(counter + ". Titel: " + loan.getBook().getTitle() + " - Åter: " + loan.getReturnDate()
.format(formatter));
}
System.out.println("Välj nummer för den bok du vill lämna tillbaka (0 = tillbaka): ");
String inputString = scanner.nextLine();
- Integer input = Integer.parseInt(inputString);
+ int input;
+ try {
+ input = Integer.parseInt(inputString);
+ } catch (NumberFormatException e) {
+ System.out.println("Skriv en siffra.");
+ continue;
+ }
if (input >= 1 && input <= loans.size()) {🤖 Prompt for AI Agents |
||
| case "0" -> appRunning = false; | ||
| default -> System.out.println("Ogiltigt val."); | ||
| } | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🛠️ Refactor suggestion | 🟠 Major
Move
LoanServicesinstantiation outside the loop.Creating a new
LoanServices(and thus potentially a new EntityManager reference) on every iteration of the main loop is inefficient. This should be instantiated once before the loop, similar tosearchClianduserCli.♻️ Suggested refactor
🤖 Prompt for AI Agents