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
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,8 @@ out/

.vscode/
.code-workspace

students.json
books.json
categories.json
loans.json
30 changes: 22 additions & 8 deletions src/controller/LibraryController.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package controller;

import data.FileDataModel;
import jsonlib.JSONSerializableFactory;

import java.util.List;

import model.*;
Expand All @@ -12,6 +14,13 @@ public class LibraryController {
public LibraryController() {
library = new Library();
fileDataModel = new FileDataModel();
JSONSerializableFactory.registerType("UndergraduateStudent", UndergraduateStudent.class);
JSONSerializableFactory.registerType("GraduateStudent", GraduateStudent.class);
JSONSerializableFactory.registerType("Book", Book.class);
JSONSerializableFactory.registerType("Category", Category.class);
JSONSerializableFactory.registerType("Loan", Loan.class);

loadData();
}

public void addUndergraduateStudent(String studentId, String firstName, String lastName, String major,
Expand All @@ -38,11 +47,14 @@ public List<Student> getAllStudents() {
return library.getAllStudents();
}

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 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 addCategory(String name, String description) {
Category category = new Category(name, description);
library.addCategory(category);
}

public void removeBook(String isbn) {
Expand Down Expand Up @@ -74,28 +86,30 @@ public List<Book> getBorrowedBooksByStudent(String studentId) {
return library.getBorrowedBooksByStudent(studentId);
}

public List<Book> getBooksByCategory(String categoryName) {
return library.getBooksByCategory(categoryName);
public List<Book> getBooksByCategory(Category category) {
return library.getBooksByCategory(category);
}

public List<Student> getStudentsByMajor(String major) {
return library.getStudentsByMajor(major);
}

public List<Category> getCategories() {
return library.getCategories();
}

public void saveData() {
fileDataModel.saveBooks(library.getBooks());
fileDataModel.saveCategories(library.getCategories());
fileDataModel.saveLoans(library.getLoans());
fileDataModel.saveStudents(library.getAllStudents());
}


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

}

}
6 changes: 5 additions & 1 deletion src/data/FileDataModel.java
Original file line number Diff line number Diff line change
Expand Up @@ -66,14 +66,18 @@ public String readFromFile(String filePath) {
try {
content = String.join("\n", Files.readAllLines(Paths.get(filePath)));
} catch (IOException e) {
e.printStackTrace();
return null;
}

return content;
}

public <T> List<T> loadFromFile(String filePath) {
String content = readFromFile(filePath);
if (content == null) {
return new ArrayList<>();
}

JSONParser parser = new JSONParser(content);

JSONObject jsonObj = null;
Expand Down
13 changes: 13 additions & 0 deletions src/model/BaseModel.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package model;

import jsonlib.JSONSerializable;

public abstract class BaseModel implements JSONSerializable {
public abstract String getDisplayName();

@Override
public abstract int hashCode();

@Override
public abstract boolean equals(Object obj);
}
52 changes: 41 additions & 11 deletions src/model/Book.java
Original file line number Diff line number Diff line change
@@ -1,27 +1,28 @@
package model;

import java.util.Objects;

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

public class Book implements JSONSerializable {
public class Book extends BaseModel {
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,Category category,int publicationYear,boolean isBorrowed){
this.title=title;
this.author=author;
this.category=category;
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 @@ -30,7 +31,6 @@ public void borrowBook() {
}

public void returnBook() {

this.isBorrowed = false;
}

Expand Down Expand Up @@ -59,6 +59,17 @@ public boolean getIsBorrowed() {
return this.isBorrowed;
}

@Override
public String toString() {
return String.format("%s\n\tAuthor: %s\n\tCategory: %s\n\tISBN: %s\n\tPublication Year: %d\n\tAvailable: %s\n",
title, author, category, isbn, publicationYear, isBorrowed ? "No" : "Yes");
}

@Override
public String getDisplayName() {
return String.format("%s by %s (%s)", title, author, isbn);
}

@Override
public JSONObject serialize() {
JSONDict result = new JSONDict();
Expand All @@ -80,4 +91,23 @@ public static Book deserialize(JSONDict json) {
category, json.getInteger("publicationYear"), json.getBoolean("isBorrowed"));
}

@Override
public int hashCode() {
return Objects.hash(title, author, isbn, category, publicationYear, isBorrowed);
}

@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (!(obj instanceof Book))
return false;

Book other = (Book) obj;
return this.title.equals(other.title) && this.author.equals(other.author) && this.isbn.equals(other.isbn)
&& this.category.equals(other.category) && this.publicationYear == other.publicationYear
&& this.isBorrowed == other.isBorrowed;
}
}
37 changes: 34 additions & 3 deletions src/model/Category.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package model;

import jsonlib.JSONSerializable;
import jsonlib.types.JSONObject;

import java.util.Objects;

import jsonlib.types.JSONDict;

public class Category implements JSONSerializable {
public class Category extends BaseModel {
private String name;
private String description;

Expand All @@ -23,11 +25,21 @@ public String getDescription() {
return this.description;
}

@Override
public String toString() {
return String.format("%s (%s)", name, description);
}

@Override
public String getDisplayName() {
return name;
}

@Override
public JSONObject serialize() {
JSONDict result = new JSONDict();

result.put("class", JSONObject.fromString("Book"));
result.put("class", JSONObject.fromString("Category"));
result.put("name", JSONObject.fromString(name));
result.put("description", JSONObject.fromString(description));

Expand All @@ -37,4 +49,23 @@ public JSONObject serialize() {
public static Category deserialize(JSONDict json) {
return new Category(json.getString("name"), json.getString("description"));
}

@Override
public int hashCode() {
return Objects.hash(name, description);
}

@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (!(obj instanceof Category))
return false;

Category other = (Category) obj;

return this.name.equals(other.name) && this.description.equals(other.description);
}
}
30 changes: 30 additions & 0 deletions src/model/GraduateStudent.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package model;

import jsonlib.types.JSONObject;

import java.util.Objects;

import jsonlib.types.JSONDict;

public class GraduateStudent extends Student {
Expand Down Expand Up @@ -38,6 +41,12 @@ public void setThesisTitle(String thesisTitle) {
this.thesisTitle = thesisTitle;
}

@Override
public String toString() {
return String.format("%s %s\n\tID: %s\n\tMajor: %s\n\tSupervisor: %s\n\tThesis Title: %s\n", firstName, lastName,
studentId, major, supervisor, thesisTitle);
}

@Override
public JSONObject serialize() {
JSONDict result = new JSONDict();
Expand All @@ -59,4 +68,25 @@ public static GraduateStudent deserialize(JSONDict json) {
return new GraduateStudent(json.getString("studentId"), json.getString("firstName"), json.getString("lastName"),
json.getString("major"), json.getString("supervisor"), json.getString("thesisTitle"));
}

@Override
public int hashCode() {
return Objects.hash(studentId, firstName, lastName, major, supervisor, thesisTitle);
}

@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (!(obj instanceof GraduateStudent))
return false;

GraduateStudent other = (GraduateStudent) obj;

return this.studentId.equals(other.studentId) && this.firstName.equals(other.firstName)
&& this.lastName.equals(other.lastName) && this.major.equals(other.major)
&& this.supervisor.equals(other.supervisor) && this.thesisTitle.equals(other.thesisTitle);
}
}
43 changes: 23 additions & 20 deletions src/model/Library.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,16 @@ public void addBook(Book book) {
books.add(book);
}

public void addCategory(Category category) {
categories.add(category);
}

public void removeBook(String isbn) {
for (Book book : books) {
if (book.getIsbn().equals(isbn)) {
books.remove(book);
for (int i = books.size() - 1; i >= 0; i--)
if (books.get(i).getIsbn().equals(isbn)) {
books.remove(i);
return;
}
}
}

public Book searchBookByTitle(String title) {
Expand Down Expand Up @@ -80,11 +84,11 @@ public void addStudent(Student student) {
}

public void removeStudent(String studentId) {
for (Student student : students) {
if (student.getStudentId().equals(studentId)) {
students.remove(student);
for (int i = students.size() - 1; i >= 0; i--)
if (students.get(i).getStudentId().equals(studentId)) {
students.remove(i);
return;
}
}
}

public Student searchStudentById(String studentId) {
Expand Down Expand Up @@ -121,13 +125,12 @@ public boolean borrowBook(Book book, Student student) {
}

public boolean returnBook(Book book) {
for (Loan loan : loans) {
if (loan.getBook().equals(book)) {
loans.remove(loan);
loan.getBook().returnBook();
for (int i = loans.size() - 1; i >= 0; i--)
if (loans.get(i).getBook().equals(book)) {
loans.remove(i);
return true;
}
}

return false;
}

Expand All @@ -149,14 +152,14 @@ public List<Category> getCategories() {
return this.categories;
}

public List<Book> getBooksByCategory(String categoryName) {
List<Book> booksByCategory=new ArrayList<>();
for(Book book : booksByCategory){
if(book.getCategory().getName().equals(categoryName)){
booksByCategory.add(book);
}
public List<Book> getBooksByCategory(Category category) {
List<Book> booksByCategory = new ArrayList<>();
for (Book book : booksByCategory) {
if (book.getCategory().getName().equals(category.getName())) {
booksByCategory.add(book);
}
return booksByCategory;
}
return booksByCategory;

}

Expand Down
Loading