Java 如何使用枚举设置arraylist中的优先级?

Java 如何使用枚举设置arraylist中的优先级?,java,arraylist,enums,Java,Arraylist,Enums,我正在用两个类编写一个程序。我必须提示用户以字符串形式添加项目,将优先级设置为低/高/中,并获取日期。所有这些都是ToDoItem对象,存储在名为toDoItems的ArrayList中。根据用户输入的优先级,保存项目、优先级和日期的每个ToDoItem对象都应该自行排序 例如: Add item: Run Set due date: 11/27/2015 Enter priority: High Print All items: 0. Run -1- (11/27/1993) Add it

我正在用两个类编写一个程序。我必须提示用户以字符串形式添加项目,将优先级设置为低/高/中,并获取日期。所有这些都是ToDoItem对象,存储在名为toDoItems的ArrayList中。根据用户输入的优先级,保存项目、优先级和日期的每个ToDoItem对象都应该自行排序

例如:

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

Print All items:
0. Run -1- (11/27/1993)
1. Jump -2- (11/27/1993)

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)

在某个时候,我必须能够从arrayList中删除一个ToDoItem对象,根据我写的索引:

 public static void deleteToDoItem() {
      System.out.print("Enter index of item to delete: ");
      int delete = k.nextInt();
      toDoItems.remove(i);  
   } 
这给了我一个错误

 ----jGRASP exec: javac -g MyList.java

MyList.java:75: error: class, interface, or enum expected
   public static void deleteToDoItem() {
                 ^
MyList.java:83: error: class, interface, or enum expected
         int delete = k.nextInt();
         ^
MyList.java:84: error: class, interface, or enum expected
         toDoItems.remove(i);  
         ^
MyList.java:85: error: class, interface, or enum expected
   } 
   ^
4 errors

 ----jGRASP wedge2: exit code for process is 1.
 ----jGRASP: operation complete.
我把所有的对象都放下来了,我的两个类都在工作,但我一直在思考如何编写优先级枚举。我知道在某个时候我必须:
编写表示优先级高、中、低的枚举类型。
更改此对象的优先级字段以使用此新定义的枚举类型。
&编写一个现在只接受新定义的枚举类型的方法

更新//共享完整代码 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+"- " + 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);
   }   
}          
MyList类:

import java.util.Scanner;
import java.util.ArrayList;
import java.io.*;
import java.text.*;

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();
          //int p = Integer.parseInt(prior);

          toDoItems.add(new ToDoItem(desc, prior, dueDate));
       }

       public static void printAll() {  
          //System.out.print(toDoItems.size() + ". ");
          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() {
          ///// 
      // }  
    }
import java.util.Scanner;
导入java.util.ArrayList;
导入java.io.*;
导入java.text.*;
公共类MyList{
public static ArrayList toDoItems=new ArrayList();
专用静态扫描仪k=新扫描仪(System.in);
公共静态void main(字符串[]args)引发异常{
while(true){
打印菜单();
processInput();
} 
}
公共静态无效打印菜单(){
System.out.println(“[a]添加一项”);
System.out.println(“[d]删除一项”);
System.out.println(“[t]oggle complete”);
System.out.println(“[p]print all”);
System.out.println(“[q]uit”);
}
私有静态void processInput()引发ParseException{
扫描仪s=新的扫描仪(System.in);
字符串输入=s.next();
if(输入等于(“a”)){
addToDoItem();
}   
else if(输入等于(“d”)){
deleteToDoItem();
}
else if(输入等于(“t”)){
//切换完成();
}      
else if(输入等于(“p”)){
printAll();
}
else if(输入等于(“q”)){
系统出口(0);
}      
}
私有静态void addToDoItem()引发ParseException{
System.out.print(“输入要添加到列表中的项目:”);
字符串desc=k.nextLine();
系统输出打印(“输入日期(MM/dd/YYYY):”;
字符串dueDate=k.nextLine();
ToDoItem.setDueDate(dueDate);
系统输出打印(“输入优先级(低/中/高):”;
字符串previor=k.nextLine();
//int p=整数.parseInt(之前);
添加(新的ToDoItem(描述、之前、截止日期));
}
公共静态void printAll(){
//System.out.print(toDoItems.size()+”);
对于(int index=0;index
您得到的编译错误,这是因为类的右括号不正确。删除
printAll
方法后的右括号,并将其添加到
deleteToDoItem
方法后。这将解决您的编译问题

代码的最后一部分应该是这样的:

  private static void printAll() {
    for (int index = 0; index < toDoItems.size(); index++)
      System.out.println(toDoItems.get(index));
  }

// Extra bracket is removed from here.

  public static void deleteToDoItem() {
    System.out.print("Enter index of item to delete: ");
    int delete = k.nextInt();
    toDoItems.remove(i);
  }
} // put it here.
ToDoItem
类将如下所示:

  private static void printAll() {
    for (int index = 0; index < toDoItems.size(); index++)
      System.out.println(toDoItems.get(index));
  }

// Extra bracket is removed from here.

  public static void deleteToDoItem() {
    System.out.print("Enter index of item to delete: ");
    int delete = k.nextInt();
    toDoItems.remove(i);
  }
} // put it here.
注意:它期望优先级为
字符串
,例如(
)而不是整数。现在,类的所有成员都是实例变量,而不是类变量。它们现在属于ToDoItem类的单个对象

public class ToDoItem {

  private  String description;
  private  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+"- " + df.format(dueDate);
  }

  public 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);
  }
}
您可以在
MyList
类中避免此整数转换,将
优先级
输入为(
high
low
medium
),并将其直接传递给
ToDoItem
类构造函数

int p = Integer.parseInt(prior);  // not required.
要以排序的方式打印数据,您需要实现一个自定义的
比较器
。下面是基于
ToDoItem
对象的
优先级使用自定义
比较器的代码

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();
    }
  });
  //System.out.print(toDoItems.size() + ". ");
  for (int index = 0; index < toDoItems.size(); index++)
    System.out.println(index + ". " + toDoItems.get(index));
}

共享您的完整代码?您可以为您的列表实现一个比较器,并相应地对其排序。请看这里:@Soorapadman我已经在上面发布了。你一定要阅读有关静态的内容。所有TodoItem实例共享相同的描述、dueDate和优先级。@JorgeCampos,但我必须在代码中使用enum。但我仍然不知道如何获取优先级enum@pyuntae我已经为您的代码添加了一些修复程序。检查一下,明白了。我如何获得打印数字的优先级,而不是我希望它按照已设置的优先级打印的单词LOW/HIGH/MEDIUM,以及按照优先级移动的数组索引不确定这是否是因为我将枚举代码放入ToDoItem类中,但它不断给我一个错误,即他们找不到象征value@pyuntae将
优先级
枚举代码保持在
ToDoItem
类之外。我没有得到任何错误。
public Priority getPriority() {
  return priority;
}