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
100 changes: 76 additions & 24 deletions src/data/FileDataModel.java
Original file line number Diff line number Diff line change
@@ -1,46 +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 {
public void loadBooks(String filePath) {
// TODO: Implement method 'filePath'.
throw new UnsupportedOperationException("Unimplemented method '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 List<Book> loadBooks() {
return loadFromFile(BOOKS_FILEPATH);
}

public List<Student> loadStudents() {
return loadFromFile(STUDENTS_FILEPATH);
}

public void saveBooks(List<Book> books, String filePath) {
// TODO: Implement method 'saveBooks'.
throw new UnsupportedOperationException("Unimplemented method 'saveBooks'");
public List<Category> loadCategories() {
return loadFromFile(CATEGORIES_FILEPATH);
}

public void loadStudents(String filePath) {
// TODO: Implement method 'loadStudents'.
throw new UnsupportedOperationException("Unimplemented method 'loadStudents'");
public List<Loan> loadLoans() {
return loadFromFile(LOANS_FILEPATH);
}

public void saveStudents(List<Student> students, String filePath) {
// TODO: Implement method 'saveStudents'.
throw new UnsupportedOperationException("Unimplemented method 'saveStudents'");
public void saveBooks(List<Book> books) {
writeToFile(BOOKS_FILEPATH, JSONObject.fromList(books).toString());
}

public void loadCategories(String filePath) {
// TODO: Implement method 'loadCategories'.
throw new UnsupportedOperationException("Unimplemented method 'loadCategories'");
public void saveStudents(List<Student> students) {
writeToFile(STUDENTS_FILEPATH, JSONObject.fromList(students).toString());
}

public void saveCategories(List<Category> categories, String filePath) {
// TODO: Implement method 'saveCategories'.
throw new UnsupportedOperationException("Unimplemented method 'saveCategories'");
public void saveCategories(List<Category> categories) {
writeToFile(CATEGORIES_FILEPATH, JSONObject.fromList(categories).toString());
}

public void loadLoans(String filePath) {
// TODO: Implement method 'loadLoans'.
throw new UnsupportedOperationException("Unimplemented method 'loadLoans'");
public void saveLoans(List<Loan> loans) {
writeToFile(LOANS_FILEPATH, JSONObject.fromList(loans).toString());
}

public void saveLoans(List<Loan> loans, String filePath) {
// TODO: Implement method 'saveLoans'.
throw new UnsupportedOperationException("Unimplemented method 'saveLoans'");
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 <T> List<T> 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<T> result = new ArrayList<>();
JSONList listJSON = (JSONList) jsonObj;

for (JSONObject itemJSON : listJSON) {
T item = (T) JSONSerializableFactory.deserialize((JSONDict) itemJSON);
result.add(item);
}

return result;
}

}
8 changes: 8 additions & 0 deletions src/jsonlib/JSONExpectFailedException.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package jsonlib;

public class JSONExpectFailedException extends Exception {

public JSONExpectFailedException(String msg) {
super(msg);
}
}
33 changes: 21 additions & 12 deletions src/jsonlib/JSONParser.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package jsonlib;

import jsonlib.types.JSONBoolean;
import jsonlib.types.JSONDict;
import jsonlib.types.JSONList;
import jsonlib.types.JSONNumber;
Expand Down Expand Up @@ -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());

Expand All @@ -101,8 +102,13 @@ private JSONNumber parseNumber() throws JSONExpectFailedException {
double number = Double.valueOf(integerPartStr + "." + floatingPartStr);
return new JSONNumber<Double>(sign * number);
} else {
int number = Integer.valueOf(integerPartStr.toString());
return new JSONNumber<Integer>(sign * number);
try {
int number = Integer.valueOf(integerPartStr.toString());
return new JSONNumber<Integer>(sign * number);
} catch (NumberFormatException e) {
long number = Long.valueOf(integerPartStr.toString());
return new JSONNumber<Long>(sign * number);
}
}
}

Expand Down Expand Up @@ -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()) {
Expand All @@ -160,22 +176,15 @@ 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;
}
}
return null;
}

}

class JSONExpectFailedException extends Exception {

public JSONExpectFailedException(String msg) {
super(msg);
}
}
36 changes: 36 additions & 0 deletions src/jsonlib/types/JSONBoolean.java
Original file line number Diff line number Diff line change
@@ -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;
}
}
8 changes: 8 additions & 0 deletions src/jsonlib/types/JSONDict.java
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
4 changes: 4 additions & 0 deletions src/jsonlib/types/JSONObject.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ public static <T extends JSONSerializable> JSONObject fromDict(Map<String, T> da
return new JSONDict(data);
}

public static JSONObject fromBoolean(boolean data) {
return new JSONBoolean(data);
}

@Override
public abstract String toString();
}
75 changes: 57 additions & 18 deletions src/model/Book.java
Original file line number Diff line number Diff line change
@@ -1,32 +1,71 @@
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() {
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) {
return new Book(json.getString("title"), json.getString("author"), json.getString("isbn"),
json.getInteger("publicationYear"), json.getBoolean("isBorrowed"));
}
}
Loading