0

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 = "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);
}
}

While the class Todo will hold all the tasks organized.

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("");
}
}

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

2 Answers2

0

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

J.Doe
  • 287
  • 1
  • 5
  • 16
0

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

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


abhijit gupta
  • 161
  • 1
  • 12