Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add rating field to Book #66

Merged
merged 31 commits into from Mar 20, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
8e0a684
add rate class into model
592363789 Mar 12, 2018
84909f0
add rate into BookBuilder, Book in model
592363789 Mar 12, 2018
82546f8
add rate into XmlAdatpedBook.java in model
592363789 Mar 12, 2018
ac8d146
ADD RATE INTO CLISYNTAX, COMMANDTESTUTIL, ADDCOMMANDPASERTEST AND RAT…
592363789 Mar 12, 2018
0e68d9f
add rate attribute into BookDeserializer and BookShelfDeserializer.java
592363789 Mar 12, 2018
83fca9d
ADD IN rate to ParserUtil, XmlUtilTest, ParserUtilTest, XmlAdaptedBoo…
592363789 Mar 12, 2018
af67199
delete ratecommand
592363789 Mar 12, 2018
3e6147c
add default value(string , have not change to int yet) as "-1" in con…
592363789 Mar 13, 2018
62bd703
fix the error of the constuctor of book
592363789 Mar 13, 2018
6fda165
fix bugs in testing(still string testing)
592363789 Mar 13, 2018
bae8ffd
fix checkstyle errors
592363789 Mar 13, 2018
c0cb498
fix some checkstyle errors
592363789 Mar 13, 2018
5a7ef36
fix problem about checkstyle
592363789 Mar 13, 2018
4284230
FIX SOME CHECKSTYLE ERRORS
592363789 Mar 13, 2018
74575d6
CHECKSTYLE FIX
592363789 Mar 13, 2018
7ffb8fd
fix PR Quality Review
592363789 Mar 13, 2018
1649f25
Merge branch 'master' into ratebook
592363789 Mar 14, 2018
a050ee5
add some coverage
592363789 Mar 14, 2018
102bd98
Merge branch 'ratebook' of https://github.com/592363789/main into rat…
592363789 Mar 14, 2018
5737e1f
Merge branch 'master' into ratebook
592363789 Mar 16, 2018
0b6ff00
Update Book.java
592363789 Mar 16, 2018
cb99b1b
resolve conflicits
592363789 Mar 16, 2018
f3cc484
clear the checkstyle
592363789 Mar 16, 2018
554862e
add converage
592363789 Mar 16, 2018
2734a71
improve rate to rating without changing to int
592363789 Mar 17, 2018
4b7e9d2
change rating issues
592363789 Mar 17, 2018
c29416c
solve the string into integer problem and checkstyle
592363789 Mar 17, 2018
f643581
fix rate to rating
592363789 Mar 18, 2018
a7275c5
delete two lines of rate -1
592363789 Mar 18, 2018
2da4498
fix some grammar mistakes
592363789 Mar 19, 2018
b66d010
fix some bugs
592363789 Mar 20, 2018
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions src/main/java/seedu/address/logic/parser/CliSyntax.java
Expand Up @@ -11,5 +11,6 @@ public class CliSyntax {
public static final Prefix PREFIX_DESCRIPTION = new Prefix("d/");
public static final Prefix PREFIX_ISBN = new Prefix("i/");
public static final Prefix PREFIX_TITLE = new Prefix("t/");
public static final Prefix PREFIX_RATING = new Prefix("r/");

}
20 changes: 20 additions & 0 deletions src/main/java/seedu/address/logic/parser/ParserUtil.java
Expand Up @@ -13,6 +13,7 @@
import seedu.address.model.book.Author;
import seedu.address.model.book.Category;
import seedu.address.model.book.Description;
import seedu.address.model.book.Rating;
import seedu.address.model.book.Title;

/**
Expand Down Expand Up @@ -125,4 +126,23 @@ public static Optional<Description> parseDescription(Optional<String> descriptio
return description.isPresent() ? Optional.of(parseDescription(description.get())) : Optional.empty();
}

/**
* Parses a {@code String rating} into a {@code Rating}.
* Leading and trailing whitespaces will be trimmed.
*/
public static Rating parseRating(int rating) {
requireNonNull(rating);
return new Rating(rating);
}

