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
17 changes: 9 additions & 8 deletions src/controller/LibraryController.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,8 @@

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;
Expand Down Expand Up @@ -41,9 +38,11 @@ public List<Student> getAllStudents() {
return library.getAllStudents();
}

public void addBook(String title, String author, String isbn, int publicationYear) {
Book book = new Book(title, author, isbn, publicationYear, false);
library.addBook(book);

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);

}

public void removeBook(String isbn) {
Expand Down Expand Up @@ -90,11 +89,13 @@ public void saveData() {
fileDataModel.saveStudents(library.getAllStudents());
}


public void loadData() {
library.setBooks(fileDataModel.loadBooks());
library.setCategories(fileDataModel.loadCategories());
library.setLoans(fileDataModel.loadLoans());
library.setStudents(fileDataModel.loadStudents());

}

}
28 changes: 20 additions & 8 deletions src/model/Book.java
Original file line number Diff line number Diff line change
@@ -1,23 +1,27 @@
package model;

import jsonlib.JSONSerializable;
import jsonlib.JSONSerializableFactory;
import jsonlib.types.JSONObject;
import jsonlib.types.JSONDict;

public class Book implements JSONSerializable {
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) {
this.title = title;
this.author = author;
this.isbn = isbn;
this.publicationYear = publicationYear;
this.isBorrowed = isBorrowed;


//Constructor
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;
}

// Setter
Expand All @@ -26,6 +30,7 @@ public void borrowBook() {
}

public void returnBook() {

this.isBorrowed = false;
}

Expand All @@ -42,6 +47,10 @@ public String getIsbn() {
return this.isbn;
}

public Category getCategory() {
return this.category;
}

public Integer getPublicationYear() {
return this.publicationYear;
}
Expand All @@ -58,14 +67,17 @@ 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));

return result;
}

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"));
}

}
9 changes: 7 additions & 2 deletions src/model/Library.java
Original file line number Diff line number Diff line change
Expand Up @@ -150,8 +150,13 @@ public List<Category> getCategories() {
}

public List<Book> getBooksByCategory(String categoryName) {
// TODO: Implement method 'getBooksByCategory'.
throw new UnsupportedOperationException("Unimplemented method 'getBooksByCategory'");
List<Book> booksByCategory=new ArrayList<>();
for(Book book : booksByCategory){
if(book.getCategory().getName().equals(categoryName)){
booksByCategory.add(book);
}
}
return booksByCategory;

}

Expand Down