如何在Java中创建一个任务和待办事项程序?

huangapple go评论69阅读模式
英文:

How to make a Task and Todo Program in Java?

问题

我想制作一个任务和待办事项程序。我已经在不同的类中编写了代码。Task类将表示需要完成的单个任务。

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

    public Task(String task, int priority, int workload) {
        this.task = task; // 任务描述
        this.priority = priority; // 1 = 非常重要,2 = 重要,3 = 不重要,4 = 学完葡萄牙语后
        this.workload = workload; // 完成任务所需的时间
    }

    public String getPriority(String translation) {
        if (priority == 1) {
            translation = "非常重要";
        }
        if (priority == 2) {
            translation = "重要";
        }
        if (priority == 3) {
            translation = "不重要";
        }
        if (priority == 4) {
            translation = "学完德语后";
        }
        return translation;
    }
    
    public String toString() {
        String translation = "";
        return task + "需要" + workload + "分钟,优先级为" + getPriority(translation);
    }
}

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 + "的优先级无效");
        }
        if (minutes < 0) {
            System.out.println(description + "的工作量无效");
        }
        Todo.add(Task.class.getName());
    }

    public void getTodoList() {
        Todo.forEach(item->System.out.println(Task.class.getName().toString()));
    }
   
    public void print() {
        System.out.println("待办事项:");
        System.out.println("-----");
        getTodoList();
        if (Todo == null) {
            System.out.println("你今天的任务都完成了! #TodoZero");
        }
    }
    
    public static void main(String[] args) {
        Task task;
        Todo todo;
        todo = new Todo();
        todo.addTask("去健身房", 2, 60);
        todo.addTask("阅读书籍", 1, 45);
        todo.print();
        System.out.print("");
    }
}

输出:

待办事项:
-----
Task
Task

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

去健身房需要60分钟,优先级为重要
阅读书籍需要45分钟,优先级为非常重要
英文:

I want to make a Task and Todo program. I have written the code in separated classes. The class Task will represent a single item that needs to get done.

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 = &quot;very important&quot;;
}
if (priority == 2) {
translation = &quot; important&quot;;
}
if (priority == 3) {
translation = &quot;unimportant&quot;;
}
if (priority == 4) {
translation = &quot; after learn German&quot;;
}
return translation;
}
public String toString() {
String translation = &quot;&quot;;
return task + &quot; takes &quot; + workload + &quot; minutes and has priority &quot; + getPriority(translation);
}
}

While the class Todo will hold all the tasks organized.

public class Todo {
ArrayList&lt;String&gt; Todo = new ArrayList&lt;&gt;();
public void addTask(String description, int priority, int minutes) {
if (priority &gt; 4 || priority &lt; 1) {
System.out.println(description + &quot; has invalid priority &quot;);
}
if (minutes &lt; 0) {
System.out.println(description + &quot; has invalid workload &quot;);
}
Todo.add(Task.class.getName());
}
public void getTodoList() {
Todo.forEach(item-&gt;System.out.println(Task.class.getName().toString()));
}
public void print() {
System.out.println(&quot;Todo:&quot;);
System.out.println(&quot;-----&quot;);
getTodoList();
if (Todo == null) {
System.out.println(&quot;You&#39;re all done for today! #TodoZero&quot;);
}
}
public static void main(String[] args) {
Task task;
Todo todo;
todo = new Todo();
todo.addTask(&quot;Go to gym&quot;, 2, 60);
todo.addTask(&quot;Read book&quot;, 1, 45);
todo.print();
System.out.print(&quot;&quot;);
}
}

Output:

Todo:
-----
Task
Task

But the problem is the Todo.print() method cannot print the list of tasks that have been added to Todo. The expected output should be like this:

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

答案1

得分: 0

你的输出是正确的。你不向列表中添加任务,而是添加类名。在 todo.add() 中调用 Task 的构造函数。

英文:

Your output is correct. You don't add tasks to your list, you add class name. Call Task's constructor in todo.add()

答案2

得分: 0

以下是您的Todo类中所需的更改,以获得所需的输出:

   ArrayList<Task> Todo = new ArrayList<>(); // 任务的ArrayList

     public void addTask(String description, int priority, int minutes) {
		  if (priority > 4 || priority < 1) {
			 System.out.println(description + " 优先级无效 ");
		  }

          if (minutes < 0) {
			 System.out.println(description + " 工作量无效 ");
		 }
		 Todo.add(new Task(description, priority, minutes));   // 添加任务
	 }

	 
     public void getTodoList() {
        // 遍历Todo的ArrayList
		 for (Task task : Todo){
			   System.out.println(task);
			}
	 }

请注意,我只翻译了您提供的代码部分,没有包含任何额外的内容。

英文:

Following changes are required in your Todo Class to get the desired output

   ArrayList&lt;Task&gt; Todo = new ArrayList&lt;&gt;(); // ArrayList of Tasks
public void addTask(String description, int priority, int minutes) {
if (priority &gt; 4 || priority &lt; 1) {
System.out.println(description + &quot; has invalid priority &quot;);
}
if (minutes &lt; 0) {
System.out.println(description + &quot; has invalid workload &quot;);
}
Todo.add(new Task(description, priority,minutes));   // Add Task
}
public void getTodoList() {
// Iterate over Todo ArrayList
for (Task task : Todo){
System.out.println(task);
}
}

huangapple
  • 本文由 发表于 2020年10月24日 03:06:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/64505957.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定