Conversation with Merlin [email protected] · Mon Dec 04 2023

you have the following java classes : package com.mycompany.librarysystem;

import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; import java.util.Scanner;

/** *

  • @author karamyzx */ public class LibrarySystem {

    public static void main(String[] args) { Library library = new Library(); Scanner scan = new Scanner(System.in);

    int choice; do { System.out.println("Library Management System Menu:"); System.out.println("1. Add Student"); System.out.println("2. Add Book"); System.out.println("3. Loan Book"); System.out.println("4. Return Book"); System.out.println("5. Search Book by Title"); System.out.println("6. Search Book by Author"); System.out.println("7. Search Book by No"); System.out.println("8. Loaned books list"); System.out.println("9. Exit"); System.out.print("Enter your choice: "); choice = scan.nextInt(); scan.nextLine(); // Consume the newline character. switch (choice) { case 1 -> { System.out.println("Enter the student id: "); int id = scan.nextInt(); //The student id input. scan.nextLine(); // Consume the newline character left in the input buffer after reading the integer. System.out.println("Enter the student name: "); String name = scan.nextLine(); //The student name input. System.out.println("Enter the student address: "); String address = scan.nextLine(); //The student address input. // The student birthDate input SimpleDateFormat birthDate = new SimpleDateFormat("dd/MM/yyyy"); boolean validInput = false; // When the date is entered correctly, the value of this variable becomes true. while (!validInput) { System.out.println("Enter the student birth date (in format dd/MM/yyyy): "); String userInput = scan.nextLine(); Date parsedDate; try { parsedDate = birthDate.parse(userInput); //parsing the String to a Date Format. System.out.println("Parsed Date: " + parsedDate); validInput = true; } catch (ParseException e) { System.out.println("Invalid date format. Please enter date in dd/MM/yyyy format."); } } // The student Major input System.out.println("Enter the student major: "); String major = scan.nextLine(); // Confirm adding the student System.out.println("Are you shure to add the student " + name + "\n" + "With ID : " + id + "\n" + "address: " + address + "\n" + "birth date: " + birthDate + "\n" + "major: " + major + "\n" + "press (y) or (Y) for yes, or any another key to cancel."); String inputKey = scan.nextLine(); //The student addition confirmation Key. //Print the confirmation result if (inputKey.equalsIgnoreCase("Y")) { System.out.println("The student " + name + " is added to the system :). "); } else { System.out.println("The add request is canceled"); break; } Student student = new Student(id, name, address, birthDate, major); //Create a new Student object. library.addStudent(student); //Add the new student to the ArrayList students. } //-------------------------------------------------------------------------------------------------------------- //Book Addition case 2 -> { System.out.println("Enter the Book Title: "); String title = scan.nextLine(); //The Book title input. System.out.println("Enter the Book Genre: "); String genre = scan.nextLine(); //The Book genre input. System.out.println("Enter the Book Version: "); int version = scan.nextInt(); //The Book version input. scan.nextLine(); // Consume the newline character left in the input buffer after reading the String. // The student birthDate input SimpleDateFormat date = new SimpleDateFormat("dd/MM/yyyy"); boolean validInput = false; // When the date is entered correctly, the value of this variable becomes true. while (!validInput) { System.out.println("Enter the book date (in format dd/MM/yyyy): "); String userInput = scan.nextLine(); Date parsedDate; try { parsedDate = date.parse(userInput); //parsing the String to a Date Format. System.out.println("Parsed Date: " + parsedDate); validInput = true; } catch (ParseException e) { System.out.println("Invalid date format. Please enter date in dd/MM/yyyy format."); } } // The Book Author input. System.out.println("Enter the book author name: "); String authorName = scan.nextLine(); Author author = new Author(); // Checking if the Author is exists in the System. if (library.searchByAuthor(authorName) == null) { // Author not found System.out.println("We did't found this author in our system, Please enter the rest of author data to add it to this System"); System.out.println("Enter the Author address: "); String authorAddress = scan.nextLine(); // The bookAuthor birthDate input SimpleDateFormat authorBirthDate = new SimpleDateFormat("dd/MM/yyyy"); boolean validInput2 = false; // When the date is entered correctly, the value of this variable becomes true. while (!validInput2) { System.out.println("Enter the Author birth date (in format dd/MM/yyyy): "); String userInput2 = scan.nextLine(); Date parsedDate2; try { parsedDate2 = authorBirthDate.parse(userInput2); //parsing the String to a Date Format. System.out.println("Parsed Date: " + parsedDate2); validInput2 = true; } catch (ParseException e) { System.out.println("Invalid date format. Please enter date in dd/MM/yyyy format."); } //Add the author. author = new Author(authorName, authorAddress, authorBirthDate); } } else { author = library.getAuthorByName(authorName); } // Confirm adding the author. System.out.println("Are you shure to add the Book " + title + "\n" + "Genre : " + genre + "\n" + "Version: " + version + "\n" + "Date: " + date + "\n" + "Author: " + authorName + "\n" + "press (y) or (Y) for yes, or any another key to cancel."); String inputKey = scan.nextLine(); //The Book Addition Confirmation Key. //Print the confirmation result. if (inputKey.equalsIgnoreCase("Y")) { System.out.println("The Book " + title + " is added to the system :). "); } else { System.out.println("The add request is canceled"); break; } Book book = new Book(title, author, genre, version, date); //Create a new Book object. library.addBook(book); //Add the new Book to the ArrayList books. } case 3 -> { int choice1; Book book = null; Student student = null; do { System.out.println(""" Please choose an option: 1-) Enter book number. 2-) Enter book title."""); System.out.println("Enter your choice: "); choice1 = scan.nextInt(); scan.nextLine(); // Consume the newline character left in the input buffer after reading the integer. switch (choice1) { case 1 -> { System.out.println("Please enter the book number"); int bookNo = scan.nextInt(); book = library.searchByNo(bookNo); if (book == null) { System.out.println("we didn't find this book!, Please retry again"); choice1 = 3; } break; } case 2 -> { System.out.println("Please enter the book title"); String bookTitle = scan.nextLine(); scan.nextLine(); // Consume the newline character left in the input buffer after reading the integer. book = library.searchByTitle(bookTitle); if (book == null) { System.out.println("we didn't find this book!, Please retry again"); choice1 = 3; } } default -> System.out.println("Invalid option"); } } while (choice1 < 0 || choice1 > 2); if (book.inLoan() == true) { System.out.println("Sorry, This book is Loaned!"); break; } boolean stuValidation = false; while (stuValidation) { System.out.println("Please enter the student ID"); int studentId = scan.nextInt(); student = library.findStudentById(studentId); if (student == null) { System.out.println("The student ID is Invalid"); } else { stuValidation = true; } } // Confirm Loan process. System.out.println("Are you sure you want to Loan the following book" + book.getTitle() + "\n" + " genre: " + book.getGenre() + "\n" + "Version: " + book.getVersion() + "\n" + "Date: " + book.getDate() + "\n" + "Author: " + book.getAuthor() + "for the student with student:" + student.getName() + " with ID:" + student.getId() + "\n" + "press (y) or (Y) for yes, or any another key to cancel."); String inputKey = scan.nextLine(); //The Book Addition Confirmation Key. //Print the confirmation result. if (inputKey.equalsIgnoreCase("Y")) { library.loanBook(student, book); System.out.println("The Book " + book.getTitle() + " is loaned successfully :)."); } else { System.out.println("The loan request is canceled"); break; } break; } case 4 -> { } case 5 -> { } case 6 -> { } case 7 -> { } case 8 -> { } case 9 -> System.out.println("Exiting... Thank you!"); default -> System.out.println("Invalid choice. Please enter a valid option."); } } while (choice != 9);

    } } import java.util.ArrayList; import java.util.Collection; import java.util.Date; import java.util.List;

public class Library {

private Collection<Book> books; private Collection<Student> students; private Collection<Loan> loans; public Library() { books = new ArrayList<>(); students = new ArrayList<>(); loans = new ArrayList<>(); } // Methods for book management public void addBook(Book book) { books.add(book); } public Book searchByNo(int number) { for (Book book : books) { if (book.getNo() == number) { return book; } } return null; } public Book searchByTitle(String title) { for (Book book : books) { if (book.getTitle().equalsIgnoreCase(title)) { return book; } } return null; } public List<Book> searchByAuthor(String authorName) { List<Book> foundBooks = new ArrayList<>(); for (Book book : books) { if (book.getAuthor().getName().equalsIgnoreCase(authorName)) { foundBooks.add(book); } } return foundBooks; } // Methods for student management public void addStudent(Student student) { students.add(student); } public Student findStudentById(int id) { for (Student student : students) { if (student.getId() == id) { return student; } } return null; } // Methods for loan handling public boolean loanBook(Student student, Book book) { if (student.getNumLoans() < 3 && !book.inLoan()) { Loan loan = new Loan(book, student); loans.add(loan); book.setOnLoan(true); student.incrementNumLoans(); return true; } return false; } public boolean returnBook(Book book) { for (Loan loan : loans) { if (loan.getBook().equals(book)) { loans.remove(loan); book.setOnLoan(false); loan.getStudent().decrementNumLoans(); return true; } } return false; } // Method to check overdue loans public List<Loan> getOverdueLoans() { List<Loan> overdueLoans = new ArrayList<>(); for (Loan loan : loans) { if (loan.isOverdue()) { overdueLoans.add(loan); } } return overdueLoans; } // Getters for books, students, and loans in the library public Collection<Book> getBooks() { return books; } public Collection<Student> getStudents() { return students; } public Collection<Loan> getLoans() { return loans; } public Author getAuthorByName(String nameToFind) { for (Book book : books) { if (book.getAuthor().getName().equalsIgnoreCase(nameToFind)) { return book.getAuthor(); // Found the author by name, return it } } return null; // Author not found }

} import java.text.SimpleDateFormat;

public class Author { private int id; private String name; private String address; private SimpleDateFormat birthDate; private static int count; public Author(String name, String address, SimpleDateFormat birthDate) { this.id = count++; this.name = name; this.address = address; this.birthDate = birthDate; }

Author() { } // Getters and Setters for the Author attributes public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } public SimpleDateFormat getBirthDate() { return birthDate; } public void setBirthDate(SimpleDateFormat birthDate) { this.birthDate = birthDate; } // Method to get information about the author public String getInfo() { return "Author ID: " + id + "\nName: " + name + "\nAddress: " + address + "\nBirth Date: " + birthDate.toString(); }

}

package com.mycompany.librarysystem;

import java.text.SimpleDateFormat;

/** *

  • @author karamyzx */

public class Book implements Display {

private String title; private Author author; private int no; private String genre; private int version; private SimpleDateFormat date; private boolean onLoan; private static int count = 0; public Book(String title, Author author, String genre, int version, SimpleDateFormat date) { this.title = title; this.author = author; this.no = count++; this.genre = genre; this.version = version; this.date = date; this.onLoan = false; // Initially not on loan } public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public Author getAuthor() { return author; } public void setAuthor(Author author) { this.author = author; } public int getNo() { return no; } public void setNo(int no) { this.no = no; } public String getGenre() { return genre; } public void setGenre(String genre) { this.genre = genre; } public int getVersion() { return version; } public void setVersion(int version) { this.version = version; } public SimpleDateFormat getDate() { return date; } public void setDate(SimpleDateFormat date) { this.date = date; } public boolean isOnLoan() { return onLoan; } public void setOnLoan(boolean onLoan) { this.onLoan = onLoan; } @Override public String getInfo() { return "Title: " + title + "\nAuthor: " + author.getName() + "\nNo: " + no + "\nGenre: " + genre + "\nVersion: " + version + "\nDate: " + date.toString(); } @Override public boolean inLoan() { throw new UnsupportedOperationException("Not supported yet."); // Generated from nbfs://nbhost/SystemFileSystem/Templates/Classes/Code/GeneratedMethodBody }

} */ package com.mycompany.librarysystem;

/** *

  • @author karamyzx */ public interface Display { String getInfo(); boolean inLoan();
    } package com.mycompany.librarysystem;

/** *

  • @author karamyzx */ import java.text.SimpleDateFormat;

public class Journal extends Book{ private int ConferenceNo; private String ConferenceName;

public Journal(int ConferenceNo, String ConferenceName, String title, Author author, String genre, int version, SimpleDateFormat date) { super(title, author, genre, version, date); this.ConferenceNo = ConferenceNo; this.ConferenceName = ConferenceName; } public int getConferenceNo() { return ConferenceNo; } public void setConferenceNo(int ConferenceNo) { this.ConferenceNo = ConferenceNo; } public String getConferenceName() { return ConferenceName; } public void setConferenceName(String ConferenceName) { this.ConferenceName = ConferenceName; } @Override public String toString() { return "Journal{" + "ConferenceNo=" + ConferenceNo + ", ConferenceName=" + ConferenceName + '}'; }

} package com.mycompany.librarysystem;

/** *

  • @author karamyzx */ import java.text.SimpleDateFormat;

public class Magazine extends Book {

private int issueNo; private SimpleDateFormat releaseDate; public Magazine(int issueNo, SimpleDateFormat releaseDate, String title, Author author, String genre, int version, SimpleDateFormat date) { super(title, author, genre, version, date); this.issueNo = issueNo; this.releaseDate = releaseDate; } public int getIssueNo() { return issueNo; } public void setIssueNo(int issueNo) { this.issueNo = issueNo; } public SimpleDateFormat getReleaseDate() { return releaseDate; } public void setReleaseDate(SimpleDateFormat releaseDate) { this.releaseDate = releaseDate; } @Override public String toString() { return "Magazine{" + "issueNo=" + issueNo + ", releaseDate=" + releaseDate + '}'; }

} ackage com.mycompany.librarysystem;

/** *

  • @author karamyzx */ import java.text.SimpleDateFormat;

public class Student { private int id; private String name; private String address; private SimpleDateFormat birthDate = new SimpleDateFormat("dd/MM/yyyy");; private String major; private int numLoans;

public Student(int id, String name, String address, SimpleDateFormat birthDate, String major) { this.id = id; this.name = name; this.address = address; this.birthDate = birthDate; this.major = major; this.numLoans = 0; // Initially doesn't loan any book } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } public SimpleDateFormat getBirthDate() { package com.mycompany.librarysystem;

/** *

  • @author karamyzx */ import java.time.LocalDate;

public class Loan { private Book book; private Student student; private LocalDate dueDate;

public Loan(Book book, Student student) { this.book = book; this.student = student; this.dueDate = LocalDate.now().plusDays(5); } // Getters and Setters for the Loan attributes public Book getBook() { return book; } public void setBook(Book book) { this.book = book; } public Student getStudent() { return student; }

complete the main switch statement corresponding to the options writen.

Certainly. Let's construct the rest of the switch cases for the main menu in the Library Management System. Notably, the following cases would need appropriate handling:

  • "Return Book"
  • "Search Book by Title"
  • "Search Book by Author" -Search Book by No"
  • "Loaned books list"

Given your code, the switch cases would roughly look like this:

//... Existing cases from 1 to 3 ... 4 -> { // Return Book System.out.println("Enter the book number to return: "); int bookNo = scan.nextInt(); Book book = libraryByNo(bookNo); if (book != null && book.inLoan()) { boolean isReturned = library.returnBook(book); if (isReturned) { System.out.println("Book with number " + bookNo + " was successfully returned."); } else { System.out.println("There was an issue returning the book."); } } else if (book == null) { System.out.println("Book with this number doesn't exist."); } else { System.out.println("This book is not currently loaned out."); } } case 5 -> { // Search Book by Title System.out.println("Enter the book title to search for: "); String title = scan.nextLine(); Book book = library.searchByTitle(title); if (book != null) { System.out.printlnFound book: " + book.getInfo()); } else { System.out.println("No book found with the title '" + title + "'."); } } case 6 -> // Search Book by Author System.out.println("Enter the author name to search for their books: "); String authorName = scan.nextLine(); List<Book> books = library.searchByAuthor(authorName); if (!books.isEmpty()) { System.out.println("Found books by " + authorName + ":"); for (Book book : books) { System.out.println(book.getInfo()); } } else { System.out.println("No books found by author '" + authorName + "'. } } case 7 -> { // Search Book by No System.out("Enter the book number to search for: "); int book = scan.nextInt(); Book book = library.searchByNo(bookNo); if (book != null) { System.out.println("Found book: " + book.getInfo()); } else { System.out.println("No book found with the number " + bookNo + "."); } } case 8 -> { // Loaned books list System.out.println("Loaned Books:"); Collection<Loan> loans = library.getLoans(); if (!loans.isEmpty()) { for (Loan loan : loans) { if (loan.getBook().inLoan { System.out.println(loan.getBook().getTitle() + " - Loaned to " + loan.getStudent().getName()); } } } else { System.out.println("No books are currently loaned out."); } } // Existing case 9 ...

Please make sure to review and tailor the actual functionality to align with the full definitions of the Library, Student, Book, Loan, and other related classes, as the provided pseudocode assumes certain method based on the initial switch case provided. For any methods that have not already been implemented (like Book.inLoan() and Loan.isOver()), you should provide proper implementations in their respective classes.

Additionally, there are portions of the code that might create compilation issues, like improper instantiation SimpleDateFormat objects and assigning them to date variables supposed to hold Date objects, scan() calls after scan.nextInt() without discarding the new line character, and using getInfo() methods that may need to be implemented or fixed, considering their return types and how they construct the returned strings.

Make sure you address these issues in the code to ensure that your library management system works correctly.

Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.10.1:compile (default-compile) on project LibrarySystem: Fatal error compiling: error: invalid target release: 21 -> [Help 1]

To see the full stack trace of the errors, re-run Maven with the -e switch. Re-run Maven using the -X switch to enable full debug logging. explain the bug

The error message you're seeing is because the `` release in the tasks section of your pom.xml file is set to 21, which is not a valid release version. The maven-compiler-plugin is complaining that it cannot compile your code because the target release is not a valid version.

In your pom.xml file, you have defined the target release as 21, which is not a recognized version number. The valid range of target release versions for the maven-compiler-plugin is 1.1 to 5.0.

To fix this error, you need to change the target release version to a valid value between 1.1 and 5.0. You can do this by changing the target element in the tasks section of your pom.xml file to one of the following values:

  • 1.1
  • 2.0
  • 3.0
  • 4.0
  • 5.0

For example, you can change the target element to 3.0 like this

<project> ... <build> <plugins> ... <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.10.1</version> <configuration> <target>3.0</target> ... </configuration> </plugin> ... </plugins> </build> ... </project>

After making this change, run mvn clean package again to compile your code and build your application.