Skip to content

Instantly share code, notes, and snippets.

@lucasmelin
Last active April 25, 2016 16:25
Show Gist options
  • Save lucasmelin/57f641e7f1f0c69570f018b56b876bd4 to your computer and use it in GitHub Desktop.
Save lucasmelin/57f641e7f1f0c69570f018b56b876bd4 to your computer and use it in GitHub Desktop.
Toggles task completion by inverting the boolean
/**
* Toggles task completion boolean.
* <p>
* Checks that there are tasks.
* Asks user for index of Task in the ArrayList to toggle.
* After obtaining the Task from the ArrayList flip the isCompleted boolean.
*/
private void toggleTaskComplete(){
if (tasks.isEmpty()==false){ // Make sure there are tasks
System.out.println("What is the index of the task you'd like to toggle?");
try{
int userIndex = keyboard.nextInt(); // Get index of task
Task currentTask = tasks.get(userIndex); // Get the task
currentTask.setIsComplete(!currentTask.getIsComplete()); // Invert the boolean
}catch (InputMismatchException e){
System.err.println(e.getMessage());
}
catch(IndexOutOfBoundsException e){
System.err.println(e.getMessage());
}
}
else{
System.out.print("Nothing to toggle, no tasks");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment