From bbfe3500047e71c859bbc84b0e8e2e9f3600fccb Mon Sep 17 00:00:00 2001 From: mh Date: Thu, 22 May 2025 20:37:29 +0330 Subject: [PATCH 1/4] Completed=(LibraryController+Book+Library) - add Category to book - add Category to addbook function arguments - implement getBooksByCategory function of Library --- src/controller/LibraryController.java | 8 ++++---- src/model/Book.java | 5 ++++- src/model/Library.java | 13 ++++++------- 3 files changed, 14 insertions(+), 12 deletions(-) diff --git a/src/controller/LibraryController.java b/src/controller/LibraryController.java index 885bb93..89c8bba 100644 --- a/src/controller/LibraryController.java +++ b/src/controller/LibraryController.java @@ -39,8 +39,8 @@ public List getAllStudents() { return library.getAllStudents(); } - public void addBook(String title, String author, String isbn, int publicationYear) { - Book book=new Book(title, author, isbn, publicationYear, false); + public void addBook(String title, String author, String isbn,Category category, int publicationYear) { + Book book=new Book(title, author, isbn,category, publicationYear, false); library.addBook(book); } @@ -89,11 +89,11 @@ public void saveData() { fileDataModel.saveStudents(library.getAllStudents(), ""); } - /* public void loadData() { + public void loadData() { library.setBooks(fileDataModel.loadBooks("")); library.setCategories(fileDataModel.loadCategories("")); library.setLoans(fileDataModel.loadLoans("")); library.setStudents(fileDataModel.loadStudents("")); } - */ + } diff --git a/src/model/Book.java b/src/model/Book.java index ddd15a1..b2fd731 100644 --- a/src/model/Book.java +++ b/src/model/Book.java @@ -4,14 +4,16 @@ public class Book { private String title; private String author; private String isbn; + private Category category; private int publicationYear; private boolean isBorrowed; //Constructor - public Book(String title,String author,String isbn,int publicationYear,boolean isBorrowed){ + public Book(String title,String author,String isbn,Category category,int publicationYear,boolean isBorrowed){ this.title=title; this.author=author; + this.category=category; this.isbn=isbn;this.publicationYear=publicationYear; this.isBorrowed=isBorrowed; } @@ -28,5 +30,6 @@ public void returnBook() { public String getAuthor() {return this.author;} public String getIsbn() {return this.isbn;} public Integer getPublicationYear() {return this.publicationYear;} + public Category getCategory() {return this.category;} public boolean getIsBorrowed() {return this.isBorrowed;} } diff --git a/src/model/Library.java b/src/model/Library.java index 7bd2d84..fbd68a6 100644 --- a/src/model/Library.java +++ b/src/model/Library.java @@ -135,14 +135,13 @@ public List getCategories(){ return this.categories; } public List getBooksByCategory(String categoryName) { - /*for(Category category : categories){ - if(category.getName().equals(categoryName)){ - + List booksByCategory=new ArrayList<>(); + for(Book book : booksByCategory){ + if(book.getCategory().getName().equals(categoryName)){ + booksByCategory.add(book); } - } */ - // TODO: Implement method 'getStudentsByMajor'. - throw new UnsupportedOperationException("Unimplemented method 'getStudentsByMajor'"); - + } + return booksByCategory; } public List getStudentsByMajor(String major) { From 5c682f89edbbe0f5ff85c176df538f831e3a6307 Mon Sep 17 00:00:00 2001 From: mh Date: Sat, 24 May 2025 19:26:11 +0330 Subject: [PATCH 2/4] Implement Controllers methods --- src/controller/LibraryController.java | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/src/controller/LibraryController.java b/src/controller/LibraryController.java index 89c8bba..12f99d2 100644 --- a/src/controller/LibraryController.java +++ b/src/controller/LibraryController.java @@ -2,11 +2,7 @@ import data.FileDataModel; import java.util.List; -import model.Book; -import model.GraduateStudent; -import model.Library; -import model.Student; -import model.UndergraduateStudent; +import model.*; public class LibraryController { private Library library; private FileDataModel fileDataModel; From b0cb26d2a3c0be02253bba5c07eab5b8deeb8e86 Mon Sep 17 00:00:00 2001 From: Reza <108628946+rzr8i@users.noreply.github.com> Date: Tue, 27 May 2025 12:08:43 +0330 Subject: [PATCH 3/4] Fix: add category Book serialization serialize & deserialize category field of Book class. --- src/model/Book.java | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/model/Book.java b/src/model/Book.java index a2901ff..7cbb062 100644 --- a/src/model/Book.java +++ b/src/model/Book.java @@ -62,6 +62,7 @@ public JSONObject serialize() { result.put("title", JSONObject.fromString(title)); result.put("author", JSONObject.fromString(author)); result.put("isbn", JSONObject.fromString(isbn)); + result.put("category", category.serialize()); result.put("publicationYear", JSONObject.fromNumber(publicationYear)); result.put("isBorrowed", JSONObject.fromBoolean(isBorrowed)); @@ -69,8 +70,9 @@ public JSONObject serialize() { } public static Book deserialize(JSONDict json) { + Category category = (Category) JSONSerializableFactory.deserialize((JSONDict) json.get("category")); return new Book(json.getString("title"), json.getString("author"), json.getString("isbn"), - json.getInteger("publicationYear"), json.getBoolean("isBorrowed")); + category, json.getInteger("publicationYear"), json.getBoolean("isBorrowed")); } } From 8244e77911c1e41403a102b745022b409cc0acf1 Mon Sep 17 00:00:00 2001 From: Reza <108628946+rzr8i@users.noreply.github.com> Date: Tue, 27 May 2025 12:13:06 +0330 Subject: [PATCH 4/4] Fix: Book class errors - add getCategory() - import JSONSerializableFactory --- src/model/Book.java | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/model/Book.java b/src/model/Book.java index 7cbb062..efe639a 100644 --- a/src/model/Book.java +++ b/src/model/Book.java @@ -1,6 +1,7 @@ package model; import jsonlib.JSONSerializable; +import jsonlib.JSONSerializableFactory; import jsonlib.types.JSONObject; import jsonlib.types.JSONDict; @@ -46,6 +47,10 @@ public String getIsbn() { return this.isbn; } + public Category getCategory() { + return this.category; + } + public Integer getPublicationYear() { return this.publicationYear; }