summaryrefslogtreecommitdiff
path: root/oop/10-task-tracker/src/main/java/lk/ac/pdn/ceit/tasks/InMemoryTaskManager.java
blob: 93988a37751d4f93daec437ff402390a6a0575f4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
package lk.ac.pdn.ceit.tasks;

import java.util.ArrayList;
import java.util.List;

import lk.ac.pdn.ceit.tasks.entities.Task;

public class InMemoryTaskManager implements TaskManager {
    private List<Task> tasks = new ArrayList<>();

    private int lastId = 0;

    @Override
    public Task createNewTask(String title, String description) {
        lastId++;

        Task task = new Task(lastId, title, description);

        // Add to the internally maintained task list
        tasks.add(task);

        return task;
    }

    @Override
    public Task findById(int id) {
        for (Task task : tasks) {
            if (task.getId() == id) {
                return task;
            }
        }

        return null;
    }

    @Override
    public void updateTask(Task task) {
        // Find the internal position of the task in the tasks list
        int index = 0;

        for(; index < tasks.size(); index++) {
            if (tasks.get(index).getId() == task.getId()) {
                break;
            }
        }

        // Replace the task object (related to that internal position)
        tasks.set(index, task);
    }

}