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

标签 java checkbox arraylist toggle

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

  • 如果完成,程序将打印所有arrayList对象 每个方框内都有一个“x”标记。像这样[x]
  • 如果未完成,您希望能够切换它以便取消标记每个框;因此再次留下一个空白复选框,像这样 [ ]

例如:

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)

更新//分享完整代码

ToDoItem 类:

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 类:

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() {

   }  
}

最佳答案

让我们试着给出一个对您有帮助的答案,但这并不能消除您必须做的事情:显然您正在处理代表“待办事项”的对象列表。

但现在问问自己:“还需要什么其他信息”。看起来每个项目都可以“完成”或“未完成”。所以,也许您需要为每个工作项记住这一点。当您“切换”任何工作项的状态时,这意味着更新该数据结构。

而且那个数据结构也决定了你是否打印

[ ] whatever

[X] whatever

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

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

关于java - 如何在每个 arraylist 对象旁边制作一个假复选框,并使用标记 "x"将其切换为完成,就像 [x] 一样?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33185319/

相关文章:

mysql - 使用 Windows 窗体 vb.net 在数据 GridView 中的复选框列的标题中添加一个复选框

java - 重新格式化 CSV 输入文件

java - 是否可以使用 try-catch 语句修补内存泄漏?

java - 如何在通知点击时返回应用程序

php sql 在选中复选框时获取查询

java - 如何在 Android 中的闹钟应用程序中设置闹钟铃声

javascript - 在文本区域中显示复选框的值

java - 按距当前 GPS 位置的距离对 GPS 位置集合进行排序

java - 如何在 Spring Integration 中从 XML 检索数据

java - Spring REST Controller 未映射