Skip to content

Commit

Permalink
Merge pull request #140 from sheexiong/master
Browse files Browse the repository at this point in the history
Add ListAmount and DeleteItem Command and Parser, adding new UI class to help format the output to CLI.
  • Loading branch information
sheexiong authored Mar 17, 2020
2 parents e2e3647 + d6a519e commit c7ab66e
Show file tree
Hide file tree
Showing 9 changed files with 177 additions and 11 deletions.
13 changes: 8 additions & 5 deletions src/main/java/seedu/eylah/Eylah.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package seedu.eylah;

import java.util.Scanner;
import java.util.logging.Logger;

import seedu.eylah.commons.core.LogsCenter;
Expand All @@ -12,6 +11,7 @@
import seedu.eylah.diettracker.logic.parser.exceptions.ParseException;
import seedu.eylah.diettracker.model.Model;
import seedu.eylah.diettracker.model.ModelManager;
import seedu.eylah.ui.Ui;

/**
* The main entry for the EYLAH.
Expand All @@ -27,8 +27,11 @@ public class Eylah {
protected seedu.eylah.expensesplitter.logic.Logic splitterLogic;
protected seedu.eylah.expensesplitter.model.Model splitterModel;

private Ui ui;

public Eylah() {
logger.info("=============================[ Initializing EYLAH ]===========================");
ui = new Ui();
}

/**
Expand All @@ -40,17 +43,17 @@ public Eylah() {
*/
public void run() {
boolean isExit = false;
Scanner scanner = new Scanner(System.in);
ui.showWelcome();
System.out.println("Enter mode (diet/splitting): ");
String input = scanner.nextLine();
String input = ui.readCommand();
if (input.equals("diet")) {
// Diet mode
logger.info("Entering Diet MODE.");
dietModel = new ModelManager();
dietLogic = new LogicManager(dietModel, null);
while (!isExit) {
System.out.println("Enter Diet Command: ");
input = scanner.nextLine();
input = ui.readCommand();
try {
CommandResult commandResult = dietLogic.execute(input);
// Here will print out the respond to user
Expand All @@ -67,7 +70,7 @@ public void run() {
splitterLogic = new seedu.eylah.expensesplitter.logic.LogicManager(splitterModel, null);
while (!isExit) {
System.out.println("Enter Splitting Command: ");
input = scanner.nextLine();
input = ui.readCommand();
try {
seedu.eylah.expensesplitter.logic.commands.CommandResult commandResult =
splitterLogic.execute(input);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ public AddItemCommand(Item item, ArrayList<Person> persons) {
@Override
public CommandResult execute(Model model) throws CommandException {
requireNonNull(model);

model.addEntry(toBeAdded);
return new CommandResult(String.format(MESSAGE_SUCCESS, toBeAdded));
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,42 @@
package seedu.eylah.expensesplitter.logic.commands;

import static java.util.Objects.requireNonNull;

import seedu.eylah.commons.core.index.Index;
import seedu.eylah.expensesplitter.logic.commands.exceptions.CommandException;
import seedu.eylah.expensesplitter.model.Model;

/**
* Delete item from the receipt.
* Deletes an item from the list of available items.
*/
public class DeleteItemCommand {
public class DeleteItemCommand extends Command {

public static final String COMMAND_WORD = "deleteItem";

public static final String MESSAGE_USAGE = COMMAND_WORD
+ ": Deletes the item identified by the index number used in the displayed item list.\n"
+ "Parameters: INDEX (must be a positive integer)\n"
+ "Example: " + COMMAND_WORD + " 1";

public static final String MESSAGE_DELETE_PERSON_SUCCESS = "Deleted Person: %1$s";

private final Index targetIndex;

public DeleteItemCommand(Index targetIndex) {
this.targetIndex = targetIndex;
}

@Override
public CommandResult execute(Model model) throws CommandException {
requireNonNull(model);
model.deleteEntry(targetIndex.getZeroBased());
return new CommandResult(String.format(MESSAGE_DELETE_PERSON_SUCCESS, targetIndex.getOneBased()));
}

@Override
public boolean equals(Object other) {
return other == this // short circuit if same object
|| (other instanceof DeleteItemCommand // instanceof handles nulls
&& targetIndex.equals(((DeleteItemCommand) other).targetIndex)); // state check
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package seedu.eylah.expensesplitter.logic.commands;

import static java.util.Objects.requireNonNull;

import seedu.eylah.expensesplitter.model.Model;

/**
* Lists all users with their amount.
*/
public class ListAmountCommand extends Command {

public static final String COMMAND_WORD = "listamount";

public static final String MESSAGE_SUCCESS = "Listed all person with their amount.";

@Override
public CommandResult execute(Model model) {
requireNonNull(model);
model.listAmount();
return new CommandResult(MESSAGE_SUCCESS);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package seedu.eylah.expensesplitter.logic.parser;

import static seedu.eylah.commons.core.Messages.MESSAGE_INVALID_COMMAND_FORMAT;

import seedu.eylah.commons.core.index.Index;
import seedu.eylah.expensesplitter.logic.commands.DeleteItemCommand;
import seedu.eylah.expensesplitter.logic.parser.exceptions.ParseException;

/**
* Parses input arguments and creates a new DeleteItemCommand object
*/
public class DeleteItemCommandParser implements Parser<DeleteItemCommand> {

/**
* Parses the given {@code String} of arguments in the context of the DeleteCommand
* and returns a DeleteCommand object for execution.
* @throws ParseException if the user input does not conform the expected format
*/
public DeleteItemCommand parse(String args) throws ParseException {
try {
Index index = ParserUtil.parseIndex(args);
return new DeleteItemCommand(index);
} catch (ParseException pe) {
throw new ParseException(
String.format(MESSAGE_INVALID_COMMAND_FORMAT, DeleteItemCommand.MESSAGE_USAGE), pe);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package seedu.eylah.expensesplitter.logic.parser;

import static seedu.eylah.commons.core.Messages.MESSAGE_INVALID_COMMAND_FORMAT;

import seedu.eylah.expensesplitter.logic.commands.ListAmountCommand;
import seedu.eylah.expensesplitter.logic.commands.ListReceiptCommand;
import seedu.eylah.expensesplitter.logic.parser.exceptions.ParseException;

/**
* Parses input arguments and creates a new ListAmountCommand object.
*/
public class ListAmountCommandParser implements Parser<ListAmountCommand> {

/**
* Parses the given {@code String} of arguments in the context of the ListReceiptCommand
* and returns an ListReceiptCommand object for execution.
* @throws ParseException if the user input does not conform the expected format
*/
@Override
public ListAmountCommand parse(String args) throws ParseException {

if (args.equals("listamount")) {
return new ListAmountCommand();
} else {
throw new ParseException(String.format(MESSAGE_INVALID_COMMAND_FORMAT, ListReceiptCommand.MESSAGE_USAGE));
}

}
}
9 changes: 9 additions & 0 deletions src/main/java/seedu/eylah/expensesplitter/model/Model.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,17 @@ public interface Model {
*/
void deleteEntry(int index);

/**
* Updates the amount the Person owes you. If amount is $0 then the Person is deleted.
*
* @param p the person to pay
*/
void paidPerson(Person p);

/**
* Command for listing all person with their amount.
*/
void listAmount();

/**
* Command for listing all items in that receipt.
Expand Down
15 changes: 12 additions & 3 deletions src/main/java/seedu/eylah/expensesplitter/model/ModelManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,6 @@
import seedu.eylah.commons.core.LogsCenter;
import seedu.eylah.expensesplitter.model.person.Person;

//import static java.util.Objects.requireNonNull;
//import static seedu.eylah.commons.util.CollectionUtil.requireAllNonNull;

/**
* Represents the in-memory model of the address book data.
*/
Expand Down Expand Up @@ -64,6 +61,18 @@ public void listReceipt() {

}

/**
* Command for listing all person with their amount.
*/
@Override
public void listAmount() {

}

/**
* Intentionally left empty.
* To be implemented later.
*/
@Override
public void paidPerson(Person p) {

Expand Down
31 changes: 31 additions & 0 deletions src/main/java/seedu/eylah/ui/Ui.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,39 @@
package seedu.eylah.ui;

import java.util.Scanner;

/**
* Main CLI Ui component for the application.
*/
public class Ui {

private static final String WELCOME_MESSAGE = "Welcome to EYLAH, this is a debug version of interface :)";
private static final String USAGE = "To use the application, enter `diet` or "
+ "`splitter` to enter the different mode.";
private static final String LS = System.lineSeparator();

private Scanner scanner;

public Ui() {
scanner = new Scanner(System.in);
}

private void showToUser(String... message) {
for (String m : message) {
System.out.println(m.replace("\n", LS));
}
}

public String readCommand() {
return scanner.nextLine();
}

public void showWelcome() {
showToUser(WELCOME_MESSAGE, USAGE);
}

public static String formatListAmount() {
return "";
}

}

0 comments on commit c7ab66e

Please sign in to comment.