From fd248269f4a29665b59037d5146252652b16010d Mon Sep 17 00:00:00 2001 From: Ng Shu Lin Jane Date: Mon, 28 Sep 2020 16:23:29 +0800 Subject: [PATCH 01/43] Set theme jekyll-theme-slate --- docs/_config.yml | 1 + 1 file changed, 1 insertion(+) create mode 100644 docs/_config.yml diff --git a/docs/_config.yml b/docs/_config.yml new file mode 100644 index 0000000000..c741881743 --- /dev/null +++ b/docs/_config.yml @@ -0,0 +1 @@ +theme: jekyll-theme-slate \ No newline at end of file From 5d4b09d3a49ebd9a2fd2499da2c721b524019343 Mon Sep 17 00:00:00 2001 From: neojiaern Date: Fri, 2 Oct 2020 11:16:29 +0800 Subject: [PATCH 02/43] Modify name in about us --- docs/team/johndoe.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/team/johndoe.md b/docs/team/johndoe.md index ab75b391b8..b66440e51a 100644 --- a/docs/team/johndoe.md +++ b/docs/team/johndoe.md @@ -1,4 +1,4 @@ -# John Doe - Project Portfolio Page +# Jia Ern - Project Portfolio Page ## Overview From a1bb768807bf5f16e063f4b26b9b39e3470e1ce0 Mon Sep 17 00:00:00 2001 From: Jane Date: Fri, 2 Oct 2020 11:16:29 +0800 Subject: [PATCH 03/43] Modify name --- docs/AboutUs.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/AboutUs.md b/docs/AboutUs.md index 0f072953ea..b8072e8ed0 100644 --- a/docs/AboutUs.md +++ b/docs/AboutUs.md @@ -2,7 +2,7 @@ Display | Name | Github Profile | Portfolio --------|:----:|:--------------:|:---------: -![](https://via.placeholder.com/100.png?text=Photo) | John Doe | [Github](https://github.com/) | [Portfolio](docs/team/johndoe.md) +![](https://via.placeholder.com/100.png?text=Photo) | Jane Ng | [Github](https://github.com/) | [Portfolio](docs/team/johndoe.md) ![](https://via.placeholder.com/100.png?text=Photo) | Don Joe | [Github](https://github.com/) | [Portfolio](docs/team/johndoe.md) ![](https://via.placeholder.com/100.png?text=Photo) | Ron John | [Github](https://github.com/) | [Portfolio](docs/team/johndoe.md) ![](https://via.placeholder.com/100.png?text=Photo) | John Roe | [Github](https://github.com/) | [Portfolio](docs/team/johndoe.md) From b304d14ad7ce51bf5f297af159fa7aa97a8caddb Mon Sep 17 00:00:00 2001 From: Darticune Date: Fri, 2 Oct 2020 11:16:55 +0800 Subject: [PATCH 04/43] Updated Yan An's personal information in the doc/AboutUs.md --- docs/AboutUs.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/AboutUs.md b/docs/AboutUs.md index 0f072953ea..f7f42794c3 100644 --- a/docs/AboutUs.md +++ b/docs/AboutUs.md @@ -6,4 +6,4 @@ Display | Name | Github Profile | Portfolio ![](https://via.placeholder.com/100.png?text=Photo) | Don Joe | [Github](https://github.com/) | [Portfolio](docs/team/johndoe.md) ![](https://via.placeholder.com/100.png?text=Photo) | Ron John | [Github](https://github.com/) | [Portfolio](docs/team/johndoe.md) ![](https://via.placeholder.com/100.png?text=Photo) | John Roe | [Github](https://github.com/) | [Portfolio](docs/team/johndoe.md) -![](https://via.placeholder.com/100.png?text=Photo) | Don Roe | [Github](https://github.com/) | [Portfolio](docs/team/johndoe.md) +![](https://via.placeholder.com/100.png?text=Photo) | Tan Yan An | [Github](https://github.com/Darticune) | [Portfolio](docs/team/yanAn.md) From fdf0ac08dfc02b004e78defda21337cb8e5706a9 Mon Sep 17 00:00:00 2001 From: gua-guargia Date: Fri, 2 Oct 2020 11:17:42 +0800 Subject: [PATCH 05/43] Edit AboutUs.md --- docs/AboutUs.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/AboutUs.md b/docs/AboutUs.md index 0f072953ea..04485167c5 100644 --- a/docs/AboutUs.md +++ b/docs/AboutUs.md @@ -6,4 +6,4 @@ Display | Name | Github Profile | Portfolio ![](https://via.placeholder.com/100.png?text=Photo) | Don Joe | [Github](https://github.com/) | [Portfolio](docs/team/johndoe.md) ![](https://via.placeholder.com/100.png?text=Photo) | Ron John | [Github](https://github.com/) | [Portfolio](docs/team/johndoe.md) ![](https://via.placeholder.com/100.png?text=Photo) | John Roe | [Github](https://github.com/) | [Portfolio](docs/team/johndoe.md) -![](https://via.placeholder.com/100.png?text=Photo) | Don Roe | [Github](https://github.com/) | [Portfolio](docs/team/johndoe.md) +![](https://via.placeholder.com/100.png?text=Photo) | Jiayi Zhang | [Github](https://github.com/) | [Portfolio](docs/team/johndoe.md) From 2b18f2b61ed55eb93497bef10850fb566e1b0c1d Mon Sep 17 00:00:00 2001 From: Zhu-Zeyu <20001115zhuzeyu@gmail.com> Date: Fri, 2 Oct 2020 11:19:23 +0800 Subject: [PATCH 06/43] About Us --- docs/AboutUs.md | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/docs/AboutUs.md b/docs/AboutUs.md index 0f072953ea..ab7493ba1a 100644 --- a/docs/AboutUs.md +++ b/docs/AboutUs.md @@ -2,8 +2,4 @@ Display | Name | Github Profile | Portfolio --------|:----:|:--------------:|:---------: -![](https://via.placeholder.com/100.png?text=Photo) | John Doe | [Github](https://github.com/) | [Portfolio](docs/team/johndoe.md) -![](https://via.placeholder.com/100.png?text=Photo) | Don Joe | [Github](https://github.com/) | [Portfolio](docs/team/johndoe.md) -![](https://via.placeholder.com/100.png?text=Photo) | Ron John | [Github](https://github.com/) | [Portfolio](docs/team/johndoe.md) -![](https://via.placeholder.com/100.png?text=Photo) | John Roe | [Github](https://github.com/) | [Portfolio](docs/team/johndoe.md) -![](https://via.placeholder.com/100.png?text=Photo) | Don Roe | [Github](https://github.com/) | [Portfolio](docs/team/johndoe.md) +![](https://via.placeholder.com/100.png?text=Photo) | Zhu Zeyu | [Github](https://github.com/) | [Portfolio](docs/team/johndoe.md) \ No newline at end of file From 5533e78d2ca21441a983b621fd927667184e27df Mon Sep 17 00:00:00 2001 From: neojiaern Date: Fri, 2 Oct 2020 11:22:16 +0800 Subject: [PATCH 07/43] Modify name in aboutus.md --- docs/AboutUs.md | 2 +- docs/team/johndoe.md | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/docs/AboutUs.md b/docs/AboutUs.md index 0f072953ea..3889d35330 100644 --- a/docs/AboutUs.md +++ b/docs/AboutUs.md @@ -2,7 +2,7 @@ Display | Name | Github Profile | Portfolio --------|:----:|:--------------:|:---------: -![](https://via.placeholder.com/100.png?text=Photo) | John Doe | [Github](https://github.com/) | [Portfolio](docs/team/johndoe.md) +![](https://via.placeholder.com/100.png?text=Photo) | Jia Ern | [Github](https://github.com/) | [Portfolio](docs/team/johndoe.md) ![](https://via.placeholder.com/100.png?text=Photo) | Don Joe | [Github](https://github.com/) | [Portfolio](docs/team/johndoe.md) ![](https://via.placeholder.com/100.png?text=Photo) | Ron John | [Github](https://github.com/) | [Portfolio](docs/team/johndoe.md) ![](https://via.placeholder.com/100.png?text=Photo) | John Roe | [Github](https://github.com/) | [Portfolio](docs/team/johndoe.md) diff --git a/docs/team/johndoe.md b/docs/team/johndoe.md index b66440e51a..ab75b391b8 100644 --- a/docs/team/johndoe.md +++ b/docs/team/johndoe.md @@ -1,4 +1,4 @@ -# Jia Ern - Project Portfolio Page +# John Doe - Project Portfolio Page ## Overview From cd8cccaa37e63e4560392a6b625e4afcb8bb6d72 Mon Sep 17 00:00:00 2001 From: gua-guargia Date: Mon, 5 Oct 2020 16:35:58 +0800 Subject: [PATCH 08/43] Add tp trial structure --- src/main/java/seedu/duke/Duke.java | 32 ++++++++++ src/main/java/seedu/duke/command/Command.java | 19 ++++++ .../seedu/duke/command/addCardCommand.java | 16 +++++ .../seedu/duke/command/addChapterCommand.java | 20 +++++++ .../seedu/duke/command/addModuleCommand.java | 18 ++++++ .../seedu/duke/command/backModuleCommand.java | 17 ++++++ .../seedu/duke/command/goModuleCommand.java | 18 ++++++ src/main/java/seedu/duke/level/Admin.java | 26 ++++++++ src/main/java/seedu/duke/level/Card.java | 24 ++++++++ src/main/java/seedu/duke/level/Chapter.java | 35 +++++++++++ src/main/java/seedu/duke/level/Module.java | 34 +++++++++++ src/main/java/seedu/duke/tool/Access.java | 60 +++++++++++++++++++ src/main/java/seedu/duke/tool/Parser.java | 32 ++++++++++ src/main/java/seedu/duke/tool/Ui.java | 33 ++++++++++ 14 files changed, 384 insertions(+) create mode 100644 src/main/java/seedu/duke/command/Command.java create mode 100644 src/main/java/seedu/duke/command/addCardCommand.java create mode 100644 src/main/java/seedu/duke/command/addChapterCommand.java create mode 100644 src/main/java/seedu/duke/command/addModuleCommand.java create mode 100644 src/main/java/seedu/duke/command/backModuleCommand.java create mode 100644 src/main/java/seedu/duke/command/goModuleCommand.java create mode 100644 src/main/java/seedu/duke/level/Admin.java create mode 100644 src/main/java/seedu/duke/level/Card.java create mode 100644 src/main/java/seedu/duke/level/Chapter.java create mode 100644 src/main/java/seedu/duke/level/Module.java create mode 100644 src/main/java/seedu/duke/tool/Access.java create mode 100644 src/main/java/seedu/duke/tool/Parser.java create mode 100644 src/main/java/seedu/duke/tool/Ui.java diff --git a/src/main/java/seedu/duke/Duke.java b/src/main/java/seedu/duke/Duke.java index 5c74e68d59..8c2883ef40 100644 --- a/src/main/java/seedu/duke/Duke.java +++ b/src/main/java/seedu/duke/Duke.java @@ -1,11 +1,41 @@ package seedu.duke; +import seedu.duke.command.Command; +import seedu.duke.level.Admin; +import seedu.duke.tool.Access; +import seedu.duke.tool.Parser; +import seedu.duke.tool.Ui; + import java.util.Scanner; public class Duke { /** * Main entry-point for the java.duke.Duke application. */ + private Ui ui; + private Admin admin; + private Access access; + + public Duke() { + ui = new Ui(); + admin = new Admin(); + access = new Access(); + } + + public void run() { + boolean isExit = false; + while (!isExit) { + String fullCommand = ui.readCommand(); + ui.showLine(); // show the divider line ("_______") + Command c = Parser.parse(fullCommand); + //System.out.println(fullCommand); + c.execute(access, ui, admin); + isExit = c.isExit(); + ui.showLine(); + System.out.println(access.getLevel()); + } + } + public static void main(String[] args) { String logo = " ____ _ \n" + "| _ \\ _ _| | _____ \n" @@ -17,5 +47,7 @@ public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.println("Hello " + in.nextLine()); + + new Duke().run(); } } diff --git a/src/main/java/seedu/duke/command/Command.java b/src/main/java/seedu/duke/command/Command.java new file mode 100644 index 0000000000..72b99ce989 --- /dev/null +++ b/src/main/java/seedu/duke/command/Command.java @@ -0,0 +1,19 @@ +package seedu.duke.command; + +import seedu.duke.tool.Access; +import seedu.duke.level.Admin; +import seedu.duke.tool.Ui; + +public abstract class Command { + String fullCommand; + + public Command(String fullCommand) { + this.fullCommand = fullCommand; + } + + public abstract void execute(Access access, Ui ui, Admin admin); + + public boolean isExit() { + return false; + }; +} diff --git a/src/main/java/seedu/duke/command/addCardCommand.java b/src/main/java/seedu/duke/command/addCardCommand.java new file mode 100644 index 0000000000..862d8026ef --- /dev/null +++ b/src/main/java/seedu/duke/command/addCardCommand.java @@ -0,0 +1,16 @@ +package seedu.duke.command; + +import seedu.duke.tool.Access; +import seedu.duke.level.Admin; +import seedu.duke.tool.Ui; + +public class addCardCommand extends Command { + public addCardCommand(String fullCommand) { + super(fullCommand); + } + + @Override + public void execute(Access access, Ui ui, Admin admin) { + + } +} diff --git a/src/main/java/seedu/duke/command/addChapterCommand.java b/src/main/java/seedu/duke/command/addChapterCommand.java new file mode 100644 index 0000000000..57996da738 --- /dev/null +++ b/src/main/java/seedu/duke/command/addChapterCommand.java @@ -0,0 +1,20 @@ +package seedu.duke.command; + +import seedu.duke.level.Module; +import seedu.duke.tool.Access; +import seedu.duke.level.Admin; +import seedu.duke.tool.Ui; + +public class addChapterCommand extends Command { + public addChapterCommand(String fullCommand) { + super(fullCommand); + } + + @Override + public void execute(Access access, Ui ui, Admin admin) { + String filter = fullCommand.replace("addChapter ", ""); + if(access.getModuleLevel() != "") { + admin.addChapter(filter, access.getModuleLevel()); + } + } +} diff --git a/src/main/java/seedu/duke/command/addModuleCommand.java b/src/main/java/seedu/duke/command/addModuleCommand.java new file mode 100644 index 0000000000..9ca46a7d01 --- /dev/null +++ b/src/main/java/seedu/duke/command/addModuleCommand.java @@ -0,0 +1,18 @@ +package seedu.duke.command; + +import seedu.duke.tool.Access; +import seedu.duke.level.Admin; +import seedu.duke.level.Module; +import seedu.duke.tool.Ui; + +public class addModuleCommand extends Command { + public addModuleCommand(String fullCommand) { + super(fullCommand); + } + + @Override + public void execute(Access access, Ui ui, Admin admin) { + String filter = fullCommand.replace("addModule ", ""); + admin.addModule(new Module(filter)); + } +} diff --git a/src/main/java/seedu/duke/command/backModuleCommand.java b/src/main/java/seedu/duke/command/backModuleCommand.java new file mode 100644 index 0000000000..fa9c6e89bd --- /dev/null +++ b/src/main/java/seedu/duke/command/backModuleCommand.java @@ -0,0 +1,17 @@ +package seedu.duke.command; + +import seedu.duke.command.Command; +import seedu.duke.level.Admin; +import seedu.duke.tool.Access; +import seedu.duke.tool.Ui; + +public class backModuleCommand extends Command { + public backModuleCommand(String fullCommand) { + super(fullCommand); + } + + @Override + public void execute(Access access, Ui ui, Admin admin) { + access.setModuleLevel(""); + } +} diff --git a/src/main/java/seedu/duke/command/goModuleCommand.java b/src/main/java/seedu/duke/command/goModuleCommand.java new file mode 100644 index 0000000000..731fffa23c --- /dev/null +++ b/src/main/java/seedu/duke/command/goModuleCommand.java @@ -0,0 +1,18 @@ +package seedu.duke.command; + +import seedu.duke.command.Command; +import seedu.duke.level.Admin; +import seedu.duke.tool.Access; +import seedu.duke.tool.Ui; + +public class goModuleCommand extends Command { + public goModuleCommand(String fullCommand) { + super(fullCommand); + } + + @Override + public void execute(Access access, Ui ui, Admin admin) { + String filter = fullCommand.replace("goModule ", ""); + access.setModuleLevel(filter); + } +} diff --git a/src/main/java/seedu/duke/level/Admin.java b/src/main/java/seedu/duke/level/Admin.java new file mode 100644 index 0000000000..3ebd939fce --- /dev/null +++ b/src/main/java/seedu/duke/level/Admin.java @@ -0,0 +1,26 @@ +package seedu.duke.level; + +import seedu.duke.level.Module; + +import java.util.ArrayList; + +public class Admin { + protected ArrayList modules; + protected int moduleAmount = 0; + + public Admin() { + modules = new ArrayList(); + moduleAmount = 0; + } + + public void addModule(Module module){ + modules.add(module); + // modules.get(moduleAmount).doneAddModule(); + moduleAmount++; + } + + public void addChapter(String filter, String moduleLevel) { + int index = modules.indexOf(moduleLevel); + modules.get(index).add(new Chapter(filter)); + } +} diff --git a/src/main/java/seedu/duke/level/Card.java b/src/main/java/seedu/duke/level/Card.java new file mode 100644 index 0000000000..21b2fa4a7f --- /dev/null +++ b/src/main/java/seedu/duke/level/Card.java @@ -0,0 +1,24 @@ +package seedu.duke.level; + +public class Card { + protected String question; + protected String answer; + protected int totalCard = 0; + + public Card(String question, String answer) { + this.question = question; + this.answer = answer; + totalCard++; + } + + public String getQuestion() { + return question; + } + + public String getAnswer() { + return answer; + } + + public void doneAddCard() { + } +} diff --git a/src/main/java/seedu/duke/level/Chapter.java b/src/main/java/seedu/duke/level/Chapter.java new file mode 100644 index 0000000000..98da6012ac --- /dev/null +++ b/src/main/java/seedu/duke/level/Chapter.java @@ -0,0 +1,35 @@ +package seedu.duke.level; + +import java.util.ArrayList; + +public class Chapter { + protected ArrayList cards; + protected int cardAmount; + protected String chapterName; + protected int totalChapter = 0; + + public Chapter(String chapterName) { + this.chapterName = chapterName; + cards = new ArrayList(); + cardAmount = 0; + totalChapter++; + } + + public void add(Card card){ + cards.add(card); + cards.get(cardAmount).doneAddCard(); + cardAmount++; + } + + public void doneAddChapter() { + totalChapter++; + System.out.println(" Got it. I've added this task:"); + System.out.println(" " + getChapter()); + System.out.println(" Now you have " + totalChapter +" modules in the list."); + } + + private String getChapter() { + return chapterName; + } + +} diff --git a/src/main/java/seedu/duke/level/Module.java b/src/main/java/seedu/duke/level/Module.java new file mode 100644 index 0000000000..4cfb34b5ff --- /dev/null +++ b/src/main/java/seedu/duke/level/Module.java @@ -0,0 +1,34 @@ +package seedu.duke.level; + +import java.util.ArrayList; + +public class Module { + protected ArrayList chapters; + protected int chapterAmount; + protected String moduleName; + protected int totalModule = 0; + + public Module(String moduleName) { + this.moduleName = moduleName; + chapters = new ArrayList(); + chapterAmount = 0; + totalModule++; + } + + public void add(Chapter chapter){ + chapters.add(chapter); + chapters.get(chapterAmount).doneAddChapter(); + chapterAmount++; + } + + public void doneAddModule() { + totalModule++; + System.out.println(" Got it. I've added this task:"); + System.out.println(" " + getModule()); + System.out.println(" Now you have " + totalModule +" modules in the list."); + } + + private String getModule() { + return moduleName; + } +} diff --git a/src/main/java/seedu/duke/tool/Access.java b/src/main/java/seedu/duke/tool/Access.java new file mode 100644 index 0000000000..b79771b7b3 --- /dev/null +++ b/src/main/java/seedu/duke/tool/Access.java @@ -0,0 +1,60 @@ +package seedu.duke.tool; + +public class Access { + protected String level; + protected String adminLevel; + protected String moduleLevel; + protected String chapterLevel; + protected String cardLevel; + + public Access(String level) { + this.level = level; + } + + public Access() { + this.level = "admin"; + this.adminLevel = "admin"; + this.cardLevel = ""; + this.moduleLevel = ""; + this.chapterLevel = ""; + } + + public void incrementLevel(String newLevel) { + this.level = level + "/" + newLevel; + } + + public void decreaseLevel(String currentLevel) { + level.replace("/" + currentLevel, ""); + } + + public String getModuleLevel() { + return moduleLevel; + } + + public String getLevel() { + return level; + } + + public String getAdminLevel() { + return adminLevel; + } + + public String getChapterLevel() { + return chapterLevel; + } + + public String getCardLevel() { + return cardLevel; + } + + public void setModuleLevel(String moduleLevel) { + if(moduleLevel != "") { + this.moduleLevel = moduleLevel; + this.level = level + "/" + moduleLevel; + return; + } + String replacement = "/" + this.moduleLevel; + this.level = level.replace(replacement, ""); + this.moduleLevel = moduleLevel; + } +} diff --git a/src/main/java/seedu/duke/tool/Parser.java b/src/main/java/seedu/duke/tool/Parser.java new file mode 100644 index 0000000000..5d84c9cfeb --- /dev/null +++ b/src/main/java/seedu/duke/tool/Parser.java @@ -0,0 +1,32 @@ +package seedu.duke.tool; + +import seedu.duke.command.*; + +public class Parser { + + /** + * Select the corresponding Command class through interpreting the command entered by user. + * Returns the correct Command class. + * + * @param fullCommand Command entered by user. + * @return Construct new Command that is corresponding to the command entered by user. + */ + public static Command parse(String fullCommand) { + if(fullCommand.toLowerCase().contains("addmodule")) { + return new addModuleCommand(fullCommand); + } + else if(fullCommand.toLowerCase().contains("addchapter")) { + return new addChapterCommand(fullCommand); + } + else if(fullCommand.toLowerCase().contains("addcard")) { + return new addCardCommand(fullCommand); + } + else if(fullCommand.toLowerCase().contains("gomodule")) { + return new goModuleCommand(fullCommand); + } + else if(fullCommand.toLowerCase().contains("backmodule")) { + return new backModuleCommand(fullCommand); + } + return null; + } +} \ No newline at end of file diff --git a/src/main/java/seedu/duke/tool/Ui.java b/src/main/java/seedu/duke/tool/Ui.java new file mode 100644 index 0000000000..0441f56c46 --- /dev/null +++ b/src/main/java/seedu/duke/tool/Ui.java @@ -0,0 +1,33 @@ +package seedu.duke.tool; + +import java.util.Scanner; + +public class Ui { + public static final String lineSplit = " ____________________________________________________________"; + public static final String logo = " ____ _ \n" + + "| _ \\ _ _| | _____ \n" + + "| | | | | | | |/ / _ \\\n" + + "| |_| | |_| | < __/\n" + + "|____/ \\__,_|_|\\_\\___|\n"; + + /** + * Prints line for splitting contents. + */ + public void showLine() { + System.out.println(lineSplit); + } + + /** + * Read the command entered by the user. + * Returns the command entered by the user. + * + * @return String of command entered by the user. + */ + public String readCommand() { + String line; + Scanner in = new Scanner(System.in); + line = in.nextLine(); + return line; + } + +} From 7c0361f60ae08b8a0e49b117860e64d65d438cf9 Mon Sep 17 00:00:00 2001 From: Jane Date: Tue, 6 Oct 2020 10:35:12 +0800 Subject: [PATCH 09/43] Add the ability to add and list cards Add the ability to exit the program --- src/main/java/Kaji.java | 34 ++++++++ src/main/java/commands/AddCommand.java | 31 +++++++ src/main/java/commands/Command.java | 10 +++ src/main/java/commands/ExitCommand.java | 18 ++++ src/main/java/commands/ListCommand.java | 23 +++++ .../java/exception/InvalidInputException.java | 4 + src/main/java/manager/card/Card.java | 15 ++++ src/main/java/manager/chapter/CardList.java | 29 +++++++ src/main/java/parser/Parser.java | 87 +++++++++++++++++++ src/main/java/ui/Ui.java | 59 +++++++++++++ 10 files changed, 310 insertions(+) create mode 100644 src/main/java/Kaji.java create mode 100644 src/main/java/commands/AddCommand.java create mode 100644 src/main/java/commands/Command.java create mode 100644 src/main/java/commands/ExitCommand.java create mode 100644 src/main/java/commands/ListCommand.java create mode 100644 src/main/java/exception/InvalidInputException.java create mode 100644 src/main/java/manager/card/Card.java create mode 100644 src/main/java/manager/chapter/CardList.java create mode 100644 src/main/java/parser/Parser.java create mode 100644 src/main/java/ui/Ui.java diff --git a/src/main/java/Kaji.java b/src/main/java/Kaji.java new file mode 100644 index 0000000000..e357a2ce0b --- /dev/null +++ b/src/main/java/Kaji.java @@ -0,0 +1,34 @@ +import commands.Command; +import exception.InvalidInputException; +import manager.chapter.CardList; +import parser.Parser; +import ui.Ui; + +public class Kaji { + private CardList cards; + private Ui ui; + + public Kaji() { + ui = new Ui(); + cards = new CardList(); + } + + public void run() { + ui.showWelcome(); + boolean isExit = false; + while (!isExit) { + try { + String fullCommand = ui.readCommand(); + Command c = Parser.parse(fullCommand); + c.execute(cards, ui); + isExit = c.isExit(); + } catch (InvalidInputException e) { + System.out.println("Invalid input given"); + } + } + } + + public static void main(String[] args) { + new Kaji().run(); + } +} diff --git a/src/main/java/commands/AddCommand.java b/src/main/java/commands/AddCommand.java new file mode 100644 index 0000000000..f6dd274d1e --- /dev/null +++ b/src/main/java/commands/AddCommand.java @@ -0,0 +1,31 @@ +package commands; + +import manager.card.Card; +import manager.chapter.CardList; +import ui.Ui; + +public class AddCommand extends Command { + public static final String COMMAND_WORD = "add"; + + public static final String QUESTION_ANSWER_PREFIX = " \\| "; + public static final String QUESTION_PREFIX = "q:"; + public static final String ANSWER_PREFIX = "a:"; + + private final Card card; + + public AddCommand(String question, String answer) { + this.card = new Card(question, answer); + } + + @Override + public void execute(CardList cards, Ui ui) { + cards.addCard(card); + int cardCount = cards.getCardCount(); + ui.showCardAdded(cards.getCard(cardCount - 1), cardCount); + } + + @Override + public boolean isExit() { + return false; + } +} diff --git a/src/main/java/commands/Command.java b/src/main/java/commands/Command.java new file mode 100644 index 0000000000..61529438a4 --- /dev/null +++ b/src/main/java/commands/Command.java @@ -0,0 +1,10 @@ +package commands; + +import manager.chapter.CardList; +import ui.Ui; + +public abstract class Command { + public abstract void execute(CardList cards, Ui ui); + + public abstract boolean isExit(); +} diff --git a/src/main/java/commands/ExitCommand.java b/src/main/java/commands/ExitCommand.java new file mode 100644 index 0000000000..04fa8c425f --- /dev/null +++ b/src/main/java/commands/ExitCommand.java @@ -0,0 +1,18 @@ +package commands; + +import manager.chapter.CardList; +import ui.Ui; + +public class ExitCommand extends Command { + public static final String COMMAND_WORD = "exit"; + + @Override + public void execute(CardList cards, Ui ui) { + ui.showExit(); + } + + @Override + public boolean isExit() { + return true; + } +} diff --git a/src/main/java/commands/ListCommand.java b/src/main/java/commands/ListCommand.java new file mode 100644 index 0000000000..ec05991dd6 --- /dev/null +++ b/src/main/java/commands/ListCommand.java @@ -0,0 +1,23 @@ +package commands; + +import manager.card.Card; +import manager.chapter.CardList; +import ui.Ui; + +import java.util.ArrayList; + +public class ListCommand extends Command { + public static final String COMMAND_WORD = "list"; + + @Override + public void execute(CardList cards, Ui ui) { + ArrayList allCards = cards.getAllCards(); + int cardCount = cards.getCardCount(); + ui.showCardList(allCards, cardCount); + } + + @Override + public boolean isExit() { + return false; + } +} diff --git a/src/main/java/exception/InvalidInputException.java b/src/main/java/exception/InvalidInputException.java new file mode 100644 index 0000000000..1ef1ba5156 --- /dev/null +++ b/src/main/java/exception/InvalidInputException.java @@ -0,0 +1,4 @@ +package exception; + +public class InvalidInputException extends Exception { +} diff --git a/src/main/java/manager/card/Card.java b/src/main/java/manager/card/Card.java new file mode 100644 index 0000000000..7e53dadc93 --- /dev/null +++ b/src/main/java/manager/card/Card.java @@ -0,0 +1,15 @@ +package manager.card; + +public class Card { + private String question; + private String answer; + + public Card(String question, String answer) { + this.question = question; + this.answer = answer; + } + + public String toString() { + return "[Q] " + question + " | [A] " + answer; + } +} diff --git a/src/main/java/manager/chapter/CardList.java b/src/main/java/manager/chapter/CardList.java new file mode 100644 index 0000000000..4d37eaa535 --- /dev/null +++ b/src/main/java/manager/chapter/CardList.java @@ -0,0 +1,29 @@ +package manager.chapter; + +import manager.card.Card; + +import java.util.ArrayList; + +public class CardList { + private final ArrayList cards; + + public CardList() { + cards = new ArrayList<>(); + } + + public void addCard(Card card) { + cards.add(card); + } + + public int getCardCount() { + return cards.size(); + } + + public Card getCard(int cardIndex) { + return cards.get(cardIndex); + } + + public ArrayList getAllCards() { + return cards; + } +} diff --git a/src/main/java/parser/Parser.java b/src/main/java/parser/Parser.java new file mode 100644 index 0000000000..224bd5c22e --- /dev/null +++ b/src/main/java/parser/Parser.java @@ -0,0 +1,87 @@ +package parser; + +import commands.AddCommand; +import commands.Command; +import commands.ExitCommand; +import commands.ListCommand; +import exception.InvalidInputException; + + + +public class Parser { + public static Command parse(String fullCommand) throws InvalidInputException { + String[] commandTypeAndArgs = splitCommandTypeAndArgs(fullCommand); + String commandType = commandTypeAndArgs[0].trim().toLowerCase(); + String commandArgs = commandTypeAndArgs[1].trim(); + + switch (commandType) { + case ListCommand.COMMAND_WORD: + return prepareList(commandArgs); + case AddCommand.COMMAND_WORD: + return prepareAdd(commandArgs); + case ExitCommand.COMMAND_WORD: + return prepareExit(commandArgs); + default: + throw new InvalidInputException(); + } + } + + private static String[] splitCommandTypeAndArgs(String userCommand) { + String[] commandTypeAndParams = userCommand.trim().split(" ", 2); + if (commandTypeAndParams.length != 2) { + commandTypeAndParams = new String[]{commandTypeAndParams[0], ""}; + } + return commandTypeAndParams; + } + + private static Command prepareList(String commandArgs) throws InvalidInputException { + if (!commandArgs.isEmpty()) { + throw new InvalidInputException(); + } + return new ListCommand(); + } + + private static Command prepareAdd(String commandArgs) throws InvalidInputException { + try { + String[] args = commandArgs.split(AddCommand.QUESTION_ANSWER_PREFIX, 2); + String question = parseQuestion(args[0]); + String answer = parseAnswer(args[1]); + return new AddCommand(question, answer); + } catch (IndexOutOfBoundsException | InvalidInputException e) { + throw new InvalidInputException(); + } + } + + private static String parseQuestion(String arg) throws InvalidInputException { + if (!(arg.trim().toLowerCase().startsWith(AddCommand.QUESTION_PREFIX))) { + throw new InvalidInputException(); + } + + String question = arg.substring(2).trim(); + if (question.isEmpty()) { + throw new InvalidInputException(); + } + + return question; + } + + private static String parseAnswer(String arg) throws InvalidInputException { + if (!(arg.trim().toLowerCase().startsWith(AddCommand.ANSWER_PREFIX))) { + throw new InvalidInputException(); + } + + String answer = arg.substring(2).trim(); + if (answer.isEmpty()) { + throw new InvalidInputException(); + } + + return answer; + } + + private static Command prepareExit(String commandArgs) throws InvalidInputException { + if (!commandArgs.isEmpty()) { + throw new InvalidInputException(); + } + return new ExitCommand(); + } +} diff --git a/src/main/java/ui/Ui.java b/src/main/java/ui/Ui.java new file mode 100644 index 0000000000..c893ce84c4 --- /dev/null +++ b/src/main/java/ui/Ui.java @@ -0,0 +1,59 @@ +package ui; + +import manager.card.Card; + +import java.io.InputStream; +import java.io.PrintStream; +import java.util.ArrayList; +import java.util.Scanner; + +public class Ui { + private final Scanner in; + private final PrintStream out; + + public Ui() { + this(System.in, System.out); + } + + public Ui(InputStream in, PrintStream out) { + this.in = new Scanner(in); + this.out = out; + } + + public String readCommand() { + String userCommand = in.nextLine(); + while (userCommand.trim().isEmpty()) { + userCommand = in.nextLine(); + } + return userCommand; + } + + public void showWelcome() { + out.println("Welcome to Kaji!"); + } + + public void showCardAdded(Card card, int cardCount) { + out.println("Got it. I've added this card:"); + out.println(card); + if (cardCount == 1) { + out.println("Now you have " + cardCount + " card in the list."); + return; + } + out.println("Now you have " + cardCount + " cards in the list."); + } + + public void showCardList(ArrayList cards, int cardCount) { + if (cardCount == 0) { + out.println("There are no cards in your list."); + return; + } + out.println("Here are the tasks in your list:"); + for (Card c : cards) { + out.println((cards.indexOf(c) + 1) + "." + c); + } + } + + public void showExit() { + out.println("Exiting the program..."); + } +} From f98f5fa99f1fd81f28a47a7ac5d3ee74f1f31125 Mon Sep 17 00:00:00 2001 From: Jane Date: Tue, 6 Oct 2020 12:25:25 +0800 Subject: [PATCH 10/43] Minor change to output message --- src/main/java/ui/Ui.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/ui/Ui.java b/src/main/java/ui/Ui.java index c893ce84c4..8b91803d32 100644 --- a/src/main/java/ui/Ui.java +++ b/src/main/java/ui/Ui.java @@ -47,7 +47,7 @@ public void showCardList(ArrayList cards, int cardCount) { out.println("There are no cards in your list."); return; } - out.println("Here are the tasks in your list:"); + out.println("Here are the cards in your list:"); for (Card c : cards) { out.println((cards.indexOf(c) + 1) + "." + c); } From 0c8dda333591618f2e5fbd101679db4e04195311 Mon Sep 17 00:00:00 2001 From: neojiaern Date: Tue, 6 Oct 2020 16:15:12 +0800 Subject: [PATCH 11/43] Add revision feature --- src/main/java/commands/ReviseCommand.java | 41 ++++++++++++++++++++++ src/main/java/manager/card/Card.java | 17 +++++++++ src/main/java/manager/chapter/Chapter.java | 14 ++++++++ src/main/java/parser/Parser.java | 15 +++++++- src/main/java/ui/Ui.java | 36 +++++++++++++++++++ 5 files changed, 122 insertions(+), 1 deletion(-) create mode 100644 src/main/java/commands/ReviseCommand.java create mode 100644 src/main/java/manager/chapter/Chapter.java diff --git a/src/main/java/commands/ReviseCommand.java b/src/main/java/commands/ReviseCommand.java new file mode 100644 index 0000000000..4c0d2127ca --- /dev/null +++ b/src/main/java/commands/ReviseCommand.java @@ -0,0 +1,41 @@ +package commands; + +import manager.card.Card; +import manager.chapter.CardList; +import manager.chapter.Chapter; +import ui.Ui; + +import java.util.ArrayList; + +/** + * Starts revision for a particular chapter. + */ +public class ReviseCommand extends Command { + public static final String COMMAND_WORD = "revise"; + + public static final String MESSAGE_USAGE = COMMAND_WORD + ": Starts revision based on a particular chapter. \n" + + "Parameters: CHAPTER_NAME\n" + "Example: " + COMMAND_WORD + " Polymorphism\n"; + + public static final String MESSAGE_SUCCESS = "You have completed revision for %1$s."; + public static final String MESSAGE_NO_CARDS_IN_CHAPTER = "You have currently have no cards in %1$s."; + public static final String MESSAGE_NO_CARDS_DUE = "You have no cards due for revision in %1$s today."; + public static final String MESSAGE_SHOW_ANSWER_PROMPT = "\n[enter s to show answer]"; + + private final Chapter toRevise; + + public ReviseCommand(Chapter toRevise) { + this.toRevise = toRevise; + } + + @Override + public void execute(CardList cards, Ui ui) { + ArrayList allCards = cards.getAllCards(); + int cardCount = cards.getCardCount(); + ui.showRevisionContent(allCards, cardCount, toRevise); + } + + @Override + public boolean isExit() { + return false; + } +} diff --git a/src/main/java/manager/card/Card.java b/src/main/java/manager/card/Card.java index 7e53dadc93..f888f60959 100644 --- a/src/main/java/manager/card/Card.java +++ b/src/main/java/manager/card/Card.java @@ -1,12 +1,29 @@ package manager.card; +import java.time.LocalDate; public class Card { private String question; private String answer; + private int previousInterval; + private LocalDate date; public Card(String question, String answer) { this.question = question; this.answer = answer; + this.date = LocalDate.now(); + this.previousInterval = 1; + } + + public String getQuestion() { + return "[Q] " + question; + } + + public String getAnswer() { + return "[A] " + answer; + } + + public LocalDate getDate() { + return date; } public String toString() { diff --git a/src/main/java/manager/chapter/Chapter.java b/src/main/java/manager/chapter/Chapter.java new file mode 100644 index 0000000000..42a2d6a8a4 --- /dev/null +++ b/src/main/java/manager/chapter/Chapter.java @@ -0,0 +1,14 @@ +package manager.chapter; + +public class Chapter { + private String chapterName; + + public Chapter(String chapterName) { + this.chapterName = chapterName; + } + + @Override + public String toString() { + return "<" + chapterName + ">"; + } +} diff --git a/src/main/java/parser/Parser.java b/src/main/java/parser/Parser.java index 224bd5c22e..07b816aa0b 100644 --- a/src/main/java/parser/Parser.java +++ b/src/main/java/parser/Parser.java @@ -4,8 +4,9 @@ import commands.Command; import commands.ExitCommand; import commands.ListCommand; +import commands.ReviseCommand; import exception.InvalidInputException; - +import manager.chapter.Chapter; public class Parser { @@ -19,6 +20,8 @@ public static Command parse(String fullCommand) throws InvalidInputException { return prepareList(commandArgs); case AddCommand.COMMAND_WORD: return prepareAdd(commandArgs); + case ReviseCommand.COMMAND_WORD: + return prepareRevise(commandArgs); case ExitCommand.COMMAND_WORD: return prepareExit(commandArgs); default: @@ -78,6 +81,16 @@ private static String parseAnswer(String arg) throws InvalidInputException { return answer; } + private static Command prepareRevise(String arg) throws InvalidInputException { + if (arg.isEmpty()) { + throw new InvalidInputException(); + } + // Initialize a new Chapter object or + // Once Chapter level is implemented, create Chapter object with the given arg (chapterName) + Chapter chapter = new Chapter(arg); + return new ReviseCommand(chapter); + } + private static Command prepareExit(String commandArgs) throws InvalidInputException { if (!commandArgs.isEmpty()) { throw new InvalidInputException(); diff --git a/src/main/java/ui/Ui.java b/src/main/java/ui/Ui.java index 8b91803d32..1f3a2783f6 100644 --- a/src/main/java/ui/Ui.java +++ b/src/main/java/ui/Ui.java @@ -1,11 +1,18 @@ package ui; import manager.card.Card; +import manager.chapter.Chapter; import java.io.InputStream; import java.io.PrintStream; import java.util.ArrayList; import java.util.Scanner; +import java.time.LocalDate; + +import static commands.ReviseCommand.MESSAGE_NO_CARDS_DUE; +import static commands.ReviseCommand.MESSAGE_NO_CARDS_IN_CHAPTER; +import static commands.ReviseCommand.MESSAGE_SHOW_ANSWER_PROMPT; +import static commands.ReviseCommand.MESSAGE_SUCCESS; public class Ui { private final Scanner in; @@ -53,6 +60,35 @@ public void showCardList(ArrayList cards, int cardCount) { } } + public void showRevisionContent(ArrayList cards, int cardCount, Chapter toRevise) { + if (cardCount == 0) { + out.println(String.format(MESSAGE_NO_CARDS_IN_CHAPTER, toRevise)); + return; + } + out.println("The revision for " + toRevise + " will start now:"); + boolean isEqualsDate = false; + for (Card c : cards) { + if (c.getDate().equals(LocalDate.now())) { + isEqualsDate = true; + out.println(c.getQuestion() + MESSAGE_SHOW_ANSWER_PROMPT); + getAnswerInput(c); + } + } + if (!isEqualsDate) { + out.println(String.format(MESSAGE_NO_CARDS_DUE, toRevise)); + } + out.println(String.format(MESSAGE_SUCCESS, toRevise)); + } + + public void getAnswerInput(Card c) { + String input = in.nextLine(); + while (!input.equalsIgnoreCase("s")) { + out.println("You have entered an invalid input, please try again."); + input = in.nextLine(); + } + out.println(c.getAnswer()); + } + public void showExit() { out.println("Exiting the program..."); } From 4a3daa17a2bf7af46089781c2f5210ae96872cdb Mon Sep 17 00:00:00 2001 From: neojiaern Date: Tue, 6 Oct 2020 16:22:17 +0800 Subject: [PATCH 12/43] Fix checkstyle error --- src/main/java/manager/card/Card.java | 1 + 1 file changed, 1 insertion(+) diff --git a/src/main/java/manager/card/Card.java b/src/main/java/manager/card/Card.java index f888f60959..5c67faccd7 100644 --- a/src/main/java/manager/card/Card.java +++ b/src/main/java/manager/card/Card.java @@ -1,4 +1,5 @@ package manager.card; + import java.time.LocalDate; public class Card { From 49b3c2a606000777c75baf73372ca834a9267352 Mon Sep 17 00:00:00 2001 From: neojiaern Date: Tue, 6 Oct 2020 16:39:34 +0800 Subject: [PATCH 13/43] Change arg to commandArgs --- src/main/java/parser/Parser.java | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/src/main/java/parser/Parser.java b/src/main/java/parser/Parser.java index 07b816aa0b..0089601123 100644 --- a/src/main/java/parser/Parser.java +++ b/src/main/java/parser/Parser.java @@ -81,14 +81,11 @@ private static String parseAnswer(String arg) throws InvalidInputException { return answer; } - private static Command prepareRevise(String arg) throws InvalidInputException { - if (arg.isEmpty()) { + private static Command prepareRevise(String commandArgs) throws InvalidInputException { + if (commandArgs.isEmpty()) { throw new InvalidInputException(); } - // Initialize a new Chapter object or - // Once Chapter level is implemented, create Chapter object with the given arg (chapterName) - Chapter chapter = new Chapter(arg); - return new ReviseCommand(chapter); + return new ReviseCommand(commandArgs); } private static Command prepareExit(String commandArgs) throws InvalidInputException { From e7ee47d6be65570c17cf47583006e6ab396d4ee5 Mon Sep 17 00:00:00 2001 From: neojiaern Date: Tue, 6 Oct 2020 16:40:01 +0800 Subject: [PATCH 14/43] Change initialization location for Chapter object --- src/main/java/commands/ReviseCommand.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/java/commands/ReviseCommand.java b/src/main/java/commands/ReviseCommand.java index 4c0d2127ca..1a3c03ade6 100644 --- a/src/main/java/commands/ReviseCommand.java +++ b/src/main/java/commands/ReviseCommand.java @@ -23,8 +23,8 @@ public class ReviseCommand extends Command { private final Chapter toRevise; - public ReviseCommand(Chapter toRevise) { - this.toRevise = toRevise; + public ReviseCommand(String toRevise) { + this.toRevise = new Chapter(toRevise); } @Override From b9f1e40fb8b85ad217a8e25c7aeb5d5040d9f90a Mon Sep 17 00:00:00 2001 From: Darticune Date: Tue, 6 Oct 2020 17:59:49 +0800 Subject: [PATCH 15/43] Included JUnit tests for methods in Scheduler.java and added functions for use in revision and Decks --- src/main/java/commands/ReviseCommand.java | 1 + src/main/java/manager/card/Card.java | 12 ++-- src/main/java/scheduler/Scheduler.java | 76 ++++++++++++++++++++++ src/main/java/ui/Ui.java | 4 +- src/test/java/scheduler/SchedulerTest.java | 62 ++++++++++++++++++ 5 files changed, 149 insertions(+), 6 deletions(-) create mode 100644 src/main/java/scheduler/Scheduler.java create mode 100644 src/test/java/scheduler/SchedulerTest.java diff --git a/src/main/java/commands/ReviseCommand.java b/src/main/java/commands/ReviseCommand.java index 1a3c03ade6..ffdb3f3c9f 100644 --- a/src/main/java/commands/ReviseCommand.java +++ b/src/main/java/commands/ReviseCommand.java @@ -7,6 +7,7 @@ import java.util.ArrayList; + /** * Starts revision for a particular chapter. */ diff --git a/src/main/java/manager/card/Card.java b/src/main/java/manager/card/Card.java index 5c67faccd7..961153d3a8 100644 --- a/src/main/java/manager/card/Card.java +++ b/src/main/java/manager/card/Card.java @@ -6,12 +6,12 @@ public class Card { private String question; private String answer; private int previousInterval; - private LocalDate date; + private LocalDate dueBy; public Card(String question, String answer) { this.question = question; this.answer = answer; - this.date = LocalDate.now(); + this.dueBy = LocalDate.now(); this.previousInterval = 1; } @@ -23,8 +23,12 @@ public String getAnswer() { return "[A] " + answer; } - public LocalDate getDate() { - return date; + public LocalDate getDueBy() { + return dueBy; + } + + public void setDueBy(LocalDate newDueBy) { + dueBy = newDueBy; } public String toString() { diff --git a/src/main/java/scheduler/Scheduler.java b/src/main/java/scheduler/Scheduler.java new file mode 100644 index 0000000000..5461d8381f --- /dev/null +++ b/src/main/java/scheduler/Scheduler.java @@ -0,0 +1,76 @@ +package scheduler; + +import java.time.LocalDate; +import java.lang.Math; + +public class Scheduler { + public static final double EASY_MULTIPLIER = 1.1; + public static final double MEDIUM_MULTIPLIER = 2.2; + public static final double HARD_MULTIPLIER = 4.4; + public static final int MAX_INTERVAL = 365; + + public static boolean isDeadlineDue(LocalDate dueBy) { + return dueBy.isBefore(getCurrentDate()) || dueBy.isEqual(getCurrentDate()); + } + + public static LocalDate getCurrentDate() { + return LocalDate.now(); + } + + public static int computeEasyInterval(int previousInterval) { + int newInterval = (int) Math.round(previousInterval * EASY_MULTIPLIER); + if (newInterval > MAX_INTERVAL) { + return previousInterval; + } else { + return newInterval; + } + } + + public static LocalDate computeEasyDeadline(int previousInterval) { + int interval = computeEasyInterval(previousInterval); + return getCurrentDate().plusDays(interval); + } + + public static int computeMediumInterval(int previousInterval) { + int newInterval = (int) Math.round(previousInterval * MEDIUM_MULTIPLIER); + if (newInterval > MAX_INTERVAL) { + return previousInterval; + } else { + return newInterval; + } + } + + public static LocalDate computeMediumDeadline(int previousInterval) { + int interval = computeMediumInterval(previousInterval); + return getCurrentDate().plusDays(interval); + } + + public static int computeHardInterval(int previousInterval) { + int newInterval = (int) Math.round(previousInterval * HARD_MULTIPLIER); + if (newInterval > MAX_INTERVAL) { + return previousInterval; + } else { + return newInterval; + } + } + + public static LocalDate computeHardDeadline(int previousInterval) { + int interval = computeHardInterval(previousInterval); + return getCurrentDate().plusDays(interval); + } + + public static int computeDeckInterval(double totalMultiplier, int cardCount, int previousInterval) { + double averageMultiplier = (totalMultiplier / cardCount); + int newInterval = (int) Math.round(averageMultiplier * previousInterval); + if (newInterval > MAX_INTERVAL) { + return previousInterval; + } else { + return newInterval; + } + } + + public static LocalDate computeDeckDeadline(double totalMultiplier, int cardCount, int previousInterval) { + int interval = computeDeckInterval(totalMultiplier, cardCount, previousInterval); + return getCurrentDate().plusDays(interval); + } +} diff --git a/src/main/java/ui/Ui.java b/src/main/java/ui/Ui.java index 1f3a2783f6..c95d7341da 100644 --- a/src/main/java/ui/Ui.java +++ b/src/main/java/ui/Ui.java @@ -2,12 +2,12 @@ import manager.card.Card; import manager.chapter.Chapter; +import scheduler.Scheduler; import java.io.InputStream; import java.io.PrintStream; import java.util.ArrayList; import java.util.Scanner; -import java.time.LocalDate; import static commands.ReviseCommand.MESSAGE_NO_CARDS_DUE; import static commands.ReviseCommand.MESSAGE_NO_CARDS_IN_CHAPTER; @@ -68,7 +68,7 @@ public void showRevisionContent(ArrayList cards, int cardCount, Chapter to out.println("The revision for " + toRevise + " will start now:"); boolean isEqualsDate = false; for (Card c : cards) { - if (c.getDate().equals(LocalDate.now())) { + if (Scheduler.isDeadlineDue(c.getDueBy())) { isEqualsDate = true; out.println(c.getQuestion() + MESSAGE_SHOW_ANSWER_PROMPT); getAnswerInput(c); diff --git a/src/test/java/scheduler/SchedulerTest.java b/src/test/java/scheduler/SchedulerTest.java new file mode 100644 index 0000000000..72b52eaf1b --- /dev/null +++ b/src/test/java/scheduler/SchedulerTest.java @@ -0,0 +1,62 @@ +package scheduler; + +import org.junit.jupiter.api.Test; +import java.time.LocalDate; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.junit.jupiter.api.Assertions.assertFalse; + +class SchedulerTest { + + @Test + void computeEasyDeadline_newIntervalOverLimit() { + assertEquals(LocalDate.now().plusDays(333), Scheduler.computeEasyDeadline(333)); + } + + @Test + void computeEasyDeadline_newIntervalWithinLimit() { + assertEquals(LocalDate.now().plusDays(1), Scheduler.computeEasyDeadline(1)); + } + + @Test + void computeMediumDeadline_newIntervalOverLimit() { + assertEquals(LocalDate.now().plusDays(333), Scheduler.computeMediumDeadline(333)); + } + + @Test + void computeMediumDeadline_newIntervalWithinLimit() { + assertEquals(LocalDate.now().plusDays(2), Scheduler.computeMediumDeadline(1)); + } + + @Test + void computeHardDeadline_newIntervalOverLimit() { + assertEquals(LocalDate.now().plusDays(333), Scheduler.computeHardDeadline(333)); + } + + @Test + void computeHardDeadline_newIntervalWithinLimit() { + assertEquals(LocalDate.now().plusDays(4), Scheduler.computeHardDeadline(1)); + } + + @Test + void computeDeckDeadline_newIntervalOverLimit() { + assertEquals(LocalDate.now().plusDays(333), Scheduler.computeDeckDeadline(20, 1, 333)); + } + + @Test + void computeDeckDeadline_newIntervalWithinLimit() { + assertEquals(LocalDate.now().plusDays(20), Scheduler.computeDeckDeadline(20, 1, 1)); + } + + @Test + void isDeadlineDue_deadlineIsDue() { + assertTrue(Scheduler.isDeadlineDue(Scheduler.getCurrentDate().minusDays(1))); + } + + @Test + void isDeadlineDue_deadlineNotDue() { + assertFalse(Scheduler.isDeadlineDue(Scheduler.getCurrentDate().plusDays(1))); + } + +} \ No newline at end of file From 07a5ed0ade481764ccac080d63bd236bad947ffc Mon Sep 17 00:00:00 2001 From: Darticune Date: Tue, 6 Oct 2020 19:36:55 +0800 Subject: [PATCH 16/43] Updated JUnit tests for Scheduler.java with proper naming convention --- src/test/java/scheduler/SchedulerTest.java | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/src/test/java/scheduler/SchedulerTest.java b/src/test/java/scheduler/SchedulerTest.java index 72b52eaf1b..31e4d62975 100644 --- a/src/test/java/scheduler/SchedulerTest.java +++ b/src/test/java/scheduler/SchedulerTest.java @@ -10,52 +10,52 @@ class SchedulerTest { @Test - void computeEasyDeadline_newIntervalOverLimit() { + void computeEasyDeadline_newIntervalOverLimit_intervalUnchanged() { assertEquals(LocalDate.now().plusDays(333), Scheduler.computeEasyDeadline(333)); } @Test - void computeEasyDeadline_newIntervalWithinLimit() { + void computeEasyDeadline_newIntervalWithinLimit_increasedInterval() { assertEquals(LocalDate.now().plusDays(1), Scheduler.computeEasyDeadline(1)); } @Test - void computeMediumDeadline_newIntervalOverLimit() { + void computeMediumDeadline_newIntervalOverLimit_intervalUnchanged() { assertEquals(LocalDate.now().plusDays(333), Scheduler.computeMediumDeadline(333)); } @Test - void computeMediumDeadline_newIntervalWithinLimit() { + void computeMediumDeadline_newIntervalWithinLimit_increasedInterval() { assertEquals(LocalDate.now().plusDays(2), Scheduler.computeMediumDeadline(1)); } @Test - void computeHardDeadline_newIntervalOverLimit() { + void computeHardDeadline_newIntervalOverLimit_intervalUnchanged() { assertEquals(LocalDate.now().plusDays(333), Scheduler.computeHardDeadline(333)); } @Test - void computeHardDeadline_newIntervalWithinLimit() { + void computeHardDeadline_newIntervalWithinLimit_increasedInterval() { assertEquals(LocalDate.now().plusDays(4), Scheduler.computeHardDeadline(1)); } @Test - void computeDeckDeadline_newIntervalOverLimit() { + void computeDeckDeadline_newIntervalOverLimit_intervalUnchanged() { assertEquals(LocalDate.now().plusDays(333), Scheduler.computeDeckDeadline(20, 1, 333)); } @Test - void computeDeckDeadline_newIntervalWithinLimit() { + void computeDeckDeadline_newIntervalWithinLimit_increasedInterval() { assertEquals(LocalDate.now().plusDays(20), Scheduler.computeDeckDeadline(20, 1, 1)); } @Test - void isDeadlineDue_deadlineIsDue() { + void isDeadlineDue_deadlineIsDue_true() { assertTrue(Scheduler.isDeadlineDue(Scheduler.getCurrentDate().minusDays(1))); } @Test - void isDeadlineDue_deadlineNotDue() { + void isDeadlineDue_deadlineNotDue_false() { assertFalse(Scheduler.isDeadlineDue(Scheduler.getCurrentDate().plusDays(1))); } From 0cc640d93a0b7709396dfa551ddca039ee7c9ad5 Mon Sep 17 00:00:00 2001 From: neojiaern Date: Wed, 7 Oct 2020 00:06:32 +0800 Subject: [PATCH 17/43] Move loop function of revision from Ui to RevisionCommand --- src/main/java/commands/ReviseCommand.java | 22 ++++++++++++++++++-- src/main/java/ui/Ui.java | 25 +++++++---------------- 2 files changed, 27 insertions(+), 20 deletions(-) diff --git a/src/main/java/commands/ReviseCommand.java b/src/main/java/commands/ReviseCommand.java index ffdb3f3c9f..cd6030e082 100644 --- a/src/main/java/commands/ReviseCommand.java +++ b/src/main/java/commands/ReviseCommand.java @@ -3,6 +3,7 @@ import manager.card.Card; import manager.chapter.CardList; import manager.chapter.Chapter; +import scheduler.Scheduler; import ui.Ui; import java.util.ArrayList; @@ -18,7 +19,7 @@ public class ReviseCommand extends Command { + "Parameters: CHAPTER_NAME\n" + "Example: " + COMMAND_WORD + " Polymorphism\n"; public static final String MESSAGE_SUCCESS = "You have completed revision for %1$s."; - public static final String MESSAGE_NO_CARDS_IN_CHAPTER = "You have currently have no cards in %1$s."; + public static final String MESSAGE_NO_CARDS_IN_CHAPTER = "You currently have no cards in %1$s."; public static final String MESSAGE_NO_CARDS_DUE = "You have no cards due for revision in %1$s today."; public static final String MESSAGE_SHOW_ANSWER_PROMPT = "\n[enter s to show answer]"; @@ -32,7 +33,24 @@ public ReviseCommand(String toRevise) { public void execute(CardList cards, Ui ui) { ArrayList allCards = cards.getAllCards(); int cardCount = cards.getCardCount(); - ui.showRevisionContent(allCards, cardCount, toRevise); + if (cardCount == 0) { + ui.showToUser(String.format(MESSAGE_NO_CARDS_IN_CHAPTER, toRevise)); + return; + } + ui.showToUser("The revision for " + toRevise + " will start now:"); + int count = 1; + for (Card c : allCards) { + if (Scheduler.isDeadlineDue(c.getDueBy())) { + ui.showToUser("Question " + count + ":"); + ui.showRevisionContent(c); + count++; + } + } + if (count == 1) { + ui.showToUser(String.format(MESSAGE_NO_CARDS_DUE, toRevise)); + return; + } + ui.showToUser(String.format(MESSAGE_SUCCESS, toRevise)); } @Override diff --git a/src/main/java/ui/Ui.java b/src/main/java/ui/Ui.java index c95d7341da..3e108a7189 100644 --- a/src/main/java/ui/Ui.java +++ b/src/main/java/ui/Ui.java @@ -60,24 +60,13 @@ public void showCardList(ArrayList cards, int cardCount) { } } - public void showRevisionContent(ArrayList cards, int cardCount, Chapter toRevise) { - if (cardCount == 0) { - out.println(String.format(MESSAGE_NO_CARDS_IN_CHAPTER, toRevise)); - return; - } - out.println("The revision for " + toRevise + " will start now:"); - boolean isEqualsDate = false; - for (Card c : cards) { - if (Scheduler.isDeadlineDue(c.getDueBy())) { - isEqualsDate = true; - out.println(c.getQuestion() + MESSAGE_SHOW_ANSWER_PROMPT); - getAnswerInput(c); - } - } - if (!isEqualsDate) { - out.println(String.format(MESSAGE_NO_CARDS_DUE, toRevise)); - } - out.println(String.format(MESSAGE_SUCCESS, toRevise)); + public void showToUser(String message) { + out.println(message); + } + + public void showRevisionContent(Card c) { + out.println(c.getQuestion() + MESSAGE_SHOW_ANSWER_PROMPT); + getAnswerInput(c); } public void getAnswerInput(Card c) { From e703c901081edbe81d9ae6d8e102b0dfa37731b9 Mon Sep 17 00:00:00 2001 From: Zhu-Zeyu <20001115zhuzeyu@gmail.com> Date: Wed, 7 Oct 2020 00:37:59 +0800 Subject: [PATCH 18/43] Add help command and capability to store questions --- questions.txt | 0 src/main/java/Kaji.java | 10 +++- src/main/java/commands/HelpCommand.java | 18 ++++++ .../exception/InvalidFileFormatException.java | 4 ++ src/main/java/parser/Parser.java | 60 +++++++++++++++---- src/main/java/storage/Storage.java | 49 +++++++++++++++ src/main/java/ui/Ui.java | 20 ++++++- 7 files changed, 148 insertions(+), 13 deletions(-) create mode 100644 questions.txt create mode 100644 src/main/java/commands/HelpCommand.java create mode 100644 src/main/java/exception/InvalidFileFormatException.java create mode 100644 src/main/java/storage/Storage.java diff --git a/questions.txt b/questions.txt new file mode 100644 index 0000000000..e69de29bb2 diff --git a/src/main/java/Kaji.java b/src/main/java/Kaji.java index e357a2ce0b..e1b32e7be7 100644 --- a/src/main/java/Kaji.java +++ b/src/main/java/Kaji.java @@ -2,8 +2,11 @@ import exception.InvalidInputException; import manager.chapter.CardList; import parser.Parser; +import storage.Storage; import ui.Ui; +import java.io.IOException; + public class Kaji { private CardList cards; private Ui ui; @@ -15,15 +18,20 @@ public Kaji() { public void run() { ui.showWelcome(); + ui.showHelpList(); boolean isExit = false; + Storage.getFileContents(cards); while (!isExit) { try { String fullCommand = ui.readCommand(); Command c = Parser.parse(fullCommand); c.execute(cards, ui); + Storage.writeToFile(cards); isExit = c.isExit(); } catch (InvalidInputException e) { System.out.println("Invalid input given"); + } catch (IOException e) { + System.out.println(" Something went wrong: " + e.getMessage()); } } } @@ -31,4 +39,4 @@ public void run() { public static void main(String[] args) { new Kaji().run(); } -} +} \ No newline at end of file diff --git a/src/main/java/commands/HelpCommand.java b/src/main/java/commands/HelpCommand.java new file mode 100644 index 0000000000..9cc600f8c2 --- /dev/null +++ b/src/main/java/commands/HelpCommand.java @@ -0,0 +1,18 @@ +package commands; + +import manager.chapter.CardList; +import ui.Ui; + +public class HelpCommand extends Command{ + public static final String COMMAND_WORD = "help"; + + @Override + public void execute(CardList cards, Ui ui) { + ui.showHelpList(); + } + + @Override + public boolean isExit() { + return true; + } +} \ No newline at end of file diff --git a/src/main/java/exception/InvalidFileFormatException.java b/src/main/java/exception/InvalidFileFormatException.java new file mode 100644 index 0000000000..3d6b882b43 --- /dev/null +++ b/src/main/java/exception/InvalidFileFormatException.java @@ -0,0 +1,4 @@ +package exception; + +public class InvalidFileFormatException extends Exception { +} \ No newline at end of file diff --git a/src/main/java/parser/Parser.java b/src/main/java/parser/Parser.java index 0089601123..a905208033 100644 --- a/src/main/java/parser/Parser.java +++ b/src/main/java/parser/Parser.java @@ -5,8 +5,11 @@ import commands.ExitCommand; import commands.ListCommand; import commands.ReviseCommand; +import commands.HelpCommand; +import exception.InvalidFileFormatException; import exception.InvalidInputException; import manager.chapter.Chapter; +import storage.Storage; public class Parser { @@ -16,16 +19,18 @@ public static Command parse(String fullCommand) throws InvalidInputException { String commandArgs = commandTypeAndArgs[1].trim(); switch (commandType) { - case ListCommand.COMMAND_WORD: - return prepareList(commandArgs); - case AddCommand.COMMAND_WORD: - return prepareAdd(commandArgs); - case ReviseCommand.COMMAND_WORD: - return prepareRevise(commandArgs); - case ExitCommand.COMMAND_WORD: - return prepareExit(commandArgs); - default: - throw new InvalidInputException(); + case ListCommand.COMMAND_WORD: + return prepareList(commandArgs); + case AddCommand.COMMAND_WORD: + return prepareAdd(commandArgs); + case ReviseCommand.COMMAND_WORD: + return prepareRevise(commandArgs); + case ExitCommand.COMMAND_WORD: + return prepareExit(commandArgs); + case HelpCommand.COMMAND_WORD: + return prepareHelp(commandArgs); + default: + throw new InvalidInputException(); } } @@ -94,4 +99,37 @@ private static Command prepareExit(String commandArgs) throws InvalidInputExcept } return new ExitCommand(); } -} + + private static Command prepareHelp(String commandArgs) throws InvalidInputException { + if (!commandArgs.isEmpty()) { + throw new InvalidInputException(); + } + return new HelpCommand(); + } + + public static String parseQuestioninFile(String arg) throws InvalidFileFormatException { + if (!(arg.trim().startsWith(Storage.QUESTION_PREFIX))) { + throw new InvalidFileFormatException(); + } + + String question = arg.substring(3).trim(); + if (question.isEmpty()) { + throw new InvalidFileFormatException(); + } + + return question; + } + + public static String parseAnswerinFile(String arg) throws InvalidFileFormatException { + if (!(arg.trim().startsWith(Storage.ANSWER_PREFIX))) { + throw new InvalidFileFormatException(); + } + + String answer = arg.substring(3).trim(); + if (answer.isEmpty()) { + throw new InvalidFileFormatException(); + } + + return answer; + } +} \ No newline at end of file diff --git a/src/main/java/storage/Storage.java b/src/main/java/storage/Storage.java new file mode 100644 index 0000000000..d1fd264846 --- /dev/null +++ b/src/main/java/storage/Storage.java @@ -0,0 +1,49 @@ +package storage; + +import commands.AddCommand; +import commands.Command; +import exception.InvalidFileFormatException; +import exception.InvalidInputException; +import manager.card.Card; +import manager.chapter.CardList; +import parser.Parser; +import java.io.File; +import java.io.FileNotFoundException; +import java.io.FileWriter; +import java.io.IOException; +import java.util.Scanner; + +public class Storage { + public static final String FILE_PATHWAY = "questions.txt"; // file pathway + + public static final String QUESTION_ANSWER_PREFIX = " \\| "; + public static final String QUESTION_PREFIX = "[Q]"; + public static final String ANSWER_PREFIX = "[A]"; + + public static void getFileContents(CardList cards) { + try { + File f = new File(FILE_PATHWAY); // create a File for the given file path + Scanner s = new Scanner(f); // create a Scanner using the File as the source + while(s.hasNext()) { + String fileCommand = s.nextLine(); + String[] args = fileCommand.split(QUESTION_ANSWER_PREFIX, 2); + String question = Parser.parseQuestioninFile(args[0]); + String answer = Parser.parseAnswerinFile(args[1]); + Card card = new Card(question, answer); + cards.addCard(card); + } + } catch (FileNotFoundException e) { + System.out.println("File not found"); + } catch (InvalidFileFormatException e) { + System.out.println("The format of some commands in the file is invalid"); + } + } + + public static void writeToFile(CardList cards) throws IOException { + FileWriter fw = new FileWriter(FILE_PATHWAY); + for (int i=0; i Date: Wed, 7 Oct 2020 13:03:38 +0800 Subject: [PATCH 19/43] Add rate feature for each card under revision --- src/main/java/commands/ReviseCommand.java | 57 ++++++++++++++++++++++- src/main/java/manager/card/Card.java | 4 ++ src/main/java/ui/Ui.java | 21 +++++++-- 3 files changed, 75 insertions(+), 7 deletions(-) diff --git a/src/main/java/commands/ReviseCommand.java b/src/main/java/commands/ReviseCommand.java index cd6030e082..b9b53310a5 100644 --- a/src/main/java/commands/ReviseCommand.java +++ b/src/main/java/commands/ReviseCommand.java @@ -22,6 +22,12 @@ public class ReviseCommand extends Command { public static final String MESSAGE_NO_CARDS_IN_CHAPTER = "You currently have no cards in %1$s."; public static final String MESSAGE_NO_CARDS_DUE = "You have no cards due for revision in %1$s today."; public static final String MESSAGE_SHOW_ANSWER_PROMPT = "\n[enter s to show answer]"; + public static final String MESSAGE_SHOW_RATING_PROMPT = "How well did you do for this card?\n" + + "[enter e(easy), m(medium), h(hard), c(cannot answer)]"; + public static final String EASY = "e"; + public static final String MEDIUM = "m"; + public static final String HARD = "h"; + public static final String CANNOT_ANSWER = "c"; private final Chapter toRevise; @@ -32,6 +38,7 @@ public ReviseCommand(String toRevise) { @Override public void execute(CardList cards, Ui ui) { ArrayList allCards = cards.getAllCards(); + ArrayList repeatCards = new ArrayList<>(); int cardCount = cards.getCardCount(); if (cardCount == 0) { ui.showToUser(String.format(MESSAGE_NO_CARDS_IN_CHAPTER, toRevise)); @@ -41,8 +48,9 @@ public void execute(CardList cards, Ui ui) { int count = 1; for (Card c : allCards) { if (Scheduler.isDeadlineDue(c.getDueBy())) { - ui.showToUser("Question " + count + ":"); - ui.showRevisionContent(c); + ui.showToUser("\nQuestion " + count + ":"); + ui.showCard(c); + repeatCards = rateCard(ui, repeatCards, c); count++; } } @@ -50,9 +58,54 @@ public void execute(CardList cards, Ui ui) { ui.showToUser(String.format(MESSAGE_NO_CARDS_DUE, toRevise)); return; } + + repeatRevision(ui, repeatCards, count); ui.showToUser(String.format(MESSAGE_SUCCESS, toRevise)); } + private ArrayList rateCard(Ui ui, ArrayList repeatCards, Card c) { + String input = ui.getRating(); + boolean isInvalid = true; + while (isInvalid) { + switch (input.toLowerCase()) { + case EASY: + c.setDueBy(Scheduler.computeEasyDeadline(c.getPreviousInterval())); + isInvalid = false; + break; + case MEDIUM: + c.setDueBy(Scheduler.computeMediumDeadline(c.getPreviousInterval())); + isInvalid = false; + break; + case HARD: + c.setDueBy(Scheduler.computeHardDeadline(c.getPreviousInterval())); + isInvalid = false; + break; + case CANNOT_ANSWER: + repeatCards.add(c); + isInvalid = false; + break; + default: + ui.showToUser("You have entered an invalid input, please try again."); + input = ui.getRating(); + } + } + return repeatCards; + } + + private void repeatRevision(Ui ui, ArrayList cards, int count) { + while (cards.size() != 0) { + System.out.println(cards.size()); + ArrayList repeatCards = new ArrayList<>(); + for (Card c : cards) { + ui.showToUser("\nQuestion " + count + ":"); + ui.showCard(c); + repeatCards = rateCard(ui, repeatCards, c); + count++; + } + cards = new ArrayList<>(repeatCards); + } + } + @Override public boolean isExit() { return false; diff --git a/src/main/java/manager/card/Card.java b/src/main/java/manager/card/Card.java index 961153d3a8..89fbf69b0f 100644 --- a/src/main/java/manager/card/Card.java +++ b/src/main/java/manager/card/Card.java @@ -27,6 +27,10 @@ public LocalDate getDueBy() { return dueBy; } + public int getPreviousInterval() { + return previousInterval; + } + public void setDueBy(LocalDate newDueBy) { dueBy = newDueBy; } diff --git a/src/main/java/ui/Ui.java b/src/main/java/ui/Ui.java index 3e108a7189..d0786ab172 100644 --- a/src/main/java/ui/Ui.java +++ b/src/main/java/ui/Ui.java @@ -9,10 +9,12 @@ import java.util.ArrayList; import java.util.Scanner; -import static commands.ReviseCommand.MESSAGE_NO_CARDS_DUE; -import static commands.ReviseCommand.MESSAGE_NO_CARDS_IN_CHAPTER; +import static commands.ReviseCommand.CANNOT_ANSWER; +import static commands.ReviseCommand.EASY; +import static commands.ReviseCommand.HARD; +import static commands.ReviseCommand.MEDIUM; import static commands.ReviseCommand.MESSAGE_SHOW_ANSWER_PROMPT; -import static commands.ReviseCommand.MESSAGE_SUCCESS; +import static commands.ReviseCommand.MESSAGE_SHOW_RATING_PROMPT; public class Ui { private final Scanner in; @@ -36,7 +38,11 @@ public String readCommand() { } public void showWelcome() { - out.println("Welcome to Kaji!"); + out.println("Welcome to Kaji!\n"); + } + + public void printEmptyLine() { + out.println(); } public void showCardAdded(Card card, int cardCount) { @@ -64,7 +70,7 @@ public void showToUser(String message) { out.println(message); } - public void showRevisionContent(Card c) { + public void showCard(Card c) { out.println(c.getQuestion() + MESSAGE_SHOW_ANSWER_PROMPT); getAnswerInput(c); } @@ -78,6 +84,11 @@ public void getAnswerInput(Card c) { out.println(c.getAnswer()); } + public String getRating() { + out.println(MESSAGE_SHOW_RATING_PROMPT); + return in.nextLine(); + } + public void showExit() { out.println("Exiting the program..."); } From 6dfb5fa0d7b9113d25a7680cd0da9dc6adaf7d2a Mon Sep 17 00:00:00 2001 From: neojiaern Date: Wed, 7 Oct 2020 13:04:07 +0800 Subject: [PATCH 20/43] Add print empty line after each output in run function --- src/main/java/Kaji.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/main/java/Kaji.java b/src/main/java/Kaji.java index e357a2ce0b..6552e377a1 100644 --- a/src/main/java/Kaji.java +++ b/src/main/java/Kaji.java @@ -21,9 +21,10 @@ public void run() { String fullCommand = ui.readCommand(); Command c = Parser.parse(fullCommand); c.execute(cards, ui); + ui.printEmptyLine(); isExit = c.isExit(); } catch (InvalidInputException e) { - System.out.println("Invalid input given"); + System.out.println("Invalid input given.\n"); } } } From 68ecf35801961a98b3c4aaa70e117a06c69fe7d1 Mon Sep 17 00:00:00 2001 From: Zhu-Zeyu <20001115zhuzeyu@gmail.com> Date: Wed, 7 Oct 2020 21:57:51 +0800 Subject: [PATCH 21/43] Fix error --- src/main/java/Kaji.java | 2 +- src/main/java/commands/HelpCommand.java | 2 +- src/main/java/exception/InvalidFileFormatException.java | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/main/java/Kaji.java b/src/main/java/Kaji.java index e1b32e7be7..df3c88c8dd 100644 --- a/src/main/java/Kaji.java +++ b/src/main/java/Kaji.java @@ -39,4 +39,4 @@ public void run() { public static void main(String[] args) { new Kaji().run(); } -} \ No newline at end of file +} diff --git a/src/main/java/commands/HelpCommand.java b/src/main/java/commands/HelpCommand.java index 9cc600f8c2..57ffc14f2f 100644 --- a/src/main/java/commands/HelpCommand.java +++ b/src/main/java/commands/HelpCommand.java @@ -15,4 +15,4 @@ public void execute(CardList cards, Ui ui) { public boolean isExit() { return true; } -} \ No newline at end of file +} diff --git a/src/main/java/exception/InvalidFileFormatException.java b/src/main/java/exception/InvalidFileFormatException.java index 3d6b882b43..bc6cc54e1d 100644 --- a/src/main/java/exception/InvalidFileFormatException.java +++ b/src/main/java/exception/InvalidFileFormatException.java @@ -1,4 +1,4 @@ package exception; public class InvalidFileFormatException extends Exception { -} \ No newline at end of file +} From 6fc44fcb0325a40be7497a8b3e4096aa87fc23d6 Mon Sep 17 00:00:00 2001 From: Zhu-Zeyu <20001115zhuzeyu@gmail.com> Date: Wed, 7 Oct 2020 22:13:00 +0800 Subject: [PATCH 22/43] Check coding standard --- src/main/java/commands/HelpCommand.java | 2 +- src/main/java/parser/Parser.java | 26 ++++++++++++------------- src/main/java/storage/Storage.java | 8 ++++---- src/main/java/ui/Ui.java | 2 +- 4 files changed, 19 insertions(+), 19 deletions(-) diff --git a/src/main/java/commands/HelpCommand.java b/src/main/java/commands/HelpCommand.java index 57ffc14f2f..989205e98c 100644 --- a/src/main/java/commands/HelpCommand.java +++ b/src/main/java/commands/HelpCommand.java @@ -3,7 +3,7 @@ import manager.chapter.CardList; import ui.Ui; -public class HelpCommand extends Command{ +public class HelpCommand extends Command { public static final String COMMAND_WORD = "help"; @Override diff --git a/src/main/java/parser/Parser.java b/src/main/java/parser/Parser.java index a905208033..e6ca513d55 100644 --- a/src/main/java/parser/Parser.java +++ b/src/main/java/parser/Parser.java @@ -19,18 +19,18 @@ public static Command parse(String fullCommand) throws InvalidInputException { String commandArgs = commandTypeAndArgs[1].trim(); switch (commandType) { - case ListCommand.COMMAND_WORD: - return prepareList(commandArgs); - case AddCommand.COMMAND_WORD: - return prepareAdd(commandArgs); - case ReviseCommand.COMMAND_WORD: - return prepareRevise(commandArgs); - case ExitCommand.COMMAND_WORD: - return prepareExit(commandArgs); - case HelpCommand.COMMAND_WORD: - return prepareHelp(commandArgs); - default: - throw new InvalidInputException(); + case ListCommand.COMMAND_WORD: + return prepareList(commandArgs); + case AddCommand.COMMAND_WORD: + return prepareAdd(commandArgs); + case ReviseCommand.COMMAND_WORD: + return prepareRevise(commandArgs); + case ExitCommand.COMMAND_WORD: + return prepareExit(commandArgs); + case HelpCommand.COMMAND_WORD: + return prepareHelp(commandArgs); + default: + throw new InvalidInputException(); } } @@ -132,4 +132,4 @@ public static String parseAnswerinFile(String arg) throws InvalidFileFormatExcep return answer; } -} \ No newline at end of file +} diff --git a/src/main/java/storage/Storage.java b/src/main/java/storage/Storage.java index d1fd264846..675ec84a4a 100644 --- a/src/main/java/storage/Storage.java +++ b/src/main/java/storage/Storage.java @@ -24,7 +24,7 @@ public static void getFileContents(CardList cards) { try { File f = new File(FILE_PATHWAY); // create a File for the given file path Scanner s = new Scanner(f); // create a Scanner using the File as the source - while(s.hasNext()) { + while (s.hasNext()) { String fileCommand = s.nextLine(); String[] args = fileCommand.split(QUESTION_ANSWER_PREFIX, 2); String question = Parser.parseQuestioninFile(args[0]); @@ -41,9 +41,9 @@ public static void getFileContents(CardList cards) { public static void writeToFile(CardList cards) throws IOException { FileWriter fw = new FileWriter(FILE_PATHWAY); - for (int i=0; i Date: Wed, 7 Oct 2020 22:13:08 +0800 Subject: [PATCH 23/43] Revert "Add help command and capability to store questions" --- questions.txt | 0 src/main/java/Kaji.java | 10 +--- src/main/java/commands/HelpCommand.java | 18 ------ .../exception/InvalidFileFormatException.java | 4 -- src/main/java/parser/Parser.java | 60 ++++--------------- src/main/java/storage/Storage.java | 49 --------------- src/main/java/ui/Ui.java | 20 +------ 7 files changed, 13 insertions(+), 148 deletions(-) delete mode 100644 questions.txt delete mode 100644 src/main/java/commands/HelpCommand.java delete mode 100644 src/main/java/exception/InvalidFileFormatException.java delete mode 100644 src/main/java/storage/Storage.java diff --git a/questions.txt b/questions.txt deleted file mode 100644 index e69de29bb2..0000000000 diff --git a/src/main/java/Kaji.java b/src/main/java/Kaji.java index e1b32e7be7..e357a2ce0b 100644 --- a/src/main/java/Kaji.java +++ b/src/main/java/Kaji.java @@ -2,11 +2,8 @@ import exception.InvalidInputException; import manager.chapter.CardList; import parser.Parser; -import storage.Storage; import ui.Ui; -import java.io.IOException; - public class Kaji { private CardList cards; private Ui ui; @@ -18,20 +15,15 @@ public Kaji() { public void run() { ui.showWelcome(); - ui.showHelpList(); boolean isExit = false; - Storage.getFileContents(cards); while (!isExit) { try { String fullCommand = ui.readCommand(); Command c = Parser.parse(fullCommand); c.execute(cards, ui); - Storage.writeToFile(cards); isExit = c.isExit(); } catch (InvalidInputException e) { System.out.println("Invalid input given"); - } catch (IOException e) { - System.out.println(" Something went wrong: " + e.getMessage()); } } } @@ -39,4 +31,4 @@ public void run() { public static void main(String[] args) { new Kaji().run(); } -} \ No newline at end of file +} diff --git a/src/main/java/commands/HelpCommand.java b/src/main/java/commands/HelpCommand.java deleted file mode 100644 index 9cc600f8c2..0000000000 --- a/src/main/java/commands/HelpCommand.java +++ /dev/null @@ -1,18 +0,0 @@ -package commands; - -import manager.chapter.CardList; -import ui.Ui; - -public class HelpCommand extends Command{ - public static final String COMMAND_WORD = "help"; - - @Override - public void execute(CardList cards, Ui ui) { - ui.showHelpList(); - } - - @Override - public boolean isExit() { - return true; - } -} \ No newline at end of file diff --git a/src/main/java/exception/InvalidFileFormatException.java b/src/main/java/exception/InvalidFileFormatException.java deleted file mode 100644 index 3d6b882b43..0000000000 --- a/src/main/java/exception/InvalidFileFormatException.java +++ /dev/null @@ -1,4 +0,0 @@ -package exception; - -public class InvalidFileFormatException extends Exception { -} \ No newline at end of file diff --git a/src/main/java/parser/Parser.java b/src/main/java/parser/Parser.java index a905208033..0089601123 100644 --- a/src/main/java/parser/Parser.java +++ b/src/main/java/parser/Parser.java @@ -5,11 +5,8 @@ import commands.ExitCommand; import commands.ListCommand; import commands.ReviseCommand; -import commands.HelpCommand; -import exception.InvalidFileFormatException; import exception.InvalidInputException; import manager.chapter.Chapter; -import storage.Storage; public class Parser { @@ -19,18 +16,16 @@ public static Command parse(String fullCommand) throws InvalidInputException { String commandArgs = commandTypeAndArgs[1].trim(); switch (commandType) { - case ListCommand.COMMAND_WORD: - return prepareList(commandArgs); - case AddCommand.COMMAND_WORD: - return prepareAdd(commandArgs); - case ReviseCommand.COMMAND_WORD: - return prepareRevise(commandArgs); - case ExitCommand.COMMAND_WORD: - return prepareExit(commandArgs); - case HelpCommand.COMMAND_WORD: - return prepareHelp(commandArgs); - default: - throw new InvalidInputException(); + case ListCommand.COMMAND_WORD: + return prepareList(commandArgs); + case AddCommand.COMMAND_WORD: + return prepareAdd(commandArgs); + case ReviseCommand.COMMAND_WORD: + return prepareRevise(commandArgs); + case ExitCommand.COMMAND_WORD: + return prepareExit(commandArgs); + default: + throw new InvalidInputException(); } } @@ -99,37 +94,4 @@ private static Command prepareExit(String commandArgs) throws InvalidInputExcept } return new ExitCommand(); } - - private static Command prepareHelp(String commandArgs) throws InvalidInputException { - if (!commandArgs.isEmpty()) { - throw new InvalidInputException(); - } - return new HelpCommand(); - } - - public static String parseQuestioninFile(String arg) throws InvalidFileFormatException { - if (!(arg.trim().startsWith(Storage.QUESTION_PREFIX))) { - throw new InvalidFileFormatException(); - } - - String question = arg.substring(3).trim(); - if (question.isEmpty()) { - throw new InvalidFileFormatException(); - } - - return question; - } - - public static String parseAnswerinFile(String arg) throws InvalidFileFormatException { - if (!(arg.trim().startsWith(Storage.ANSWER_PREFIX))) { - throw new InvalidFileFormatException(); - } - - String answer = arg.substring(3).trim(); - if (answer.isEmpty()) { - throw new InvalidFileFormatException(); - } - - return answer; - } -} \ No newline at end of file +} diff --git a/src/main/java/storage/Storage.java b/src/main/java/storage/Storage.java deleted file mode 100644 index d1fd264846..0000000000 --- a/src/main/java/storage/Storage.java +++ /dev/null @@ -1,49 +0,0 @@ -package storage; - -import commands.AddCommand; -import commands.Command; -import exception.InvalidFileFormatException; -import exception.InvalidInputException; -import manager.card.Card; -import manager.chapter.CardList; -import parser.Parser; -import java.io.File; -import java.io.FileNotFoundException; -import java.io.FileWriter; -import java.io.IOException; -import java.util.Scanner; - -public class Storage { - public static final String FILE_PATHWAY = "questions.txt"; // file pathway - - public static final String QUESTION_ANSWER_PREFIX = " \\| "; - public static final String QUESTION_PREFIX = "[Q]"; - public static final String ANSWER_PREFIX = "[A]"; - - public static void getFileContents(CardList cards) { - try { - File f = new File(FILE_PATHWAY); // create a File for the given file path - Scanner s = new Scanner(f); // create a Scanner using the File as the source - while(s.hasNext()) { - String fileCommand = s.nextLine(); - String[] args = fileCommand.split(QUESTION_ANSWER_PREFIX, 2); - String question = Parser.parseQuestioninFile(args[0]); - String answer = Parser.parseAnswerinFile(args[1]); - Card card = new Card(question, answer); - cards.addCard(card); - } - } catch (FileNotFoundException e) { - System.out.println("File not found"); - } catch (InvalidFileFormatException e) { - System.out.println("The format of some commands in the file is invalid"); - } - } - - public static void writeToFile(CardList cards) throws IOException { - FileWriter fw = new FileWriter(FILE_PATHWAY); - for (int i=0; i Date: Wed, 7 Oct 2020 23:09:28 +0800 Subject: [PATCH 24/43] resolve merge conflicts --- questions.txt | 1 + src/main/java/Kaji.java | 8 ++++++++ 2 files changed, 9 insertions(+) create mode 100644 questions.txt diff --git a/questions.txt b/questions.txt new file mode 100644 index 0000000000..8b13789179 --- /dev/null +++ b/questions.txt @@ -0,0 +1 @@ + diff --git a/src/main/java/Kaji.java b/src/main/java/Kaji.java index 6552e377a1..f0a2880e4d 100644 --- a/src/main/java/Kaji.java +++ b/src/main/java/Kaji.java @@ -2,8 +2,11 @@ import exception.InvalidInputException; import manager.chapter.CardList; import parser.Parser; +import storage.Storage; import ui.Ui; +import java.io.IOException; + public class Kaji { private CardList cards; private Ui ui; @@ -15,16 +18,21 @@ public Kaji() { public void run() { ui.showWelcome(); + ui.showHelpList(); boolean isExit = false; + Storage.getFileContents(cards); while (!isExit) { try { String fullCommand = ui.readCommand(); Command c = Parser.parse(fullCommand); c.execute(cards, ui); ui.printEmptyLine(); + Storage.writeToFile(cards); isExit = c.isExit(); } catch (InvalidInputException e) { System.out.println("Invalid input given.\n"); + } catch (IOException e) { + System.out.println(" Something went wrong: " + e.getMessage()); } } } From 633a1e713f45d2b053343f0c696655c9c6506708 Mon Sep 17 00:00:00 2001 From: Zhu-Zeyu <20001115zhuzeyu@gmail.com> Date: Wed, 7 Oct 2020 23:47:04 +0800 Subject: [PATCH 25/43] modify showHelpList --- questions.txt | 2 +- src/main/java/commands/AddCommand.java | 3 +++ src/main/java/commands/ExitCommand.java | 3 +++ src/main/java/commands/HelpCommand.java | 3 +++ src/main/java/commands/ListCommand.java | 3 +++ src/main/java/ui/Ui.java | 20 ++++++-------------- 6 files changed, 19 insertions(+), 15 deletions(-) diff --git a/questions.txt b/questions.txt index 8b13789179..3ac230ec09 100644 --- a/questions.txt +++ b/questions.txt @@ -1 +1 @@ - +[Q] da | [A] da diff --git a/src/main/java/commands/AddCommand.java b/src/main/java/commands/AddCommand.java index f6dd274d1e..6f0e8b3eb9 100644 --- a/src/main/java/commands/AddCommand.java +++ b/src/main/java/commands/AddCommand.java @@ -7,6 +7,9 @@ public class AddCommand extends Command { public static final String COMMAND_WORD = "add"; + public static final String MESSAGE_USAGE = COMMAND_WORD + ": Adds a flashcard to the schedule manager. \n" + + "Parameters: q:QUESTION | a:ANSWER\n" + "Example: " + COMMAND_WORD + " q:What is the result of one plus one | a:two\n"; + public static final String QUESTION_ANSWER_PREFIX = " \\| "; public static final String QUESTION_PREFIX = "q:"; public static final String ANSWER_PREFIX = "a:"; diff --git a/src/main/java/commands/ExitCommand.java b/src/main/java/commands/ExitCommand.java index 04fa8c425f..f943ff3946 100644 --- a/src/main/java/commands/ExitCommand.java +++ b/src/main/java/commands/ExitCommand.java @@ -6,6 +6,9 @@ public class ExitCommand extends Command { public static final String COMMAND_WORD = "exit"; + public static final String MESSAGE_USAGE = COMMAND_WORD + ": Exits the program. \n" + + "Example: " + COMMAND_WORD + "\n"; + @Override public void execute(CardList cards, Ui ui) { ui.showExit(); diff --git a/src/main/java/commands/HelpCommand.java b/src/main/java/commands/HelpCommand.java index 989205e98c..ae336211ca 100644 --- a/src/main/java/commands/HelpCommand.java +++ b/src/main/java/commands/HelpCommand.java @@ -6,6 +6,9 @@ public class HelpCommand extends Command { public static final String COMMAND_WORD = "help"; + public static final String MESSAGE_USAGE = COMMAND_WORD + ": Shows a list of commands available. \n" + + "Example: " + COMMAND_WORD + "\n"; + @Override public void execute(CardList cards, Ui ui) { ui.showHelpList(); diff --git a/src/main/java/commands/ListCommand.java b/src/main/java/commands/ListCommand.java index ec05991dd6..1e39a2eed3 100644 --- a/src/main/java/commands/ListCommand.java +++ b/src/main/java/commands/ListCommand.java @@ -9,6 +9,9 @@ public class ListCommand extends Command { public static final String COMMAND_WORD = "list"; + public static final String MESSAGE_USAGE = COMMAND_WORD + ": Shows a list of flashcards available. \n" + + "Example: " + COMMAND_WORD + "\n"; + @Override public void execute(CardList cards, Ui ui) { ArrayList allCards = cards.getAllCards(); diff --git a/src/main/java/ui/Ui.java b/src/main/java/ui/Ui.java index 9e4ea96fb1..e08a8ad5af 100644 --- a/src/main/java/ui/Ui.java +++ b/src/main/java/ui/Ui.java @@ -1,5 +1,6 @@ package ui; +import commands.*; import manager.card.Card; import manager.chapter.Chapter; import scheduler.Scheduler; @@ -95,19 +96,10 @@ public void showExit() { public void showHelpList() { out.println("Here is a list of commands available:" + "\n"); - out.println("1. Shows a list of flashcards available"); - out.println(" Format: list"); - - out.println("2. Adds a flashcard to the schedule manager"); - out.println(" Format: add q:QUESTION | a:ANSWER"); - - out.println("3. Revises flashcards one by one"); - out.println(" Format: revise"); - - out.println("4. Exits the program"); - out.println(" Format: exit"); - - out.println("4. Shows a list of commands available"); - out.println(" Format: help"); + out.println("1. " + ListCommand.MESSAGE_USAGE); + out.println("2. " + ReviseCommand.MESSAGE_USAGE); + out.println("3. " + HelpCommand.MESSAGE_USAGE); + out.println("4. " + AddCommand.MESSAGE_USAGE); + out.println("5. " + ExitCommand.MESSAGE_USAGE); } } From f6adf44b1108fc675d256c9b487b05b78d6ba8a4 Mon Sep 17 00:00:00 2001 From: Zhu-Zeyu <20001115zhuzeyu@gmail.com> Date: Wed, 7 Oct 2020 23:55:20 +0800 Subject: [PATCH 26/43] Check coding style --- src/main/java/commands/AddCommand.java | 3 ++- src/main/java/ui/Ui.java | 7 ++++++- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/src/main/java/commands/AddCommand.java b/src/main/java/commands/AddCommand.java index 6f0e8b3eb9..781d5ffc74 100644 --- a/src/main/java/commands/AddCommand.java +++ b/src/main/java/commands/AddCommand.java @@ -8,7 +8,8 @@ public class AddCommand extends Command { public static final String COMMAND_WORD = "add"; public static final String MESSAGE_USAGE = COMMAND_WORD + ": Adds a flashcard to the schedule manager. \n" - + "Parameters: q:QUESTION | a:ANSWER\n" + "Example: " + COMMAND_WORD + " q:What is the result of one plus one | a:two\n"; + + "Parameters: q:QUESTION | a:ANSWER\n" + + "Example: " + COMMAND_WORD + " q:What is the result of one plus one | a:two\n"; public static final String QUESTION_ANSWER_PREFIX = " \\| "; public static final String QUESTION_PREFIX = "q:"; diff --git a/src/main/java/ui/Ui.java b/src/main/java/ui/Ui.java index e08a8ad5af..aa93dc6a07 100644 --- a/src/main/java/ui/Ui.java +++ b/src/main/java/ui/Ui.java @@ -1,6 +1,11 @@ package ui; -import commands.*; +import commands.AddCommand; +import commands.ExitCommand; +import commands.ListCommand; +import commands.HelpCommand; +import commands.ReviseCommand; + import manager.card.Card; import manager.chapter.Chapter; import scheduler.Scheduler; From eb5eb0b43a7627a6c21c09cfd0dfd9a290c042ec Mon Sep 17 00:00:00 2001 From: Jane Date: Thu, 8 Oct 2020 00:34:51 +0800 Subject: [PATCH 27/43] Add JUnit Tests for invalid input for add, list and exit commands --- src/test/java/parser/ParserTest.java | 41 ++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 src/test/java/parser/ParserTest.java diff --git a/src/test/java/parser/ParserTest.java b/src/test/java/parser/ParserTest.java new file mode 100644 index 0000000000..33145bfe1d --- /dev/null +++ b/src/test/java/parser/ParserTest.java @@ -0,0 +1,41 @@ +package parser; + +import exception.InvalidInputException; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertThrows; + +class ParserTest { + @Test + public void parse_addCommandInvalidArgs_expectException() { + Parser parser = new Parser(); + final String[] inputs = { + "add", + "add ", + "add wrong args format", + // no question and answer separator prefix + "add q:When is v1.0 due? a:15 Oct 2020", + // no question prefix + "add When is v1.0 due? | a:15 Oct 2020", + // no answer prefix + "add q:When is v1.0 due? | 15 Oct 2020", + }; + for (String input : inputs) { + assertThrows(InvalidInputException.class, () -> parser.parse(input)); + } + } + + @Test + public void parse_listCommandWithArgs_expectException() { + Parser parser = new Parser(); + final String input = "list args"; + assertThrows(InvalidInputException.class, () -> parser.parse(input)); + } + + @Test + public void parse_exitCommandWithArgs_expectException() { + Parser parser = new Parser(); + final String input = "exit args"; + assertThrows(InvalidInputException.class, () -> parser.parse(input)); + } +} \ No newline at end of file From d565a703c5ba2e7c2a6e374f7e185da5918e8b32 Mon Sep 17 00:00:00 2001 From: Jane Date: Thu, 8 Oct 2020 00:41:06 +0800 Subject: [PATCH 28/43] Fix checkstyle error --- src/test/java/parser/ParserTest.java | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/src/test/java/parser/ParserTest.java b/src/test/java/parser/ParserTest.java index 33145bfe1d..f9d327ca20 100644 --- a/src/test/java/parser/ParserTest.java +++ b/src/test/java/parser/ParserTest.java @@ -10,15 +10,15 @@ class ParserTest { public void parse_addCommandInvalidArgs_expectException() { Parser parser = new Parser(); final String[] inputs = { - "add", - "add ", - "add wrong args format", - // no question and answer separator prefix - "add q:When is v1.0 due? a:15 Oct 2020", - // no question prefix - "add When is v1.0 due? | a:15 Oct 2020", - // no answer prefix - "add q:When is v1.0 due? | 15 Oct 2020", + "add", + "add ", + "add wrong args format", + // no question and answer separator prefix + "add q:When is v1.0 due? a:15 Oct 2020", + // no question prefix + "add When is v1.0 due? | a:15 Oct 2020", + // no answer prefix + "add q:When is v1.0 due? | 15 Oct 2020", }; for (String input : inputs) { assertThrows(InvalidInputException.class, () -> parser.parse(input)); @@ -38,4 +38,4 @@ public void parse_exitCommandWithArgs_expectException() { final String input = "exit args"; assertThrows(InvalidInputException.class, () -> parser.parse(input)); } -} \ No newline at end of file +} From 50d87b0a50d34b11769b64ec6e76462f88df113e Mon Sep 17 00:00:00 2001 From: gua-guargia Date: Thu, 8 Oct 2020 01:46:19 +0800 Subject: [PATCH 29/43] Add structure of the KAJI: admin, module, chapter and cards --- src/main/java/seedu/duke/Duke.java | 30 +++-- src/main/java/seedu/duke/command/Command.java | 4 +- .../seedu/duke/command/addCardCommand.java | 5 +- .../seedu/duke/command/addChapterCommand.java | 11 +- .../seedu/duke/command/addModuleCommand.java | 13 +- .../duke/command/backChapterCommand.java | 16 +++ .../seedu/duke/command/backModuleCommand.java | 5 +- .../seedu/duke/command/goChapterCommand.java | 41 +++++++ .../seedu/duke/command/goModuleCommand.java | 30 ++++- .../seedu/duke/command/randomCommand.java | 16 +++ src/main/java/seedu/duke/level/Admin.java | 22 ++-- src/main/java/seedu/duke/level/Card.java | 4 + src/main/java/seedu/duke/level/Chapter.java | 13 +- src/main/java/seedu/duke/level/Module.java | 22 +++- src/main/java/seedu/duke/tool/Access.java | 106 ++++++++++++---- src/main/java/seedu/duke/tool/Parser.java | 10 +- src/main/java/seedu/duke/tool/Storage.java | 113 ++++++++++++++++++ 17 files changed, 390 insertions(+), 71 deletions(-) create mode 100644 src/main/java/seedu/duke/command/backChapterCommand.java create mode 100644 src/main/java/seedu/duke/command/goChapterCommand.java create mode 100644 src/main/java/seedu/duke/command/randomCommand.java create mode 100644 src/main/java/seedu/duke/tool/Storage.java diff --git a/src/main/java/seedu/duke/Duke.java b/src/main/java/seedu/duke/Duke.java index 8c2883ef40..de0de5af19 100644 --- a/src/main/java/seedu/duke/Duke.java +++ b/src/main/java/seedu/duke/Duke.java @@ -4,22 +4,32 @@ import seedu.duke.level.Admin; import seedu.duke.tool.Access; import seedu.duke.tool.Parser; +import seedu.duke.tool.Storage; import seedu.duke.tool.Ui; -import java.util.Scanner; +import java.io.FileNotFoundException; public class Duke { /** * Main entry-point for the java.duke.Duke application. */ private Ui ui; - private Admin admin; private Access access; + private Storage storage; - public Duke() { + public Duke(String filePath) { ui = new Ui(); - admin = new Admin(); - access = new Access(); + storage = new Storage(filePath); + try { + access = new Access(new Admin(storage.loadModule())); + ui.showLine(); // show the divider line ("_______") + Command c = Parser.parse("list"); + c.execute(access, ui, storage); + ui.showLine(); + } catch (FileNotFoundException e) { + storage.createAdmin(); + access = new Access(); + } } public void run() { @@ -28,8 +38,7 @@ public void run() { String fullCommand = ui.readCommand(); ui.showLine(); // show the divider line ("_______") Command c = Parser.parse(fullCommand); - //System.out.println(fullCommand); - c.execute(access, ui, admin); + c.execute(access, ui, storage); isExit = c.isExit(); ui.showLine(); System.out.println(access.getLevel()); @@ -43,11 +52,6 @@ public static void main(String[] args) { + "| |_| | |_| | < __/\n" + "|____/ \\__,_|_|\\_\\___|\n"; System.out.println("Hello from\n" + logo); - System.out.println("What is your name?"); - - Scanner in = new Scanner(System.in); - System.out.println("Hello " + in.nextLine()); - - new Duke().run(); + new Duke("data/admin").run(); } } diff --git a/src/main/java/seedu/duke/command/Command.java b/src/main/java/seedu/duke/command/Command.java index 72b99ce989..01b754f1c3 100644 --- a/src/main/java/seedu/duke/command/Command.java +++ b/src/main/java/seedu/duke/command/Command.java @@ -1,7 +1,7 @@ package seedu.duke.command; import seedu.duke.tool.Access; -import seedu.duke.level.Admin; +import seedu.duke.tool.Storage; import seedu.duke.tool.Ui; public abstract class Command { @@ -11,7 +11,7 @@ public Command(String fullCommand) { this.fullCommand = fullCommand; } - public abstract void execute(Access access, Ui ui, Admin admin); + public abstract void execute(Access access, Ui ui, Storage storage); public boolean isExit() { return false; diff --git a/src/main/java/seedu/duke/command/addCardCommand.java b/src/main/java/seedu/duke/command/addCardCommand.java index 862d8026ef..8b108f97e6 100644 --- a/src/main/java/seedu/duke/command/addCardCommand.java +++ b/src/main/java/seedu/duke/command/addCardCommand.java @@ -1,7 +1,7 @@ package seedu.duke.command; import seedu.duke.tool.Access; -import seedu.duke.level.Admin; +import seedu.duke.tool.Storage; import seedu.duke.tool.Ui; public class addCardCommand extends Command { @@ -10,7 +10,6 @@ public addCardCommand(String fullCommand) { } @Override - public void execute(Access access, Ui ui, Admin admin) { - + public void execute(Access access, Ui ui, Storage storage) { } } diff --git a/src/main/java/seedu/duke/command/addChapterCommand.java b/src/main/java/seedu/duke/command/addChapterCommand.java index 57996da738..9d57a24dc0 100644 --- a/src/main/java/seedu/duke/command/addChapterCommand.java +++ b/src/main/java/seedu/duke/command/addChapterCommand.java @@ -1,8 +1,10 @@ package seedu.duke.command; +import seedu.duke.level.Admin; +import seedu.duke.level.Chapter; import seedu.duke.level.Module; import seedu.duke.tool.Access; -import seedu.duke.level.Admin; +import seedu.duke.tool.Storage; import seedu.duke.tool.Ui; public class addChapterCommand extends Command { @@ -11,10 +13,13 @@ public addChapterCommand(String fullCommand) { } @Override - public void execute(Access access, Ui ui, Admin admin) { + public void execute(Access access, Ui ui, Storage storage) { String filter = fullCommand.replace("addChapter ", ""); if(access.getModuleLevel() != "") { - admin.addChapter(filter, access.getModuleLevel()); + Module newModule = access.getModule(); + newModule.add(new Chapter(filter)); + access.setModule(newModule); + storage.createChapter(filter, access.getModuleLevel()); } } } diff --git a/src/main/java/seedu/duke/command/addModuleCommand.java b/src/main/java/seedu/duke/command/addModuleCommand.java index 9ca46a7d01..c13b7a3203 100644 --- a/src/main/java/seedu/duke/command/addModuleCommand.java +++ b/src/main/java/seedu/duke/command/addModuleCommand.java @@ -1,8 +1,10 @@ package seedu.duke.command; -import seedu.duke.tool.Access; import seedu.duke.level.Admin; +import seedu.duke.level.Chapter; +import seedu.duke.tool.Access; import seedu.duke.level.Module; +import seedu.duke.tool.Storage; import seedu.duke.tool.Ui; public class addModuleCommand extends Command { @@ -11,8 +13,13 @@ public addModuleCommand(String fullCommand) { } @Override - public void execute(Access access, Ui ui, Admin admin) { + public void execute(Access access, Ui ui, Storage storage) { String filter = fullCommand.replace("addModule ", ""); - admin.addModule(new Module(filter)); + if(access.getModuleLevel() == "") { + Admin newAdmin = access.getAdmin(); + newAdmin.add(new Module(filter)); + access.setAdmin(newAdmin); + storage.createModule(filter); + } } } diff --git a/src/main/java/seedu/duke/command/backChapterCommand.java b/src/main/java/seedu/duke/command/backChapterCommand.java new file mode 100644 index 0000000000..333418bd23 --- /dev/null +++ b/src/main/java/seedu/duke/command/backChapterCommand.java @@ -0,0 +1,16 @@ +package seedu.duke.command; + +import seedu.duke.tool.Access; +import seedu.duke.tool.Storage; +import seedu.duke.tool.Ui; + +public class backChapterCommand extends Command { + public backChapterCommand(String fullCommand) { + super(fullCommand); + } + + @Override + public void execute(Access access, Ui ui, Storage storage) { + access.setChapterLevel(""); + } +} diff --git a/src/main/java/seedu/duke/command/backModuleCommand.java b/src/main/java/seedu/duke/command/backModuleCommand.java index fa9c6e89bd..54f2d98075 100644 --- a/src/main/java/seedu/duke/command/backModuleCommand.java +++ b/src/main/java/seedu/duke/command/backModuleCommand.java @@ -1,8 +1,7 @@ package seedu.duke.command; -import seedu.duke.command.Command; -import seedu.duke.level.Admin; import seedu.duke.tool.Access; +import seedu.duke.tool.Storage; import seedu.duke.tool.Ui; public class backModuleCommand extends Command { @@ -11,7 +10,7 @@ public backModuleCommand(String fullCommand) { } @Override - public void execute(Access access, Ui ui, Admin admin) { + public void execute(Access access, Ui ui, Storage storage) { access.setModuleLevel(""); } } diff --git a/src/main/java/seedu/duke/command/goChapterCommand.java b/src/main/java/seedu/duke/command/goChapterCommand.java new file mode 100644 index 0000000000..ef2e2eb65c --- /dev/null +++ b/src/main/java/seedu/duke/command/goChapterCommand.java @@ -0,0 +1,41 @@ +package seedu.duke.command; + +import seedu.duke.level.Chapter; +import seedu.duke.tool.Access; +import seedu.duke.tool.Storage; +import seedu.duke.tool.Ui; + +import java.io.FileNotFoundException; +import java.util.ArrayList; + +public class goChapterCommand extends Command{ + + public goChapterCommand(String fullCommand) { + super(fullCommand); + } + + @Override + public void execute(Access access, Ui ui, Storage storage) { + String filter = fullCommand.replace("goChapter ", ""); + boolean isLevelExist = false; + ArrayList chapters = access.getModule().getChapter(); + for (Chapter chapter : chapters) { + if(filter.equalsIgnoreCase(chapter.getChapter())) { + access.setModuleLevel(filter); + isLevelExist = true; + try { + Chapter newChapter = new Chapter(chapter.getChapter(), storage.loadCard(access.getModuleLevel(), chapter.getChapter())); + access.setChapter(newChapter); + } catch (FileNotFoundException e) { + Chapter newChapter = new Chapter(chapter.getChapter()); + access.setChapter(newChapter); + System.out.println("Hihi, seems like it is a new module, you can try to add chapter inside!"); + } + break; + } + } + if (isLevelExist == false) { + System.out.println("Sorry, I cannot find this module, please add this module first"); + } + } +} diff --git a/src/main/java/seedu/duke/command/goModuleCommand.java b/src/main/java/seedu/duke/command/goModuleCommand.java index 731fffa23c..a9f3edd7f9 100644 --- a/src/main/java/seedu/duke/command/goModuleCommand.java +++ b/src/main/java/seedu/duke/command/goModuleCommand.java @@ -1,18 +1,40 @@ package seedu.duke.command; -import seedu.duke.command.Command; -import seedu.duke.level.Admin; +import seedu.duke.level.Module; import seedu.duke.tool.Access; +import seedu.duke.tool.Storage; import seedu.duke.tool.Ui; +import java.io.FileNotFoundException; +import java.util.ArrayList; + public class goModuleCommand extends Command { public goModuleCommand(String fullCommand) { super(fullCommand); } @Override - public void execute(Access access, Ui ui, Admin admin) { + public void execute(Access access, Ui ui, Storage storage) { String filter = fullCommand.replace("goModule ", ""); - access.setModuleLevel(filter); + boolean isLevelExist = false; + ArrayList modules = access.getAdmin().getModules(); + for (Module module : modules) { + if(filter.equalsIgnoreCase(module.getModule())) { + access.setModuleLevel(filter); + isLevelExist = true; + try { + Module newModule = new Module(module.getModule(), storage.loadChapter(module.getModule())); + access.setModule(newModule); + } catch (FileNotFoundException e) { + Module newModule = new Module(module.getModule()); + access.setModule(newModule); + System.out.println("Hihi, seems like it is a new module, you can try to add chapter inside!"); + } + break; + } + } + if (isLevelExist == false) { + System.out.println("Sorry, I cannot find this module, please add this module first"); + } } } diff --git a/src/main/java/seedu/duke/command/randomCommand.java b/src/main/java/seedu/duke/command/randomCommand.java new file mode 100644 index 0000000000..3370773a71 --- /dev/null +++ b/src/main/java/seedu/duke/command/randomCommand.java @@ -0,0 +1,16 @@ +package seedu.duke.command; + +import seedu.duke.tool.Access; +import seedu.duke.tool.Storage; +import seedu.duke.tool.Ui; + +public class randomCommand extends Command { + public randomCommand(String fullCommand) { + super(fullCommand); + } + + @Override + public void execute(Access access, Ui ui, Storage storage) { + System.out.println("Sorry, I cannot understand this command"); + } +} diff --git a/src/main/java/seedu/duke/level/Admin.java b/src/main/java/seedu/duke/level/Admin.java index 3ebd939fce..c1877cde5d 100644 --- a/src/main/java/seedu/duke/level/Admin.java +++ b/src/main/java/seedu/duke/level/Admin.java @@ -8,19 +8,25 @@ public class Admin { protected ArrayList modules; protected int moduleAmount = 0; + //if there is no storage public Admin() { - modules = new ArrayList(); + modules = new ArrayList<>(); moduleAmount = 0; } - public void addModule(Module module){ - modules.add(module); - // modules.get(moduleAmount).doneAddModule(); - moduleAmount++; + //if there is storage + public Admin(ArrayList modules) { + this.modules = new ArrayList<>(modules); + moduleAmount = modules.size(); + } + + public ArrayList getModules() { + return modules; } - public void addChapter(String filter, String moduleLevel) { - int index = modules.indexOf(moduleLevel); - modules.get(index).add(new Chapter(filter)); + public void add(Module module) { + modules.add(module); + moduleAmount++; + modules.get(moduleAmount).doneAddModule(); } } diff --git a/src/main/java/seedu/duke/level/Card.java b/src/main/java/seedu/duke/level/Card.java index 21b2fa4a7f..b69a0f2666 100644 --- a/src/main/java/seedu/duke/level/Card.java +++ b/src/main/java/seedu/duke/level/Card.java @@ -11,6 +11,7 @@ public Card(String question, String answer) { totalCard++; } + public String getQuestion() { return question; } @@ -20,5 +21,8 @@ public String getAnswer() { } public void doneAddCard() { + System.out.println(" Got it. I've added this card:"); + System.out.println(" " + getQuestion() + " " + getAnswer()); + System.out.println(" Now you have " + totalCard +" cards in the list."); } } diff --git a/src/main/java/seedu/duke/level/Chapter.java b/src/main/java/seedu/duke/level/Chapter.java index 98da6012ac..0165c2aa93 100644 --- a/src/main/java/seedu/duke/level/Chapter.java +++ b/src/main/java/seedu/duke/level/Chapter.java @@ -15,6 +15,12 @@ public Chapter(String chapterName) { totalChapter++; } + public Chapter(String chapterName, ArrayList cards) { + this.chapterName = chapterName; + this.cards = new ArrayList<>(cards); + cardAmount = cards.size(); + } + public void add(Card card){ cards.add(card); cards.get(cardAmount).doneAddCard(); @@ -22,13 +28,12 @@ public void add(Card card){ } public void doneAddChapter() { - totalChapter++; - System.out.println(" Got it. I've added this task:"); + System.out.println(" Got it. I've added this chapter:"); System.out.println(" " + getChapter()); - System.out.println(" Now you have " + totalChapter +" modules in the list."); + System.out.println(" Now you have " + totalChapter +" chapters in the list."); } - private String getChapter() { + public String getChapter() { return chapterName; } diff --git a/src/main/java/seedu/duke/level/Module.java b/src/main/java/seedu/duke/level/Module.java index 4cfb34b5ff..b07de19d76 100644 --- a/src/main/java/seedu/duke/level/Module.java +++ b/src/main/java/seedu/duke/level/Module.java @@ -6,7 +6,7 @@ public class Module { protected ArrayList chapters; protected int chapterAmount; protected String moduleName; - protected int totalModule = 0; + protected static int totalModule = 0; public Module(String moduleName) { this.moduleName = moduleName; @@ -15,6 +15,13 @@ public Module(String moduleName) { totalModule++; } + public Module(String moduleName, ArrayList chapters) { + this.moduleName = moduleName; + this.chapters = new ArrayList<>(chapters); + chapterAmount = chapters.size(); + totalModule++; + } + public void add(Chapter chapter){ chapters.add(chapter); chapters.get(chapterAmount).doneAddChapter(); @@ -22,13 +29,20 @@ public void add(Chapter chapter){ } public void doneAddModule() { - totalModule++; - System.out.println(" Got it. I've added this task:"); + System.out.println(" Got it. I've added this module:"); System.out.println(" " + getModule()); System.out.println(" Now you have " + totalModule +" modules in the list."); } - private String getModule() { + public String getModule() { return moduleName; } + + public void setChapters(ArrayList chapters) { + this.chapters = chapters; + } + + public ArrayList getChapter() { + return chapters; + } } diff --git a/src/main/java/seedu/duke/tool/Access.java b/src/main/java/seedu/duke/tool/Access.java index b79771b7b3..5f87673822 100644 --- a/src/main/java/seedu/duke/tool/Access.java +++ b/src/main/java/seedu/duke/tool/Access.java @@ -1,30 +1,38 @@ package seedu.duke.tool; +import seedu.duke.level.Admin; +import seedu.duke.level.Chapter; +import seedu.duke.level.Module; + +import java.util.ArrayList; + public class Access { protected String level; protected String adminLevel; protected String moduleLevel; protected String chapterLevel; - protected String cardLevel; + protected Chapter chapter; + protected Module module; + protected Admin admin; - public Access(String level) { - this.level = level; + public Access(Admin admin) { + this.admin = admin; + this.module = null; + this.chapter = null; + this.level = "admin"; + this.adminLevel = "admin"; + this.moduleLevel = ""; + this.chapterLevel = ""; } public Access() { this.level = "admin"; this.adminLevel = "admin"; - this.cardLevel = ""; this.moduleLevel = ""; this.chapterLevel = ""; - } - - public void incrementLevel(String newLevel) { - this.level = level + "/" + newLevel; - } - - public void decreaseLevel(String currentLevel) { - level.replace("/" + currentLevel, ""); + this.module = null; + this.chapter = null; + this.admin = new Admin(); } public String getModuleLevel() { @@ -35,26 +43,78 @@ public String getLevel() { return level; } - public String getAdminLevel() { - return adminLevel; - } - public String getChapterLevel() { return chapterLevel; } - public String getCardLevel() { - return cardLevel; + public Module getModule() { + return module; + } + + public Chapter getChapter() { + return chapter; + } + + public Admin getAdmin() { + return admin; + } + + public void setAdmin(Admin admin) { + this.admin = admin; + } + + public void setModule(Module module) { + this.module = module; + } + + public void setChapter(Chapter chapter) { + this.chapter = chapter; } public void setModuleLevel(String moduleLevel) { - if(moduleLevel != "") { + if(this.chapterLevel != "") { + System.out.println("Sorry, you currently are in the chapter level, please go back to admin level first."); + } + else if (this.moduleLevel != "") { + if (moduleLevel == "") { + String replacement = "/" + this.moduleLevel; + this.level = level.replace(replacement, ""); + this.moduleLevel = moduleLevel; + this.module = null; + } + else { + System.out.println("Sorry, you are already in the module level, please go back to admin level first."); + } + } + else { this.moduleLevel = moduleLevel; this.level = level + "/" + moduleLevel; - return; + this.module = new Module(moduleLevel); } - String replacement = "/" + this.moduleLevel; - this.level = level.replace(replacement, ""); - this.moduleLevel = moduleLevel; } + + public void setChapterLevel(String chapterLevel) { + if(this.moduleLevel == "") { //wrong level + //throw new Exception(); + System.out.println("Sorry, you currently are in the admin level, please enter module level first."); + } + else if(this.chapterLevel != "") { + if(chapterLevel == "") { //correct level, correct command + this.chapterLevel = chapterLevel; + this.level = level + "/" + chapterLevel; + this.chapter = new Chapter(chapterLevel); + } + else { + System.out.println("Sorry, you are already in the chapter level, " + + "please go back to module level first."); + } + } + else{ //go back + String replacement = "/" + this.chapterLevel; + this.level = level.replace(replacement, ""); + this.chapterLevel = chapterLevel; + this.chapter = null; + } + } + } diff --git a/src/main/java/seedu/duke/tool/Parser.java b/src/main/java/seedu/duke/tool/Parser.java index 5d84c9cfeb..27697aee55 100644 --- a/src/main/java/seedu/duke/tool/Parser.java +++ b/src/main/java/seedu/duke/tool/Parser.java @@ -24,9 +24,17 @@ else if(fullCommand.toLowerCase().contains("addcard")) { else if(fullCommand.toLowerCase().contains("gomodule")) { return new goModuleCommand(fullCommand); } + else if(fullCommand.toLowerCase().contains("gochapter")) { + return new goChapterCommand(fullCommand); + } else if(fullCommand.toLowerCase().contains("backmodule")) { return new backModuleCommand(fullCommand); } - return null; + else if(fullCommand.toLowerCase().contains("backchapter")) { + return new backChapterCommand(fullCommand); + } + else { + return new randomCommand(fullCommand); + } } } \ No newline at end of file diff --git a/src/main/java/seedu/duke/tool/Storage.java b/src/main/java/seedu/duke/tool/Storage.java new file mode 100644 index 0000000000..ac0c59b525 --- /dev/null +++ b/src/main/java/seedu/duke/tool/Storage.java @@ -0,0 +1,113 @@ +package seedu.duke.tool; + +import seedu.duke.level.Card; +import seedu.duke.level.Chapter; +import seedu.duke.level.Module; + +import java.io.File; +import java.io.FileNotFoundException; +import java.io.FileWriter; +import java.io.IOException; +import java.util.ArrayList; +import java.util.Scanner; + +public class Storage { + protected String filePath; + + public Storage(String filePath){ + this.filePath = filePath; + } + + //create the folder --> 'data/admin' + public void createAdmin() { + File f = new File(filePath); + boolean success = f.getParentFile().mkdir(); + System.out.println(" "+ filePath); + if(success) { + System.out.println(" Successfully created new directory"); + } + else { + System.out.println(" Failed to create new directory"); + } + } + + public void createModule(String moduleName) { + File f = new File(filePath + "/" + moduleName); + boolean success = f.getParentFile().mkdir(); + if(success) { + System.out.println(" Successfully created new directory " + moduleName); + } + else { + System.out.println(" Failed to create new directory"); + } + } + + public void createChapter(String chapterName, String moduleName) { + File f = new File(filePath + "/" + moduleName + "/" + chapterName + ".txt"); + boolean success = f.getParentFile().mkdir(); + if(success) { + System.out.println(" Successfully created new directory " + chapterName); + } + else { + System.out.println(" Failed to create new directory"); + } + } + + public ArrayList loadModule() throws FileNotFoundException { + File f = new File(filePath); + ArrayList modules = new ArrayList(); + Scanner s = new Scanner(f); + int totalModule = 0; + + String contents[] = f.list(); + System.out.println("List of files and directories in the specified directory:"); + for(int i=0; i loadChapter(String module) throws FileNotFoundException { + File f = new File(filePath + "/" + module); + ArrayList chapters = new ArrayList(); + Scanner s = new Scanner(f); + int totalChapter = 0; + String contents[] = f.list(); + System.out.println("List of files and directories in the specified directory:"); + for(int i=0; i loadCard(String module, String chapter) throws FileNotFoundException { + File f = new File(filePath + "/" + module + "/" + chapter + ".txt"); + ArrayList cards = new ArrayList(); + Scanner s = new Scanner(f); + int totalCards = 0; + while (s.hasNext()) { + //to read the card + } + return cards; + } + + +/* + + public void writeFile(ArrayList taskList, int taskAmount) throws IOException { + FileWriter fw = new FileWriter(filePath, false); + fw.write(""); + fw.close(); + + for(int i=0; i Date: Thu, 8 Oct 2020 14:09:39 +0800 Subject: [PATCH 30/43] Implement access flashcard deck by deck --- src/main/java/Kaji.java | 23 +++- .../{seedu/duke/tool => access}/Access.java | 37 +++--- src/main/java/commands/AddCommand.java | 15 ++- src/main/java/commands/Command.java | 5 +- src/main/java/commands/ExitCommand.java | 4 +- src/main/java/commands/HelpCommand.java | 4 +- src/main/java/commands/ListCommand.java | 4 +- src/main/java/commands/ReviseCommand.java | 4 +- src/main/java/commands/addChapterCommand.java | 35 ++++++ src/main/java/commands/addModuleCommand.java | 36 ++++++ .../java/commands/backChapterCommand.java | 20 ++++ src/main/java/commands/backModuleCommand.java | 20 ++++ .../goChapterCommand.java | 33 +++-- .../command => commands}/goModuleCommand.java | 30 +++-- .../duke/level => manager/admin}/Admin.java | 6 +- src/main/java/manager/chapter/Chapter.java | 33 ++++- .../duke/level => manager/module}/Module.java | 6 +- src/main/java/parser/Parser.java | 61 +++++++++- src/main/java/seedu/duke/Duke.java | 57 --------- src/main/java/seedu/duke/command/Command.java | 19 --- .../seedu/duke/command/addCardCommand.java | 15 --- .../seedu/duke/command/addChapterCommand.java | 25 ---- .../seedu/duke/command/addModuleCommand.java | 25 ---- .../duke/command/backChapterCommand.java | 16 --- .../seedu/duke/command/backModuleCommand.java | 16 --- .../seedu/duke/command/randomCommand.java | 16 --- src/main/java/seedu/duke/level/Card.java | 28 ----- src/main/java/seedu/duke/level/Chapter.java | 40 ------- src/main/java/seedu/duke/tool/Parser.java | 40 ------- src/main/java/seedu/duke/tool/Storage.java | 113 ------------------ src/main/java/seedu/duke/tool/Ui.java | 33 ----- src/main/java/storage/Storage.java | 95 ++++++++++++++- src/main/java/ui/Ui.java | 10 ++ 33 files changed, 411 insertions(+), 513 deletions(-) rename src/main/java/{seedu/duke/tool => access}/Access.java (79%) create mode 100644 src/main/java/commands/addChapterCommand.java create mode 100644 src/main/java/commands/addModuleCommand.java create mode 100644 src/main/java/commands/backChapterCommand.java create mode 100644 src/main/java/commands/backModuleCommand.java rename src/main/java/{seedu/duke/command => commands}/goChapterCommand.java (55%) rename src/main/java/{seedu/duke/command => commands}/goModuleCommand.java (63%) rename src/main/java/{seedu/duke/level => manager/admin}/Admin.java (91%) rename src/main/java/{seedu/duke/level => manager/module}/Module.java (91%) delete mode 100644 src/main/java/seedu/duke/Duke.java delete mode 100644 src/main/java/seedu/duke/command/Command.java delete mode 100644 src/main/java/seedu/duke/command/addCardCommand.java delete mode 100644 src/main/java/seedu/duke/command/addChapterCommand.java delete mode 100644 src/main/java/seedu/duke/command/addModuleCommand.java delete mode 100644 src/main/java/seedu/duke/command/backChapterCommand.java delete mode 100644 src/main/java/seedu/duke/command/backModuleCommand.java delete mode 100644 src/main/java/seedu/duke/command/randomCommand.java delete mode 100644 src/main/java/seedu/duke/level/Card.java delete mode 100644 src/main/java/seedu/duke/level/Chapter.java delete mode 100644 src/main/java/seedu/duke/tool/Parser.java delete mode 100644 src/main/java/seedu/duke/tool/Storage.java delete mode 100644 src/main/java/seedu/duke/tool/Ui.java diff --git a/src/main/java/Kaji.java b/src/main/java/Kaji.java index f0a2880e4d..a7c9071eca 100644 --- a/src/main/java/Kaji.java +++ b/src/main/java/Kaji.java @@ -1,19 +1,35 @@ +import access.Access; import commands.Command; import exception.InvalidInputException; +import manager.admin.Admin; import manager.chapter.CardList; import parser.Parser; import storage.Storage; import ui.Ui; +import java.io.FileNotFoundException; import java.io.IOException; public class Kaji { private CardList cards; private Ui ui; + private Access access; + private Storage storage; - public Kaji() { + + public Kaji(String filePath) { ui = new Ui(); cards = new CardList(); + storage = new Storage(filePath); + try { + Admin admin = new Admin(storage.loadModule()); + access = new Access(admin); + ui.showLine(); // show the divider line ("_______") + ui.showLine(); + } catch (FileNotFoundException e) { + storage.createAdmin(); + access = new Access(); + } } public void run() { @@ -23,9 +39,10 @@ public void run() { Storage.getFileContents(cards); while (!isExit) { try { + ui.showLevel(access); String fullCommand = ui.readCommand(); Command c = Parser.parse(fullCommand); - c.execute(cards, ui); + c.execute(cards, ui, access, storage); ui.printEmptyLine(); Storage.writeToFile(cards); isExit = c.isExit(); @@ -38,6 +55,6 @@ public void run() { } public static void main(String[] args) { - new Kaji().run(); + new Kaji("data/admin").run(); } } diff --git a/src/main/java/seedu/duke/tool/Access.java b/src/main/java/access/Access.java similarity index 79% rename from src/main/java/seedu/duke/tool/Access.java rename to src/main/java/access/Access.java index 5f87673822..8b392aa0a6 100644 --- a/src/main/java/seedu/duke/tool/Access.java +++ b/src/main/java/access/Access.java @@ -1,10 +1,8 @@ -package seedu.duke.tool; +package access; -import seedu.duke.level.Admin; -import seedu.duke.level.Chapter; -import seedu.duke.level.Module; - -import java.util.ArrayList; +import manager.admin.Admin; +import manager.chapter.Chapter; +import manager.module.Module; public class Access { protected String level; @@ -95,25 +93,26 @@ else if (this.moduleLevel != "") { public void setChapterLevel(String chapterLevel) { if(this.moduleLevel == "") { //wrong level - //throw new Exception(); System.out.println("Sorry, you currently are in the admin level, please enter module level first."); } - else if(this.chapterLevel != "") { - if(chapterLevel == "") { //correct level, correct command + else { + if(this.chapterLevel != "") { + if(chapterLevel == "") { + String replacement = "/" + this.chapterLevel; + this.level = level.replace(replacement, ""); + this.chapterLevel = chapterLevel; + this.chapter = null; + } + else { + System.out.println("Sorry, you are already in the chapter level, " + + "please go back to module level first."); + } + } + else { this.chapterLevel = chapterLevel; this.level = level + "/" + chapterLevel; this.chapter = new Chapter(chapterLevel); } - else { - System.out.println("Sorry, you are already in the chapter level, " + - "please go back to module level first."); - } - } - else{ //go back - String replacement = "/" + this.chapterLevel; - this.level = level.replace(replacement, ""); - this.chapterLevel = chapterLevel; - this.chapter = null; } } diff --git a/src/main/java/commands/AddCommand.java b/src/main/java/commands/AddCommand.java index 781d5ffc74..145f58f947 100644 --- a/src/main/java/commands/AddCommand.java +++ b/src/main/java/commands/AddCommand.java @@ -1,7 +1,9 @@ package commands; +import access.Access; import manager.card.Card; import manager.chapter.CardList; +import storage.Storage; import ui.Ui; public class AddCommand extends Command { @@ -22,10 +24,15 @@ public AddCommand(String question, String answer) { } @Override - public void execute(CardList cards, Ui ui) { - cards.addCard(card); - int cardCount = cards.getCardCount(); - ui.showCardAdded(cards.getCard(cardCount - 1), cardCount); + public void execute(CardList cards, Ui ui, Access access, Storage storage) { + if(access.getChapterLevel() != "") { + cards.addCard(card); + int cardCount = cards.getCardCount(); + ui.showCardAdded(cards.getCard(cardCount - 1), cardCount); + } + else { + System.out.println("Sorry, you currently are in the wrong level, please enter chapter level first."); + } } @Override diff --git a/src/main/java/commands/Command.java b/src/main/java/commands/Command.java index 61529438a4..b20f77ff33 100644 --- a/src/main/java/commands/Command.java +++ b/src/main/java/commands/Command.java @@ -1,10 +1,13 @@ package commands; +import access.Access; import manager.chapter.CardList; +import storage.Storage; import ui.Ui; public abstract class Command { - public abstract void execute(CardList cards, Ui ui); + + public abstract void execute(CardList cards, Ui ui, Access access, Storage storage); public abstract boolean isExit(); } diff --git a/src/main/java/commands/ExitCommand.java b/src/main/java/commands/ExitCommand.java index f943ff3946..94fc24a056 100644 --- a/src/main/java/commands/ExitCommand.java +++ b/src/main/java/commands/ExitCommand.java @@ -1,6 +1,8 @@ package commands; +import access.Access; import manager.chapter.CardList; +import storage.Storage; import ui.Ui; public class ExitCommand extends Command { @@ -10,7 +12,7 @@ public class ExitCommand extends Command { + "Example: " + COMMAND_WORD + "\n"; @Override - public void execute(CardList cards, Ui ui) { + public void execute(CardList cards, Ui ui, Access access, Storage storage) { ui.showExit(); } diff --git a/src/main/java/commands/HelpCommand.java b/src/main/java/commands/HelpCommand.java index ae336211ca..9ab99c8738 100644 --- a/src/main/java/commands/HelpCommand.java +++ b/src/main/java/commands/HelpCommand.java @@ -1,6 +1,8 @@ package commands; +import access.Access; import manager.chapter.CardList; +import storage.Storage; import ui.Ui; public class HelpCommand extends Command { @@ -10,7 +12,7 @@ public class HelpCommand extends Command { + "Example: " + COMMAND_WORD + "\n"; @Override - public void execute(CardList cards, Ui ui) { + public void execute(CardList cards, Ui ui, Access access, Storage storage) { ui.showHelpList(); } diff --git a/src/main/java/commands/ListCommand.java b/src/main/java/commands/ListCommand.java index 1e39a2eed3..537d721f7e 100644 --- a/src/main/java/commands/ListCommand.java +++ b/src/main/java/commands/ListCommand.java @@ -1,7 +1,9 @@ package commands; +import access.Access; import manager.card.Card; import manager.chapter.CardList; +import storage.Storage; import ui.Ui; import java.util.ArrayList; @@ -13,7 +15,7 @@ public class ListCommand extends Command { + "Example: " + COMMAND_WORD + "\n"; @Override - public void execute(CardList cards, Ui ui) { + public void execute(CardList cards, Ui ui, Access access, Storage storage) { ArrayList allCards = cards.getAllCards(); int cardCount = cards.getCardCount(); ui.showCardList(allCards, cardCount); diff --git a/src/main/java/commands/ReviseCommand.java b/src/main/java/commands/ReviseCommand.java index b9b53310a5..54494d9d7a 100644 --- a/src/main/java/commands/ReviseCommand.java +++ b/src/main/java/commands/ReviseCommand.java @@ -1,9 +1,11 @@ package commands; +import access.Access; import manager.card.Card; import manager.chapter.CardList; import manager.chapter.Chapter; import scheduler.Scheduler; +import storage.Storage; import ui.Ui; import java.util.ArrayList; @@ -36,7 +38,7 @@ public ReviseCommand(String toRevise) { } @Override - public void execute(CardList cards, Ui ui) { + public void execute(CardList cards, Ui ui, Access access, Storage storage) { ArrayList allCards = cards.getAllCards(); ArrayList repeatCards = new ArrayList<>(); int cardCount = cards.getCardCount(); diff --git a/src/main/java/commands/addChapterCommand.java b/src/main/java/commands/addChapterCommand.java new file mode 100644 index 0000000000..321d94e3b7 --- /dev/null +++ b/src/main/java/commands/addChapterCommand.java @@ -0,0 +1,35 @@ +package commands; + +import manager.chapter.CardList; +import manager.chapter.Chapter; +import manager.module.Module; +import access.Access; +import storage.Storage; +import ui.Ui; + +public class addChapterCommand extends Command { + public static final String COMMAND_WORD = "addchapter"; + String chapterCode; + + public addChapterCommand(String chapterCode) { + this.chapterCode = chapterCode; + } + + @Override + public void execute(CardList cards, Ui ui, Access access, Storage storage) { + if(access.getModuleLevel() != "") { + Module newModule = access.getModule(); + newModule.add(new Chapter(chapterCode)); + access.setModule(newModule); + storage.createChapter(chapterCode, access.getModuleLevel()); + } + else { + System.out.println("Sorry, you currently are in the admin level, please enter module level first."); + } + } + + @Override + public boolean isExit() { + return false; + } +} diff --git a/src/main/java/commands/addModuleCommand.java b/src/main/java/commands/addModuleCommand.java new file mode 100644 index 0000000000..5e9bf46c93 --- /dev/null +++ b/src/main/java/commands/addModuleCommand.java @@ -0,0 +1,36 @@ +package commands; + +import access.Access; +import manager.admin.Admin; +import manager.chapter.CardList; +import manager.module.Module; +import storage.Storage; +import ui.Ui; + +public class addModuleCommand extends Command { + public static final String COMMAND_WORD = "addmodule"; + String moduleCode; + + public addModuleCommand(String moduleCode) { + this.moduleCode = moduleCode; + } + + @Override + public void execute(CardList cards, Ui ui, Access access, Storage storage) { + if(access.getModuleLevel() == "") { + Admin newAdmin = access.getAdmin(); + newAdmin.add(new Module(moduleCode)); + access.setAdmin(newAdmin); + storage.createModule(moduleCode); + } + else { + System.out.println("Sorry, you currently are in the module/chapter level, " + + "please go back to Admin level first."); + } + } + + @Override + public boolean isExit() { + return false; + } +} diff --git a/src/main/java/commands/backChapterCommand.java b/src/main/java/commands/backChapterCommand.java new file mode 100644 index 0000000000..f05550decc --- /dev/null +++ b/src/main/java/commands/backChapterCommand.java @@ -0,0 +1,20 @@ +package commands; + +import access.Access; +import manager.chapter.CardList; +import storage.Storage; +import ui.Ui; + +public class backChapterCommand extends Command { + public static final String COMMAND_WORD = "backchapter"; + + @Override + public void execute(CardList cards, Ui ui, Access access, Storage storage) { + access.setChapterLevel(""); + } + + @Override + public boolean isExit() { + return false; + } +} diff --git a/src/main/java/commands/backModuleCommand.java b/src/main/java/commands/backModuleCommand.java new file mode 100644 index 0000000000..63af9cda59 --- /dev/null +++ b/src/main/java/commands/backModuleCommand.java @@ -0,0 +1,20 @@ +package commands; + +import access.Access; +import manager.chapter.CardList; +import storage.Storage; +import ui.Ui; + +public class backModuleCommand extends Command { + public static final String COMMAND_WORD = "backmodule"; + + @Override + public void execute(CardList cards, Ui ui, Access access, Storage storage) { + access.setModuleLevel(""); + } + + @Override + public boolean isExit() { + return false; + } +} diff --git a/src/main/java/seedu/duke/command/goChapterCommand.java b/src/main/java/commands/goChapterCommand.java similarity index 55% rename from src/main/java/seedu/duke/command/goChapterCommand.java rename to src/main/java/commands/goChapterCommand.java index ef2e2eb65c..46aff02363 100644 --- a/src/main/java/seedu/duke/command/goChapterCommand.java +++ b/src/main/java/commands/goChapterCommand.java @@ -1,27 +1,29 @@ -package seedu.duke.command; +package commands; -import seedu.duke.level.Chapter; -import seedu.duke.tool.Access; -import seedu.duke.tool.Storage; -import seedu.duke.tool.Ui; +import manager.chapter.CardList; +import access.Access; +import manager.chapter.Chapter; +import storage.Storage; +import ui.Ui; import java.io.FileNotFoundException; import java.util.ArrayList; -public class goChapterCommand extends Command{ +public class goChapterCommand extends Command { + public static final String COMMAND_WORD = "gochapter"; + String chapterCode; - public goChapterCommand(String fullCommand) { - super(fullCommand); + public goChapterCommand(String chapterCode) { + this.chapterCode = chapterCode; } @Override - public void execute(Access access, Ui ui, Storage storage) { - String filter = fullCommand.replace("goChapter ", ""); + public void execute(CardList cards, Ui ui, Access access, Storage storage) { boolean isLevelExist = false; ArrayList chapters = access.getModule().getChapter(); for (Chapter chapter : chapters) { - if(filter.equalsIgnoreCase(chapter.getChapter())) { - access.setModuleLevel(filter); + if(chapterCode.equalsIgnoreCase(chapter.getChapter())) { + access.setChapterLevel(chapterCode); isLevelExist = true; try { Chapter newChapter = new Chapter(chapter.getChapter(), storage.loadCard(access.getModuleLevel(), chapter.getChapter())); @@ -35,7 +37,12 @@ public void execute(Access access, Ui ui, Storage storage) { } } if (isLevelExist == false) { - System.out.println("Sorry, I cannot find this module, please add this module first"); + System.out.println("Sorry, I cannot find this chapter, please add this chapter first"); } } + + @Override + public boolean isExit() { + return false; + } } diff --git a/src/main/java/seedu/duke/command/goModuleCommand.java b/src/main/java/commands/goModuleCommand.java similarity index 63% rename from src/main/java/seedu/duke/command/goModuleCommand.java rename to src/main/java/commands/goModuleCommand.java index a9f3edd7f9..ad9bf7ffd4 100644 --- a/src/main/java/seedu/duke/command/goModuleCommand.java +++ b/src/main/java/commands/goModuleCommand.java @@ -1,26 +1,29 @@ -package seedu.duke.command; +package commands; -import seedu.duke.level.Module; -import seedu.duke.tool.Access; -import seedu.duke.tool.Storage; -import seedu.duke.tool.Ui; +import access.Access; +import manager.chapter.CardList; +import manager.module.Module; +import storage.Storage; +import ui.Ui; import java.io.FileNotFoundException; import java.util.ArrayList; public class goModuleCommand extends Command { - public goModuleCommand(String fullCommand) { - super(fullCommand); + public static final String COMMAND_WORD = "gomodule"; + String moduleCode; + + public goModuleCommand(String moduleCode) { + this.moduleCode = moduleCode; } @Override - public void execute(Access access, Ui ui, Storage storage) { - String filter = fullCommand.replace("goModule ", ""); + public void execute(CardList cards, Ui ui, Access access, Storage storage) { boolean isLevelExist = false; ArrayList modules = access.getAdmin().getModules(); for (Module module : modules) { - if(filter.equalsIgnoreCase(module.getModule())) { - access.setModuleLevel(filter); + if(moduleCode.equalsIgnoreCase(module.getModule())) { + access.setModuleLevel(moduleCode); isLevelExist = true; try { Module newModule = new Module(module.getModule(), storage.loadChapter(module.getModule())); @@ -37,4 +40,9 @@ public void execute(Access access, Ui ui, Storage storage) { System.out.println("Sorry, I cannot find this module, please add this module first"); } } + + @Override + public boolean isExit() { + return false; + } } diff --git a/src/main/java/seedu/duke/level/Admin.java b/src/main/java/manager/admin/Admin.java similarity index 91% rename from src/main/java/seedu/duke/level/Admin.java rename to src/main/java/manager/admin/Admin.java index c1877cde5d..87fa4f02d4 100644 --- a/src/main/java/seedu/duke/level/Admin.java +++ b/src/main/java/manager/admin/Admin.java @@ -1,6 +1,6 @@ -package seedu.duke.level; +package manager.admin; -import seedu.duke.level.Module; +import manager.module.Module; import java.util.ArrayList; @@ -26,7 +26,7 @@ public ArrayList getModules() { public void add(Module module) { modules.add(module); - moduleAmount++; modules.get(moduleAmount).doneAddModule(); + moduleAmount++; } } diff --git a/src/main/java/manager/chapter/Chapter.java b/src/main/java/manager/chapter/Chapter.java index 42a2d6a8a4..88d498783b 100644 --- a/src/main/java/manager/chapter/Chapter.java +++ b/src/main/java/manager/chapter/Chapter.java @@ -1,14 +1,45 @@ package manager.chapter; +import manager.card.Card; + +import java.util.ArrayList; + public class Chapter { - private String chapterName; + protected String chapterName; + protected ArrayList cards; + protected int cardAmount; + protected static int totalChapter = 0; public Chapter(String chapterName) { this.chapterName = chapterName; + cards = new ArrayList(); + cardAmount = 0; + totalChapter++; } @Override public String toString() { return "<" + chapterName + ">"; } + + public Chapter(String chapterName, ArrayList cards) { + this.chapterName = chapterName; + this.cards = new ArrayList<>(cards); + cardAmount = cards.size(); + } + + public void add(Card card){ + cards.add(card); + cardAmount++; + } + + public void doneAddChapter() { + System.out.println(" Got it. I've added this chapter:"); + System.out.println(" " + getChapter()); + System.out.println(" Now you have " + totalChapter +" chapters in the list."); + } + + public String getChapter() { + return chapterName; + } } diff --git a/src/main/java/seedu/duke/level/Module.java b/src/main/java/manager/module/Module.java similarity index 91% rename from src/main/java/seedu/duke/level/Module.java rename to src/main/java/manager/module/Module.java index b07de19d76..d4e84128d9 100644 --- a/src/main/java/seedu/duke/level/Module.java +++ b/src/main/java/manager/module/Module.java @@ -1,4 +1,6 @@ -package seedu.duke.level; +package manager.module; + +import manager.chapter.Chapter; import java.util.ArrayList; @@ -42,7 +44,7 @@ public void setChapters(ArrayList chapters) { this.chapters = chapters; } - public ArrayList getChapter() { + public ArrayList getChapter() { return chapters; } } diff --git a/src/main/java/parser/Parser.java b/src/main/java/parser/Parser.java index e6ca513d55..76ca912357 100644 --- a/src/main/java/parser/Parser.java +++ b/src/main/java/parser/Parser.java @@ -1,11 +1,6 @@ package parser; -import commands.AddCommand; -import commands.Command; -import commands.ExitCommand; -import commands.ListCommand; -import commands.ReviseCommand; -import commands.HelpCommand; +import commands.*; import exception.InvalidFileFormatException; import exception.InvalidInputException; import manager.chapter.Chapter; @@ -29,11 +24,65 @@ public static Command parse(String fullCommand) throws InvalidInputException { return prepareExit(commandArgs); case HelpCommand.COMMAND_WORD: return prepareHelp(commandArgs); + case addModuleCommand.COMMAND_WORD: + return prepareAddModule(commandArgs); + case addChapterCommand.COMMAND_WORD: + return prepareAddChapter(commandArgs); + case backModuleCommand.COMMAND_WORD: + return prepareBackModule(commandArgs); + case backChapterCommand.COMMAND_WORD: + return prepareBackChapter(commandArgs); + case goModuleCommand.COMMAND_WORD: + return prepareGoModule(commandArgs); + case goChapterCommand.COMMAND_WORD: + return prepareGoChapter(commandArgs); default: throw new InvalidInputException(); } } + private static Command prepareGoChapter(String commandArgs) throws InvalidInputException { + if (commandArgs.isEmpty()) { + throw new InvalidInputException(); + } + return new goChapterCommand(commandArgs); + } + + private static Command prepareGoModule(String commandArgs) throws InvalidInputException { + if (commandArgs.isEmpty()) { + throw new InvalidInputException(); + } + return new goModuleCommand(commandArgs); + } + + private static Command prepareBackChapter(String commandArgs) throws InvalidInputException { + if (!commandArgs.isEmpty()) { + throw new InvalidInputException(); + } + return new backChapterCommand(); + } + + private static Command prepareBackModule(String commandArgs) throws InvalidInputException { + if (!commandArgs.isEmpty()) { + throw new InvalidInputException(); + } + return new backModuleCommand(); + } + + private static Command prepareAddChapter(String commandArgs) throws InvalidInputException { + if (commandArgs.isEmpty()) { + throw new InvalidInputException(); + } + return new addChapterCommand(commandArgs); + } + + private static Command prepareAddModule(String commandArgs) throws InvalidInputException { + if (commandArgs.isEmpty()) { + throw new InvalidInputException(); + } + return new addModuleCommand(commandArgs); + } + private static String[] splitCommandTypeAndArgs(String userCommand) { String[] commandTypeAndParams = userCommand.trim().split(" ", 2); if (commandTypeAndParams.length != 2) { diff --git a/src/main/java/seedu/duke/Duke.java b/src/main/java/seedu/duke/Duke.java deleted file mode 100644 index de0de5af19..0000000000 --- a/src/main/java/seedu/duke/Duke.java +++ /dev/null @@ -1,57 +0,0 @@ -package seedu.duke; - -import seedu.duke.command.Command; -import seedu.duke.level.Admin; -import seedu.duke.tool.Access; -import seedu.duke.tool.Parser; -import seedu.duke.tool.Storage; -import seedu.duke.tool.Ui; - -import java.io.FileNotFoundException; - -public class Duke { - /** - * Main entry-point for the java.duke.Duke application. - */ - private Ui ui; - private Access access; - private Storage storage; - - public Duke(String filePath) { - ui = new Ui(); - storage = new Storage(filePath); - try { - access = new Access(new Admin(storage.loadModule())); - ui.showLine(); // show the divider line ("_______") - Command c = Parser.parse("list"); - c.execute(access, ui, storage); - ui.showLine(); - } catch (FileNotFoundException e) { - storage.createAdmin(); - access = new Access(); - } - } - - public void run() { - boolean isExit = false; - while (!isExit) { - String fullCommand = ui.readCommand(); - ui.showLine(); // show the divider line ("_______") - Command c = Parser.parse(fullCommand); - c.execute(access, ui, storage); - isExit = c.isExit(); - ui.showLine(); - System.out.println(access.getLevel()); - } - } - - public static void main(String[] args) { - String logo = " ____ _ \n" - + "| _ \\ _ _| | _____ \n" - + "| | | | | | | |/ / _ \\\n" - + "| |_| | |_| | < __/\n" - + "|____/ \\__,_|_|\\_\\___|\n"; - System.out.println("Hello from\n" + logo); - new Duke("data/admin").run(); - } -} diff --git a/src/main/java/seedu/duke/command/Command.java b/src/main/java/seedu/duke/command/Command.java deleted file mode 100644 index 01b754f1c3..0000000000 --- a/src/main/java/seedu/duke/command/Command.java +++ /dev/null @@ -1,19 +0,0 @@ -package seedu.duke.command; - -import seedu.duke.tool.Access; -import seedu.duke.tool.Storage; -import seedu.duke.tool.Ui; - -public abstract class Command { - String fullCommand; - - public Command(String fullCommand) { - this.fullCommand = fullCommand; - } - - public abstract void execute(Access access, Ui ui, Storage storage); - - public boolean isExit() { - return false; - }; -} diff --git a/src/main/java/seedu/duke/command/addCardCommand.java b/src/main/java/seedu/duke/command/addCardCommand.java deleted file mode 100644 index 8b108f97e6..0000000000 --- a/src/main/java/seedu/duke/command/addCardCommand.java +++ /dev/null @@ -1,15 +0,0 @@ -package seedu.duke.command; - -import seedu.duke.tool.Access; -import seedu.duke.tool.Storage; -import seedu.duke.tool.Ui; - -public class addCardCommand extends Command { - public addCardCommand(String fullCommand) { - super(fullCommand); - } - - @Override - public void execute(Access access, Ui ui, Storage storage) { - } -} diff --git a/src/main/java/seedu/duke/command/addChapterCommand.java b/src/main/java/seedu/duke/command/addChapterCommand.java deleted file mode 100644 index 9d57a24dc0..0000000000 --- a/src/main/java/seedu/duke/command/addChapterCommand.java +++ /dev/null @@ -1,25 +0,0 @@ -package seedu.duke.command; - -import seedu.duke.level.Admin; -import seedu.duke.level.Chapter; -import seedu.duke.level.Module; -import seedu.duke.tool.Access; -import seedu.duke.tool.Storage; -import seedu.duke.tool.Ui; - -public class addChapterCommand extends Command { - public addChapterCommand(String fullCommand) { - super(fullCommand); - } - - @Override - public void execute(Access access, Ui ui, Storage storage) { - String filter = fullCommand.replace("addChapter ", ""); - if(access.getModuleLevel() != "") { - Module newModule = access.getModule(); - newModule.add(new Chapter(filter)); - access.setModule(newModule); - storage.createChapter(filter, access.getModuleLevel()); - } - } -} diff --git a/src/main/java/seedu/duke/command/addModuleCommand.java b/src/main/java/seedu/duke/command/addModuleCommand.java deleted file mode 100644 index c13b7a3203..0000000000 --- a/src/main/java/seedu/duke/command/addModuleCommand.java +++ /dev/null @@ -1,25 +0,0 @@ -package seedu.duke.command; - -import seedu.duke.level.Admin; -import seedu.duke.level.Chapter; -import seedu.duke.tool.Access; -import seedu.duke.level.Module; -import seedu.duke.tool.Storage; -import seedu.duke.tool.Ui; - -public class addModuleCommand extends Command { - public addModuleCommand(String fullCommand) { - super(fullCommand); - } - - @Override - public void execute(Access access, Ui ui, Storage storage) { - String filter = fullCommand.replace("addModule ", ""); - if(access.getModuleLevel() == "") { - Admin newAdmin = access.getAdmin(); - newAdmin.add(new Module(filter)); - access.setAdmin(newAdmin); - storage.createModule(filter); - } - } -} diff --git a/src/main/java/seedu/duke/command/backChapterCommand.java b/src/main/java/seedu/duke/command/backChapterCommand.java deleted file mode 100644 index 333418bd23..0000000000 --- a/src/main/java/seedu/duke/command/backChapterCommand.java +++ /dev/null @@ -1,16 +0,0 @@ -package seedu.duke.command; - -import seedu.duke.tool.Access; -import seedu.duke.tool.Storage; -import seedu.duke.tool.Ui; - -public class backChapterCommand extends Command { - public backChapterCommand(String fullCommand) { - super(fullCommand); - } - - @Override - public void execute(Access access, Ui ui, Storage storage) { - access.setChapterLevel(""); - } -} diff --git a/src/main/java/seedu/duke/command/backModuleCommand.java b/src/main/java/seedu/duke/command/backModuleCommand.java deleted file mode 100644 index 54f2d98075..0000000000 --- a/src/main/java/seedu/duke/command/backModuleCommand.java +++ /dev/null @@ -1,16 +0,0 @@ -package seedu.duke.command; - -import seedu.duke.tool.Access; -import seedu.duke.tool.Storage; -import seedu.duke.tool.Ui; - -public class backModuleCommand extends Command { - public backModuleCommand(String fullCommand) { - super(fullCommand); - } - - @Override - public void execute(Access access, Ui ui, Storage storage) { - access.setModuleLevel(""); - } -} diff --git a/src/main/java/seedu/duke/command/randomCommand.java b/src/main/java/seedu/duke/command/randomCommand.java deleted file mode 100644 index 3370773a71..0000000000 --- a/src/main/java/seedu/duke/command/randomCommand.java +++ /dev/null @@ -1,16 +0,0 @@ -package seedu.duke.command; - -import seedu.duke.tool.Access; -import seedu.duke.tool.Storage; -import seedu.duke.tool.Ui; - -public class randomCommand extends Command { - public randomCommand(String fullCommand) { - super(fullCommand); - } - - @Override - public void execute(Access access, Ui ui, Storage storage) { - System.out.println("Sorry, I cannot understand this command"); - } -} diff --git a/src/main/java/seedu/duke/level/Card.java b/src/main/java/seedu/duke/level/Card.java deleted file mode 100644 index b69a0f2666..0000000000 --- a/src/main/java/seedu/duke/level/Card.java +++ /dev/null @@ -1,28 +0,0 @@ -package seedu.duke.level; - -public class Card { - protected String question; - protected String answer; - protected int totalCard = 0; - - public Card(String question, String answer) { - this.question = question; - this.answer = answer; - totalCard++; - } - - - public String getQuestion() { - return question; - } - - public String getAnswer() { - return answer; - } - - public void doneAddCard() { - System.out.println(" Got it. I've added this card:"); - System.out.println(" " + getQuestion() + " " + getAnswer()); - System.out.println(" Now you have " + totalCard +" cards in the list."); - } -} diff --git a/src/main/java/seedu/duke/level/Chapter.java b/src/main/java/seedu/duke/level/Chapter.java deleted file mode 100644 index 0165c2aa93..0000000000 --- a/src/main/java/seedu/duke/level/Chapter.java +++ /dev/null @@ -1,40 +0,0 @@ -package seedu.duke.level; - -import java.util.ArrayList; - -public class Chapter { - protected ArrayList cards; - protected int cardAmount; - protected String chapterName; - protected int totalChapter = 0; - - public Chapter(String chapterName) { - this.chapterName = chapterName; - cards = new ArrayList(); - cardAmount = 0; - totalChapter++; - } - - public Chapter(String chapterName, ArrayList cards) { - this.chapterName = chapterName; - this.cards = new ArrayList<>(cards); - cardAmount = cards.size(); - } - - public void add(Card card){ - cards.add(card); - cards.get(cardAmount).doneAddCard(); - cardAmount++; - } - - public void doneAddChapter() { - System.out.println(" Got it. I've added this chapter:"); - System.out.println(" " + getChapter()); - System.out.println(" Now you have " + totalChapter +" chapters in the list."); - } - - public String getChapter() { - return chapterName; - } - -} diff --git a/src/main/java/seedu/duke/tool/Parser.java b/src/main/java/seedu/duke/tool/Parser.java deleted file mode 100644 index 27697aee55..0000000000 --- a/src/main/java/seedu/duke/tool/Parser.java +++ /dev/null @@ -1,40 +0,0 @@ -package seedu.duke.tool; - -import seedu.duke.command.*; - -public class Parser { - - /** - * Select the corresponding Command class through interpreting the command entered by user. - * Returns the correct Command class. - * - * @param fullCommand Command entered by user. - * @return Construct new Command that is corresponding to the command entered by user. - */ - public static Command parse(String fullCommand) { - if(fullCommand.toLowerCase().contains("addmodule")) { - return new addModuleCommand(fullCommand); - } - else if(fullCommand.toLowerCase().contains("addchapter")) { - return new addChapterCommand(fullCommand); - } - else if(fullCommand.toLowerCase().contains("addcard")) { - return new addCardCommand(fullCommand); - } - else if(fullCommand.toLowerCase().contains("gomodule")) { - return new goModuleCommand(fullCommand); - } - else if(fullCommand.toLowerCase().contains("gochapter")) { - return new goChapterCommand(fullCommand); - } - else if(fullCommand.toLowerCase().contains("backmodule")) { - return new backModuleCommand(fullCommand); - } - else if(fullCommand.toLowerCase().contains("backchapter")) { - return new backChapterCommand(fullCommand); - } - else { - return new randomCommand(fullCommand); - } - } -} \ No newline at end of file diff --git a/src/main/java/seedu/duke/tool/Storage.java b/src/main/java/seedu/duke/tool/Storage.java deleted file mode 100644 index ac0c59b525..0000000000 --- a/src/main/java/seedu/duke/tool/Storage.java +++ /dev/null @@ -1,113 +0,0 @@ -package seedu.duke.tool; - -import seedu.duke.level.Card; -import seedu.duke.level.Chapter; -import seedu.duke.level.Module; - -import java.io.File; -import java.io.FileNotFoundException; -import java.io.FileWriter; -import java.io.IOException; -import java.util.ArrayList; -import java.util.Scanner; - -public class Storage { - protected String filePath; - - public Storage(String filePath){ - this.filePath = filePath; - } - - //create the folder --> 'data/admin' - public void createAdmin() { - File f = new File(filePath); - boolean success = f.getParentFile().mkdir(); - System.out.println(" "+ filePath); - if(success) { - System.out.println(" Successfully created new directory"); - } - else { - System.out.println(" Failed to create new directory"); - } - } - - public void createModule(String moduleName) { - File f = new File(filePath + "/" + moduleName); - boolean success = f.getParentFile().mkdir(); - if(success) { - System.out.println(" Successfully created new directory " + moduleName); - } - else { - System.out.println(" Failed to create new directory"); - } - } - - public void createChapter(String chapterName, String moduleName) { - File f = new File(filePath + "/" + moduleName + "/" + chapterName + ".txt"); - boolean success = f.getParentFile().mkdir(); - if(success) { - System.out.println(" Successfully created new directory " + chapterName); - } - else { - System.out.println(" Failed to create new directory"); - } - } - - public ArrayList loadModule() throws FileNotFoundException { - File f = new File(filePath); - ArrayList modules = new ArrayList(); - Scanner s = new Scanner(f); - int totalModule = 0; - - String contents[] = f.list(); - System.out.println("List of files and directories in the specified directory:"); - for(int i=0; i loadChapter(String module) throws FileNotFoundException { - File f = new File(filePath + "/" + module); - ArrayList chapters = new ArrayList(); - Scanner s = new Scanner(f); - int totalChapter = 0; - String contents[] = f.list(); - System.out.println("List of files and directories in the specified directory:"); - for(int i=0; i loadCard(String module, String chapter) throws FileNotFoundException { - File f = new File(filePath + "/" + module + "/" + chapter + ".txt"); - ArrayList cards = new ArrayList(); - Scanner s = new Scanner(f); - int totalCards = 0; - while (s.hasNext()) { - //to read the card - } - return cards; - } - - -/* - - public void writeFile(ArrayList taskList, int taskAmount) throws IOException { - FileWriter fw = new FileWriter(filePath, false); - fw.write(""); - fw.close(); - - for(int i=0; i 'data/admin' + public void createAdmin() { + File f = new File(filePath); + boolean success = f.getParentFile().mkdir(); + System.out.println(" "+ filePath); + if(success) { + System.out.println(" Successfully created new directory"); + } + else { + System.out.println(" Failed to create new directory"); + } + } + + public void createModule(String moduleName) { + File f = new File(filePath + "/" + moduleName); + boolean success = f.getParentFile().mkdir(); + //File f1 = f.getParentFile(); + //String v = f1.getAbsolutePath(); + //System.out.println(" getParentFile: " + v); + //System.out.println(" filePath: " + f.getPath()); + if(success) { + System.out.println(" Successfully created new directory " + moduleName); + } + else { + System.out.println(" Failed to create new directory"); + } + } + + public void createChapter(String chapterName, String moduleName) { + File f = new File(filePath + "/" + moduleName + "/" + chapterName + ".txt"); + boolean success = f.getParentFile().mkdir(); + if(success) { + System.out.println(" Successfully created new directory " + chapterName); + } + else { + System.out.println(" Failed to create new directory"); + } + } + + public ArrayList loadModule() throws FileNotFoundException { + File f = new File(filePath); + ArrayList modules = new ArrayList(); + Scanner s = new Scanner(f); + + String contents[] = f.list(); + System.out.println("List of files and directories in the specified directory:"); + for(int i=0; i loadChapter(String module) throws FileNotFoundException { + File f = new File(filePath + "/" + module); + ArrayList chapters = new ArrayList(); + Scanner s = new Scanner(f); + String contents[] = f.list(); + System.out.println("List of files and directories in the specified directory:"); + for(int i=0; i loadCard(String module, String chapter) throws FileNotFoundException { + File f = new File(filePath + "/" + module + "/" + chapter + ".txt"); + ArrayList cards = new ArrayList(); + Scanner s = new Scanner(f); + int totalCards = 0; + while (s.hasNext()) { + //to read the card + } + return cards; + } + public static void getFileContents(CardList cards) { try { File f = new File(FILE_PATHWAY); // create a File for the given file path @@ -46,4 +131,8 @@ public static void writeToFile(CardList cards) throws IOException { } fw.close(); } + + public String getFilePath() { + return filePath; + } } diff --git a/src/main/java/ui/Ui.java b/src/main/java/ui/Ui.java index aa93dc6a07..a0230afbd1 100644 --- a/src/main/java/ui/Ui.java +++ b/src/main/java/ui/Ui.java @@ -1,5 +1,6 @@ package ui; +import access.Access; import commands.AddCommand; import commands.ExitCommand; import commands.ListCommand; @@ -25,6 +26,7 @@ public class Ui { private final Scanner in; private final PrintStream out; + private static final String lineSplit = " ____________________________________________________________"; public Ui() { this(System.in, System.out); @@ -47,6 +49,10 @@ public void showWelcome() { out.println("Welcome to Kaji!\n"); } + public void showLevel(Access access) { + out.println(access.getLevel()); + } + public void printEmptyLine() { out.println(); } @@ -107,4 +113,8 @@ public void showHelpList() { out.println("4. " + AddCommand.MESSAGE_USAGE); out.println("5. " + ExitCommand.MESSAGE_USAGE); } + + public void showLine() { + System.out.println(lineSplit); + } } From e9efc73da9aa65962e3e9dd5a085ce8cba133952 Mon Sep 17 00:00:00 2001 From: gua-guargia Date: Thu, 8 Oct 2020 14:39:56 +0800 Subject: [PATCH 31/43] Fix checkStyleMain errors --- src/main/java/access/Access.java | 36 ++++++++--------- ...terCommand.java => AddChapterCommand.java} | 9 ++--- src/main/java/commands/AddCommand.java | 5 +-- ...duleCommand.java => AddModuleCommand.java} | 13 +++---- ...erCommand.java => BackChapterCommand.java} | 2 +- ...uleCommand.java => BackModuleCommand.java} | 2 +- ...pterCommand.java => GoChapterCommand.java} | 9 +++-- ...oduleCommand.java => GoModuleCommand.java} | 6 +-- src/main/java/manager/chapter/Chapter.java | 4 +- src/main/java/manager/module/Module.java | 4 +- src/main/java/parser/Parser.java | 39 ++++++++++++------- src/main/java/storage/Storage.java | 31 +++++++-------- 12 files changed, 81 insertions(+), 79 deletions(-) rename src/main/java/commands/{addChapterCommand.java => AddChapterCommand.java} (82%) rename src/main/java/commands/{addModuleCommand.java => AddModuleCommand.java} (74%) rename src/main/java/commands/{backChapterCommand.java => BackChapterCommand.java} (88%) rename src/main/java/commands/{backModuleCommand.java => BackModuleCommand.java} (88%) rename src/main/java/commands/{goChapterCommand.java => GoChapterCommand.java} (83%) rename src/main/java/commands/{goModuleCommand.java => GoModuleCommand.java} (89%) diff --git a/src/main/java/access/Access.java b/src/main/java/access/Access.java index 8b392aa0a6..8040f89741 100644 --- a/src/main/java/access/Access.java +++ b/src/main/java/access/Access.java @@ -70,21 +70,20 @@ public void setChapter(Chapter chapter) { } public void setModuleLevel(String moduleLevel) { - if(this.chapterLevel != "") { - System.out.println("Sorry, you currently are in the chapter level, please go back to admin level first."); - } - else if (this.moduleLevel != "") { + if (this.chapterLevel != "") { + System.out.println("Sorry, you currently are in the chapter level, " + + "please go back to admin level first."); + } else if (this.moduleLevel != "") { if (moduleLevel == "") { String replacement = "/" + this.moduleLevel; this.level = level.replace(replacement, ""); this.moduleLevel = moduleLevel; this.module = null; + } else { + System.out.println("Sorry, you are already in the module level, " + + "please go back to admin level first."); } - else { - System.out.println("Sorry, you are already in the module level, please go back to admin level first."); - } - } - else { + } else { this.moduleLevel = moduleLevel; this.level = level + "/" + moduleLevel; this.module = new Module(moduleLevel); @@ -92,23 +91,20 @@ else if (this.moduleLevel != "") { } public void setChapterLevel(String chapterLevel) { - if(this.moduleLevel == "") { //wrong level + if (this.moduleLevel == "") { //wrong level System.out.println("Sorry, you currently are in the admin level, please enter module level first."); - } - else { - if(this.chapterLevel != "") { - if(chapterLevel == "") { + } else { + if (this.chapterLevel != "") { + if (chapterLevel == "") { String replacement = "/" + this.chapterLevel; this.level = level.replace(replacement, ""); this.chapterLevel = chapterLevel; this.chapter = null; + } else { + System.out.println("Sorry, you are already in the chapter level, " + + "please go back to module level first."); } - else { - System.out.println("Sorry, you are already in the chapter level, " + - "please go back to module level first."); - } - } - else { + } else { this.chapterLevel = chapterLevel; this.level = level + "/" + chapterLevel; this.chapter = new Chapter(chapterLevel); diff --git a/src/main/java/commands/addChapterCommand.java b/src/main/java/commands/AddChapterCommand.java similarity index 82% rename from src/main/java/commands/addChapterCommand.java rename to src/main/java/commands/AddChapterCommand.java index 321d94e3b7..a262729093 100644 --- a/src/main/java/commands/addChapterCommand.java +++ b/src/main/java/commands/AddChapterCommand.java @@ -7,23 +7,22 @@ import storage.Storage; import ui.Ui; -public class addChapterCommand extends Command { +public class AddChapterCommand extends Command { public static final String COMMAND_WORD = "addchapter"; String chapterCode; - public addChapterCommand(String chapterCode) { + public AddChapterCommand(String chapterCode) { this.chapterCode = chapterCode; } @Override public void execute(CardList cards, Ui ui, Access access, Storage storage) { - if(access.getModuleLevel() != "") { + if (access.getModuleLevel() != "") { Module newModule = access.getModule(); newModule.add(new Chapter(chapterCode)); access.setModule(newModule); storage.createChapter(chapterCode, access.getModuleLevel()); - } - else { + } else { System.out.println("Sorry, you currently are in the admin level, please enter module level first."); } } diff --git a/src/main/java/commands/AddCommand.java b/src/main/java/commands/AddCommand.java index 145f58f947..a7ce019caa 100644 --- a/src/main/java/commands/AddCommand.java +++ b/src/main/java/commands/AddCommand.java @@ -25,12 +25,11 @@ public AddCommand(String question, String answer) { @Override public void execute(CardList cards, Ui ui, Access access, Storage storage) { - if(access.getChapterLevel() != "") { + if (access.getChapterLevel() != "") { cards.addCard(card); int cardCount = cards.getCardCount(); ui.showCardAdded(cards.getCard(cardCount - 1), cardCount); - } - else { + } else { System.out.println("Sorry, you currently are in the wrong level, please enter chapter level first."); } } diff --git a/src/main/java/commands/addModuleCommand.java b/src/main/java/commands/AddModuleCommand.java similarity index 74% rename from src/main/java/commands/addModuleCommand.java rename to src/main/java/commands/AddModuleCommand.java index 5e9bf46c93..b5f410c23a 100644 --- a/src/main/java/commands/addModuleCommand.java +++ b/src/main/java/commands/AddModuleCommand.java @@ -7,25 +7,24 @@ import storage.Storage; import ui.Ui; -public class addModuleCommand extends Command { +public class AddModuleCommand extends Command { public static final String COMMAND_WORD = "addmodule"; String moduleCode; - public addModuleCommand(String moduleCode) { + public AddModuleCommand(String moduleCode) { this.moduleCode = moduleCode; } @Override public void execute(CardList cards, Ui ui, Access access, Storage storage) { - if(access.getModuleLevel() == "") { + if (access.getModuleLevel() == "") { Admin newAdmin = access.getAdmin(); newAdmin.add(new Module(moduleCode)); access.setAdmin(newAdmin); storage.createModule(moduleCode); - } - else { - System.out.println("Sorry, you currently are in the module/chapter level, " + - "please go back to Admin level first."); + } else { + System.out.println("Sorry, you currently are in the module/chapter level, " + + "please go back to Admin level first."); } } diff --git a/src/main/java/commands/backChapterCommand.java b/src/main/java/commands/BackChapterCommand.java similarity index 88% rename from src/main/java/commands/backChapterCommand.java rename to src/main/java/commands/BackChapterCommand.java index f05550decc..ef26ff1bce 100644 --- a/src/main/java/commands/backChapterCommand.java +++ b/src/main/java/commands/BackChapterCommand.java @@ -5,7 +5,7 @@ import storage.Storage; import ui.Ui; -public class backChapterCommand extends Command { +public class BackChapterCommand extends Command { public static final String COMMAND_WORD = "backchapter"; @Override diff --git a/src/main/java/commands/backModuleCommand.java b/src/main/java/commands/BackModuleCommand.java similarity index 88% rename from src/main/java/commands/backModuleCommand.java rename to src/main/java/commands/BackModuleCommand.java index 63af9cda59..c5fe3afd2b 100644 --- a/src/main/java/commands/backModuleCommand.java +++ b/src/main/java/commands/BackModuleCommand.java @@ -5,7 +5,7 @@ import storage.Storage; import ui.Ui; -public class backModuleCommand extends Command { +public class BackModuleCommand extends Command { public static final String COMMAND_WORD = "backmodule"; @Override diff --git a/src/main/java/commands/goChapterCommand.java b/src/main/java/commands/GoChapterCommand.java similarity index 83% rename from src/main/java/commands/goChapterCommand.java rename to src/main/java/commands/GoChapterCommand.java index 46aff02363..1fd9770973 100644 --- a/src/main/java/commands/goChapterCommand.java +++ b/src/main/java/commands/GoChapterCommand.java @@ -9,11 +9,11 @@ import java.io.FileNotFoundException; import java.util.ArrayList; -public class goChapterCommand extends Command { +public class GoChapterCommand extends Command { public static final String COMMAND_WORD = "gochapter"; String chapterCode; - public goChapterCommand(String chapterCode) { + public GoChapterCommand(String chapterCode) { this.chapterCode = chapterCode; } @@ -22,11 +22,12 @@ public void execute(CardList cards, Ui ui, Access access, Storage storage) { boolean isLevelExist = false; ArrayList chapters = access.getModule().getChapter(); for (Chapter chapter : chapters) { - if(chapterCode.equalsIgnoreCase(chapter.getChapter())) { + if (chapterCode.equalsIgnoreCase(chapter.getChapter())) { access.setChapterLevel(chapterCode); isLevelExist = true; try { - Chapter newChapter = new Chapter(chapter.getChapter(), storage.loadCard(access.getModuleLevel(), chapter.getChapter())); + Chapter newChapter = new Chapter(chapter.getChapter(), + storage.loadCard(access.getModuleLevel(), chapter.getChapter())); access.setChapter(newChapter); } catch (FileNotFoundException e) { Chapter newChapter = new Chapter(chapter.getChapter()); diff --git a/src/main/java/commands/goModuleCommand.java b/src/main/java/commands/GoModuleCommand.java similarity index 89% rename from src/main/java/commands/goModuleCommand.java rename to src/main/java/commands/GoModuleCommand.java index ad9bf7ffd4..1e6f31704d 100644 --- a/src/main/java/commands/goModuleCommand.java +++ b/src/main/java/commands/GoModuleCommand.java @@ -9,11 +9,11 @@ import java.io.FileNotFoundException; import java.util.ArrayList; -public class goModuleCommand extends Command { +public class GoModuleCommand extends Command { public static final String COMMAND_WORD = "gomodule"; String moduleCode; - public goModuleCommand(String moduleCode) { + public GoModuleCommand(String moduleCode) { this.moduleCode = moduleCode; } @@ -22,7 +22,7 @@ public void execute(CardList cards, Ui ui, Access access, Storage storage) { boolean isLevelExist = false; ArrayList modules = access.getAdmin().getModules(); for (Module module : modules) { - if(moduleCode.equalsIgnoreCase(module.getModule())) { + if (moduleCode.equalsIgnoreCase(module.getModule())) { access.setModuleLevel(moduleCode); isLevelExist = true; try { diff --git a/src/main/java/manager/chapter/Chapter.java b/src/main/java/manager/chapter/Chapter.java index 88d498783b..145b223292 100644 --- a/src/main/java/manager/chapter/Chapter.java +++ b/src/main/java/manager/chapter/Chapter.java @@ -28,7 +28,7 @@ public Chapter(String chapterName, ArrayList cards) { cardAmount = cards.size(); } - public void add(Card card){ + public void add(Card card) { cards.add(card); cardAmount++; } @@ -36,7 +36,7 @@ public void add(Card card){ public void doneAddChapter() { System.out.println(" Got it. I've added this chapter:"); System.out.println(" " + getChapter()); - System.out.println(" Now you have " + totalChapter +" chapters in the list."); + System.out.println(" Now you have " + totalChapter + " chapters in the list."); } public String getChapter() { diff --git a/src/main/java/manager/module/Module.java b/src/main/java/manager/module/Module.java index d4e84128d9..8ea658a96a 100644 --- a/src/main/java/manager/module/Module.java +++ b/src/main/java/manager/module/Module.java @@ -24,7 +24,7 @@ public Module(String moduleName, ArrayList chapters) { totalModule++; } - public void add(Chapter chapter){ + public void add(Chapter chapter) { chapters.add(chapter); chapters.get(chapterAmount).doneAddChapter(); chapterAmount++; @@ -33,7 +33,7 @@ public void add(Chapter chapter){ public void doneAddModule() { System.out.println(" Got it. I've added this module:"); System.out.println(" " + getModule()); - System.out.println(" Now you have " + totalModule +" modules in the list."); + System.out.println(" Now you have " + totalModule + " modules in the list."); } public String getModule() { diff --git a/src/main/java/parser/Parser.java b/src/main/java/parser/Parser.java index 76ca912357..36217c1b3f 100644 --- a/src/main/java/parser/Parser.java +++ b/src/main/java/parser/Parser.java @@ -1,9 +1,20 @@ package parser; -import commands.*; +import commands.Command; +import commands.ListCommand; +import commands.AddChapterCommand; +import commands.AddCommand; +import commands.AddModuleCommand; +import commands.BackChapterCommand; +import commands.GoChapterCommand; +import commands.HelpCommand; +import commands.ReviseCommand; +import commands.ExitCommand; +import commands.GoModuleCommand; +import commands.BackModuleCommand; + import exception.InvalidFileFormatException; import exception.InvalidInputException; -import manager.chapter.Chapter; import storage.Storage; @@ -24,17 +35,17 @@ public static Command parse(String fullCommand) throws InvalidInputException { return prepareExit(commandArgs); case HelpCommand.COMMAND_WORD: return prepareHelp(commandArgs); - case addModuleCommand.COMMAND_WORD: + case AddModuleCommand.COMMAND_WORD: return prepareAddModule(commandArgs); - case addChapterCommand.COMMAND_WORD: + case AddChapterCommand.COMMAND_WORD: return prepareAddChapter(commandArgs); - case backModuleCommand.COMMAND_WORD: + case BackModuleCommand.COMMAND_WORD: return prepareBackModule(commandArgs); - case backChapterCommand.COMMAND_WORD: + case BackChapterCommand.COMMAND_WORD: return prepareBackChapter(commandArgs); - case goModuleCommand.COMMAND_WORD: + case GoModuleCommand.COMMAND_WORD: return prepareGoModule(commandArgs); - case goChapterCommand.COMMAND_WORD: + case GoChapterCommand.COMMAND_WORD: return prepareGoChapter(commandArgs); default: throw new InvalidInputException(); @@ -45,42 +56,42 @@ private static Command prepareGoChapter(String commandArgs) throws InvalidInputE if (commandArgs.isEmpty()) { throw new InvalidInputException(); } - return new goChapterCommand(commandArgs); + return new GoChapterCommand(commandArgs); } private static Command prepareGoModule(String commandArgs) throws InvalidInputException { if (commandArgs.isEmpty()) { throw new InvalidInputException(); } - return new goModuleCommand(commandArgs); + return new GoModuleCommand(commandArgs); } private static Command prepareBackChapter(String commandArgs) throws InvalidInputException { if (!commandArgs.isEmpty()) { throw new InvalidInputException(); } - return new backChapterCommand(); + return new BackChapterCommand(); } private static Command prepareBackModule(String commandArgs) throws InvalidInputException { if (!commandArgs.isEmpty()) { throw new InvalidInputException(); } - return new backModuleCommand(); + return new BackModuleCommand(); } private static Command prepareAddChapter(String commandArgs) throws InvalidInputException { if (commandArgs.isEmpty()) { throw new InvalidInputException(); } - return new addChapterCommand(commandArgs); + return new AddChapterCommand(commandArgs); } private static Command prepareAddModule(String commandArgs) throws InvalidInputException { if (commandArgs.isEmpty()) { throw new InvalidInputException(); } - return new addModuleCommand(commandArgs); + return new AddModuleCommand(commandArgs); } private static String[] splitCommandTypeAndArgs(String userCommand) { diff --git a/src/main/java/storage/Storage.java b/src/main/java/storage/Storage.java index fbab03280d..71b02f55f5 100644 --- a/src/main/java/storage/Storage.java +++ b/src/main/java/storage/Storage.java @@ -23,7 +23,7 @@ public class Storage { protected String filePath; - public Storage(String filePath){ + public Storage(String filePath) { this.filePath = filePath; } @@ -31,12 +31,11 @@ public Storage(String filePath){ public void createAdmin() { File f = new File(filePath); boolean success = f.getParentFile().mkdir(); - System.out.println(" "+ filePath); - if(success) { - System.out.println(" Successfully created new directory"); - } - else { - System.out.println(" Failed to create new directory"); + System.out.println(filePath); + if (success) { + System.out.println("Successfully created new directory"); + } else { + System.out.println("Failed to create new directory"); } } @@ -47,10 +46,9 @@ public void createModule(String moduleName) { //String v = f1.getAbsolutePath(); //System.out.println(" getParentFile: " + v); //System.out.println(" filePath: " + f.getPath()); - if(success) { + if (success) { System.out.println(" Successfully created new directory " + moduleName); - } - else { + } else { System.out.println(" Failed to create new directory"); } } @@ -58,10 +56,9 @@ public void createModule(String moduleName) { public void createChapter(String chapterName, String moduleName) { File f = new File(filePath + "/" + moduleName + "/" + chapterName + ".txt"); boolean success = f.getParentFile().mkdir(); - if(success) { + if (success) { System.out.println(" Successfully created new directory " + chapterName); - } - else { + } else { System.out.println(" Failed to create new directory"); } } @@ -71,9 +68,9 @@ public ArrayList loadModule() throws FileNotFoundException { ArrayList modules = new ArrayList(); Scanner s = new Scanner(f); - String contents[] = f.list(); + String[] contents = f.list(); System.out.println("List of files and directories in the specified directory:"); - for(int i=0; i loadChapter(String module) throws FileNotFoundExceptio File f = new File(filePath + "/" + module); ArrayList chapters = new ArrayList(); Scanner s = new Scanner(f); - String contents[] = f.list(); + String[] contents = f.list(); System.out.println("List of files and directories in the specified directory:"); - for(int i=0; i Date: Thu, 8 Oct 2020 14:47:51 +0800 Subject: [PATCH 32/43] Fix IOtest error --- src/test/java/seedu/duke/DukeTest.java | 12 ------------ 1 file changed, 12 deletions(-) delete mode 100644 src/test/java/seedu/duke/DukeTest.java diff --git a/src/test/java/seedu/duke/DukeTest.java b/src/test/java/seedu/duke/DukeTest.java deleted file mode 100644 index 2dda5fd651..0000000000 --- a/src/test/java/seedu/duke/DukeTest.java +++ /dev/null @@ -1,12 +0,0 @@ -package seedu.duke; - -import static org.junit.jupiter.api.Assertions.assertTrue; - -import org.junit.jupiter.api.Test; - -class DukeTest { - @Test - public void sampleTest() { - assertTrue(true); - } -} From cc76a1b8555b0dbc3dd62526e8ed65802d12a122 Mon Sep 17 00:00:00 2001 From: gua-guargia Date: Thu, 8 Oct 2020 14:57:41 +0800 Subject: [PATCH 33/43] Fix IOfile error --- src/main/java/seedu/duke/Duke.java | 21 +++++++++++++++++++++ src/test/java/seedu/duke/DukeTest.java | 12 ++++++++++++ 2 files changed, 33 insertions(+) create mode 100644 src/main/java/seedu/duke/Duke.java create mode 100644 src/test/java/seedu/duke/DukeTest.java diff --git a/src/main/java/seedu/duke/Duke.java b/src/main/java/seedu/duke/Duke.java new file mode 100644 index 0000000000..9f8063ee94 --- /dev/null +++ b/src/main/java/seedu/duke/Duke.java @@ -0,0 +1,21 @@ +package seedu.duke; + +import java.util.Scanner; + +public class Duke { + /** + * Main entry-point for the java.duke.Duke application. + */ + public static void main(String[] args) { + String logo = " ____ _ \n" + + "| _ \\ _ _| | _____ \n" + + "| | | | | | | |/ / _ \\\n" + + "| |_| | |_| | < __/\n" + + "|____/ \\__,_|_|\\_\\___|\n"; + System.out.println("Hello from\n" + logo); + System.out.println("What is your name?"); + + Scanner in = new Scanner(System.in); + System.out.println("Hello " + in.nextLine()); + } +} \ No newline at end of file diff --git a/src/test/java/seedu/duke/DukeTest.java b/src/test/java/seedu/duke/DukeTest.java new file mode 100644 index 0000000000..2dda5fd651 --- /dev/null +++ b/src/test/java/seedu/duke/DukeTest.java @@ -0,0 +1,12 @@ +package seedu.duke; + +import static org.junit.jupiter.api.Assertions.assertTrue; + +import org.junit.jupiter.api.Test; + +class DukeTest { + @Test + public void sampleTest() { + assertTrue(true); + } +} From 6b4536c411b0aeca10211fe32fff4ae1c0f6ea4a Mon Sep 17 00:00:00 2001 From: neojiaern Date: Thu, 8 Oct 2020 16:20:58 +0800 Subject: [PATCH 34/43] Add junit testing for rateCard method --- src/main/java/commands/ReviseCommand.java | 9 +++--- src/main/java/ui/Ui.java | 4 --- src/test/java/commands/ReviseCommandTest.java | 31 +++++++++++++++++++ 3 files changed, 36 insertions(+), 8 deletions(-) create mode 100644 src/test/java/commands/ReviseCommandTest.java diff --git a/src/main/java/commands/ReviseCommand.java b/src/main/java/commands/ReviseCommand.java index b9b53310a5..6f0d247ef3 100644 --- a/src/main/java/commands/ReviseCommand.java +++ b/src/main/java/commands/ReviseCommand.java @@ -50,7 +50,8 @@ public void execute(CardList cards, Ui ui) { if (Scheduler.isDeadlineDue(c.getDueBy())) { ui.showToUser("\nQuestion " + count + ":"); ui.showCard(c); - repeatCards = rateCard(ui, repeatCards, c); + String input = ui.getRating(); + repeatCards = rateCard(ui, repeatCards, c, input); count++; } } @@ -63,8 +64,7 @@ public void execute(CardList cards, Ui ui) { ui.showToUser(String.format(MESSAGE_SUCCESS, toRevise)); } - private ArrayList rateCard(Ui ui, ArrayList repeatCards, Card c) { - String input = ui.getRating(); + public static ArrayList rateCard(Ui ui, ArrayList repeatCards, Card c, String input) { boolean isInvalid = true; while (isInvalid) { switch (input.toLowerCase()) { @@ -99,7 +99,8 @@ private void repeatRevision(Ui ui, ArrayList cards, int count) { for (Card c : cards) { ui.showToUser("\nQuestion " + count + ":"); ui.showCard(c); - repeatCards = rateCard(ui, repeatCards, c); + String input = ui.getRating(); + repeatCards = rateCard(ui, repeatCards, c, input); count++; } cards = new ArrayList<>(repeatCards); diff --git a/src/main/java/ui/Ui.java b/src/main/java/ui/Ui.java index aa93dc6a07..6a8e297e30 100644 --- a/src/main/java/ui/Ui.java +++ b/src/main/java/ui/Ui.java @@ -15,10 +15,6 @@ import java.util.ArrayList; import java.util.Scanner; -import static commands.ReviseCommand.CANNOT_ANSWER; -import static commands.ReviseCommand.EASY; -import static commands.ReviseCommand.HARD; -import static commands.ReviseCommand.MEDIUM; import static commands.ReviseCommand.MESSAGE_SHOW_ANSWER_PROMPT; import static commands.ReviseCommand.MESSAGE_SHOW_RATING_PROMPT; diff --git a/src/test/java/commands/ReviseCommandTest.java b/src/test/java/commands/ReviseCommandTest.java new file mode 100644 index 0000000000..e179131d97 --- /dev/null +++ b/src/test/java/commands/ReviseCommandTest.java @@ -0,0 +1,31 @@ +package commands; + +import manager.card.Card; +import org.junit.jupiter.api.Test; +import ui.Ui; + +import java.util.ArrayList; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +public class ReviseCommandTest { + @Test + public void rateCard_cannotAnswer_returnsSizeOne() { + String question = "1+1?"; + String answer = "2"; + ArrayList cards = new ArrayList<>(); + Card card = new Card(question, answer); + Ui ui = new Ui(); + assertEquals(ReviseCommand.rateCard(ui, cards, card, "c").size(), 1); + } + + @Test + public void rateCard_easy_returnsSizeZero() { + String question = "1+1?"; + String answer = "2"; + ArrayList cards = new ArrayList<>(); + Card card = new Card(question, answer); + Ui ui = new Ui(); + assertEquals(ReviseCommand.rateCard(ui, cards, card, "e").size(), 0); + } +} From f3cf2b29beab92c1e810b13ee9b7cc56efe03aef Mon Sep 17 00:00:00 2001 From: neojiaern Date: Thu, 8 Oct 2020 16:23:55 +0800 Subject: [PATCH 35/43] Fix help command exiting after call --- src/main/java/commands/HelpCommand.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/commands/HelpCommand.java b/src/main/java/commands/HelpCommand.java index ae336211ca..fe76a7d19a 100644 --- a/src/main/java/commands/HelpCommand.java +++ b/src/main/java/commands/HelpCommand.java @@ -16,6 +16,6 @@ public void execute(CardList cards, Ui ui) { @Override public boolean isExit() { - return true; + return false; } } From eef3374823900fdd974295fafca173a5adfdf2d6 Mon Sep 17 00:00:00 2001 From: gua-guargia Date: Thu, 8 Oct 2020 17:11:15 +0800 Subject: [PATCH 36/43] Resolve peer review --- src/main/java/access/Access.java | 6 +++--- src/main/java/commands/AddChapterCommand.java | 12 ++++++------ src/main/java/commands/AddCommand.java | 10 +++++----- src/main/java/commands/AddModuleCommand.java | 12 ++++++------ src/main/java/ui/Ui.java | 4 ---- 5 files changed, 20 insertions(+), 24 deletions(-) diff --git a/src/main/java/access/Access.java b/src/main/java/access/Access.java index 8040f89741..7580dd2c26 100644 --- a/src/main/java/access/Access.java +++ b/src/main/java/access/Access.java @@ -70,11 +70,11 @@ public void setChapter(Chapter chapter) { } public void setModuleLevel(String moduleLevel) { - if (this.chapterLevel != "") { + if (this.chapterLevel.equals("")) { System.out.println("Sorry, you currently are in the chapter level, " + "please go back to admin level first."); - } else if (this.moduleLevel != "") { - if (moduleLevel == "") { + } else if (!(this.moduleLevel.equals(""))) { + if (moduleLevel.equals("")) { String replacement = "/" + this.moduleLevel; this.level = level.replace(replacement, ""); this.moduleLevel = moduleLevel; diff --git a/src/main/java/commands/AddChapterCommand.java b/src/main/java/commands/AddChapterCommand.java index a262729093..cdfc127d32 100644 --- a/src/main/java/commands/AddChapterCommand.java +++ b/src/main/java/commands/AddChapterCommand.java @@ -17,14 +17,14 @@ public AddChapterCommand(String chapterCode) { @Override public void execute(CardList cards, Ui ui, Access access, Storage storage) { - if (access.getModuleLevel() != "") { - Module newModule = access.getModule(); - newModule.add(new Chapter(chapterCode)); - access.setModule(newModule); - storage.createChapter(chapterCode, access.getModuleLevel()); - } else { + if (access.getModuleLevel().equals("")) { System.out.println("Sorry, you currently are in the admin level, please enter module level first."); + return; } + Module newModule = access.getModule(); + newModule.add(new Chapter(chapterCode)); + access.setModule(newModule); + storage.createChapter(chapterCode, access.getModuleLevel()); } @Override diff --git a/src/main/java/commands/AddCommand.java b/src/main/java/commands/AddCommand.java index a7ce019caa..a04fbf43e2 100644 --- a/src/main/java/commands/AddCommand.java +++ b/src/main/java/commands/AddCommand.java @@ -25,13 +25,13 @@ public AddCommand(String question, String answer) { @Override public void execute(CardList cards, Ui ui, Access access, Storage storage) { - if (access.getChapterLevel() != "") { - cards.addCard(card); - int cardCount = cards.getCardCount(); - ui.showCardAdded(cards.getCard(cardCount - 1), cardCount); - } else { + if (access.getChapterLevel().equals("")) { System.out.println("Sorry, you currently are in the wrong level, please enter chapter level first."); + return; } + cards.addCard(card); + int cardCount = cards.getCardCount(); + ui.showCardAdded(cards.getCard(cardCount - 1), cardCount); } @Override diff --git a/src/main/java/commands/AddModuleCommand.java b/src/main/java/commands/AddModuleCommand.java index b5f410c23a..dd79002b3d 100644 --- a/src/main/java/commands/AddModuleCommand.java +++ b/src/main/java/commands/AddModuleCommand.java @@ -17,15 +17,15 @@ public AddModuleCommand(String moduleCode) { @Override public void execute(CardList cards, Ui ui, Access access, Storage storage) { - if (access.getModuleLevel() == "") { - Admin newAdmin = access.getAdmin(); - newAdmin.add(new Module(moduleCode)); - access.setAdmin(newAdmin); - storage.createModule(moduleCode); - } else { + if (!(access.getModuleLevel().equals(""))) { System.out.println("Sorry, you currently are in the module/chapter level, " + "please go back to Admin level first."); + return; } + Admin newAdmin = access.getAdmin(); + newAdmin.add(new Module(moduleCode)); + access.setAdmin(newAdmin); + storage.createModule(moduleCode); } @Override diff --git a/src/main/java/ui/Ui.java b/src/main/java/ui/Ui.java index a0230afbd1..5de54d45f1 100644 --- a/src/main/java/ui/Ui.java +++ b/src/main/java/ui/Ui.java @@ -113,8 +113,4 @@ public void showHelpList() { out.println("4. " + AddCommand.MESSAGE_USAGE); out.println("5. " + ExitCommand.MESSAGE_USAGE); } - - public void showLine() { - System.out.println(lineSplit); - } } From 1ed133d77ca017839d8b1bac275ea589a022afb4 Mon Sep 17 00:00:00 2001 From: gua-guargia Date: Thu, 8 Oct 2020 17:15:47 +0800 Subject: [PATCH 37/43] Fix Build and check with Gradle --- src/main/java/Kaji.java | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/main/java/Kaji.java b/src/main/java/Kaji.java index a7c9071eca..6fca5db1fe 100644 --- a/src/main/java/Kaji.java +++ b/src/main/java/Kaji.java @@ -24,8 +24,6 @@ public Kaji(String filePath) { try { Admin admin = new Admin(storage.loadModule()); access = new Access(admin); - ui.showLine(); // show the divider line ("_______") - ui.showLine(); } catch (FileNotFoundException e) { storage.createAdmin(); access = new Access(); From 6c0669cbfd86632ce3b0316bd067a29692ce62d4 Mon Sep 17 00:00:00 2001 From: gua-guargia Date: Thu, 8 Oct 2020 19:20:51 +0800 Subject: [PATCH 38/43] Edit Access.java: wrong logic --- src/main/java/access/Access.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/access/Access.java b/src/main/java/access/Access.java index 7580dd2c26..9c4dbe27e2 100644 --- a/src/main/java/access/Access.java +++ b/src/main/java/access/Access.java @@ -70,7 +70,7 @@ public void setChapter(Chapter chapter) { } public void setModuleLevel(String moduleLevel) { - if (this.chapterLevel.equals("")) { + if (!(this.chapterLevel.equals(""))) { System.out.println("Sorry, you currently are in the chapter level, " + "please go back to admin level first."); } else if (!(this.moduleLevel.equals(""))) { From c7b985dd1363e69d23518047dfbb13ef9c46781b Mon Sep 17 00:00:00 2001 From: neojiaern Date: Sat, 10 Oct 2020 13:00:33 +0800 Subject: [PATCH 39/43] Add remove feature for flahscards --- src/main/java/commands/RemoveCommand.java | 40 +++++++++++++++++++++ src/main/java/manager/chapter/CardList.java | 4 +++ src/main/java/parser/Parser.java | 16 +++++++++ 3 files changed, 60 insertions(+) create mode 100644 src/main/java/commands/RemoveCommand.java diff --git a/src/main/java/commands/RemoveCommand.java b/src/main/java/commands/RemoveCommand.java new file mode 100644 index 0000000000..ad4fd7027b --- /dev/null +++ b/src/main/java/commands/RemoveCommand.java @@ -0,0 +1,40 @@ +package commands; + +import access.Access; +import manager.card.Card; +import manager.chapter.CardList; +import storage.Storage; +import ui.Ui; + +public class RemoveCommand extends Command { + public static final String COMMAND_WORD = "remove"; + + public static final String MESSAGE_USAGE = COMMAND_WORD + + ": Removes flashcard based on a specified index in the list. \n" + + "Parameters: INDEX\n" + "Example: " + COMMAND_WORD + " 2\n"; + + public static final String MESSAGE_SUCCESS = "The following flashcard has been removed:\n"; + public static final String MESSAGE_INVALID_INDEX = "The flashcard is not found, please try again."; + + private final int removeIndex; + + public RemoveCommand(int removeIndex) { + this.removeIndex = removeIndex; + } + + @Override + public void execute(CardList cards, Ui ui, Access access, Storage storage) { + try { + Card card = cards.getCard(removeIndex); + ui.showToUser(MESSAGE_SUCCESS + card.toString()); + cards.removeCard(removeIndex); + } catch (IndexOutOfBoundsException e) { + ui.showToUser(MESSAGE_INVALID_INDEX); + } + } + + @Override + public boolean isExit() { + return false; + } +} diff --git a/src/main/java/manager/chapter/CardList.java b/src/main/java/manager/chapter/CardList.java index 4d37eaa535..393160a2f1 100644 --- a/src/main/java/manager/chapter/CardList.java +++ b/src/main/java/manager/chapter/CardList.java @@ -15,6 +15,10 @@ public void addCard(Card card) { cards.add(card); } + public void removeCard(int removeIndex) { + cards.remove(removeIndex); + } + public int getCardCount() { return cards.size(); } diff --git a/src/main/java/parser/Parser.java b/src/main/java/parser/Parser.java index 36217c1b3f..f9a37196e2 100644 --- a/src/main/java/parser/Parser.java +++ b/src/main/java/parser/Parser.java @@ -8,6 +8,7 @@ import commands.BackChapterCommand; import commands.GoChapterCommand; import commands.HelpCommand; +import commands.RemoveCommand; import commands.ReviseCommand; import commands.ExitCommand; import commands.GoModuleCommand; @@ -29,6 +30,8 @@ public static Command parse(String fullCommand) throws InvalidInputException { return prepareList(commandArgs); case AddCommand.COMMAND_WORD: return prepareAdd(commandArgs); + case RemoveCommand.COMMAND_WORD: + return prepareRemove(commandArgs); case ReviseCommand.COMMAND_WORD: return prepareRevise(commandArgs); case ExitCommand.COMMAND_WORD: @@ -120,6 +123,19 @@ private static Command prepareAdd(String commandArgs) throws InvalidInputExcepti } } + private static Command prepareRemove(String commandArgs) throws InvalidInputException { + int removeIndex; + if (commandArgs.isEmpty()) { + throw new InvalidInputException(); + } + try { + removeIndex = Integer.parseInt(commandArgs) - 1; + } catch (NumberFormatException e) { + throw new InvalidInputException(); + } + return new RemoveCommand(removeIndex); + } + private static String parseQuestion(String arg) throws InvalidInputException { if (!(arg.trim().toLowerCase().startsWith(AddCommand.QUESTION_PREFIX))) { throw new InvalidInputException(); From fa519bbe87731abf70f4caa7ac71d53cb8abce59 Mon Sep 17 00:00:00 2001 From: neojiaern Date: Sat, 10 Oct 2020 13:00:51 +0800 Subject: [PATCH 40/43] Remove unused imports --- src/main/java/ui/Ui.java | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/main/java/ui/Ui.java b/src/main/java/ui/Ui.java index b9ad7f3850..f343f69b07 100644 --- a/src/main/java/ui/Ui.java +++ b/src/main/java/ui/Ui.java @@ -8,8 +8,6 @@ import commands.ReviseCommand; import manager.card.Card; -import manager.chapter.Chapter; -import scheduler.Scheduler; import java.io.InputStream; import java.io.PrintStream; From 375801959f3bd3875c4a634b864f906b790d9aa8 Mon Sep 17 00:00:00 2001 From: neojiaern Date: Sat, 10 Oct 2020 13:01:43 +0800 Subject: [PATCH 41/43] Add junit testing to test remove feature in parser --- src/test/java/parser/ParserTest.java | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/src/test/java/parser/ParserTest.java b/src/test/java/parser/ParserTest.java index f9d327ca20..8c41b81013 100644 --- a/src/test/java/parser/ParserTest.java +++ b/src/test/java/parser/ParserTest.java @@ -38,4 +38,23 @@ public void parse_exitCommandWithArgs_expectException() { final String input = "exit args"; assertThrows(InvalidInputException.class, () -> parser.parse(input)); } + + @Test + public void parse_removeCommandEmptyArgs_exception() { + Parser parser = new Parser(); + final String[] inputs = { + "remove", + "remove ", + }; + for (String input : inputs) { + assertThrows(InvalidInputException.class, () -> parser.parse(input)); + } + } + + @Test + public void parse_removeCommandNonIntegerArgs_exception() { + Parser parser = new Parser(); + String input = "remove two"; + assertThrows(InvalidInputException.class, () -> parser.parse(input)); + } } From 405b9665120d00dbab092a0bd41eb5fcb8880483 Mon Sep 17 00:00:00 2001 From: neojiaern Date: Sat, 10 Oct 2020 13:08:58 +0800 Subject: [PATCH 42/43] Fix checkstyle test issues --- src/test/java/parser/ParserTest.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/test/java/parser/ParserTest.java b/src/test/java/parser/ParserTest.java index 8c41b81013..2c62227d01 100644 --- a/src/test/java/parser/ParserTest.java +++ b/src/test/java/parser/ParserTest.java @@ -43,8 +43,8 @@ public void parse_exitCommandWithArgs_expectException() { public void parse_removeCommandEmptyArgs_exception() { Parser parser = new Parser(); final String[] inputs = { - "remove", - "remove ", + "remove", + "remove ", }; for (String input : inputs) { assertThrows(InvalidInputException.class, () -> parser.parse(input)); From 77cd9e388c8836627e625529075bd4e2d30e69c7 Mon Sep 17 00:00:00 2001 From: gua-guargia Date: Sun, 11 Oct 2020 21:24:41 +0800 Subject: [PATCH 43/43] Add moduleList and chapterList class --- src/main/java/access/Access.java | 106 +++++++++++++----- src/main/java/commands/AddChapterCommand.java | 19 +++- src/main/java/commands/AddModuleCommand.java | 17 ++- src/main/java/commands/GoChapterCommand.java | 23 ++-- src/main/java/commands/GoModuleCommand.java | 21 ++-- src/main/java/manager/admin/Admin.java | 17 +-- src/main/java/manager/admin/ModuleList.java | 29 +++++ src/main/java/manager/chapter/CardList.java | 6 +- src/main/java/manager/chapter/Chapter.java | 33 ++---- src/main/java/manager/module/ChapterList.java | 29 +++++ src/main/java/manager/module/Module.java | 35 ++---- src/main/java/storage/Storage.java | 87 ++++++++++---- src/main/java/ui/Ui.java | 14 +++ 13 files changed, 290 insertions(+), 146 deletions(-) create mode 100644 src/main/java/manager/admin/ModuleList.java create mode 100644 src/main/java/manager/module/ChapterList.java diff --git a/src/main/java/access/Access.java b/src/main/java/access/Access.java index 9c4dbe27e2..a553ba9c36 100644 --- a/src/main/java/access/Access.java +++ b/src/main/java/access/Access.java @@ -12,6 +12,9 @@ public class Access { protected Chapter chapter; protected Module module; protected Admin admin; + protected boolean isAdminLevel; + protected boolean isModuleLevel; + protected boolean isChapterLevel; public Access(Admin admin) { this.admin = admin; @@ -21,6 +24,9 @@ public Access(Admin admin) { this.adminLevel = "admin"; this.moduleLevel = ""; this.chapterLevel = ""; + this.isAdminLevel = true; + this.isModuleLevel = false; + this.isChapterLevel = false; } public Access() { @@ -31,6 +37,9 @@ public Access() { this.module = null; this.chapter = null; this.admin = new Admin(); + this.isAdminLevel = true; + this.isModuleLevel = false; + this.isChapterLevel = false; } public String getModuleLevel() { @@ -69,47 +78,84 @@ public void setChapter(Chapter chapter) { this.chapter = chapter; } + public boolean isAdminLevel() { + return isAdminLevel; + } + + public boolean isModuleLevel() { + return isModuleLevel; + } + + public boolean isChapterLevel() { + return isChapterLevel; + } + public void setModuleLevel(String moduleLevel) { - if (!(this.chapterLevel.equals(""))) { - System.out.println("Sorry, you currently are in the chapter level, " - + "please go back to admin level first."); - } else if (!(this.moduleLevel.equals(""))) { - if (moduleLevel.equals("")) { - String replacement = "/" + this.moduleLevel; - this.level = level.replace(replacement, ""); - this.moduleLevel = moduleLevel; - this.module = null; - } else { + if (isChapterLevel) { + System.out.println("Sorry, you currently are in the chapter level."); + return; + } + + if (isModuleLevel) { + if (!(moduleLevel.equals(""))) { System.out.println("Sorry, you are already in the module level, " + "please go back to admin level first."); + return; + } + + String replacement = "/" + this.moduleLevel; + this.level = level.replace(replacement, ""); + this.moduleLevel = moduleLevel; + this.module = null; + this.isModuleLevel = false; + this.isAdminLevel = true; + return; + } + + if (isAdminLevel) { + if (moduleLevel.equals("")) { + System.out.println("Sorry, you are already in the admin level."); + return; } - } else { this.moduleLevel = moduleLevel; this.level = level + "/" + moduleLevel; this.module = new Module(moduleLevel); + this.isModuleLevel = true; + this.isAdminLevel = false; } } public void setChapterLevel(String chapterLevel) { - if (this.moduleLevel == "") { //wrong level - System.out.println("Sorry, you currently are in the admin level, please enter module level first."); - } else { - if (this.chapterLevel != "") { - if (chapterLevel == "") { - String replacement = "/" + this.chapterLevel; - this.level = level.replace(replacement, ""); - this.chapterLevel = chapterLevel; - this.chapter = null; - } else { - System.out.println("Sorry, you are already in the chapter level, " - + "please go back to module level first."); - } - } else { - this.chapterLevel = chapterLevel; - this.level = level + "/" + chapterLevel; - this.chapter = new Chapter(chapterLevel); + if (isAdminLevel) { //wrong level + System.out.println("Sorry, you currently are in the admin level."); + return; + } + + if (isChapterLevel) { + if (!(chapterLevel.equals(""))) { + System.out.println("Sorry, you are already in the chapter level, " + + "please go back to module level first."); + return; } + String replacement = "/" + this.chapterLevel; + this.level = level.replace(replacement, ""); + this.chapterLevel = chapterLevel; + this.chapter = null; + this.isChapterLevel = false; + this.isModuleLevel = true; + return; } - } -} + if (isModuleLevel) { + if (chapterLevel.equals("")) { + System.out.println("Sorry, you are already in the module level."); + return; + } + this.chapterLevel = chapterLevel; + this.level = level + "/" + chapterLevel; + this.chapter = new Chapter(chapterLevel); + this.isChapterLevel = true; + this.isModuleLevel = false; + } + } +} \ No newline at end of file diff --git a/src/main/java/commands/AddChapterCommand.java b/src/main/java/commands/AddChapterCommand.java index cdfc127d32..b421450b8a 100644 --- a/src/main/java/commands/AddChapterCommand.java +++ b/src/main/java/commands/AddChapterCommand.java @@ -2,6 +2,7 @@ import manager.chapter.CardList; import manager.chapter.Chapter; +import manager.module.ChapterList; import manager.module.Module; import access.Access; import storage.Storage; @@ -9,22 +10,27 @@ public class AddChapterCommand extends Command { public static final String COMMAND_WORD = "addchapter"; - String chapterCode; + private final Chapter chapter; public AddChapterCommand(String chapterCode) { - this.chapterCode = chapterCode; + this.chapter = new Chapter(chapterCode); } @Override public void execute(CardList cards, Ui ui, Access access, Storage storage) { - if (access.getModuleLevel().equals("")) { - System.out.println("Sorry, you currently are in the admin level, please enter module level first."); + if (access.isAdminLevel() || access.isChapterLevel()) { + System.out.println("Sorry, you currently are in the admin/chapter level, " + + "please go to module level first."); return; } + Module newModule = access.getModule(); - newModule.add(new Chapter(chapterCode)); + ChapterList chapters = newModule.getChapters(); + chapters.addChapter(chapter); + int chapterCount = chapters.getChapterCount(); + ui.showChapterAdded(chapter, chapterCount); access.setModule(newModule); - storage.createChapter(chapterCode, access.getModuleLevel()); + storage.createChapter(chapter.getChapterName(), access.getModuleLevel()); } @Override @@ -32,3 +38,4 @@ public boolean isExit() { return false; } } + diff --git a/src/main/java/commands/AddModuleCommand.java b/src/main/java/commands/AddModuleCommand.java index dd79002b3d..c9a3759f21 100644 --- a/src/main/java/commands/AddModuleCommand.java +++ b/src/main/java/commands/AddModuleCommand.java @@ -2,6 +2,7 @@ import access.Access; import manager.admin.Admin; +import manager.admin.ModuleList; import manager.chapter.CardList; import manager.module.Module; import storage.Storage; @@ -9,23 +10,27 @@ public class AddModuleCommand extends Command { public static final String COMMAND_WORD = "addmodule"; - String moduleCode; + private final Module module; public AddModuleCommand(String moduleCode) { - this.moduleCode = moduleCode; + this.module = new Module(moduleCode); } @Override public void execute(CardList cards, Ui ui, Access access, Storage storage) { - if (!(access.getModuleLevel().equals(""))) { + if (access.isModuleLevel() || access.isChapterLevel()) { System.out.println("Sorry, you currently are in the module/chapter level, " - + "please go back to Admin level first."); + + "please go to admin level first."); return; } + Admin newAdmin = access.getAdmin(); - newAdmin.add(new Module(moduleCode)); + ModuleList modules = newAdmin.getModules(); + modules.addModule(module); + int moduleCount = modules.getModuleCount(); + ui.showModuleAdded(module, moduleCount); access.setAdmin(newAdmin); - storage.createModule(moduleCode); + storage.createModule(module.getModuleName()); } @Override diff --git a/src/main/java/commands/GoChapterCommand.java b/src/main/java/commands/GoChapterCommand.java index 1fd9770973..82a023a16e 100644 --- a/src/main/java/commands/GoChapterCommand.java +++ b/src/main/java/commands/GoChapterCommand.java @@ -1,8 +1,10 @@ package commands; +import manager.card.Card; import manager.chapter.CardList; import access.Access; import manager.chapter.Chapter; +import manager.module.ChapterList; import storage.Storage; import ui.Ui; @@ -20,19 +22,21 @@ public GoChapterCommand(String chapterCode) { @Override public void execute(CardList cards, Ui ui, Access access, Storage storage) { boolean isLevelExist = false; - ArrayList chapters = access.getModule().getChapter(); - for (Chapter chapter : chapters) { - if (chapterCode.equalsIgnoreCase(chapter.getChapter())) { + ChapterList chapters = access.getModule().getChapters(); + ArrayList allChapters = chapters.getAllChapters(); + for (Chapter chapter : allChapters) { + if (chapterCode.equalsIgnoreCase(chapter.getChapterName())) { access.setChapterLevel(chapterCode); isLevelExist = true; try { - Chapter newChapter = new Chapter(chapter.getChapter(), - storage.loadCard(access.getModuleLevel(), chapter.getChapter())); - access.setChapter(newChapter); + ArrayList allCards = storage.loadCard(access.getModuleLevel(), chapter.getChapterName()); + if (allCards.size() == 0) { + System.out.println("This is a new chapter, you can try to add flashcards inside!"); + } + chapter.setCards(allCards); + access.setChapter(chapter); } catch (FileNotFoundException e) { - Chapter newChapter = new Chapter(chapter.getChapter()); - access.setChapter(newChapter); - System.out.println("Hihi, seems like it is a new module, you can try to add chapter inside!"); + System.out.println("The chapter file cannot be found."); } break; } @@ -47,3 +51,4 @@ public boolean isExit() { return false; } } + diff --git a/src/main/java/commands/GoModuleCommand.java b/src/main/java/commands/GoModuleCommand.java index 1e6f31704d..026f59ced7 100644 --- a/src/main/java/commands/GoModuleCommand.java +++ b/src/main/java/commands/GoModuleCommand.java @@ -1,7 +1,9 @@ package commands; import access.Access; +import manager.admin.ModuleList; import manager.chapter.CardList; +import manager.chapter.Chapter; import manager.module.Module; import storage.Storage; import ui.Ui; @@ -20,18 +22,21 @@ public GoModuleCommand(String moduleCode) { @Override public void execute(CardList cards, Ui ui, Access access, Storage storage) { boolean isLevelExist = false; - ArrayList modules = access.getAdmin().getModules(); - for (Module module : modules) { - if (moduleCode.equalsIgnoreCase(module.getModule())) { + ModuleList modules = access.getAdmin().getModules(); + ArrayList allModules = modules.getAllModules(); + for (Module module : allModules) { + if (moduleCode.equalsIgnoreCase(module.getModuleName())) { access.setModuleLevel(moduleCode); isLevelExist = true; try { - Module newModule = new Module(module.getModule(), storage.loadChapter(module.getModule())); - access.setModule(newModule); + ArrayList chapters = storage.loadChapter(module.getModuleName()); + if (chapters.size() == 0) { + System.out.println("This is a new module, you can try to add chapters inside!"); + } + module.setChapters(chapters); + access.setModule(module); } catch (FileNotFoundException e) { - Module newModule = new Module(module.getModule()); - access.setModule(newModule); - System.out.println("Hihi, seems like it is a new module, you can try to add chapter inside!"); + System.out.println("The module folder cannot be found."); } break; } diff --git a/src/main/java/manager/admin/Admin.java b/src/main/java/manager/admin/Admin.java index 87fa4f02d4..50ef275714 100644 --- a/src/main/java/manager/admin/Admin.java +++ b/src/main/java/manager/admin/Admin.java @@ -5,28 +5,19 @@ import java.util.ArrayList; public class Admin { - protected ArrayList modules; - protected int moduleAmount = 0; + protected ModuleList modules; //if there is no storage public Admin() { - modules = new ArrayList<>(); - moduleAmount = 0; + modules = new ModuleList(); } //if there is storage public Admin(ArrayList modules) { - this.modules = new ArrayList<>(modules); - moduleAmount = modules.size(); + this.modules = new ModuleList(modules); } - public ArrayList getModules() { + public ModuleList getModules() { return modules; } - - public void add(Module module) { - modules.add(module); - modules.get(moduleAmount).doneAddModule(); - moduleAmount++; - } } diff --git a/src/main/java/manager/admin/ModuleList.java b/src/main/java/manager/admin/ModuleList.java new file mode 100644 index 0000000000..fa9c6e1ab1 --- /dev/null +++ b/src/main/java/manager/admin/ModuleList.java @@ -0,0 +1,29 @@ +package manager.admin; + +import manager.module.Module; + +import java.util.ArrayList; + +public class ModuleList { + private final ArrayList modules; + + public ModuleList() { + modules = new ArrayList<>(); + } + + public ModuleList(ArrayList modules) { + this.modules = modules; + } + + public void addModule(Module module) { + modules.add(module); + } + + public ArrayList getAllModules() { + return modules; + } + + public int getModuleCount() { + return modules.size(); + } +} diff --git a/src/main/java/manager/chapter/CardList.java b/src/main/java/manager/chapter/CardList.java index 393160a2f1..095cb206c3 100644 --- a/src/main/java/manager/chapter/CardList.java +++ b/src/main/java/manager/chapter/CardList.java @@ -8,7 +8,11 @@ public class CardList { private final ArrayList cards; public CardList() { - cards = new ArrayList<>(); + this.cards = new ArrayList<>(); + } + + public CardList(ArrayList cards) { + this.cards = new ArrayList<>(cards); } public void addCard(Card card) { diff --git a/src/main/java/manager/chapter/Chapter.java b/src/main/java/manager/chapter/Chapter.java index 145b223292..e9bccff7f1 100644 --- a/src/main/java/manager/chapter/Chapter.java +++ b/src/main/java/manager/chapter/Chapter.java @@ -6,40 +6,23 @@ public class Chapter { protected String chapterName; - protected ArrayList cards; - protected int cardAmount; - protected static int totalChapter = 0; + protected CardList cards; public Chapter(String chapterName) { this.chapterName = chapterName; - cards = new ArrayList(); - cardAmount = 0; - totalChapter++; + this.cards = new CardList(); } - @Override - public String toString() { - return "<" + chapterName + ">"; - } - - public Chapter(String chapterName, ArrayList cards) { - this.chapterName = chapterName; - this.cards = new ArrayList<>(cards); - cardAmount = cards.size(); - } - - public void add(Card card) { - cards.add(card); - cardAmount++; + public String getChapterName() { + return chapterName; } - public void doneAddChapter() { - System.out.println(" Got it. I've added this chapter:"); - System.out.println(" " + getChapter()); - System.out.println(" Now you have " + totalChapter + " chapters in the list."); + public void setCards(ArrayList cards) { + this.cards = new CardList(cards); } - public String getChapter() { + @Override + public String toString() { return chapterName; } } diff --git a/src/main/java/manager/module/ChapterList.java b/src/main/java/manager/module/ChapterList.java new file mode 100644 index 0000000000..01055df847 --- /dev/null +++ b/src/main/java/manager/module/ChapterList.java @@ -0,0 +1,29 @@ +package manager.module; + +import manager.chapter.Chapter; + +import java.util.ArrayList; + +public class ChapterList { + private final ArrayList chapters; + + public ChapterList() { + chapters = new ArrayList<>(); + } + + public ChapterList(ArrayList chapters) { + this.chapters = chapters; + } + + public void addChapter(Chapter chapter) { + chapters.add(chapter); + } + + public ArrayList getAllChapters() { + return chapters; + } + + public int getChapterCount() { + return chapters.size(); + } +} diff --git a/src/main/java/manager/module/Module.java b/src/main/java/manager/module/Module.java index 8ea658a96a..0c01387f37 100644 --- a/src/main/java/manager/module/Module.java +++ b/src/main/java/manager/module/Module.java @@ -5,46 +5,33 @@ import java.util.ArrayList; public class Module { - protected ArrayList chapters; - protected int chapterAmount; + protected ChapterList chapters; protected String moduleName; - protected static int totalModule = 0; public Module(String moduleName) { this.moduleName = moduleName; - chapters = new ArrayList(); - chapterAmount = 0; - totalModule++; + chapters = new ChapterList(); } public Module(String moduleName, ArrayList chapters) { this.moduleName = moduleName; - this.chapters = new ArrayList<>(chapters); - chapterAmount = chapters.size(); - totalModule++; + this.chapters = new ChapterList(chapters); } - public void add(Chapter chapter) { - chapters.add(chapter); - chapters.get(chapterAmount).doneAddChapter(); - chapterAmount++; - } - public void doneAddModule() { - System.out.println(" Got it. I've added this module:"); - System.out.println(" " + getModule()); - System.out.println(" Now you have " + totalModule + " modules in the list."); + public String getModuleName() { + return moduleName; } - public String getModule() { - return moduleName; + public ChapterList getChapters() { + return chapters; } public void setChapters(ArrayList chapters) { - this.chapters = chapters; + this.chapters = new ChapterList(chapters); } - public ArrayList getChapter() { - return chapters; + public String toString() { + return moduleName; } -} +} \ No newline at end of file diff --git a/src/main/java/storage/Storage.java b/src/main/java/storage/Storage.java index 71b02f55f5..472b458613 100644 --- a/src/main/java/storage/Storage.java +++ b/src/main/java/storage/Storage.java @@ -30,44 +30,71 @@ public Storage(String filePath) { //create the folder --> 'data/admin' public void createAdmin() { File f = new File(filePath); - boolean success = f.getParentFile().mkdir(); - System.out.println(filePath); - if (success) { - System.out.println("Successfully created new directory"); + System.out.println("Filepath: " + filePath); + + boolean dataDirExists = f.getParentFile().exists(); + boolean dataDirCreated = false; + if (!dataDirExists) { + dataDirCreated = f.getParentFile().mkdir(); + } else { + System.out.println("Directory " + f.getParentFile().getName() + " already exists"); + } + if (dataDirCreated) { + System.out.println("Successfully created new directory " + f.getParentFile().getName()); + } + + boolean adminDirExists = f.exists(); + boolean adminDirCreated = false; + if (!adminDirExists) { + adminDirCreated = f.mkdir(); } else { - System.out.println("Failed to create new directory"); + System.out.println("Directory " + f + " already exists"); + } + if (adminDirCreated) { + System.out.println("Successfully created new directory " + f); } } public void createModule(String moduleName) { File f = new File(filePath + "/" + moduleName); - boolean success = f.getParentFile().mkdir(); - //File f1 = f.getParentFile(); - //String v = f1.getAbsolutePath(); - //System.out.println(" getParentFile: " + v); - //System.out.println(" filePath: " + f.getPath()); - if (success) { - System.out.println(" Successfully created new directory " + moduleName); + boolean moduleDirExists = f.exists(); + boolean moduleDirCreated = false; + if (!moduleDirExists) { + moduleDirCreated = f.mkdir(); } else { - System.out.println(" Failed to create new directory"); + System.out.println(" Directory " + f + " already exists"); + } + if (moduleDirCreated) { + System.out.println(" Successfully created new directory " + f); } } public void createChapter(String chapterName, String moduleName) { - File f = new File(filePath + "/" + moduleName + "/" + chapterName + ".txt"); - boolean success = f.getParentFile().mkdir(); - if (success) { - System.out.println(" Successfully created new directory " + chapterName); - } else { - System.out.println(" Failed to create new directory"); + try { + File f = new File(filePath + "/" + moduleName + "/" + chapterName + ".txt"); + boolean chapterFileExists = f.exists(); + boolean chapterFileCreated = false; + if (!chapterFileExists) { + chapterFileCreated = f.createNewFile(); + } else { + System.out.println(" File " + f + " already exists"); + } + if (chapterFileCreated) { + System.out.println(" Successfully created new file " + chapterName + ".txt"); + } + } catch (IOException e) { + System.out.println("Error creating the file."); } } public ArrayList loadModule() throws FileNotFoundException { File f = new File(filePath); - ArrayList modules = new ArrayList(); - Scanner s = new Scanner(f); + boolean dirExists = f.exists(); + if (!dirExists) { + throw new FileNotFoundException(); + } + ArrayList modules = new ArrayList<>(); String[] contents = f.list(); System.out.println("List of files and directories in the specified directory:"); for (int i = 0; i < contents.length; i++) { @@ -79,9 +106,16 @@ public ArrayList loadModule() throws FileNotFoundException { public ArrayList loadChapter(String module) throws FileNotFoundException { File f = new File(filePath + "/" + module); - ArrayList chapters = new ArrayList(); - Scanner s = new Scanner(f); + boolean dirExists = f.exists(); + if (!dirExists) { + throw new FileNotFoundException(); + } + + ArrayList chapters = new ArrayList<>(); String[] contents = f.list(); + if (contents.length == 0) { + return chapters; + } System.out.println("List of files and directories in the specified directory:"); for (int i = 0; i < contents.length; i++) { String target = contents[i].replace(".txt", ""); @@ -93,7 +127,12 @@ public ArrayList loadChapter(String module) throws FileNotFoundExceptio public ArrayList loadCard(String module, String chapter) throws FileNotFoundException { File f = new File(filePath + "/" + module + "/" + chapter + ".txt"); - ArrayList cards = new ArrayList(); + boolean fileExists = f.exists(); + if (!fileExists) { + throw new FileNotFoundException(); + } + + ArrayList cards = new ArrayList<>(); Scanner s = new Scanner(f); int totalCards = 0; while (s.hasNext()) { diff --git a/src/main/java/ui/Ui.java b/src/main/java/ui/Ui.java index f343f69b07..161bfb2800 100644 --- a/src/main/java/ui/Ui.java +++ b/src/main/java/ui/Ui.java @@ -8,6 +8,8 @@ import commands.ReviseCommand; import manager.card.Card; +import manager.chapter.Chapter; +import manager.module.Module; import java.io.InputStream; import java.io.PrintStream; @@ -107,4 +109,16 @@ public void showHelpList() { out.println("4. " + AddCommand.MESSAGE_USAGE); out.println("5. " + ExitCommand.MESSAGE_USAGE); } + + public void showModuleAdded(Module module, int moduleCount) { + out.println(" Got it. I've added this module:"); + out.println(" " + module); + out.println(" Now you have " + moduleCount + " modules in the list."); + } + + public void showChapterAdded(Chapter chapter, int chapterCount) { + out.println(" Got it. I've added this chapter:"); + out.println(" " + chapter); + out.println(" Now you have " + chapterCount + " chapters in the list."); + } }