有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

输出中的java切换完成和日期问题

我有两门课,我的程序应该像任务管理器一样工作。我可以添加项目,删除项目,设置每个项目的优先级,当项目到期时,在“[]”/“[x]”框中用“x”标记切换到完成或未完成,我可以打印所有项目。我运行了我的程序,但每次我添加一个具有设置到期日的项目时,它会将我项目中的所有日期转换为我输入的最后一个到期日。此外,如果我的物品已经有了“x”标记,并且我将其切换为不完整,那么我应该能够从“[x]”到“[]”中取消标记“x”,但它不会这样做。我做错了什么

以下是我的输出:

     ----jGRASP exec: java MyList

[a]dd an item
[d]elete an item
[t]oggle complete
[p]rint all
[q]uit
a
Enter an item to add to list: Run
Enter Date (MM/dd/YYYY): 11/27/1993
Enter priority (Low/Medium/High): high
[a]dd an item
[d]elete an item
[t]oggle complete
[p]rint all
[q]uit
p
0. [ ] Run -1- (11/27/1993)
[a]dd an item
[d]elete an item
[t]oggle complete
[p]rint all
[q]uit
a
Enter an item to add to list: Jump
Enter Date (MM/dd/YYYY): 11/28/1889
Enter priority (Low/Medium/High): medium
[a]dd an item
[d]elete an item
[t]oggle complete
[p]rint all
[q]uit
p
0. [ ] Run -1- (11/28/1889)
1. [ ] Jump -2- (11/28/1889)
[a]dd an item
[d]elete an item
[t]oggle complete
[p]rint all
[q]uit
a
Enter an item to add to list: Walk
Enter Date (MM/dd/YYYY): 11/19/1993
Enter priority (Low/Medium/High): low
[a]dd an item
[d]elete an item
[t]oggle complete
[p]rint all
[q]uit
p
0. [ ] Run -1- (11/19/1993)
1. [ ] Jump -2- (11/19/1993)
2. [ ] Walk -3- (11/19/1993)
[a]dd an item
[d]elete an item
[t]oggle complete
[p]rint all
[q]uit
a
Enter an item to add to list: Jog
Enter Date (MM/dd/YYYY): 11/23/1993
Enter priority (Low/Medium/High): medium
[a]dd an item
[d]elete an item
[t]oggle complete
[p]rint all
[q]uit
p
0. [ ] Run -1- (11/23/1993)
1. [ ] Jump -2- (11/23/1993)
2. [ ] Jog -2- (11/23/1993)
3. [ ] Walk -3- (11/23/1993)
[a]dd an item
[d]elete an item
[t]oggle complete
[p]rint all
[q]uit
t
Enter index of item to toggle complete: 0
0. [X] Run -1- (11/23/1993)
1. [ ] Jump -2- (11/23/1993)
2. [ ] Jog -2- (11/23/1993)
3. [ ] Walk -3- (11/23/1993)
[a]dd an item
[d]elete an item
[t]oggle complete
[p]rint all
[q]uit
t
Enter index of item to toggle complete: 0
0. [X] Run -1- (11/23/1993)
1. [ ] Jump -2- (11/23/1993)
2. [ ] Jog -2- (11/23/1993)
3. [ ] Walk -3- (11/23/1993)
[a]dd an item
[d]elete an item
[t]oggle complete
[p]rint all
[q]uit
q

 ----jGRASP: operation complete.

以下是我对每门课的代码:

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() {
      System.out.print("Enter index of item to toggle complete: ");
      int toggle = k.nextInt();
      for (int index = 0; index < toDoItems.size(); index++) {
         if(toggle == index) {
            System.out.println(index + ". [X] " + toDoItems.get(index));
         }
         else {
            System.out.println(index + ". [ ] " + toDoItems.get(index));
         }     
      }   
   }  
}

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;
      }      
   }

共 (1) 个答案

  1. # 1 楼答案

    要设置截止日期,请从private static Date dueDate;中删除staticstatic使得您创建的对象只有一个共享实例,这意味着当您更改它一次时,每隔一次调用它时,它也会反映出该更改

    至于取消切换索引,在它进入条件toggle == index之后,你不需要检查它是否已经被切换。因此,如果您输入t,然后输入1作为切换/取消切换的索引,只有该项将被切换,其余项将为空

    由于您有一个toDoItems作为类私有变量,我建议在ToDoItem类中添加isToggled布尔变量,以便检查该特定项是否已被切换

    所以在ToDoItem课堂上:

    private String description;
    private Date dueDate;
    private Priority priority;
    private boolean isToggled;
    ....
    public ToDoItem(String desc) {
        description = desc;
        dueDate = null;
        priority = priority.HIGH;
        isToggled = false;
    }
    ....
    public void setToggle(boolean toggled) {
        isToggled = toggled;
    }
    public boolean isToggled() {
        return isToggled;
    }
    

    MyList课堂上:

    public static void toggleComplete() {
        System.out.print("Enter index of item to toggle complete: ");
        int toggle = k.nextInt();
    
        toDoItems.get(toggle).setToggle(!toDoItems.get(toggle).isToggled()); 
    
        // No need for a for-loop since `ArrayList` has the `get` method to do this for you
        //for (int index = 0; index < toDoItems.size(); index++) {
        //    if(toggle == index) {
        //        System.out.println(index + ". [X] " + toDoItems.get(index));
        //    }
        //    else {
        //        System.out.println(index + ". [ ] " + toDoItems.get(index));
        //   }     
        //}   
    }