Skip to content

Commit

Permalink
Merge branch 'delete_Ver.2'
Browse files Browse the repository at this point in the history
  • Loading branch information
jingrui-who committed Oct 21, 2016
2 parents c07cb97 + fa4b1ca commit 3b26a35
Show file tree
Hide file tree
Showing 2 changed files with 101 additions and 66 deletions.
130 changes: 80 additions & 50 deletions src/main/java/seedu/address/logic/commands/DeleteCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

import seedu.address.commons.core.Messages;
import seedu.address.commons.core.UnmodifiableObservableList;
import seedu.address.commons.exceptions.IllegalValueException;
import seedu.address.model.task.ReadOnlyTask;
import seedu.address.model.task.UniqueTaskList.TaskNotFoundException;

Expand All @@ -26,76 +27,105 @@ public class DeleteCommand extends Command {

public static final String MESSAGE_DELETE_TASK_SUCCESS = "Deleted task: %1$s";

public final ArrayList<String> targetIndexes;

public DeleteCommand(ArrayList<String> targetIndexes) {
this.targetIndexes = targetIndexes;
private static final String MESSAGE_CATEGORY_CONSTRAINTS = "The Task Category provided is invalid!\n"
+ "Valid Categories: [E, D, T]\n"
+ "Example: " + COMMAND_WORD + " T1, E2, D3\n"
+ "Example: " + COMMAND_WORD + " T1-10";

public final ArrayList<Integer> targetIndexesE = new ArrayList<Integer>();
public final ArrayList<Integer> targetIndexesD = new ArrayList<Integer>();
public final ArrayList<Integer> targetIndexesT = new ArrayList<Integer>();
public final ArrayList<String> pass;


public DeleteCommand(ArrayList<String> targetIndexes) throws IllegalValueException {
pass = targetIndexes;
for(int i= 0; i < targetIndexes.size(); i++){
String temp = targetIndexes.get(i);
String stringIdx = temp.substring(1);
Integer intIdx = Integer.valueOf(stringIdx);
if(temp.charAt(0)=='E'){
targetIndexesE.add(intIdx);
}
else if(temp.charAt(0)=='D'){
targetIndexesD.add(intIdx);
}
else if(temp.charAt(0)=='T'){
targetIndexesT.add(intIdx);
}
else{
throw new IllegalValueException(MESSAGE_CATEGORY_CONSTRAINTS);
}
}
}


@Override
public CommandResult execute() {

ArrayList<String> pass = new ArrayList<String>(targetIndexes);
Collections.sort(targetIndexes);
Collections.reverse(targetIndexes);
//System.out.println(targetIndexes);
//System.out.println(pass);

UnmodifiableObservableList<ReadOnlyTask> lastShownEventList = model.getFilteredEventList();
UnmodifiableObservableList<ReadOnlyTask> lastShownDeadlineList = model.getFilteredDeadlineList();
UnmodifiableObservableList<ReadOnlyTask> lastShownTodoList = model.getFilteredTodoList();
//ArrayList<ReadOnlyTask> pass = new ArrayList<ReadOnlyTask>();

for(int i =0; i < targetIndexes.size(); i++){
if(Character.toUpperCase(targetIndexes.get(i).charAt(0))=='E'){
if (lastShownEventList.size() < Character.getNumericValue(targetIndexes.get(i).charAt(1))) {
if(targetIndexesE.size()>0){
Collections.sort(targetIndexesE);
Collections.reverse(targetIndexesE);
for(int i=0; i<targetIndexesE.size();i++){
Integer idx = targetIndexesE.get(i);
if (lastShownEventList.size() < idx) {
indicateAttemptToExecuteIncorrectCommand();
return new CommandResult(Messages.MESSAGE_INVALID_TASK_DISPLAYED_INDEX);
}
ReadOnlyTask taskToDelete = lastShownEventList.get(idx-1);
try {
model.deleteTask(taskToDelete);
} catch (TaskNotFoundException e) {
indicateAttemptToExecuteIncorrectCommand();
return new CommandResult(Messages.MESSAGE_INVALID_TASK_DISPLAYED_INDEX);
}
}
}

ReadOnlyTask taskToDelete = lastShownEventList.get(Character.getNumericValue(targetIndexes.get(i).charAt(1)) - 1);

if(targetIndexesD.size()>0){
Collections.sort(targetIndexesD);
Collections.reverse(targetIndexesD);
for(int i=0; i<targetIndexesD.size();i++){
Integer idx = targetIndexesD.get(i);
if (lastShownDeadlineList.size() < idx) {
indicateAttemptToExecuteIncorrectCommand();
return new CommandResult(Messages.MESSAGE_INVALID_TASK_DISPLAYED_INDEX);
}
ReadOnlyTask taskToDelete = lastShownDeadlineList.get(idx-1);
try {
model.deleteTask(taskToDelete);
} catch (TaskNotFoundException e) {
indicateAttemptToExecuteIncorrectCommand();
return new CommandResult(Messages.MESSAGE_INVALID_TASK_DISPLAYED_INDEX);
}
}
}

if(targetIndexesT.size()>0){
Collections.sort(targetIndexesT);
Collections.reverse(targetIndexesT);
for(int i=0; i<targetIndexesT.size();i++){
Integer idx = targetIndexesT.get(i);
if (lastShownTodoList.size() < idx) {
indicateAttemptToExecuteIncorrectCommand();
return new CommandResult(Messages.MESSAGE_INVALID_TASK_DISPLAYED_INDEX);
}

ReadOnlyTask taskToDelete = lastShownTodoList.get(idx-1);

try {
model.deleteTask(taskToDelete);
} catch (TaskNotFoundException pnfe) {
assert false : "The target task cannot be missing";
} catch (TaskNotFoundException e) {
indicateAttemptToExecuteIncorrectCommand();
return new CommandResult(Messages.MESSAGE_INVALID_TASK_DISPLAYED_INDEX);
}
}
else if(Character.toUpperCase(targetIndexes.get(i).charAt(0))=='D'){
if (lastShownDeadlineList.size() < Character.getNumericValue(targetIndexes.get(i).charAt(1))) {
indicateAttemptToExecuteIncorrectCommand();
return new CommandResult(Messages.MESSAGE_INVALID_TASK_DISPLAYED_INDEX);
}

ReadOnlyTask taskToDelete = lastShownDeadlineList.get(Character.getNumericValue(targetIndexes.get(i).charAt(1)) - 1);

try {
model.deleteTask(taskToDelete);
} catch (TaskNotFoundException pnfe) {
assert false : "The target Deadline cannot be missing";
}
}

else if(Character.toUpperCase(targetIndexes.get(i).charAt(0))=='T'){
if (lastShownTodoList.size() < Character.getNumericValue(targetIndexes.get(i).charAt(1))) {
indicateAttemptToExecuteIncorrectCommand();
return new CommandResult(Messages.MESSAGE_INVALID_TASK_DISPLAYED_INDEX);
}

ReadOnlyTask taskToDelete = lastShownTodoList.get(Character.getNumericValue(targetIndexes.get(i).charAt(1)) - 1);

try {
model.deleteTask(taskToDelete);
} catch (TaskNotFoundException pnfe) {
assert false : "The target Deadline cannot be missing";
}
}

}

return new CommandResult(String.format(MESSAGE_DELETE_TASK_SUCCESS, pass));

}

}
37 changes: 21 additions & 16 deletions src/main/java/seedu/address/logic/parser/Parser.java
Original file line number Diff line number Diff line change
Expand Up @@ -241,55 +241,60 @@ private static Set<String> getTagsFromArgs(String tagArguments) throws IllegalVa
* @param args full command args string
* @return the prepared command
*/
private Command prepareDelete(String args) {
private Command prepareDelete(String args){
final Matcher matcher = KEYWORDS_ARGS_FORMAT.matcher(args.trim());
if (!matcher.matches()) {
return new IncorrectCommand(String.format(MESSAGE_INVALID_COMMAND_FORMAT,
DeleteCommand.MESSAGE_USAGE));
}

char cat = args.charAt(1);
Collection<String> indexes = Arrays.asList(args.trim().replaceAll(" ", "").split(",")); //might need to change split regex to ; instead of ,

if(args.contains("-")){
ArrayList<String> indexes = new ArrayList<String> (Arrays.asList(args.trim().replaceAll(" ", "").split(","))); //might need to change split regex to ; instead of ,

if(args.contains("-")){
char cat = args.charAt(1);
String[] temp = args.replaceAll(" ", "").replaceAll(Character.toString(cat),"").split("-");
int start;
int end;
//check format of start and end
try{
start = Integer.parseInt(temp[0]);
end = Integer.parseInt(temp[temp.length-1]);
}catch(NumberFormatException nfe){
return new IncorrectCommand(
String.format(MESSAGE_INVALID_COMMAND_FORMAT, DeleteCommand.MESSAGE_USAGE));
}
//making format of String: T(start), T2, T3.....T(end)
String newArgs = Character.toString(cat).concat(Integer.toString(start));
for(int i = start+1; i<= end; i++){
newArgs = newArgs.concat(",".concat(Character.toString(cat)));
newArgs = newArgs.concat(Integer.toString(i));
}
indexes = Arrays.asList(newArgs.trim().replaceAll(" ", "").split(",")); //might need to change split regex to ; instead of ,
indexes = new ArrayList<String> (Arrays.asList(newArgs.trim().replaceAll(" ", "").split(",")));
}

Iterator<String> itr = indexes.iterator();
ArrayList<String> pass = new ArrayList<String>();
pass.addAll(indexes);
Optional<Integer> index = parseIndex(Character.toString(itr.next().charAt(1)));
//System.out.println(index.isPresent() + args);

String tempIndex = itr.next();
String indexToDelete = tempIndex.substring(1, tempIndex.length());
Optional<Integer> index = parseIndex(indexToDelete);
if(!index.isPresent()){
return new IncorrectCommand(
String.format(MESSAGE_INVALID_COMMAND_FORMAT, DeleteCommand.MESSAGE_USAGE));
}

}
while(itr.hasNext()){
index = parseIndex(Character.toString(itr.next().charAt(1)));
tempIndex = itr.next();
indexToDelete = tempIndex.substring(1, tempIndex.length());
index = parseIndex(indexToDelete);
// System.out.println(index.isPresent() + args + indexes.size());
if(!index.isPresent()){
return new IncorrectCommand(
String.format(MESSAGE_INVALID_COMMAND_FORMAT, SelectCommand.MESSAGE_USAGE));
String.format(MESSAGE_INVALID_COMMAND_FORMAT, DeleteCommand.MESSAGE_USAGE));
}
}
return new DeleteCommand(pass);
try {
return new DeleteCommand(indexes);
} catch (IllegalValueException ive) {
return new IncorrectCommand(ive.getMessage());
}
}

private Command prepareEdit(String args) {
Expand Down

0 comments on commit 3b26a35

Please sign in to comment.