Skip to content
Open
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
23 changes: 23 additions & 0 deletions src/main/java/com/example/library/demo/Menu.java
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,7 @@ public void executeCommand() {
LocalDateTime todayDate = LocalDateTime.now();
LocalDateTime returnDate = todayDate.plusDays(7);
Issue issue = new Issue(todayDate,returnDate,student,bookIssue);
libraryService.addIssue(issue);
student.addIssue(issue);
bookIssue.setQuantity(bookIssue.getQuantity()-1);
libraryService.addBook(bookIssue);
Expand All @@ -187,6 +188,28 @@ public void executeCommand() {
break;
case 7:
// Call listBooksByUsn method
// Ask for student usn and check if already exists
System.out.println("Enter student usn: ");
String studentUsn2 = scanner.nextLine();

Student student2;
Optional<Student> studentOptional2 = libraryService.findStudentByUsn(studentUsn2);
if (!studentOptional2.isPresent()) {
System.out.println("Student with usn " + studentUsn2 + " not found.");
break;
} else {
student2 = studentOptional2.get();
List<Issue> studentIssues = student2.getIssues();
if (studentIssues.isEmpty()) {
System.out.println("Student " + student2.getName() + " has no issued books.");
} else {
System.out.println(student2.getName() + "'s issued books: ");
System.out.println("Book Title Student Name Return date");
for (Issue issue : studentIssues) {
issue.printIssueInfo();
}
}
}
break;
case 8:
System.out.println("Exiting program.");
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/com/example/library/demo/model/Issue.java
Original file line number Diff line number Diff line change
Expand Up @@ -86,4 +86,8 @@ public boolean equals(Object o) {
public int hashCode() {
return Objects.hash(issueId, issueDate, returnDate, issueStudent, issueBook);
}

public void printIssueInfo() {
System.out.printf("%-20s %-20s %-15s\n", issueBook.getTitle(), issueStudent.getName(), returnDate);
}
}
2 changes: 1 addition & 1 deletion src/main/java/com/example/library/demo/model/Student.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public class Student {
@Column(name = "student_name")
private String name;

@OneToMany(mappedBy = "issueStudent", orphanRemoval = true)
@OneToMany(mappedBy = "issueStudent", cascade = CascadeType.ALL, fetch = FetchType.EAGER, orphanRemoval = true)
private List<Issue> issues = new ArrayList<>();

public Student() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,10 @@ public List<Book> findBooksByAuthor(Author author){
return bookRepository.findAllByAuthor(author.getAuthorId());
}

// Issue Repository
public void addIssue (Issue issue) {
issueRepository.save(issue);
}
/*public List<Book> findByUsn(Student student){
return bookRepository.findAllByUsn(student.getUsn());
}*/
Expand Down
Binary file modified target/classes/com/example/library/demo/Menu.class
Binary file not shown.
Binary file modified target/classes/com/example/library/demo/model/Author.class
Binary file not shown.
Binary file modified target/classes/com/example/library/demo/model/Book.class
Binary file not shown.
Binary file modified target/classes/com/example/library/demo/model/Issue.class
Binary file not shown.
Binary file modified target/classes/com/example/library/demo/model/Student.class
Binary file not shown.
Binary file not shown.