Skip to content

Commit

Permalink
Fixes some bugs
Browse files Browse the repository at this point in the history
- Throws error when no parameter commands are called with command
- Fixes program not exiting when a fatal error occurs when running
- Allows save data to be created through nested folders
- Fixed notification of when parsing corrupted lines
  • Loading branch information
Eclipse-Dominator committed Sep 19, 2022
1 parent 72ab977 commit 21b3bd9
Show file tree
Hide file tree
Showing 10 changed files with 131 additions and 43 deletions.
12 changes: 10 additions & 2 deletions src/main/java/duke/command/ByeCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,23 @@

import java.io.IOException;

import duke.exceptions.UnknownCommandException;
import duke.inputoutput.DukeCliSettings;
import duke.inputoutput.DukeIo;
import duke.util.ParsedData;
import duke.util.Storage;
import duke.util.TaskList;

/**
* Command class that exit the program. When bye is entered
*/
public class ByeCommand implements Command {
public class ByeCommand extends NoParamCommand {
private static final String OUTRO = "Pff.. Not like I want to see you again";

public ByeCommand(ParsedData data) {
super(data);
}

/**
* Returns true when asked if program should exit.
*
Expand All @@ -27,9 +33,11 @@ public boolean isExit() {
* {@inheritDoc} Prints goodbye message and exits program.
*
* @throws IOException raised if an error occured when saving
* @throws UnknownCommandException when extra parameters is included
*/
@Override
public void execute(TaskList tasks, DukeIo io, Storage storage) throws IOException {
public void execute(TaskList tasks, DukeIo io, Storage storage) throws IOException, UnknownCommandException {
checkSingleArgumentGuard();
io.printTask(OUTRO, DukeCliSettings.WRAP_INDENT);
storage.saveTasks(tasks);
}
Expand Down
13 changes: 11 additions & 2 deletions src/main/java/duke/command/ListCommand.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
package duke.command;

import duke.exceptions.UnknownCommandException;
import duke.inputoutput.DukeIo;
import duke.util.ParsedData;
import duke.util.Storage;
import duke.util.TaskList;

/**
* Command to list out all the current tasks.
*/
public class ListCommand implements Command {
public class ListCommand extends NoParamCommand {

public ListCommand(ParsedData data) {
super(data);
}

/**
* {@inheritDoc} List command does not exit
Expand All @@ -19,9 +25,12 @@ public boolean isExit() {

/**
* Prints out all the current tasks added.
*
* @throws UnknownCommandException when extra parameters is included
*/
@Override
public void execute(TaskList tasks, DukeIo io, Storage storage) {
public void execute(TaskList tasks, DukeIo io, Storage storage) throws UnknownCommandException {
checkSingleArgumentGuard();
io.printNumberedList(tasks.getTasks());
}

Expand Down
29 changes: 29 additions & 0 deletions src/main/java/duke/command/NoParamCommand.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package duke.command;

import duke.exceptions.UnknownCommandException;
import duke.util.ParsedData;

/**
* Abstract class to handle commands that requires no argument
*/
abstract class NoParamCommand implements Command {

private boolean invalid;

NoParamCommand(ParsedData data) {
if (data.description.isEmpty()) {
invalid = false;
} else {
invalid = true;
}
}

/**
* Guards to ensure commands contains no extra parameters
*/
protected void checkSingleArgumentGuard() throws UnknownCommandException {
if (invalid) {
throw new UnknownCommandException();
}
}
}
13 changes: 11 additions & 2 deletions src/main/java/duke/command/ResetAliasCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,23 @@

import java.io.IOException;

import duke.exceptions.UnknownCommandException;
import duke.inputoutput.DukeIo;
import duke.util.ParsedData;
import duke.util.Storage;
import duke.util.TaskList;

/**
* Command class that resets set aliases
*/
public class ResetAliasCommand implements Command {
public class ResetAliasCommand extends NoParamCommand {

private static final String OUTRO = "Back to beginning!";

public ResetAliasCommand(ParsedData data) {
super(data);
}

/**
* Returns true when asked if program should exit.
*
Expand All @@ -26,9 +33,11 @@ public boolean isExit() {
* {@inheritDoc} Resets all added aliases.
*
* @throws IOException raised if an error occured when saving
* @throws UnknownCommandException when extra parameters is included
*/
@Override
public void execute(TaskList tasks, DukeIo io, Storage storage) throws IOException {
public void execute(TaskList tasks, DukeIo io, Storage storage) throws IOException, UnknownCommandException {
checkSingleArgumentGuard();
io.printTask(OUTRO);
CommandSelector.reset();
}
Expand Down
16 changes: 14 additions & 2 deletions src/main/java/duke/command/SwapFaceCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@
import duke.exceptions.GuiOnlyException;
import duke.exceptions.ImageDownloadFailedException;
import duke.exceptions.OperatonIsStillRunningException;
import duke.exceptions.UnknownCommandException;
import duke.gui.GuiDataController;
import duke.inputoutput.DukeGuiIo;
import duke.inputoutput.DukeIo;
import duke.util.ParsedData;
import duke.util.Storage;
import duke.util.TaskList;
import javafx.concurrent.Task;
Expand All @@ -17,14 +19,18 @@
/**
* Command to list out all the current tasks.
*/
public class SwapFaceCommand implements Command {
public class SwapFaceCommand extends NoParamCommand {
private static final String RESPONSE = "I'm gonna replace us! We are not real after all! Goodbye!";
private static final String SUCCESS = "Nice to meet you, I am duke.";
private static final String ANIME_FAKE_IMAGE = "https://www.thiswaifudoesnotexist.net/example-%d.jpg";
private static final String HUMAN_FAKE_IMAGE = "https://thispersondoesnotexist.com/image";

private static boolean isRunning = false;

public SwapFaceCommand(ParsedData data) {
super(data);
}

/**
* {@inheritDoc} List command does not exit
*/
Expand All @@ -35,10 +41,16 @@ public boolean isExit() {

/**
* Prints out all the current tasks added.
*
* @throws GuiOnlyException when called via CLI mode
* @throws OperatonIsStillRunningException when another swap face command is running
* @throws UnknownCommandException when extra parameters is included
*/
@Override
public void execute(TaskList tasks, DukeIo io, Storage storage)
throws GuiOnlyException, OperatonIsStillRunningException {
throws GuiOnlyException, OperatonIsStillRunningException, UnknownCommandException {

checkSingleArgumentGuard();
if (!(io instanceof DukeGuiIo)) {
throw new GuiOnlyException();
}
Expand Down
5 changes: 5 additions & 0 deletions src/main/java/duke/inputoutput/DukeAbstractIo.java
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,11 @@ public void printError(Exception e) {
printTask(String.format("🙄 OOPS!!! %s", e.getMessage()));
}

@Override
public void printError(String msg) {
printError(new Exception(msg));
}

protected boolean isBitFlag(int bitsValue, DukeCliSettings flagEnum) {
return (bitsValue & flagEnum.value) == flagEnum.value;
}
Expand Down
7 changes: 7 additions & 0 deletions src/main/java/duke/inputoutput/DukeIo.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,13 @@ public interface DukeIo {
*/
void printError(Exception e);

/**
* Prints the message in the format of an exception
*
* @param msg message to print
*/
void printError(String msg);

/**
* Prints Text with selected features
*
Expand Down
25 changes: 8 additions & 17 deletions src/main/java/duke/main/Duke.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@
*/
public class Duke {
private static final String LOGO =
"Welcome to\n" + " ____ _ \n" + "| _ \\ _ _| | _____ \n" + "| | | | | | | |/ / _ \\\n"
+ "| |_| | |_| | < __/\n" + "|____/ \\__,_|_|\\_\\___|\n" + " Chatbot!\n";
"Welcome to\n" + " ____ _ \n" + "| _ \\ _ _| | _____ \n" + "| | | | | | | |/ / _ \\\n"
+ "| |_| | |_| | < __/\n" + "|____/ \\__,_|_|\\_\\___|\n" + " Chatbot!\n";

private static final String INTRO = "Hey hey hey! I'm Duke\n" + "What can I do for you?";

Expand Down Expand Up @@ -48,9 +48,11 @@ public boolean handleInput(String txt) {
c.execute(tasks, userInputOutput, dukeData);
} catch (DukeException e) {
userInputOutput.printError(e);
return true;
} catch (IOException e) {
userInputOutput.printError(e);
return true;
userInputOutput.printError(FATAL_EXIT);
return false;
}

return !c.isExit();
Expand Down Expand Up @@ -84,18 +86,7 @@ public static Duke createApplication() {
* @return returns an instance of Duke
*/
public static Duke createApplication(DukeIo userIo) {
Storage dukeData;
TaskList tasks;
try {
dukeData = Storage.createStorage();
tasks = new TaskList(dukeData.readFile());
} catch (IOException e) {
userIo.printError(e);
userIo.printTask(FATAL_EXIT);
return null;
}

return new Duke(tasks, dukeData, userIo);
return createApplication(userIo, "");
}

/**
Expand All @@ -110,10 +101,10 @@ public static Duke createApplication(DukeIo userIo, String filePath) {
TaskList tasks;
try {
dukeData = Storage.createStorage(filePath);
tasks = new TaskList(dukeData.readFile());
tasks = new TaskList(dukeData.readFile(userIo));
} catch (IOException e) {
userIo.printError(e);
userIo.printTask(FATAL_EXIT);
userIo.printError(FATAL_EXIT);
return null;
}

Expand Down
12 changes: 6 additions & 6 deletions src/main/java/duke/util/DataParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,13 @@ public static Command dataToCommand(ParsedData data) {

switch (CommandSelector.getCs().getCommand(data.command)) {
case BYE:
return new ByeCommand();
return new ByeCommand(data);
case LIST:
return new ListCommand();
return new ListCommand(data);
case SWAP:
return new SwapFaceCommand(data);
case RESETALIAS:
return new ResetAliasCommand(data);
case MARK:
return new MarkCommand(data);
case UNMARK:
Expand All @@ -94,10 +98,6 @@ public static Command dataToCommand(ParsedData data) {
return new AliasCommand(data);
case DELETECOMMAND:
return new DeleteAliasCommand(data);
case SWAP:
return new SwapFaceCommand();
case RESETALIAS:
return new ResetAliasCommand();
case INVALID:
default:
return new InvalidCommand();
Expand Down
42 changes: 30 additions & 12 deletions src/main/java/duke/util/Storage.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import java.util.Scanner;

import duke.exceptions.CorruptedLineException;
import duke.inputoutput.DukeIo;
import duke.task.Task;

/**
Expand All @@ -17,6 +18,7 @@
public class Storage {

private static final String DEFAULT_SAVE_PATH = "data/SavedData.duke";
private static final String PARSE_FAIL = "I was unable to parse line %s of the save file!";
private File file;

private Storage(File file) {
Expand All @@ -31,12 +33,11 @@ private Storage(File file) {
* @throws IOException Throws if pathing cannot exist.
*/
public static Storage createStorage(String path) throws IOException {
File newFile = new File(path);
File parentFolder = newFile.getParentFile();
if (parentFolder != null) {
parentFolder.mkdir();
if (path.trim().isEmpty()) {
path = DEFAULT_SAVE_PATH;
}
newFile.createNewFile();
File newFile = new File(path);
ensureExistance(newFile);
return new Storage(newFile);
}

Expand All @@ -50,13 +51,24 @@ public static Storage createStorage() throws IOException {
return createStorage(DEFAULT_SAVE_PATH);
}

private static void ensureExistance(File file) throws IOException {
if (file.exists()) {
return;
}
File parentFolder = file.getParentFile();
if (parentFolder != null) {
parentFolder.mkdirs();
}
file.createNewFile();
}

/**
* Read the save file and convert it to a list of Task.
*
* @return List of Tasks
* @throws FileNotFoundException Throws when save file does not exist
*/
public List<Task> readFile() throws FileNotFoundException {
public List<Task> readFile(DukeIo io) throws FileNotFoundException {
List<Task> ret = new ArrayList<>();
List<Integer> corruptedLines = new ArrayList<>();

Expand All @@ -75,6 +87,16 @@ public List<Task> readFile() throws FileNotFoundException {
}
}
sc.close();
if (corruptedLines.size() == 0) {
return ret;
}
StringBuilder joinedList = corruptedLines.stream().collect(StringBuilder::new, (sb, num) -> {
sb.append(num);
sb.append(", ");
}, StringBuilder::append);
// removes the ", " of the last string
joinedList.setLength(joinedList.length() - 2);
io.printError(String.format(PARSE_FAIL, joinedList.toString()));
return ret;
}

Expand All @@ -85,9 +107,7 @@ public List<Task> readFile() throws FileNotFoundException {
* @throws IOException Throws when save file doesn't exist
*/
public void saveData(ParsedData[] dataList) throws IOException {
if (!file.exists()) {
file.createNewFile();
}
ensureExistance(file);

assert file.exists();
StringBuilder sb = new StringBuilder();
Expand Down Expand Up @@ -117,9 +137,7 @@ public void saveTasks(TaskList tl) throws IOException {
* @throws IOException Throws when save file is missing
*/
public void saveTask(Task task) throws IOException {
if (!file.exists()) {
file.createNewFile();
}
ensureExistance(file);
assert file.exists();

FileWriter fw = new FileWriter(file, true);
Expand Down

0 comments on commit 21b3bd9

Please sign in to comment.