/**
* Parses a {@code Optional<String> rating} into an {@code Optional<Rating>}
* if {@code rating} is present.
* See header comment of this class regarding the use of {@code Optional} parameters.
*/
public static Optional<Rating> parseRating(Optional<Integer> rating) {
requireNonNull(rating);
return rating.isPresent() ? Optional.of(parseRating(rating.get())) : Optional.empty();
}

}
20 changes: 20 additions & 0 deletions src/main/java/seedu/address/model/book/Book.java
Expand Up @@ -18,6 +18,7 @@ public class Book {
private final Title title;
private final UniqueList<Category> categories;
private final Description description;
private final Rating rating;
private final Gid gid;
private final Isbn isbn;
private final PublicationDate publicationDate;
Expand All @@ -35,6 +36,21 @@ public Book(Gid gid, Isbn isbn, Set<Author> authors, Title title, Set<Category>
this.title = title;
this.categories = new UniqueList<>(categories);
this.description = description;
this.rating = new Rating(-1);
this.publicationDate = publicationDate;
this.publisher = publisher;
}

public Book(Gid gid, Isbn isbn, Set<Author> authors, Title title, Set<Category> categories,
Description description, Rating rating, Publisher publisher, PublicationDate publicationDate) {
requireAllNonNull(gid, isbn, authors, title, categories, description, publisher, publicationDate);
this.gid = gid;
this.isbn = isbn;
this.authors = new UniqueList<>(authors);
this.title = title;
this.categories = new UniqueList<>(categories);
this.description = description;
this.rating = rating;
this.publicationDate = publicationDate;
this.publisher = publisher;
}
Expand Down Expand Up @@ -63,6 +79,10 @@ public Description getDescription() {
return description;
}

public Rating getRating() {
return rating;
}

public Gid getGid() {
return gid;
}
Expand Down
40 changes: 40 additions & 0 deletions src/main/java/seedu/address/model/book/Rating.java
@@ -0,0 +1,40 @@
package seedu.address.model.book;

import static java.util.Objects.requireNonNull;

/**
* Represents a book's rating.
* Guarantees: immutable.
*/
public class Rating {

public final Integer rating;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use an int instead?


/**
* Constructs a {@code Rating}.
*
* @param rating A book rating.
*/
public Rating(Integer rating) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use an int instead?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using int will cause some error in the following override methods, so I changed to Integer

requireNonNull(rating);
this.rating = rating;
}

@Override
public String toString() {
return rating.toString();
}

@Override
public boolean equals(Object other) {
return other == this // short circuit if same object
|| (other instanceof Rating // instanceof handles nulls
&& this.rating.equals(((Rating) other).rating)); // state check
}

@Override
public int hashCode() {
return rating.hashCode();
}

}
Expand Up @@ -36,7 +36,6 @@ public class BookDeserializer extends StdDeserializer<Book> {
public Book deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException {
JsonRoot root = jp.readValueAs(JsonRoot.class);
JsonVolumeInfo volumeInfo = root.volumeInfo;

Isbn isbn = getIsbnFromIndustryIdentifiers(volumeInfo.industryIdentifiers);

if (isbn == null) {
Expand All @@ -46,7 +45,8 @@ public Book deserialize(JsonParser jp, DeserializationContext ctxt) throws IOExc
return new Book(new Gid(root.id), isbn,
BookDataUtil.getAuthorSet(volumeInfo.authors), new Title(volumeInfo.title),
getCategorySet(volumeInfo.categories), getDescription(volumeInfo.description),
new Publisher(volumeInfo.publisher), new PublicationDate(volumeInfo.publishedDate));
new Publisher(volumeInfo.publisher),
new PublicationDate(volumeInfo.publishedDate));
}

private Isbn getIsbnFromIndustryIdentifiers(JsonIndustryIdentifiers[] iiArray) {
Expand Down
Expand Up @@ -48,7 +48,6 @@ public BookShelf deserialize(JsonParser jp, DeserializationContext ctxt) throws
/** Converts a JsonVolume into a Book, and add it into the book shelf. */
private void convertAndAddBook(BookShelf bookShelf, JsonVolume volume, Gid gid) {
JsonVolumeInfo volumeInfo = volume.volumeInfo;

Isbn isbn = getIsbnFromIndustryIdentifiers(volumeInfo.industryIdentifiers);

if (isbn == null) {
Expand All @@ -60,6 +59,7 @@ private void convertAndAddBook(BookShelf bookShelf, JsonVolume volume, Gid gid)
BookDataUtil.getAuthorSet(volumeInfo.authors), new Title(volumeInfo.title),
BookDataUtil.getCategorySet(volumeInfo.categories), new Description(volumeInfo.description),
new Publisher(volumeInfo.publisher), new PublicationDate(volumeInfo.publishedDate));

try {
bookShelf.addBook(book);
} catch (DuplicateBookException e) {
Expand Down
33 changes: 32 additions & 1 deletion src/main/java/seedu/address/storage/XmlAdaptedBook.java
Expand Up @@ -16,6 +16,7 @@
import seedu.address.model.book.Isbn;
import seedu.address.model.book.PublicationDate;
import seedu.address.model.book.Publisher;
import seedu.address.model.book.Rating;
import seedu.address.model.book.Title;

/**
Expand All @@ -34,6 +35,8 @@ public class XmlAdaptedBook {
@XmlElement(required = true)
private String description;
@XmlElement(required = true)
private Integer rating;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use an int instead, if possible.

@XmlElement(required = true)
private String publisher;
@XmlElement(required = true)
private String publicationDate;
Expand All @@ -53,16 +56,36 @@ public XmlAdaptedBook() {}
* Constructs an {@code XmlAdaptedBook} with the given book details.
*/
public XmlAdaptedBook(String gid, String isbn, String title, String description,
List<XmlAdaptedAuthor> authors, List<XmlAdaptedCategory> categories,
String publisher, String publicationDate) {
this.title = title;
this.description = description;
this.rating = -1;
if (authors != null) {
this.authors = new ArrayList<>(authors);
}
if (categories != null) {
this.categories = new ArrayList<>(categories);
}
this.gid = gid;
this.isbn = isbn;
this.publicationDate = publicationDate;
this.publisher = publisher;
}

public XmlAdaptedBook(String gid, String isbn, String title, String description, Integer rate,
List<XmlAdaptedAuthor> authors, List<XmlAdaptedCategory> categories,
String publisher, String publicationDate) {
this.title = title;
this.description = description;
this.rating = -1;
if (authors != null) {
this.authors = new ArrayList<>(authors);
}
if (categories != null) {
this.categories = new ArrayList<>(categories);
}
this.rating = rate;
this.gid = gid;
this.isbn = isbn;
this.publicationDate = publicationDate;
Expand All @@ -79,6 +102,7 @@ public XmlAdaptedBook(Book source) {
isbn = source.getIsbn().isbn;
title = source.getTitle().title;
description = source.getDescription().description;
rating = source.getRating().rating;
authors = new ArrayList<>();
for (Author author : source.getAuthors()) {
authors.add(new XmlAdaptedAuthor(author));
Expand Down Expand Up @@ -118,6 +142,12 @@ public Book toModelType() throws IllegalValueException {
}
final Description description = new Description(this.description);

if (this.rating == null) {
throw new IllegalValueException(String.format(MISSING_FIELD_MESSAGE_FORMAT,
Rating.class.getSimpleName()));
}
final Rating rating = new Rating(this.rating);

if (this.gid == null) {
throw new IllegalValueException(String.format(MISSING_FIELD_MESSAGE_FORMAT,
Gid.class.getSimpleName()));
Expand All @@ -143,7 +173,7 @@ public Book toModelType() throws IllegalValueException {
final PublicationDate publicationDate = new PublicationDate(this.publicationDate);

return new Book(gid, isbn, new HashSet<>(bookAuthors), title,
new HashSet<>(bookCategories), description, publisher, publicationDate);
new HashSet<>(bookCategories), description, rating, publisher, publicationDate);
}

@Override
Expand All @@ -159,6 +189,7 @@ public boolean equals(Object other) {
XmlAdaptedBook otherBook = (XmlAdaptedBook) other;
return Objects.equals(title, otherBook.title)
&& Objects.equals(description, otherBook.description)
&& Objects.equals(rating, otherBook.rating)
&& authors.equals(otherBook.authors)
&& categories.equals(otherBook.categories)
&& Objects.equals(gid, otherBook.gid)
Expand Down
Expand Up @@ -4,6 +4,7 @@
<books>
<title>Artemis</title>
<description>This is Artemis.</description>
<rating>Rate</rating>
<authors>Andy Weir</authors>
<categories>Fiction</categories>
<gid>ry3GjwEACAAJ</gid>
Expand Down
Expand Up @@ -3,6 +3,7 @@
<books>
<title>Artemis</title>
<description>This is Artemis.</description>
<rating>-1</rating>
<authors>Andy Weir</authors>
<categories>Fiction</categories>
<gid>ry3GjwEACAAJ</gid>
Expand All @@ -13,6 +14,7 @@
<books>
<title>Babylon's Ashes</title>
<description>This is Babylon's Ashes.</description>
<rating>-1</rating>
<authors>James S. A. Corey</authors>
<categories>Fiction</categories>
<gid>3jsYCwAAQBAJ</gid>
Expand All @@ -23,6 +25,7 @@
<books>
<title>The Collapsing Empire</title>
<description>This is The Collapsing Empire.</description>
<rating>-1</rating>
<authors>John Scalzi</authors>
<categories>Fiction</categories>
<gid>2SoaDAAAQBAJ</gid>
Expand All @@ -33,6 +36,7 @@
<books>
<title>Consider Phlebas</title>
<description>This is Consider Phlebas.</description>
<rating>-1</rating>
<authors>Iain M. Banks</authors>
<categories>Fiction</categories>
<categories>Science Fiction</categories>
Expand All @@ -44,6 +48,7 @@
<books>
<title>Waking Gods</title>
<description>This is Waking Gods.</description>
<rating>-1</rating>
<authors>Sylvain Neuvel</authors>
<categories>Fiction</categories>
<categories>Science Fiction</categories>
Expand Down
1 change: 1 addition & 0 deletions src/test/data/XmlUtilTest/missingBookField.xml
Expand Up @@ -2,6 +2,7 @@
<!-- Book with missing title field -->
<book>
<description>This is Artemis.</description>
<rating>-1</rating>
<authors>Andy Weir</authors>
<categories>Fiction</categories>
</book>
6 changes: 6 additions & 0 deletions src/test/data/XmlUtilTest/tempBookShelf.xml
Expand Up @@ -3,6 +3,7 @@
<books>
<title>Artemis</title>
<description>This is Artemis.</description>
<rating>-1</rating>
<authors>Andy Weir</authors>
<categories>Fiction</categories>
<githubUsername></githubUsername>
Expand All @@ -13,7 +14,12 @@
<books>
<title>Babylon's Ashes</title>
<description>This is Babylon's Ashes.</description>
<rating>-1</rating>
<authors>James S. A. Corey</authors>
<categories>Fiction</categories>
<githubUsername></githubUsername>
<street></street>
<postalCode></postalCode>
<city></city>
</books>
</bookshelf>
7 changes: 4 additions & 3 deletions src/test/data/XmlUtilTest/validBook.xml
@@ -1,12 +1,13 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>

<book>
<gid>ry3GjwEACAAJ</gid>
<isbn>9780525572664</isbn>
<title>Artemis</title>
<description>This is Artemis.</description>
<rating>-1</rating>
<authors>Andy Weir</authors>
<categories>Fiction</categories>
<gid>ry3GjwEACAAJ</gid>
<isbn>9780525572664</isbn>
<publisher></publisher>
<publisher> </publisher>
<publicationDate>2017-11-14</publicationDate>
</book>
4 changes: 4 additions & 0 deletions src/test/data/XmlUtilTest/validBookShelf.xml
Expand Up @@ -3,6 +3,7 @@
<books>
<title>Artemis</title>
<description>This is Artemis.</description>
<rating>-1</rating>
<authors>Andy Weir</authors>
<categories>Fiction</categories>
<gid>ry3GjwEACAAJ</gid>
Expand All @@ -13,6 +14,7 @@
<books>
<title>Babylon's Ashes</title>
<description>This is Babylon's Ashes.</description>
<rating>-1</rating>
<authors>James S. A. Corey</authors>
<categories>Fiction</categories>
<gid>3jsYCwAAQBAJ</gid>
Expand All @@ -23,6 +25,7 @@
<books>
<title>The Collapsing Empire</title>
<description>This is The Collapsing Empire.</description>
<rating>-1</rating>
<authors>John Scalzi</authors>
<categories>Fiction</categories>
<gid>2SoaDAAAQBAJ</gid>
Expand All @@ -33,6 +36,7 @@
<books>
<title>Waking Gods</title>
<description>This is Waking Gods.</description>
<rating>-1</rating>
<authors>Sylvain Neuvel</authors>
<categories>Fiction</categories>
<categories>Science Fiction</categories>
Expand Down
5 changes: 3 additions & 2 deletions src/test/java/seedu/address/commons/util/XmlUtilTest.java
Expand Up @@ -35,13 +35,14 @@ public class XmlUtilTest {

private static final String VALID_TITLE = "Artemis";
private static final String VALID_DESCRIPTION = "This is Artemis.";
private static final int VALID_RATING = -1;
private static final List<XmlAdaptedAuthor> VALID_AUTHORS =
Collections.singletonList(new XmlAdaptedAuthor("Andy Weir"));
private static final List<XmlAdaptedCategory> VALID_CATEGORIES =
Collections.singletonList(new XmlAdaptedCategory("Fiction"));
private static final String VALID_GID = "ry3GjwEACAAJ";
private static final String VALID_ISBN = "9780525572664";
private static final String VALID_PUBLISHER = "";
private static final String VALID_PUBLISHER = " ";
private static final String VALID_PUBLICATION_DATE = "2017-11-14";

@Rule
Expand Down Expand Up @@ -82,7 +83,7 @@ public void xmlAdaptedBookFromFile_fileWithMissingBookField_validResult() throws
XmlAdaptedBook actualBook = XmlUtil.getDataFromFile(
MISSING_BOOK_FIELD_FILE, XmlAdaptedBookWithRootElement.class);
XmlAdaptedBook expectedBook = new XmlAdaptedBook(null, null, null,
VALID_DESCRIPTION, VALID_AUTHORS, VALID_CATEGORIES, null, null);
VALID_DESCRIPTION, VALID_RATING, VALID_AUTHORS, VALID_CATEGORIES, null, null);
assertEquals(expectedBook, actualBook);
}

Expand Down