【问题标题】:How to make a Task and Todo Program in Java?如何用 Java 制作任务和待办事项程序?
【发布时间】:2021-02-06 20:53:09
【问题描述】:

我想做一个任务和待办事项程序。我已经在单独的类中编写了代码。 class Task 将代表需要完成的单个项目。

public class Task {
private String task;
private int priority;
private int workload;

public Task(String task, int priority, int workload) {
    this.task = task; // Description of the task
    this.priority = priority; // 1 = very important, 2 = important, 3 = unimportant, 4 = after learn
                              // Portuguese
    this.workload = workload; // amount of time to complete the task
}

public String getPriority(String translation) {
    if (priority == 1) {
        translation = "very important";
    }
    if (priority == 2) {
        translation = " important";
    }
    if (priority == 3) {
        translation = "unimportant";
    }
    if (priority == 4) {
        translation = " after learn German";
    }
    return translation;
}

public String toString() {
    String translation = "";
    return task + " takes " + workload + " minutes and has priority " + getPriority(translation);
}
}

class Todo 将组织所有任务。

public class Todo {
ArrayList<String> Todo = new ArrayList<>();

public void addTask(String description, int priority, int minutes) {
    if (priority > 4 || priority < 1) {
        System.out.println(description + " has invalid priority ");
    }
    if (minutes < 0) {
        System.out.println(description + " has invalid workload ");
    }
    Todo.add(Task.class.getName());
}

public void getTodoList() {
    Todo.forEach(item->System.out.println(Task.class.getName().toString()));
}

public void print() {
    System.out.println("Todo:");
    System.out.println("-----");
    getTodoList();
    if (Todo == null) {
        System.out.println("You're all done for today! #TodoZero");
    }
}

public static void main(String[] args) {
    Task task;
    Todo todo;
    todo = new Todo();
    todo.addTask("Go to gym", 2, 60);
    todo.addTask("Read book", 1, 45);
    todo.print();
    System.out.print("");
}
}

输出:

Todo:
-----
Task
Task

但问题是Todo.print() method 无法打印已添加到 Todo 的任务列表。预期的输出应该是这样的:

Go to gym takes 60 minutes and has priority important
Read book takes 45 minutes and has priority very important

【问题讨论】:

  • 您希望System.out.println(Task.class.getName().toString()) 打印什么?
  • 另外,在getPriority(String translation),你没有使用这个参数。
  • 其实,我想打印出预期的输出(如底部所述)
  • 我知道,但我的问题仍然存在:您希望System.out.println(Task.class.getName().toString()) 打印什么?
  • getTodoList() 方法的名称意味着它会返回一些东西。但事实并非如此。你想把它重命名为printTodoList。还是您打算让它返回任务列表?

标签: java class interface tostring extend


【解决方案1】:

您的 Todo 类需要进行以下更改才能获得所需的输出

   ArrayList<Task> Todo = new ArrayList<>(); // ArrayList of Tasks

     public void addTask(String description, int priority, int minutes) {
          if (priority > 4 || priority < 1) {
             System.out.println(description + " has invalid priority ");
          }

          if (minutes < 0) {
             System.out.println(description + " has invalid workload ");
         }
         Todo.add(new Task(description, priority,minutes));   // Add Task
     }

     
     public void getTodoList() {
        // Iterate over Todo ArrayList
         for (Task task : Todo){
               System.out.println(task);
            }
     }


【讨论】:

    【解决方案2】:

    您的输出是正确的。您不会将任务添加到列表中,而是添加班级名称。在 todo.add() 中调用 Task 的构造函数

    【讨论】:

      猜你喜欢
      • 2017-10-10
      • 1970-01-01
      • 2020-09-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多