I have to create a toggleComplete() method that adds a checkbox next to each arrayList object and prompt user to enter the index of the arrayList object they want to delete.
- If it is completed, the program will print all the arrayList objects with an "x" mark in each of the boxes. Like so [x]
- If it is not completed, you want to able to toggle it so that you can unmark each of the boxes; therefore leaving a blank checkbox again, like so [ ]
For example:
Add item: Run
Set due date: 11/27/2015
Enter priority: High
Print All items:
0. [ ] Run -1- (11/27/1993)
Add item: Jump
Set due date: 11/28/1993
Enter priority: Low
Add item: Walk
Set due date: 11/19/1993
Enter priority: Medium
Print All items:
0. [ ] Run -1- (11/27/1993)
1. [ ] Walk -2- (11/19/1993)
2. [ ] Jump -3- (11/27/1993)
Toggle Complete: 1
Print All items:
0. [ ] Run -1- (11/27/1993)
1. [x] Walk -2- (11/19/1993)
2. [ ] Jump -3- (11/27/1993)
Toggle Complete: 1
Print All items:
0. [ ] Run -1- (11/27/1993)
1. [ ] Walk -2- (11/19/1993)
2. [ ] Jump -3- (11/27/1993)
UPDATE //Sharing full code
ToDoItem class:
import java.text.*;
import java.util.Date;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.*;
public class ToDoItem {
private String description;
private static Date dueDate;
private Priority priority;
private static DateFormat df = new SimpleDateFormat("MM/dd/yyyy");
public ToDoItem() {
}
public ToDoItem(String desc) {
description = desc;
dueDate = null;
priority = priority.HIGH;
}
public ToDoItem(String desccription, String d) throws ParseException{
this.description = description;
dueDate = df.parse(d);
}
public ToDoItem(String description, String p, String d) throws ParseException{
this.description = description;
this.priority = Priority.valueOf(p.toUpperCase());
dueDate = df.parse(d);
}
public String toString() {
return description + " -"+priority.getValue()+"- (" + df.format(dueDate) + ")";
}
public static void setDueDate(String s) {
try {
dueDate = df.parse(s);
} catch(Exception ex) {
System.out.println(ex);
}
}
public String getDescription() {
return description;
}
public String getDueDate() {
return df.format(dueDate);
}
public Priority getPriority() {
return priority;
}
}
enum Priority {
HIGH(1), MEDIUM(2), LOW(3);
private int value;
Priority(int value) {
this.value = value;
}
public int getValue() {
return value;
}
}
MyList class:
import java.util.Scanner;
import java.util.ArrayList;
import java.util.*;
import java.text.*;
import java.util.Collections;
public class MyList {
public static ArrayList<ToDoItem> toDoItems = new ArrayList<>();
private static Scanner k = new Scanner(System.in);
public static void main(String[] args) throws ParseException {
while(true) {
printMenu();
processInput();
}
}
public static void printMenu() {
System.out.println("[a]dd an item");
System.out.println("[d]elete an item");
System.out.println("[t]oggle complete");
System.out.println("[p]rint all");
System.out.println("[q]uit");
}
private static void processInput() throws ParseException {
Scanner s = new Scanner(System.in);
String input = s.next();
if(input.equals("a")) {
addToDoItem();
}
else if(input.equals("d")) {
deleteToDoItem();
}
else if(input.equals("t")) {
toggleComplete();
}
else if(input.equals("p")) {
printAll();
}
else if(input.equals("q")) {
System.exit(0);
}
}
private static void addToDoItem() throws ParseException {
System.out.print("Enter an item to add to list: ");
String desc = k.nextLine();
System.out.print("Enter Date (MM/dd/YYYY): ");
String dueDate = k.nextLine();
ToDoItem.setDueDate(dueDate);
System.out.print("Enter priority (Low/Medium/High): ");
String prior = k.nextLine();
toDoItems.add(new ToDoItem(desc, prior, dueDate));
}
public static void printAll() {
Collections.sort(toDoItems, new Comparator<ToDoItem>() {
@Override
public int compare(ToDoItem o1, ToDoItem o2) {
return o1.getPriority().getValue() - o2.getPriority().getValue();
}
});
for (int index = 0; index < toDoItems.size(); index++)
System.out.println(index + ". [ ] " + toDoItems.get(index));
}
public static void deleteToDoItem() {
int index = 0;
System.out.print("Enter index of item to delete: ");
int delete = k.nextInt();
toDoItems.remove(index);
}
public static void toggleComplete() {
}
}