Skip to content

Commit

Permalink
Merge pull request #72 from haoyangw/enhancement-refactorAtomicHabits
Browse files Browse the repository at this point in the history
Refactor atomic habits feature
  • Loading branch information
nichyjt authored Mar 15, 2023
2 parents 35ae004 + 02def75 commit 360bcd7
Show file tree
Hide file tree
Showing 15 changed files with 722 additions and 226 deletions.
126 changes: 112 additions & 14 deletions src/main/java/wellnus/atomichabit/command/AddCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,128 @@

import wellnus.atomichabit.feature.AtomicHabit;
import wellnus.atomichabit.feature.AtomicHabitList;
import wellnus.atomichabit.feature.AtomicHabitManager;
import wellnus.command.Command;
import wellnus.command.CommandParser;
import wellnus.exception.BadCommandException;
import wellnus.ui.TextUi;

import java.util.HashMap;

public class AddCommand extends Command {
public static final String COMMAND_WORD = "add";
private static final String COMMAND_DETAILED_DESCRIPTION = "";
private static final String COMMAND_KEYWORD = "add";
private static final String COMMAND_INVALID_ARGUMENTS_MESSAGE = "Wrong arguments given to 'hb add'!";
private static final String COMMAND_NAME_ARGUMENT = "name";
private static final int COMMAND_NUM_OF_ARGUMENTS = 2;
private static final String COMMAND_SUPPORTED_ARGUMENTS = "";
private static final String COMMAND_WRONG_KEYWORD_MESSAGE = "Wrong command issued by the user, expected 'hb add'?";
private static final String FEEDBACK_STRING_ONE = "Yay! You have added a new habit:";
private static final String FEEDBACK_STRING_TWO = " was successfully added";
private String description;
private static final String FEEDBACK_STRING_TWO = "was successfully added";
private final AtomicHabitList atomicHabits;
private final CommandParser commandParser;
private final TextUi textUi;

public AddCommand(HashMap<String, String> arguments, AtomicHabitList atomicHabits) {
super(arguments);
this.atomicHabits = atomicHabits;
this.commandParser = new CommandParser();
this.textUi = new TextUi();
}

private AtomicHabitList getAtomicHabits() {
return atomicHabits;
}

private TextUi getTextUi() {
return textUi;
}

/**
* Identifies this Command's keyword. Override this in subclasses so
* toString() returns the correct String representation.
*
* @return String Keyword of this Command
*/
@Override
protected String getCommandKeyword() {
return COMMAND_KEYWORD;
}

public AddCommand(String description) {
this.description = description;
/**
* Returns a detailed user-friendly description of what this specific command does.
*
* @return String Detailed explanation of this command
*/
@Override
protected String getDetailedDescription() {
return COMMAND_DETAILED_DESCRIPTION;
}

/**
* Identifies the feature that this Command is associated with. Override
* this in subclasses so toString() returns the correct String representation.
*
* @return String Keyword for the feature associated with this Command
*/
@Override
protected String getFeatureKeyword() {
return AtomicHabitManager.FEATURE_NAME;
}

/**
* Returns all the supported arguments for this Command.
*
* @return String All supported arguments for this Command
*/
@Override
protected String getSupportedCommandArguments() {
return COMMAND_SUPPORTED_ARGUMENTS;
}

/**
* Adds of the new atomic habit into our list of atomic habits.
*
* After that, print a message telling the user what the new habit added is
*/
@Override
public void execute() {
try {
validateCommand(super.getArguments());
} catch (BadCommandException badCommandException) {
String NO_ADDITIONAL_MESSAGE = "";
this.getTextUi().printErrorFor(badCommandException, NO_ADDITIONAL_MESSAGE);
return;
}
String name = super.getArguments().get(AddCommand.COMMAND_NAME_ARGUMENT);
AtomicHabit habit = new AtomicHabit(name);
this.getAtomicHabits().addAtomicHabit(habit);
String messageToUser = FEEDBACK_STRING_ONE + System.lineSeparator();
messageToUser += String.format("'%s' %s", habit, FEEDBACK_STRING_TWO);
getTextUi().printOutputMessage(messageToUser);
}

/**
* Method to execute adding of an atomic habit feature into atomicHabitList
* Validate the arguments and payloads from a commandMap generated by CommandParser.<br>
* <br>
* If no exceptions are thrown, command is valid.
*
* @param atomicHabits
* @return CommandResult that contains feedback to the user
* @param arguments Argument-Payload map generated by CommandParser
* @throws BadCommandException If the arguments have any issues
*/
@Override
public CommandResult execute(AtomicHabitList atomicHabits) {
AtomicHabit habit = new AtomicHabit(description);
atomicHabits.addAtomicHabit(habit);
return new CommandResult(FEEDBACK_STRING_ONE
+ System.lineSeparator() + "'"
+ habit + "'" + FEEDBACK_STRING_TWO);
public void validateCommand(HashMap<String, String> arguments) throws BadCommandException {
if (arguments.size() != AddCommand.COMMAND_NUM_OF_ARGUMENTS) {
throw new BadCommandException(AddCommand.COMMAND_INVALID_ARGUMENTS_MESSAGE);
}
String givenCommandKeyword = arguments.get(AtomicHabitManager.FEATURE_NAME);
if (!givenCommandKeyword.equals(AddCommand.COMMAND_KEYWORD)) {
throw new BadCommandException(AddCommand.COMMAND_WRONG_KEYWORD_MESSAGE);
}
String name = arguments.get(AddCommand.COMMAND_NAME_ARGUMENT);
if (name.isEmpty()) {
throw new BadCommandException(AddCommand.COMMAND_INVALID_ARGUMENTS_MESSAGE);
}
}
}

Expand Down
16 changes: 0 additions & 16 deletions src/main/java/wellnus/atomichabit/command/Command.java

This file was deleted.

25 changes: 0 additions & 25 deletions src/main/java/wellnus/atomichabit/command/CommandResult.java

This file was deleted.

120 changes: 108 additions & 12 deletions src/main/java/wellnus/atomichabit/command/ExitCommand.java
Original file line number Diff line number Diff line change
@@ -1,29 +1,125 @@
package wellnus.atomichabit.command;

import wellnus.atomichabit.feature.AtomicHabitList;
import wellnus.atomichabit.feature.AtomicHabitManager;
import wellnus.command.Command;
import wellnus.exception.BadCommandException;
import wellnus.ui.TextUi;

import java.util.HashMap;

public class ExitCommand extends Command {
public static final String COMMAND_WORD = "exit";
public static final String MESSAGE_EXIT = "Thank you for using atomic habits. Do not forget about me!";
private static final int COMMAND_NUM_OF_ARGUMENTS = 1;
private static final String COMMAND_INVALID_ARGUMENTS_MESSAGE = "That is not a valid exit command for "
+ "atomic habits!";
private static final String COMMAND_INVALID_COMMAND_MESSAGE = "Wrong command given for exit!";
private static final String COMMAND_KEYWORD = "exit";
private static final String COMMAND_DETAILED_DESCRIPTION = "";
private static final String COMMAND_SUPPORTED_ARGUMENTS = "";
private static final String EXIT_MESSAGE = "Thank you for using atomic habits. Do not forget about me!";
private final TextUi textUi;

public ExitCommand(HashMap<String, String> arguments) {
super(arguments);
this.textUi = new TextUi();
}

private TextUi getTextUi() {
return textUi;
}

/**
* Check if a ByeCommand is executed and user wants to exit program
*
* @param command User command
* @return true If user wants to exit false if not
*/
public static boolean isExit(Command command) {
return command instanceof ExitCommand;
}

/**
* Executes the command and returns the result.
* Identifies this Command's keyword. Override this in subclasses so
* toString() returns the correct String representation.
*
* @return CommandResult which is a exit message to user
* @return String Keyword of this Command
*/
@Override
public CommandResult execute(AtomicHabitList atomicHabits) {
return new CommandResult(MESSAGE_EXIT);
protected String getCommandKeyword() {
return COMMAND_KEYWORD;
}

/**
* Check if a ByeCommand is executed and user wants to exit program
* Returns a detailed user-friendly description of what this specific command does.
*
* @param command user command
* @return true if user wants to exit false if not
* @return String Detailed explanation of this command
*/
public static boolean isExit(Command command) {
return command instanceof ExitCommand;
@Override
protected String getDetailedDescription() {
return COMMAND_DETAILED_DESCRIPTION;
}

/**
* Identifies the feature that this Command is associated with. Override
* this in subclasses so toString() returns the correct String representation.
*
* @return String Keyword for the feature associated with this Command
*/
@Override
protected String getFeatureKeyword() {
return AtomicHabitManager.FEATURE_NAME;
}

/**
* Returns all the supported arguments for this Command.
*
* @return String All supported arguments for this Command
*/
@Override
protected String getSupportedCommandArguments() {
return COMMAND_SUPPORTED_ARGUMENTS;
}

/**
* Prints the exit message for the atomic habits feature on the user's screen.
*/
@Override
public void execute() {
try {
validateCommand(super.getArguments());
} catch (BadCommandException badCommandException) {
String NO_ADDITIONAL_MESSAGE = "";
this.getTextUi().printErrorFor(badCommandException, NO_ADDITIONAL_MESSAGE);
return;
}
getTextUi().printOutputMessage(EXIT_MESSAGE);
}

/**
* Validate the arguments and payloads from a commandMap generated by CommandParser <br>
* <br>
* The validation logic and strictness is up to the implementer. <br>
* <br>
* As a guideline, <code>isValidCommand</code> should minimally: <br>
* <li>Verify that ALL MANDATORY arguments exist</li>
* <li>Verify that ALL MANDATORY payloads exist</li>
* <li>Safely verify the payload type (int, date, etc should be properly processed)</li>
* <br>
* Additionally, payload value cleanup (such as trimming) is also possible. <br>
* As Java is pass (object reference) by value, any changes made to commandMap
* will persist out of the function call.
*
* @param arguments Argument-Payload map generated by CommandParser
* @throws BadCommandException if the commandMap has any issues
*/
@Override
public void validateCommand(HashMap<String, String> arguments) throws BadCommandException {
if (arguments.size() != ExitCommand.COMMAND_NUM_OF_ARGUMENTS) {
throw new BadCommandException(ExitCommand.COMMAND_INVALID_ARGUMENTS_MESSAGE);
}
String commandKeyword = arguments.get(AtomicHabitManager.FEATURE_NAME);
if (!commandKeyword.equals(ExitCommand.COMMAND_KEYWORD)) {
throw new BadCommandException(ExitCommand.COMMAND_INVALID_COMMAND_MESSAGE);
}
}
}

Expand Down
14 changes: 0 additions & 14 deletions src/main/java/wellnus/atomichabit/command/InvalidCommand.java

This file was deleted.

Loading

0 comments on commit 360bcd7

Please sign in to comment.