From 79b9960abf987dc8714e84f7760c36a9a2eef414 Mon Sep 17 00:00:00 2001 From: Reza Date: Thu, 22 May 2025 19:52:32 +0330 Subject: [PATCH 1/5] Don't get filePath in fileDataModel's methods --- src/data/FileDataModel.java | 23 ++++++++++++++--------- 1 file changed, 14 insertions(+), 9 deletions(-) diff --git a/src/data/FileDataModel.java b/src/data/FileDataModel.java index 326f288..e8e4296 100644 --- a/src/data/FileDataModel.java +++ b/src/data/FileDataModel.java @@ -4,42 +4,47 @@ import model.*; public class FileDataModel { - public void loadBooks(String filePath) { + private static final String BOOKS_FILEPATH = "books.json"; + private static final String STUDENTS_FILEPATH = "students.json"; + private static final String CATEGORIES_FILEPATH = "categories.json"; + private static final String LOANS_FILEPATH = "loans.json"; + + public void loadBooks() { // TODO: Implement method 'filePath'. throw new UnsupportedOperationException("Unimplemented method 'filePath'"); } - public void saveBooks(List books, String filePath) { + public void saveBooks(List books) { // TODO: Implement method 'saveBooks'. throw new UnsupportedOperationException("Unimplemented method 'saveBooks'"); } - public void loadStudents(String filePath) { + public void loadStudents() { // TODO: Implement method 'loadStudents'. throw new UnsupportedOperationException("Unimplemented method 'loadStudents'"); } - public void saveStudents(List students, String filePath) { + public void saveStudents(List students) { // TODO: Implement method 'saveStudents'. throw new UnsupportedOperationException("Unimplemented method 'saveStudents'"); } - public void loadCategories(String filePath) { + public void loadCategories() { // TODO: Implement method 'loadCategories'. throw new UnsupportedOperationException("Unimplemented method 'loadCategories'"); } - public void saveCategories(List categories, String filePath) { + public void saveCategories(List categories) { // TODO: Implement method 'saveCategories'. throw new UnsupportedOperationException("Unimplemented method 'saveCategories'"); - } + } - public void loadLoans(String filePath) { + public void loadLoans() { // TODO: Implement method 'loadLoans'. throw new UnsupportedOperationException("Unimplemented method 'loadLoans'"); } - public void saveLoans(List loans, String filePath) { + public void saveLoans(List loans) { // TODO: Implement method 'saveLoans'. throw new UnsupportedOperationException("Unimplemented method 'saveLoans'"); } From b433a1eb55c91442830642cba2d68dd4e755f1b5 Mon Sep 17 00:00:00 2001 From: Reza Date: Thu, 22 May 2025 21:15:18 +0330 Subject: [PATCH 2/5] Extract JSONExpectFailedException to its own file --- src/jsonlib/JSONExpectFailedException.java | 8 ++++++++ src/jsonlib/JSONParser.java | 6 ------ 2 files changed, 8 insertions(+), 6 deletions(-) create mode 100644 src/jsonlib/JSONExpectFailedException.java diff --git a/src/jsonlib/JSONExpectFailedException.java b/src/jsonlib/JSONExpectFailedException.java new file mode 100644 index 0000000..aad1e45 --- /dev/null +++ b/src/jsonlib/JSONExpectFailedException.java @@ -0,0 +1,8 @@ +package jsonlib; + +public class JSONExpectFailedException extends Exception { + + public JSONExpectFailedException(String msg) { + super(msg); + } +} diff --git a/src/jsonlib/JSONParser.java b/src/jsonlib/JSONParser.java index 565d8b3..cac0b00 100644 --- a/src/jsonlib/JSONParser.java +++ b/src/jsonlib/JSONParser.java @@ -173,9 +173,3 @@ else if (Character.isWhitespace(peek())) } -class JSONExpectFailedException extends Exception { - - public JSONExpectFailedException(String msg) { - super(msg); - } -} From 6a239492e99ba47d52ed3ce5179e812e85290700 Mon Sep 17 00:00:00 2001 From: Reza Date: Thu, 22 May 2025 21:17:11 +0330 Subject: [PATCH 3/5] Make all models JSON serializable --- src/model/Book.java | 67 +++++++++++++++++++------- src/model/Category.java | 35 ++++++++++---- src/model/GraduateStudent.java | 53 ++++++++++++++++----- src/model/Loan.java | 66 ++++++++++++++++++------- src/model/Student.java | 74 ++++++++++++++++++++--------- src/model/UndergraduateStudent.java | 42 +++++++++++----- 6 files changed, 246 insertions(+), 91 deletions(-) diff --git a/src/model/Book.java b/src/model/Book.java index ddd15a1..7812243 100644 --- a/src/model/Book.java +++ b/src/model/Book.java @@ -1,32 +1,63 @@ package model; -public class Book { +import jsonlib.JSONSerializable; +import jsonlib.types.JSONObject; +import jsonlib.types.JSONDict; + +public class Book implements JSONSerializable { private String title; private String author; private String isbn; 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, int publicationYear, boolean isBorrowed) { + this.title = title; + this.author = author; + this.isbn = isbn; + this.publicationYear = publicationYear; + this.isBorrowed = isBorrowed; } -//Setter + + // Setter public void borrowBook() { - this.isBorrowed=true; + this.isBorrowed = true; } public void returnBook() { - this.isBorrowed=false; - } -//Getter - public String getTitle() {return this.title;} - public String getAuthor() {return this.author;} - public String getIsbn() {return this.isbn;} - public Integer getPublicationYear() {return this.publicationYear;} - public boolean getIsBorrowed() {return this.isBorrowed;} + this.isBorrowed = false; + } + + // Getter + public String getTitle() { + return this.title; + } + + public String getAuthor() { + return this.author; + } + + public String getIsbn() { + return this.isbn; + } + + public Integer getPublicationYear() { + return this.publicationYear; + } + + public boolean getIsBorrowed() { + return this.isBorrowed; + } + + @Override + public JSONObject serialize() { + // TODO: Implement method 'serialize' for Book + throw new UnsupportedOperationException("Unimplemented method 'serialize' for Book"); + } + + public static Book deserialize(JSONDict json) { + // TODO: Implemented method 'deserialize' for Book + throw new UnsupportedOperationException("Unimplemented method 'deserialize' for Book"); + } } diff --git a/src/model/Category.java b/src/model/Category.java index e1e52ac..f364943 100644 --- a/src/model/Category.java +++ b/src/model/Category.java @@ -1,18 +1,37 @@ package model; -public class Category { +import jsonlib.JSONSerializable; +import jsonlib.types.JSONObject; +import jsonlib.types.JSONDict; + +public class Category implements JSONSerializable { private String name; private String description; -//Constructor - public Category(String name,String description){ - this.name=name; - this.description=description; + + // Constructor + public Category(String name, String description) { + this.name = name; + this.description = description; } -//Getter + + // Getter public String getName() { - return this.name; + return this.name; } + public String getDescription() { - return this.description; + return this.description; + } + + @Override + public JSONObject serialize() { + // TODO: Implemented method 'serialize' for Category + throw new UnsupportedOperationException("Unimplemented method 'serialize' for Category"); + } + + + public static Category deserialize(JSONDict json) { + // TODO: Implemented method 'deserialize' for Category + throw new UnsupportedOperationException("Unimplemented method 'deserialize' for Category"); } } diff --git a/src/model/GraduateStudent.java b/src/model/GraduateStudent.java index b11b94a..1da1037 100644 --- a/src/model/GraduateStudent.java +++ b/src/model/GraduateStudent.java @@ -1,24 +1,51 @@ package model; +import jsonlib.types.JSONObject; +import jsonlib.types.JSONDict; + public class GraduateStudent extends Student { private String supervisor; private String thesisTitle; + @Override public void getStudentDetails() { // TODO: Implement method 'getStudentDetails' for GraduateStudent. throw new UnsupportedOperationException("Unimplemented method 'getStudentDetails'"); } -public GraduateStudent(String studentId,String firstName,String lastName,String major,String supervisor,String thesisTitle) -{ - super(studentId, firstName, lastName, major); - this.supervisor=supervisor; - this.thesisTitle=thesisTitle; -} -//Getter -public String getSupervisor() {return supervisor;} -public String getThesisTitle() {return thesisTitle;} -//Setter -public void setSupervisor (String supervisor) {this.supervisor=supervisor;} -public void setThesisTitle(String thesisTitle) {this.thesisTitle=thesisTitle;} -} + public GraduateStudent(String studentId, String firstName, String lastName, String major, String supervisor, + String thesisTitle) { + super(studentId, firstName, lastName, major); + this.supervisor = supervisor; + this.thesisTitle = thesisTitle; + } + + // Getter + public String getSupervisor() { + return supervisor; + } + + public String getThesisTitle() { + return thesisTitle; + } + + // Setter + public void setSupervisor(String supervisor) { + this.supervisor = supervisor; + } + + public void setThesisTitle(String thesisTitle) { + this.thesisTitle = thesisTitle; + } + + @Override + public JSONObject serialize() { + // TODO: Implemented method 'serialize' for GraduateStudent + throw new UnsupportedOperationException("Unimplemented method 'serialize' for GraduateStudent"); + } + + public static GraduateStudent deserialize(JSONDict json) { + // TODO: Implemented method 'deserialize' for GraduateStudent + throw new UnsupportedOperationException("Unimplemented method 'deserialize' for GraduateStudent"); + } +} diff --git a/src/model/Loan.java b/src/model/Loan.java index eec35bc..b155fbb 100644 --- a/src/model/Loan.java +++ b/src/model/Loan.java @@ -2,32 +2,66 @@ import java.util.Date; -public class Loan { +import jsonlib.JSONSerializable; +import jsonlib.types.JSONObject; +import jsonlib.types.JSONDict; + +public class Loan implements JSONSerializable { private Book book; private Student student; private Date loanDate; private Date dueDate; + public Loan(Book book, Student student, Date loanDate, Date dueDate) { + this.book = book; + this.student = student; + this.loanDate = loanDate; + this.dueDate = dueDate; + } + + // Seterr + public void setBook(Book book) { + this.book = book; + } + + public void setStudent(Student student) { + this.student = student; + } + + public void setLoanDate(Date loanDate) { + this.loanDate = loanDate; + } + + public void setDueDate(Date dueDate) { + this.dueDate = dueDate; + } + + // getter + public Book getBook() { + return this.book; + } + public Student getStudent() { + return this.student; + } - public Loan(Book book,Student student,Date loanDate,Date dueDate){ - this.book=book; - this.student=student; - this.loanDate=loanDate; - this.dueDate=dueDate; + public Date getLoanDate() { + return this.loanDate; } + public Date getDueDate() { + return this.dueDate; + } + @Override + public JSONObject serialize() { + // TODO: Implement method 'serialize' for Loan. + throw new UnsupportedOperationException("Unimplemented method 'serialize' for Loan"); + } -//Seterr - public void setBook (Book book) {this.book=book;} - public void setStudent (Student student) {this.student=student;} - public void setLoanDate (Date loanDate) {this.loanDate=loanDate;} - public void setDueDate (Date dueDate) {this.dueDate=dueDate;} -//getter - public Book getBook() {return this.book;} - public Student getStudent() {return this.student;} - public Date getLoanDate() {return this.loanDate;} - public Date getDueDate() {return this.dueDate;} + public static Loan deserialize(JSONDict json) { + // TODO: Implemented method 'deserialize' for Loan + throw new UnsupportedOperationException("Unimplemented method 'deserialize' for Loan"); + } } diff --git a/src/model/Student.java b/src/model/Student.java index be30326..6a2e2ad 100644 --- a/src/model/Student.java +++ b/src/model/Student.java @@ -1,26 +1,54 @@ package model; -public abstract class Student { - protected String studentId; - protected String firstName; - protected String lastName; - protected String major; -public Student(String studentId,String firstName,String lastName,String major){ - this.studentId=studentId; - this.firstName=firstName; - this.lastName=lastName; - this.major=major; -} +import jsonlib.JSONSerializable; + +public abstract class Student implements JSONSerializable { + protected String studentId; + protected String firstName; + protected String lastName; + protected String major; + + public Student(String studentId, String firstName, String lastName, String major) { + this.studentId = studentId; + this.firstName = firstName; + this.lastName = lastName; + this.major = major; + } + public abstract void getStudentDetails(); -//Setter - public void setStudentId (String studentId) {this.studentId=studentId;} - public void setFirstName (String firstName) {this.firstName=firstName;} - public void setLastName (String lastName) {this.lastName=lastName;} - public void setMajor (String major) {this.major=major;} -//Getter - public String getStudentId() {return this.studentId;} - public String getFirstName() {return this.firstName;} - public String getLastName() {return this.lastName;} - public String getMajor() {return this.major;} - -} \ No newline at end of file + + // Setter + public void setStudentId(String studentId) { + this.studentId = studentId; + } + + public void setFirstName(String firstName) { + this.firstName = firstName; + } + + public void setLastName(String lastName) { + this.lastName = lastName; + } + + public void setMajor(String major) { + this.major = major; + } + + // Getter + public String getStudentId() { + return this.studentId; + } + + public String getFirstName() { + return this.firstName; + } + + public String getLastName() { + return this.lastName; + } + + public String getMajor() { + return this.major; + } + +} diff --git a/src/model/UndergraduateStudent.java b/src/model/UndergraduateStudent.java index b7ff6b7..2657ad7 100644 --- a/src/model/UndergraduateStudent.java +++ b/src/model/UndergraduateStudent.java @@ -1,5 +1,8 @@ package model; +import jsonlib.types.JSONObject; +import jsonlib.types.JSONDict; + public class UndergraduateStudent extends Student { private int enrollmentYear; @@ -8,18 +11,31 @@ public void getStudentDetails() { // TODO: Implement method 'getStudentDetails' for UndergraduateStudent. throw new UnsupportedOperationException("Unimplemented method 'getStudentDetails'"); } - -public UndergraduateStudent(String studentId,String firstName,String lastName,String major,int enrollmentYear){ - super(studentId,firstName,lastName,major); - this.enrollmentYear=enrollmentYear; - } -//Getter -public int getEnrollmentYear(){ - return this.enrollmentYear; -} -//Setter -public void setEnrollmentYear(int enrollmentYear){ - this.enrollmentYear=enrollmentYear; -} + + public UndergraduateStudent(String studentId, String firstName, String lastName, String major, int enrollmentYear) { + super(studentId, firstName, lastName, major); + this.enrollmentYear = enrollmentYear; + } + + // Getter + public int getEnrollmentYear() { + return this.enrollmentYear; + } + + // Setter + public void setEnrollmentYear(int enrollmentYear) { + this.enrollmentYear = enrollmentYear; + } + + @Override + public JSONObject serialize() { + // TODO: Implemented method 'serialize' for UndergraduateStudent + throw new UnsupportedOperationException("Unimplemented method 'serialize' for UndergraduateStudent"); + } + + public static UndergraduateStudent deserialize(JSONDict json) { + // TODO: Implemented method 'deserialize' for UndergraduateStudent + throw new UnsupportedOperationException("Unimplemented method 'deserialize' for UndergraduateStudent"); + } } From 0cf1b818697cf44285f8e1875b6931205dff0e2c Mon Sep 17 00:00:00 2001 From: Reza Date: Thu, 22 May 2025 21:17:42 +0330 Subject: [PATCH 4/5] Implement FileDataModel class --- src/data/FileDataModel.java | 97 +++++++++++++++++++++++++++---------- 1 file changed, 72 insertions(+), 25 deletions(-) diff --git a/src/data/FileDataModel.java b/src/data/FileDataModel.java index e8e4296..70cf146 100644 --- a/src/data/FileDataModel.java +++ b/src/data/FileDataModel.java @@ -1,51 +1,98 @@ package data; +import java.io.FileWriter; +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Paths; +import java.util.ArrayList; import java.util.List; + import model.*; +import jsonlib.types.JSONObject; +import jsonlib.types.JSONList; +import jsonlib.types.JSONDict; +import jsonlib.JSONSerializableFactory; +import jsonlib.JSONParser; +import jsonlib.JSONExpectFailedException; public class FileDataModel { private static final String BOOKS_FILEPATH = "books.json"; - private static final String STUDENTS_FILEPATH = "students.json"; + private static final String STUDENTS_FILEPATH = "students.json"; private static final String CATEGORIES_FILEPATH = "categories.json"; private static final String LOANS_FILEPATH = "loans.json"; - public void loadBooks() { - // TODO: Implement method 'filePath'. - throw new UnsupportedOperationException("Unimplemented method 'filePath'"); + public List loadBooks() { + return loadFromFile(BOOKS_FILEPATH); } - public void saveBooks(List books) { - // TODO: Implement method 'saveBooks'. - throw new UnsupportedOperationException("Unimplemented method 'saveBooks'"); + public List loadStudents() { + return loadFromFile(STUDENTS_FILEPATH); } - public void loadStudents() { - // TODO: Implement method 'loadStudents'. - throw new UnsupportedOperationException("Unimplemented method 'loadStudents'"); + public List loadCategories() { + return loadFromFile(CATEGORIES_FILEPATH); } - public void saveStudents(List students) { - // TODO: Implement method 'saveStudents'. - throw new UnsupportedOperationException("Unimplemented method 'saveStudents'"); + public List loadLoans() { + return loadFromFile(LOANS_FILEPATH); } - public void loadCategories() { - // TODO: Implement method 'loadCategories'. - throw new UnsupportedOperationException("Unimplemented method 'loadCategories'"); + public void saveBooks(List books) { + writeToFile(BOOKS_FILEPATH, JSONObject.fromList(books).toString()); } - public void saveCategories(List categories) { - // TODO: Implement method 'saveCategories'. - throw new UnsupportedOperationException("Unimplemented method 'saveCategories'"); - } + public void saveStudents(List students) { + writeToFile(STUDENTS_FILEPATH, JSONObject.fromList(students).toString()); + } - public void loadLoans() { - // TODO: Implement method 'loadLoans'. - throw new UnsupportedOperationException("Unimplemented method 'loadLoans'"); + public void saveCategories(List categories) { + writeToFile(CATEGORIES_FILEPATH, JSONObject.fromList(categories).toString()); } public void saveLoans(List loans) { - // TODO: Implement method 'saveLoans'. - throw new UnsupportedOperationException("Unimplemented method 'saveLoans'"); + writeToFile(LOANS_FILEPATH, JSONObject.fromList(loans).toString()); } + + public void writeToFile(String filePath, String data) { + try (FileWriter writer = new FileWriter(filePath)) { + writer.write(data); + } catch (IOException e) { + e.printStackTrace(); + } + } + + public String readFromFile(String filePath) { + String content = new String(); + try { + content = String.join("\n", Files.readAllLines(Paths.get(filePath))); + } catch (IOException e) { + e.printStackTrace(); + } + + return content; + } + + public List loadFromFile(String filePath) { + String content = readFromFile(filePath); + JSONParser parser = new JSONParser(content); + + JSONObject jsonObj = null; + + try { + jsonObj = parser.parse(); + } catch (JSONExpectFailedException e) { + e.printStackTrace(); + } + + List result = new ArrayList<>(); + JSONList listJSON = (JSONList) jsonObj; + + for (JSONObject itemJSON : listJSON) { + T item = (T) JSONSerializableFactory.deserialize((JSONDict) itemJSON); + result.add(item); + } + + return result; + } + } From a099a5b4c11a475c1f30f31fd8af2726364b5cb2 Mon Sep 17 00:00:00 2001 From: Reza Date: Thu, 22 May 2025 22:11:09 +0330 Subject: [PATCH 5/5] Implement serialize & deserialize for all models --- src/jsonlib/JSONParser.java | 27 +++++++++++++++++----- src/jsonlib/types/JSONBoolean.java | 36 +++++++++++++++++++++++++++++ src/jsonlib/types/JSONDict.java | 8 +++++++ src/jsonlib/types/JSONObject.java | 4 ++++ src/model/Book.java | 16 +++++++++---- src/model/Category.java | 12 ++++++---- src/model/GraduateStudent.java | 19 +++++++++++---- src/model/Loan.java | 22 ++++++++++++++---- src/model/UndergraduateStudent.java | 17 ++++++++++---- 9 files changed, 135 insertions(+), 26 deletions(-) create mode 100644 src/jsonlib/types/JSONBoolean.java diff --git a/src/jsonlib/JSONParser.java b/src/jsonlib/JSONParser.java index cac0b00..7f255a2 100644 --- a/src/jsonlib/JSONParser.java +++ b/src/jsonlib/JSONParser.java @@ -1,5 +1,6 @@ package jsonlib; +import jsonlib.types.JSONBoolean; import jsonlib.types.JSONDict; import jsonlib.types.JSONList; import jsonlib.types.JSONNumber; @@ -80,7 +81,7 @@ private JSONNumber parseNumber() throws JSONExpectFailedException { StringBuilder integerPartStr = new StringBuilder(); StringBuilder floatingPartStr = new StringBuilder(); boolean isFloat = false; - + while (peek() != '\0' && Character.isDigit(peek())) integerPartStr.append(consume()); @@ -101,8 +102,13 @@ private JSONNumber parseNumber() throws JSONExpectFailedException { double number = Double.valueOf(integerPartStr + "." + floatingPartStr); return new JSONNumber(sign * number); } else { - int number = Integer.valueOf(integerPartStr.toString()); - return new JSONNumber(sign * number); + try { + int number = Integer.valueOf(integerPartStr.toString()); + return new JSONNumber(sign * number); + } catch (NumberFormatException e) { + long number = Long.valueOf(integerPartStr.toString()); + return new JSONNumber(sign * number); + } } } @@ -146,6 +152,16 @@ private JSONDict parseDict() throws JSONExpectFailedException { return dict; } + private JSONBoolean parseBoolean() throws JSONExpectFailedException { + if (peek() == 't') { + expect("true"); + return new JSONBoolean(true); + } else { + expect("false"); + return new JSONBoolean(false); + } + } + public JSONObject parse() throws JSONExpectFailedException { while (peek() != '\0') { switch (peek()) { @@ -160,11 +176,11 @@ public JSONObject parse() throws JSONExpectFailedException { return parseNumber(); else if (Character.isWhitespace(peek())) consume(); + else if (peek() == 't' || peek() == 'f') + return parseBoolean(); else // TODO: handle invalid characters properly. throw new UnsupportedOperationException("not implemented yet"); - - // TODO: handle json boolean and null break; } } @@ -172,4 +188,3 @@ else if (Character.isWhitespace(peek())) } } - diff --git a/src/jsonlib/types/JSONBoolean.java b/src/jsonlib/types/JSONBoolean.java new file mode 100644 index 0000000..88cdca0 --- /dev/null +++ b/src/jsonlib/types/JSONBoolean.java @@ -0,0 +1,36 @@ +package jsonlib.types; + +public class JSONBoolean extends JSONObject { + private boolean value; + + public JSONBoolean(boolean value) { + this.value = value; + } + + public boolean getValue() { + return this.value; + } + + @Override + public String toString() { + return value ? "true" : "false"; + } + + @Override + public int hashCode() { + return this.value ? 1 : 0; + } + + @Override + public boolean equals(Object obj) { + if (obj == null) + return false; + + if (!(obj instanceof JSONBoolean)) + return false; + + JSONBoolean other = (JSONBoolean) obj; + + return this.value == other.value; + } +} diff --git a/src/jsonlib/types/JSONDict.java b/src/jsonlib/types/JSONDict.java index 3650ffb..373c324 100644 --- a/src/jsonlib/types/JSONDict.java +++ b/src/jsonlib/types/JSONDict.java @@ -53,6 +53,14 @@ public int getInteger(String key) { return jsonNumber.getValue(); } + public boolean getBoolean(String key) { + JSONObject obj = this.get(key); + if (!(obj instanceof JSONBoolean)) + throw new IllegalArgumentException(String.format("dict[%s] is not a boolean", key)); + + return ((JSONBoolean) obj).getValue(); + } + @Override public String toString() { StringBuilder sb = new StringBuilder(); diff --git a/src/jsonlib/types/JSONObject.java b/src/jsonlib/types/JSONObject.java index 8abc580..bb35885 100644 --- a/src/jsonlib/types/JSONObject.java +++ b/src/jsonlib/types/JSONObject.java @@ -22,6 +22,10 @@ public static JSONObject fromDict(Map da return new JSONDict(data); } + public static JSONObject fromBoolean(boolean data) { + return new JSONBoolean(data); + } + @Override public abstract String toString(); } diff --git a/src/model/Book.java b/src/model/Book.java index 7812243..a2e9b7a 100644 --- a/src/model/Book.java +++ b/src/model/Book.java @@ -52,12 +52,20 @@ public boolean getIsBorrowed() { @Override public JSONObject serialize() { - // TODO: Implement method 'serialize' for Book - throw new UnsupportedOperationException("Unimplemented method 'serialize' for Book"); + JSONDict result = new JSONDict(); + + result.put("class", JSONObject.fromString("Book")); + result.put("title", JSONObject.fromString(title)); + result.put("author", JSONObject.fromString(author)); + result.put("isbn", JSONObject.fromString(isbn)); + result.put("publicationYear", JSONObject.fromNumber(publicationYear)); + result.put("isBorrowed", JSONObject.fromBoolean(isBorrowed)); + + return result; } public static Book deserialize(JSONDict json) { - // TODO: Implemented method 'deserialize' for Book - throw new UnsupportedOperationException("Unimplemented method 'deserialize' for Book"); + return new Book(json.getString("title"), json.getString("author"), json.getString("isbn"), + json.getInteger("publicationYear"), json.getBoolean("isBorrowed")); } } diff --git a/src/model/Category.java b/src/model/Category.java index f364943..13cc009 100644 --- a/src/model/Category.java +++ b/src/model/Category.java @@ -25,13 +25,17 @@ public String getDescription() { @Override public JSONObject serialize() { - // TODO: Implemented method 'serialize' for Category - throw new UnsupportedOperationException("Unimplemented method 'serialize' for Category"); + JSONDict result = new JSONDict(); + + result.put("class", JSONObject.fromString("Book")); + result.put("name", JSONObject.fromString(name)); + result.put("description", JSONObject.fromString(description)); + + return result; } public static Category deserialize(JSONDict json) { - // TODO: Implemented method 'deserialize' for Category - throw new UnsupportedOperationException("Unimplemented method 'deserialize' for Category"); + return new Category(json.getString("name"), json.getString("description")); } } diff --git a/src/model/GraduateStudent.java b/src/model/GraduateStudent.java index 1da1037..c94f859 100644 --- a/src/model/GraduateStudent.java +++ b/src/model/GraduateStudent.java @@ -40,12 +40,23 @@ public void setThesisTitle(String thesisTitle) { @Override public JSONObject serialize() { - // TODO: Implemented method 'serialize' for GraduateStudent - throw new UnsupportedOperationException("Unimplemented method 'serialize' for GraduateStudent"); + JSONDict result = new JSONDict(); + + result.put("class", JSONObject.fromString("GraduateStudent")); + + result.put("studentId", JSONObject.fromString(studentId)); + result.put("firstName", JSONObject.fromString(firstName)); + result.put("lastName", JSONObject.fromString(lastName)); + result.put("major", JSONObject.fromString(major)); + + result.put("supervisor", JSONObject.fromString(supervisor)); + result.put("thesisTitle", JSONObject.fromString(thesisTitle)); + + return result; } public static GraduateStudent deserialize(JSONDict json) { - // TODO: Implemented method 'deserialize' for GraduateStudent - throw new UnsupportedOperationException("Unimplemented method 'deserialize' for GraduateStudent"); + return new GraduateStudent(json.getString("studentId"), json.getString("firstName"), json.getString("lastName"), + json.getString("major"), json.getString("supervisor"), json.getString("thesisTitle")); } } diff --git a/src/model/Loan.java b/src/model/Loan.java index b155fbb..e654c3d 100644 --- a/src/model/Loan.java +++ b/src/model/Loan.java @@ -3,8 +3,10 @@ import java.util.Date; import jsonlib.JSONSerializable; +import jsonlib.JSONSerializableFactory; import jsonlib.types.JSONObject; import jsonlib.types.JSONDict; +import jsonlib.types.JSONNumber; public class Loan implements JSONSerializable { private Book book; @@ -55,13 +57,25 @@ public Date getDueDate() { @Override public JSONObject serialize() { - // TODO: Implement method 'serialize' for Loan. - throw new UnsupportedOperationException("Unimplemented method 'serialize' for Loan"); + JSONDict result = new JSONDict(); + + result.put("class", JSONObject.fromString("Loan")); + result.put("book", book.serialize()); + result.put("student", student.serialize()); + + result.put("loanDate", new JSONNumber(loanDate.getTime())); + result.put("dueDate", new JSONNumber(dueDate.getTime())); + return result; } public static Loan deserialize(JSONDict json) { - // TODO: Implemented method 'deserialize' for Loan - throw new UnsupportedOperationException("Unimplemented method 'deserialize' for Loan"); + JSONNumber loanDateValue = (JSONNumber) json.get("loanDate"); + JSONNumber dueDateValue = (JSONNumber) json.get("dueDate"); + + Book book = (Book) JSONSerializableFactory.deserialize((JSONDict) json.get("book")); + Student student = (Student) JSONSerializableFactory.deserialize((JSONDict) json.get("student")); + + return new Loan(book, student, new Date(loanDateValue.getValue()), new Date(dueDateValue.getValue())); } } diff --git a/src/model/UndergraduateStudent.java b/src/model/UndergraduateStudent.java index 2657ad7..4fa8990 100644 --- a/src/model/UndergraduateStudent.java +++ b/src/model/UndergraduateStudent.java @@ -29,13 +29,22 @@ public void setEnrollmentYear(int enrollmentYear) { @Override public JSONObject serialize() { - // TODO: Implemented method 'serialize' for UndergraduateStudent - throw new UnsupportedOperationException("Unimplemented method 'serialize' for UndergraduateStudent"); + JSONDict result = new JSONDict(); + + result.put("class", JSONObject.fromString("UndergraduateStudent")); + + result.put("studentId", JSONObject.fromString(studentId)); + result.put("firstName", JSONObject.fromString(firstName)); + result.put("lastName", JSONObject.fromString(lastName)); + result.put("major", JSONObject.fromString(major)); + + result.put("enrollmentYear", JSONObject.fromNumber(enrollmentYear)); + return result; } public static UndergraduateStudent deserialize(JSONDict json) { - // TODO: Implemented method 'deserialize' for UndergraduateStudent - throw new UnsupportedOperationException("Unimplemented method 'deserialize' for UndergraduateStudent"); + return new UndergraduateStudent(json.getString("studentId"), json.getString("firstName"), + json.getString("lastName"), json.getString("major"), json.getInteger("enrollmentYear")); } }