如何在每个 arraylist 对象旁边制作一个假复选框,并使用标记 "x" 将其切换为完成,就像 [x] 一样?

How to make a fake checkbox next to each arraylist object and toggle it for completion using mark "x" like so [x]?

我必须创建一个 toggleComplete() 方法,在每个 arrayList 对象旁边添加一个复选框,并提示用户输入他们要删除的 arrayList 对象的索引。

例如:

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 //分享完整代码

待办事项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;
      }      
   }

我的列表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() {

   }  
}

让我们尝试给出一个对您有帮助的答案,但这并不能消除您必须做的事情:显然您正在处理代表 "todo items".[=12 的对象列表=]

但现在问问自己:"what other information is required"。看起来每个项目都可以是 "completed",或 "not completed"。所以,也许您需要为每个工作项记住这一点。当您 "toggle" 任何工作项的状态时,这意味着更新该数据结构。

并且该数据结构还决定了您是否打印

[ ] whatever

[X] whatever

请注意:作业确实说到 "toggle" 的唯一方法是通过菜单;它不像您可以单击 X 使其消失。

你把数据打印出来;那么你可以改变它;然后你再打印出来;导致不同的输出。这就是全部秘密。