From aea8835174491f4d6129eff5a2f0ce6d856d330a Mon Sep 17 00:00:00 2001 From: dalsontws Date: Mon, 28 Oct 2019 01:31:54 +0800 Subject: [PATCH 1/8] change addressbook.json to keyboardflashcards.json --- data/addressbook.json | 40 ---------------------------------------- docs/UserGuide.adoc | 4 ++-- 2 files changed, 2 insertions(+), 42 deletions(-) delete mode 100644 data/addressbook.json diff --git a/data/addressbook.json b/data/addressbook.json deleted file mode 100644 index ce7f3eaa31d..00000000000 --- a/data/addressbook.json +++ /dev/null @@ -1,40 +0,0 @@ -{ - "flashcards" : [ { - "question" : "What is pointer in C ?", - "answer" : "A pointer variable stores the address of a memory location", - "rating" : "easy", - "categories" : [ "CS2100", "C", "POINTER" ] - }, { - "question" : "How to declare a pointer in C ?", - "answer" : "& + variable name", - "rating" : "good", - "categories" : [ "CS2100" ] - }, { - "question" : "What is internet", - "answer" : "The Internet is a network of connected computing devices", - "rating" : "good", - "categories" : [ "CS2105" ] - }, { - "question" : "How is data transmitted through net?", - "answer" : "Circuit switching / Packet switching", - "rating" : "easy", - "categories" : [ ] - }, { - "question" : "What is link transmission rate?", - "answer" : "It is aka link capacity or link bandwidth", - "rating" : "easy", - "categories" : [ ] - }, { - "question" : "Transmission delay", - "answer" : "L (bit) / R (bits.sec)", - "rating" : "good", - "categories" : [ ] - } ], - "deadlines" : [ { - "task" : "CS2103 Final Project Submission", - "date" : "10/11/2019" - }, { - "task" : "CS2101 User Guide Submission", - "date" : "12/11/2019" - } ] -} diff --git a/docs/UserGuide.adoc b/docs/UserGuide.adoc index adac200c5d2..d4ad7ff6f9e 100644 --- a/docs/UserGuide.adoc +++ b/docs/UserGuide.adoc @@ -245,8 +245,8 @@ image::deadline-entered.png[][width="600"] Removes a specific deadline into the deadline list. The list on the right-side pane will no longer have the deadline. `remove 2` - -**** +]= +]**** * Removes the deadline at the specified `INDEX` from the list. The index refers to the index number shown in the displayed deadline list on the right-sde pane. The index *must be a positive integer* 1, 2, 3, ... **** From b91cc3b595c75522773e8d5d5bda437fb1e8775a Mon Sep 17 00:00:00 2001 From: dalsontws Date: Mon, 28 Oct 2019 17:13:06 +0800 Subject: [PATCH 2/8] added bad command to set rating --- .../address/logic/commands/BadCommand.java | 60 +++++++++++++++++++ .../logic/parser/BadCommandParser.java | 31 ++++++++++ .../parser/KeyboardFlashCardsParser.java | 4 ++ .../address/model/KeyboardFlashCards.java | 2 +- .../address/model/deadline/BadQuestions.java | 42 +++++++++++++ 5 files changed, 138 insertions(+), 1 deletion(-) create mode 100644 src/main/java/seedu/address/logic/commands/BadCommand.java create mode 100644 src/main/java/seedu/address/logic/parser/BadCommandParser.java create mode 100644 src/main/java/seedu/address/model/deadline/BadQuestions.java diff --git a/src/main/java/seedu/address/logic/commands/BadCommand.java b/src/main/java/seedu/address/logic/commands/BadCommand.java new file mode 100644 index 00000000000..13ed828eddd --- /dev/null +++ b/src/main/java/seedu/address/logic/commands/BadCommand.java @@ -0,0 +1,60 @@ +//@@author: dalsontws + +package seedu.address.logic.commands; + +import static java.util.Objects.requireNonNull; + +import java.util.List; + +import seedu.address.commons.core.Messages; +import seedu.address.commons.core.index.Index; + +import seedu.address.logic.commands.exceptions.CommandException; +import seedu.address.model.Model; +import seedu.address.model.deadline.BadQuestions; +import seedu.address.model.deadline.Deadline; +import seedu.address.model.deadline.Task; +import seedu.address.model.flashcard.FlashCard; +import seedu.address.model.flashcard.Question; + + +/** + * Set certain FlashCards as 'Bad' + * This will then add these set of flashcards into a certain Deadline + */ +public class BadCommand extends Command { + + public static final String COMMAND_WORD = "bad"; + + public static final String MESSAGE_USAGE = COMMAND_WORD + + ": Sets a specific flashCard identified by the index number used in the displayed flashCard list.\n" + + "as a 'Bad' flashcard that will require re-test.\n" + + "Parameters: INDEX (must be a positive integer)\n" + + "Example: " + COMMAND_WORD + " 1"; + + public static final String MESSAGE_SUCCESS = "Flashcard has been added into Deadlines!"; + + private final Index index; + + public BadCommand(Index index) { + requireNonNull(index); + this.index = index; + } + + @Override + public CommandResult execute(Model model) throws CommandException { + requireNonNull(model); + List lastShownList = model.getFilteredFlashCardList(); + if (index.getZeroBased() >= lastShownList.size()) { + throw new CommandException(Messages.MESSAGE_INVALID_FLASHCARD_DISPLAYED_INDEX); + } + + FlashCard badFlashcard = lastShownList.get(index.getZeroBased()); + Question q = badFlashcard.getQuestion(); + Task task = new Task("To Do: Bad Questions"); + //TODO: add questions and due date into filtered bad flashcards list + Deadline deadline = new Deadline(task, BadQuestions.getBadDeadline()); + model.addDeadline(deadline); + return new CommandResult(MESSAGE_SUCCESS); + } +} diff --git a/src/main/java/seedu/address/logic/parser/BadCommandParser.java b/src/main/java/seedu/address/logic/parser/BadCommandParser.java new file mode 100644 index 00000000000..316cb175b40 --- /dev/null +++ b/src/main/java/seedu/address/logic/parser/BadCommandParser.java @@ -0,0 +1,31 @@ +//@@author: dalsontws + +package seedu.address.logic.parser; + +import static seedu.address.commons.core.Messages.MESSAGE_INVALID_COMMAND_FORMAT; + +import seedu.address.commons.core.index.Index; +import seedu.address.logic.commands.BadCommand; +import seedu.address.logic.parser.exceptions.ParseException; + +/** + * Parses input arguments and creates a new DeleteCommand object + */ +public class BadCommandParser implements Parser { + + /** + * Parses the given {@code String} of arguments in the context of the DeleteCommand + * and returns a DeleteCommand object for execution. + * @throws ParseException if the user input does not conform the expected format + */ + public BadCommand parse(String args) throws ParseException { + try { + Index index = ParserUtil.parseIndex(args); + return new BadCommand(index); + } catch (ParseException pe) { + throw new ParseException( + String.format(MESSAGE_INVALID_COMMAND_FORMAT, BadCommand.MESSAGE_USAGE), pe); + } + } + +} diff --git a/src/main/java/seedu/address/logic/parser/KeyboardFlashCardsParser.java b/src/main/java/seedu/address/logic/parser/KeyboardFlashCardsParser.java index bda5da95458..7f342ae742e 100644 --- a/src/main/java/seedu/address/logic/parser/KeyboardFlashCardsParser.java +++ b/src/main/java/seedu/address/logic/parser/KeyboardFlashCardsParser.java @@ -8,6 +8,7 @@ import java.util.regex.Pattern; import seedu.address.logic.commands.AddCommand; +import seedu.address.logic.commands.BadCommand; import seedu.address.logic.commands.CalendarCommand; import seedu.address.logic.commands.ClearCommand; import seedu.address.logic.commands.Command; @@ -144,6 +145,9 @@ private Command parseNormalCommand(Matcher matcher) throws ParseException { case DeadlineCommand.COMMAND_WORD: return new DeadlineCommandParser().parse(arguments); + case BadCommand.COMMAND_WORD: + return new BadCommandParser().parse(arguments); + case RemoveCommand.COMMAND_WORD: return new RemoveCommandParser().parse(arguments); diff --git a/src/main/java/seedu/address/model/KeyboardFlashCards.java b/src/main/java/seedu/address/model/KeyboardFlashCards.java index 2b18102dea9..a76a04459c2 100644 --- a/src/main/java/seedu/address/model/KeyboardFlashCards.java +++ b/src/main/java/seedu/address/model/KeyboardFlashCards.java @@ -92,7 +92,7 @@ public boolean hasFlashcard(FlashCard flashCard) { * Adds a flashCard to the address book. * The flashCard must not already exist in the address book. */ - public void addFlashcard (FlashCard c) { + public void addFlashcard(FlashCard c) { flashCards.add(c); //update the categoryList addCategory(c.getCategories()); diff --git a/src/main/java/seedu/address/model/deadline/BadQuestions.java b/src/main/java/seedu/address/model/deadline/BadQuestions.java new file mode 100644 index 00000000000..f755332f3bb --- /dev/null +++ b/src/main/java/seedu/address/model/deadline/BadQuestions.java @@ -0,0 +1,42 @@ +package seedu.address.model.deadline; + +import java.time.LocalDate; +import java.time.format.DateTimeFormatter; +import java.util.HashMap; + +import seedu.address.model.KeyboardFlashCards; +import seedu.address.model.Model; +import seedu.address.model.flashcard.Question; + +/** + * BadQuestions class. + * Contains a Hashmap with questions that are rated as 'bad' + * Each Question will be tagged to a specific DueDate, which will be referred to + * in the list of Deadlines + */ +public class BadQuestions { + + private HashMap badQuestions = new HashMap(); + + public BadQuestions() { + //HashMap badQuestions = new HashMap(); + } + + public void addBadQuestion(DueDate d, Question q) { + badQuestions.put(d, q); + } + + public static DueDate getBadDeadline() { + LocalDate today = LocalDate.now(); + LocalDate due = today.plusDays(3); + String formattedDate = due.format(DateTimeFormatter.ofPattern("dd/MM/yyyy")); + DueDate d = new DueDate(formattedDate); + return d; + } + + public void setBadQuestionsAsDeadline(Model model) { + Task task = new Task("Bad Questions"); + Deadline deadline = new Deadline(task, getBadDeadline()); + model.addDeadline(deadline); + } +} From 354d800f2bbbb8b7263dcf10781263d5f0883aad Mon Sep 17 00:00:00 2001 From: dalsontws Date: Wed, 30 Oct 2019 12:00:56 +0800 Subject: [PATCH 3/8] add Gson dependencies --- build.gradle | 2 ++ 1 file changed, 2 insertions(+) diff --git a/build.gradle b/build.gradle index 318157f4523..257b403e799 100644 --- a/build.gradle +++ b/build.gradle @@ -45,6 +45,8 @@ dependencies { String jUnitVersion = '5.4.0' String javaFxVersion = '11' + implementation 'com.google.code.gson:gson:2.8.6' + implementation group: 'org.openjfx', name: 'javafx-base', version: javaFxVersion, classifier: 'win' implementation group: 'org.openjfx', name: 'javafx-base', version: javaFxVersion, classifier: 'mac' implementation group: 'org.openjfx', name: 'javafx-base', version: javaFxVersion, classifier: 'linux' From 03c3f50daf1630730e3f28bf4cae27b997b9fccc Mon Sep 17 00:00:00 2001 From: dalsontws Date: Wed, 30 Oct 2019 12:01:28 +0800 Subject: [PATCH 4/8] add BadDeadlines as hashmap, save to json --- .../address/logic/commands/BadCommand.java | 30 +++++- .../address/model/deadline/BadQuestions.java | 46 ++++++++- .../seedu/address/model/deadline/DueDate.java | 1 - .../address/storage/JsonBadDeadlines.java | 99 +++++++++++++++++++ .../address/storage/JsonLoadBadDeadlines.java | 39 ++++++++ .../seedu/address/ui/DeadlineListPanel.java | 2 +- .../java/seedu/address/ui/DeadlinePanel.java | 5 + src/main/resources/view/DeadlineListCard.fxml | 1 - 8 files changed, 212 insertions(+), 11 deletions(-) create mode 100644 src/main/java/seedu/address/storage/JsonBadDeadlines.java create mode 100644 src/main/java/seedu/address/storage/JsonLoadBadDeadlines.java diff --git a/src/main/java/seedu/address/logic/commands/BadCommand.java b/src/main/java/seedu/address/logic/commands/BadCommand.java index 13ed828eddd..a0ea9b27636 100644 --- a/src/main/java/seedu/address/logic/commands/BadCommand.java +++ b/src/main/java/seedu/address/logic/commands/BadCommand.java @@ -13,9 +13,12 @@ import seedu.address.model.Model; import seedu.address.model.deadline.BadQuestions; import seedu.address.model.deadline.Deadline; +import seedu.address.model.deadline.DueDate; import seedu.address.model.deadline.Task; +import seedu.address.model.deadline.exceptions.DuplicateDeadlineException; import seedu.address.model.flashcard.FlashCard; import seedu.address.model.flashcard.Question; +import seedu.address.storage.JsonBadDeadlines; /** @@ -27,15 +30,19 @@ public class BadCommand extends Command { public static final String COMMAND_WORD = "bad"; public static final String MESSAGE_USAGE = COMMAND_WORD - + ": Sets a specific flashCard identified by the index number used in the displayed flashCard list.\n" + + ": Sets a specific flashCard identified by the\n " + + "index number used in the displayed flashCard list.\n" + "as a 'Bad' flashcard that will require re-test.\n" + "Parameters: INDEX (must be a positive integer)\n" + "Example: " + COMMAND_WORD + " 1"; public static final String MESSAGE_SUCCESS = "Flashcard has been added into Deadlines!"; + public static final String DUPLICATE_DEADLINE = "Flashcard has been added into an existing deadline!"; private final Index index; +// private static JsonBadDeadlines jsonBadDeadlines; + public BadCommand(Index index) { requireNonNull(index); this.index = index; @@ -50,11 +57,26 @@ public CommandResult execute(Model model) throws CommandException { } FlashCard badFlashcard = lastShownList.get(index.getZeroBased()); - Question q = badFlashcard.getQuestion(); + Task task = new Task("To Do: Bad Questions"); + DueDate d = BadQuestions.getBadDeadline(); + Deadline deadline = new Deadline(task, d); + //TODO: add questions and due date into filtered bad flashcards list - Deadline deadline = new Deadline(task, BadQuestions.getBadDeadline()); - model.addDeadline(deadline); + Question q = badFlashcard.getQuestion(); + + BadQuestions badQuestions = new BadQuestions(); + + badQuestions.addBadQuestion(d, q); + + badQuestions.saveAsJson(badQuestions); + + try { + model.addDeadline(deadline); + } catch (DuplicateDeadlineException e) { + return new CommandResult(DUPLICATE_DEADLINE); + } + return new CommandResult(MESSAGE_SUCCESS); } } diff --git a/src/main/java/seedu/address/model/deadline/BadQuestions.java b/src/main/java/seedu/address/model/deadline/BadQuestions.java index f755332f3bb..07d18ba3fa9 100644 --- a/src/main/java/seedu/address/model/deadline/BadQuestions.java +++ b/src/main/java/seedu/address/model/deadline/BadQuestions.java @@ -1,12 +1,16 @@ package seedu.address.model.deadline; +import java.io.FileNotFoundException; +import java.io.FileWriter; +import java.io.IOException; import java.time.LocalDate; import java.time.format.DateTimeFormatter; import java.util.HashMap; -import seedu.address.model.KeyboardFlashCards; +import com.google.gson.Gson; import seedu.address.model.Model; import seedu.address.model.flashcard.Question; +import seedu.address.storage.JsonBadDeadlines; /** * BadQuestions class. @@ -16,14 +20,27 @@ */ public class BadQuestions { - private HashMap badQuestions = new HashMap(); + private HashMap internalMap = new HashMap(); + private JsonBadDeadlines jsonBadDeadlines; public BadQuestions() { - //HashMap badQuestions = new HashMap(); +// internalMap = new HashMap(); + } + + public HashMap getBadQuestionsList() { + return internalMap; + } + + public void setBadQuestionsList(HashMap map) { + internalMap = map; } public void addBadQuestion(DueDate d, Question q) { - badQuestions.put(d, q); + internalMap.put(d.toString(), q.toString()); + } + + public void loadBadQuestions() throws FileNotFoundException { + internalMap = jsonBadDeadlines.loadJsonBadDeadlines(); } public static DueDate getBadDeadline() { @@ -39,4 +56,25 @@ public void setBadQuestionsAsDeadline(Model model) { Deadline deadline = new Deadline(task, getBadDeadline()); model.addDeadline(deadline); } + + /** + * Save as json. + */ + public void saveAsJson(BadQuestions badQuestions) { + Gson gson = new Gson(); + + // convert java object to JSON format, + // and returned as JSON formatted string + String json = gson.toJson(badQuestions.getBadQuestionsList()); + try { + //write converted json data to a file + //TODO: fix load and save json, json save replace file instead of appending + FileWriter writer = new FileWriter("data/BadDeadlines.json", true); + writer.write(json); + writer.close(); + + } catch (IOException e) { + e.printStackTrace(); + } + } } diff --git a/src/main/java/seedu/address/model/deadline/DueDate.java b/src/main/java/seedu/address/model/deadline/DueDate.java index e55b9e522af..1d836d1b1cd 100644 --- a/src/main/java/seedu/address/model/deadline/DueDate.java +++ b/src/main/java/seedu/address/model/deadline/DueDate.java @@ -59,5 +59,4 @@ public String toString() { public int hashCode() { return dateStr.hashCode(); } - } diff --git a/src/main/java/seedu/address/storage/JsonBadDeadlines.java b/src/main/java/seedu/address/storage/JsonBadDeadlines.java new file mode 100644 index 00000000000..156c8f08d6f --- /dev/null +++ b/src/main/java/seedu/address/storage/JsonBadDeadlines.java @@ -0,0 +1,99 @@ +package seedu.address.storage; + +import java.io.BufferedReader; +import java.io.File; +import java.io.FileNotFoundException; +import java.io.FileReader; +import java.io.FileWriter; +import java.io.IOException; +import java.util.HashMap; + +import com.fasterxml.jackson.core.type.TypeReference; +import com.fasterxml.jackson.databind.ObjectMapper; + +import com.google.gson.Gson; +import com.google.gson.reflect.TypeToken; +import seedu.address.model.deadline.BadQuestions; +import seedu.address.model.deadline.DueDate; +import seedu.address.model.flashcard.Question; + +/** + * The type Json store bad deadlines. + */ +public class JsonBadDeadlines { + + private HashMap badQuestions; + + /** + * Instantiates a new JsonBadDeadlines. + * Stores all the "Bad" flashcards into a Hashmap in json + */ + public JsonBadDeadlines(BadQuestions badQuestions) { + HashMap badQuestionList = badQuestions.getBadQuestionsList(); + + Gson gson = new Gson(); + + // convert java object to JSON format, + // and returned as JSON formatted string + String json = gson.toJson(badQuestionList); + try { + //write converted json data to a file named "CountryGSON.json" + FileWriter writer = new FileWriter("data/test.json"); + writer.write(json); + writer.close(); + + } catch (IOException e) { + e.printStackTrace(); + } + } + +// ObjectMapper mapper = new ObjectMapper(); +// +// /** +// * Convert Map to JSON and write to a file +// */ +// try { +// mapper.writeValue(new File("data/BadDeadlines.json"), badQuestionList); +// } catch (Exception e) { +// e.printStackTrace(); +// } +// } + + /** + * Load json bad deadlines. + * Load all the "Bad" flashcards into a Hashmap from a json + */ + public HashMap loadJsonBadDeadlines() throws FileNotFoundException { + + BadQuestions badQuestions = new BadQuestions(); + Gson gson = new Gson(); + BufferedReader br = new BufferedReader(new FileReader("data/test.json")); + + //convert the json string back to object + + HashMap mapObj = new Gson().fromJson( + br, new TypeToken>() {}.getType() + ); + + // HashMap map = gson.fromJson(br, badQuestions.getBadQuestionsList().getClass()); + System.out.println(mapObj); + return mapObj; + + +// ObjectMapper mapper = new ObjectMapper(); +// /** +// * Read JSON from a file into a HashMap +// */ +// try { +// HashMap map = mapper.readValue(new File( +// "data/BadDeadlines.json"), new TypeReference>() {}); +// System.out.println("data:" + map); +// +//// badQuestions = map; +// return map; +// } catch (Exception e) { +// e.printStackTrace(); +// return null; +// } + } +} diff --git a/src/main/java/seedu/address/storage/JsonLoadBadDeadlines.java b/src/main/java/seedu/address/storage/JsonLoadBadDeadlines.java new file mode 100644 index 00000000000..f2e620fbe94 --- /dev/null +++ b/src/main/java/seedu/address/storage/JsonLoadBadDeadlines.java @@ -0,0 +1,39 @@ +//package seedu.address.storage; +// +//import java.io.BufferedReader; +//import java.io.File; +//import java.io.FileNotFoundException; +//import java.io.FileReader; +//import java.io.FileWriter; +//import java.io.IOException; +//import java.util.HashMap; +//import java.util.Map; +// +//import com.fasterxml.jackson.core.type.TypeReference; +//import com.fasterxml.jackson.databind.ObjectMapper; +//import com.google.gson.Gson; +//import com.google.gson.reflect.TypeToken; +//import seedu.address.model.deadline.DueDate; +//import seedu.address.model.flashcard.Question; +//import seedu.address.model.deadline.BadQuestions; +// +//public class JsonLoadBadDeadlines { +//// private static HashMap badQuestions; +// +// public static void main(String[] args) throws FileNotFoundException { +// BadQuestions badQuestions = new BadQuestions(); +// Gson gson = new Gson(); +// BufferedReader br = new BufferedReader(new FileReader("data/test.json")); +// +// //convert the json string back to object +// +// HashMap mapObj = new Gson().fromJson( +// br, new TypeToken>() {}.getType() +// ); +// +// HashMap map = gson.fromJson(br, badQuestions.getBadQuestionsList().getClass()); +// System.out.println(mapObj); +//// return mapObj; +// +// } +//} diff --git a/src/main/java/seedu/address/ui/DeadlineListPanel.java b/src/main/java/seedu/address/ui/DeadlineListPanel.java index 36b61d0b98d..acc8f18207e 100644 --- a/src/main/java/seedu/address/ui/DeadlineListPanel.java +++ b/src/main/java/seedu/address/ui/DeadlineListPanel.java @@ -28,7 +28,7 @@ public DeadlineListPanel(ObservableList deadlineList) { } /** - * Custom {@code ListCell} that displays the graphics of a {@code FlashCard} using a {@code FlashCardPanel}. + * Custom {@code ListCell} that displays the graphics of a {@code FlashCard} using a {@code DeadlinePanel}. */ class DeadlineListViewCell extends ListCell { @Override diff --git a/src/main/java/seedu/address/ui/DeadlinePanel.java b/src/main/java/seedu/address/ui/DeadlinePanel.java index 726dbfd3706..d8400c3bb61 100644 --- a/src/main/java/seedu/address/ui/DeadlinePanel.java +++ b/src/main/java/seedu/address/ui/DeadlinePanel.java @@ -6,6 +6,8 @@ import javafx.scene.layout.Region; import seedu.address.model.deadline.Deadline; +import java.time.LocalDate; + //@@author dalsontws /** * An UI component that displays information of a {@code FlashCard}. @@ -30,6 +32,9 @@ public DeadlinePanel(Deadline deadline, int displayedIndex) { this.deadline = deadline; id.setText(displayedIndex + ". "); task.setText(deadline.getTask().toString()); + LocalDate today = LocalDate.now(); + deadline.getDueDate(); + dueDate.setText("Due Date: " + deadline.getDueDate().toString()); } diff --git a/src/main/resources/view/DeadlineListCard.fxml b/src/main/resources/view/DeadlineListCard.fxml index ca0fe3451a0..47ce058d22a 100644 --- a/src/main/resources/view/DeadlineListCard.fxml +++ b/src/main/resources/view/DeadlineListCard.fxml @@ -3,7 +3,6 @@ - From a2fcbae8f323005245a5eaf6a1710f0c1da307fe Mon Sep 17 00:00:00 2001 From: dalsontws Date: Wed, 30 Oct 2019 12:22:18 +0800 Subject: [PATCH 5/8] fix checkstyle, remove jsonbaddeadlines class --- .../address/logic/commands/BadCommand.java | 4 - .../address/model/deadline/BadQuestions.java | 8 +- .../address/storage/JsonBadDeadlines.java | 99 ------------------- .../java/seedu/address/ui/DeadlinePanel.java | 8 +- 4 files changed, 7 insertions(+), 112 deletions(-) delete mode 100644 src/main/java/seedu/address/storage/JsonBadDeadlines.java diff --git a/src/main/java/seedu/address/logic/commands/BadCommand.java b/src/main/java/seedu/address/logic/commands/BadCommand.java index a0ea9b27636..c50509b1480 100644 --- a/src/main/java/seedu/address/logic/commands/BadCommand.java +++ b/src/main/java/seedu/address/logic/commands/BadCommand.java @@ -18,8 +18,6 @@ import seedu.address.model.deadline.exceptions.DuplicateDeadlineException; import seedu.address.model.flashcard.FlashCard; import seedu.address.model.flashcard.Question; -import seedu.address.storage.JsonBadDeadlines; - /** * Set certain FlashCards as 'Bad' @@ -41,8 +39,6 @@ public class BadCommand extends Command { private final Index index; -// private static JsonBadDeadlines jsonBadDeadlines; - public BadCommand(Index index) { requireNonNull(index); this.index = index; diff --git a/src/main/java/seedu/address/model/deadline/BadQuestions.java b/src/main/java/seedu/address/model/deadline/BadQuestions.java index 07d18ba3fa9..1ed49ec2b6f 100644 --- a/src/main/java/seedu/address/model/deadline/BadQuestions.java +++ b/src/main/java/seedu/address/model/deadline/BadQuestions.java @@ -8,9 +8,9 @@ import java.util.HashMap; import com.google.gson.Gson; + import seedu.address.model.Model; import seedu.address.model.flashcard.Question; -import seedu.address.storage.JsonBadDeadlines; /** * BadQuestions class. @@ -21,10 +21,10 @@ public class BadQuestions { private HashMap internalMap = new HashMap(); - private JsonBadDeadlines jsonBadDeadlines; + //private JsonBadDeadlines jsonBadDeadlines; public BadQuestions() { -// internalMap = new HashMap(); + //TODO: add initialisation of bad deadline list - load json } public HashMap getBadQuestionsList() { @@ -40,7 +40,7 @@ public void addBadQuestion(DueDate d, Question q) { } public void loadBadQuestions() throws FileNotFoundException { - internalMap = jsonBadDeadlines.loadJsonBadDeadlines(); + //internalMap = jsonBadDeadlines.loadJsonBadDeadlines(); } public static DueDate getBadDeadline() { diff --git a/src/main/java/seedu/address/storage/JsonBadDeadlines.java b/src/main/java/seedu/address/storage/JsonBadDeadlines.java deleted file mode 100644 index 156c8f08d6f..00000000000 --- a/src/main/java/seedu/address/storage/JsonBadDeadlines.java +++ /dev/null @@ -1,99 +0,0 @@ -package seedu.address.storage; - -import java.io.BufferedReader; -import java.io.File; -import java.io.FileNotFoundException; -import java.io.FileReader; -import java.io.FileWriter; -import java.io.IOException; -import java.util.HashMap; - -import com.fasterxml.jackson.core.type.TypeReference; -import com.fasterxml.jackson.databind.ObjectMapper; - -import com.google.gson.Gson; -import com.google.gson.reflect.TypeToken; -import seedu.address.model.deadline.BadQuestions; -import seedu.address.model.deadline.DueDate; -import seedu.address.model.flashcard.Question; - -/** - * The type Json store bad deadlines. - */ -public class JsonBadDeadlines { - - private HashMap badQuestions; - - /** - * Instantiates a new JsonBadDeadlines. - * Stores all the "Bad" flashcards into a Hashmap in json - */ - public JsonBadDeadlines(BadQuestions badQuestions) { - HashMap badQuestionList = badQuestions.getBadQuestionsList(); - - Gson gson = new Gson(); - - // convert java object to JSON format, - // and returned as JSON formatted string - String json = gson.toJson(badQuestionList); - try { - //write converted json data to a file named "CountryGSON.json" - FileWriter writer = new FileWriter("data/test.json"); - writer.write(json); - writer.close(); - - } catch (IOException e) { - e.printStackTrace(); - } - } - -// ObjectMapper mapper = new ObjectMapper(); -// -// /** -// * Convert Map to JSON and write to a file -// */ -// try { -// mapper.writeValue(new File("data/BadDeadlines.json"), badQuestionList); -// } catch (Exception e) { -// e.printStackTrace(); -// } -// } - - /** - * Load json bad deadlines. - * Load all the "Bad" flashcards into a Hashmap from a json - */ - public HashMap loadJsonBadDeadlines() throws FileNotFoundException { - - BadQuestions badQuestions = new BadQuestions(); - Gson gson = new Gson(); - BufferedReader br = new BufferedReader(new FileReader("data/test.json")); - - //convert the json string back to object - - HashMap mapObj = new Gson().fromJson( - br, new TypeToken>() {}.getType() - ); - - // HashMap map = gson.fromJson(br, badQuestions.getBadQuestionsList().getClass()); - System.out.println(mapObj); - return mapObj; - - -// ObjectMapper mapper = new ObjectMapper(); -// /** -// * Read JSON from a file into a HashMap -// */ -// try { -// HashMap map = mapper.readValue(new File( -// "data/BadDeadlines.json"), new TypeReference>() {}); -// System.out.println("data:" + map); -// -//// badQuestions = map; -// return map; -// } catch (Exception e) { -// e.printStackTrace(); -// return null; -// } - } -} diff --git a/src/main/java/seedu/address/ui/DeadlinePanel.java b/src/main/java/seedu/address/ui/DeadlinePanel.java index d8400c3bb61..6d456cf1498 100644 --- a/src/main/java/seedu/address/ui/DeadlinePanel.java +++ b/src/main/java/seedu/address/ui/DeadlinePanel.java @@ -6,8 +6,6 @@ import javafx.scene.layout.Region; import seedu.address.model.deadline.Deadline; -import java.time.LocalDate; - //@@author dalsontws /** * An UI component that displays information of a {@code FlashCard}. @@ -32,9 +30,9 @@ public DeadlinePanel(Deadline deadline, int displayedIndex) { this.deadline = deadline; id.setText(displayedIndex + ". "); task.setText(deadline.getTask().toString()); - LocalDate today = LocalDate.now(); - deadline.getDueDate(); - + //TODO: use date to colour code each deadline + //LocalDate today = LocalDate.now(); + //deadline.getDueDate(); dueDate.setText("Due Date: " + deadline.getDueDate().toString()); } From e4b07b7fc6c21b0943669574787cf25e08e114ac Mon Sep 17 00:00:00 2001 From: dalsontws Date: Wed, 30 Oct 2019 14:20:33 +0800 Subject: [PATCH 6/8] remove unneccessary loadjson class --- .../address/storage/JsonLoadBadDeadlines.java | 39 ------------------- 1 file changed, 39 deletions(-) delete mode 100644 src/main/java/seedu/address/storage/JsonLoadBadDeadlines.java diff --git a/src/main/java/seedu/address/storage/JsonLoadBadDeadlines.java b/src/main/java/seedu/address/storage/JsonLoadBadDeadlines.java deleted file mode 100644 index f2e620fbe94..00000000000 --- a/src/main/java/seedu/address/storage/JsonLoadBadDeadlines.java +++ /dev/null @@ -1,39 +0,0 @@ -//package seedu.address.storage; -// -//import java.io.BufferedReader; -//import java.io.File; -//import java.io.FileNotFoundException; -//import java.io.FileReader; -//import java.io.FileWriter; -//import java.io.IOException; -//import java.util.HashMap; -//import java.util.Map; -// -//import com.fasterxml.jackson.core.type.TypeReference; -//import com.fasterxml.jackson.databind.ObjectMapper; -//import com.google.gson.Gson; -//import com.google.gson.reflect.TypeToken; -//import seedu.address.model.deadline.DueDate; -//import seedu.address.model.flashcard.Question; -//import seedu.address.model.deadline.BadQuestions; -// -//public class JsonLoadBadDeadlines { -//// private static HashMap badQuestions; -// -// public static void main(String[] args) throws FileNotFoundException { -// BadQuestions badQuestions = new BadQuestions(); -// Gson gson = new Gson(); -// BufferedReader br = new BufferedReader(new FileReader("data/test.json")); -// -// //convert the json string back to object -// -// HashMap mapObj = new Gson().fromJson( -// br, new TypeToken>() {}.getType() -// ); -// -// HashMap map = gson.fromJson(br, badQuestions.getBadQuestionsList().getClass()); -// System.out.println(mapObj); -//// return mapObj; -// -// } -//} From fc05aa11eb59c9fe25cd4f1a4985835934819915 Mon Sep 17 00:00:00 2001 From: dalsontws Date: Wed, 30 Oct 2019 14:55:22 +0800 Subject: [PATCH 7/8] added deadline command test --- .../logic/commands/DeadlineCommandTest.java | 63 +++++++++++++++++++ 1 file changed, 63 insertions(+) create mode 100644 src/test/java/seedu/address/logic/commands/DeadlineCommandTest.java diff --git a/src/test/java/seedu/address/logic/commands/DeadlineCommandTest.java b/src/test/java/seedu/address/logic/commands/DeadlineCommandTest.java new file mode 100644 index 00000000000..41bbd0e48bf --- /dev/null +++ b/src/test/java/seedu/address/logic/commands/DeadlineCommandTest.java @@ -0,0 +1,63 @@ +package seedu.address.logic.commands; + +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static seedu.address.testutil.Assert.assertThrows; + +import org.junit.jupiter.api.Test; + +import seedu.address.model.deadline.Deadline; +import seedu.address.testutil.DeadlineBuilder; + +public class DeadlineCommandTest { + + @Test + public void constructor_nullDeadline_throwsNullPointerException() { + assertThrows(NullPointerException.class, () -> new DeadlineCommand(null)); + } + + @Test + public void equals() { + Deadline test = new DeadlineBuilder().withTask("Test").build(); + Deadline exam = new DeadlineBuilder().withTask("Exam").build(); + DeadlineCommand addTestCommand = new DeadlineCommand(test); + DeadlineCommand addExamCommand = new DeadlineCommand(exam); + + // same object -> returns true + assertTrue(addTestCommand.equals(addTestCommand)); + + // same values -> returns true + DeadlineCommand addTestCommandCopy = new DeadlineCommand(test); + assertTrue(addTestCommand.equals(addTestCommandCopy)); + + // different types -> returns false + assertFalse(addTestCommand.equals(1)); + + // null -> returns false + assertFalse(addTestCommand.equals(null)); + + // different flashCard -> returns false + assertFalse(addTestCommand.equals(addExamCommand)); + + //same question different answer + Deadline testCopy = new DeadlineBuilder(test).withDueDate("22/12/2019").build(); + addTestCommandCopy = new DeadlineCommand(testCopy); + assertFalse(addTestCommandCopy.equals(testCopy)); + } + + @Test + public void toStringTest() { + Deadline validDeadline = new DeadlineBuilder().build(); + DeadlineCommand deadlineCommand = new DeadlineCommand(validDeadline); + //same object + assertTrue(deadlineCommand.toString().equals(deadlineCommand.toString())); + + //same value + DeadlineCommand deadlineCommandCopy = new DeadlineCommand(validDeadline); + assertTrue(deadlineCommand.toString().equals(deadlineCommandCopy.toString())); + + //same question diff answer + deadlineCommandCopy = new DeadlineCommand(new DeadlineBuilder(validDeadline).withDueDate("10/10/2019").build()); + assertFalse(deadlineCommand.toString().equals(deadlineCommandCopy.toString())); + } +} From 106ae541a8b9010539034adf06503f390db8edb8 Mon Sep 17 00:00:00 2001 From: dalsontws Date: Wed, 30 Oct 2019 15:17:39 +0800 Subject: [PATCH 8/8] add javadoc for gson implementation --- .../seedu/address/logic/parser/BadCommandParser.java | 2 +- .../seedu/address/model/deadline/BadQuestions.java | 11 +++++------ 2 files changed, 6 insertions(+), 7 deletions(-) diff --git a/src/main/java/seedu/address/logic/parser/BadCommandParser.java b/src/main/java/seedu/address/logic/parser/BadCommandParser.java index 316cb175b40..0a421803414 100644 --- a/src/main/java/seedu/address/logic/parser/BadCommandParser.java +++ b/src/main/java/seedu/address/logic/parser/BadCommandParser.java @@ -1,4 +1,4 @@ -//@@author: dalsontws +//@@author dalsontws package seedu.address.logic.parser; diff --git a/src/main/java/seedu/address/model/deadline/BadQuestions.java b/src/main/java/seedu/address/model/deadline/BadQuestions.java index 1ed49ec2b6f..7ef1f4d24e3 100644 --- a/src/main/java/seedu/address/model/deadline/BadQuestions.java +++ b/src/main/java/seedu/address/model/deadline/BadQuestions.java @@ -1,3 +1,5 @@ +//@@author dalsontws + package seedu.address.model.deadline; import java.io.FileNotFoundException; @@ -58,21 +60,18 @@ public void setBadQuestionsAsDeadline(Model model) { } /** - * Save as json. + * Using Google's Gson library, save HashMap as a JSON Object and store it + * in BadQuestions.json. This can later be used to fetch the 'bad' questions + * to test in a future date. */ public void saveAsJson(BadQuestions badQuestions) { Gson gson = new Gson(); - - // convert java object to JSON format, - // and returned as JSON formatted string String json = gson.toJson(badQuestions.getBadQuestionsList()); try { - //write converted json data to a file //TODO: fix load and save json, json save replace file instead of appending FileWriter writer = new FileWriter("data/BadDeadlines.json", true); writer.write(json); writer.close(); - } catch (IOException e) { e.printStackTrace(); }