From cbc575ec8657e3954e45b84a9c5030c353c3a5a5 Mon Sep 17 00:00:00 2001 From: Ethan Robert <ethan.rbrt90@gmail.com> Date: Fri, 28 Feb 2025 11:00:10 +0100 Subject: [PATCH] Added ToDoList (unfinished) --- src/tp03/Task.java | 14 ++++- src/tp03/ToDoList.java | 122 ++++++++++++++++++++++++++++++++++++++ src/tp03/UseToDoList.java | 41 +++++++++++++ 3 files changed, 176 insertions(+), 1 deletion(-) create mode 100644 src/tp03/ToDoList.java create mode 100644 src/tp03/UseToDoList.java diff --git a/src/tp03/Task.java b/src/tp03/Task.java index 47e196c..234c993 100644 --- a/src/tp03/Task.java +++ b/src/tp03/Task.java @@ -27,7 +27,7 @@ public class Task { } public Task(String description, LocalDate creation, LocalDate deadline, int duration) { - this.taskID = Task.counter= + this.taskID = Task.counter; Task.counter++; this.creationDate = creation; @@ -68,4 +68,16 @@ public class Task { public void delay(int nbDays) { this.deadline = this.deadline.plusDays(nbDays); } + + public LocalDate getDeadline() { + return this.deadline; + } + + public LocalDate getCreationDate() { + return this.creationDate; + } + + public int getDuration() { + return this.duration; + } } \ No newline at end of file diff --git a/src/tp03/ToDoList.java b/src/tp03/ToDoList.java new file mode 100644 index 0000000..17910f8 --- /dev/null +++ b/src/tp03/ToDoList.java @@ -0,0 +1,122 @@ +package tp03.ex02; + +import java.util.Arrays; +import java.time.LocalDate; + +public class ToDoList { + private Task[] chores; + + public ToDoList() { + this.chores = new Task[5]; + } + + public void enlarge() { + this.chores = Arrays.copyOf(this.chores, this.chores.length + 5); + } + + public String toString() { + String result = "" + this.getNbTask() + " tasks: \n"; + for (int i = 0; i < this.chores.length; i++) { + if (this.chores[i] != null) { + result += " - " + this.chores[i] + "\n"; + } + } + return result; + } + + public void addTask(Task task) { + boolean added = false; + for (int i = 0; i < this.chores.length; i++) { + if (this.chores[i] == null && !added) { + this.chores[i] = task; + added = true; + } + } + + // Enlarge if full + if (!added) { + this.enlarge(); + this.chores[this.chores.length - 5] = task; + } + } + + public void removeTask(int index) { + this.chores[index] = null; + } + + public boolean isOverwhelmed() { + return this.getNbTask() == this.chores.length; + } + + public int getNbTask() { + int nb = 0; + for (int i = 0; i < this.chores.length; i++) { + if (this.chores[i] != null) { + nb++; + } + } + return nb; + } + + public void onSickLeave(int days) { + for (int i = 0; i < this.chores.length; i++) { + if (this.chores[i] != null) { + this.chores[i].delay(days); + } + } + } + + public void emergencySort() { + int n = this.chores.length; + for (int i = 1; i < n; i++) { + Task cle = this.chores[i]; + int j = i - 1; + + // Déplace les éléments du tableau[0..i-1] qui sont supérieurs à clé + while (j >= 0 && this.chores[j].getDeadline().isAfter(cle.getDeadline())) { + this.chores[j + 1] = this.chores[j]; + j = j - 1; + } + this.chores[j + 1] = cle; + } + } + + public void senioritySort() { + int n = this.chores.length; + for (int i = 1; i < n; i++) { + Task cle = this.chores[i]; + int j = i - 1; + + // Déplace les éléments du tableau[0..i-1] qui sont supérieurs à clé + while (j >= 0 && this.chores[j].getCreationDate().isAfter(cle.getCreationDate())) { + this.chores[j + 1] = this.chores[j]; + j = j - 1; + } + this.chores[j + 1] = cle; + } + } + + public void durationSort() { + int n = this.chores.length; + for (int i = 1; i < n; i++) { + Task cle = this.chores[i]; + int j = i - 1; + + // Déplace les éléments du tableau[0..i-1] qui sont supérieurs à clé + while (j >= 0 && this.chores[j].getDuration() > cle.getDuration()) { + this.chores[j + 1] = this.chores[j]; + j = j - 1; + } + this.chores[j + 1] = cle; + } + } + + public Task[] getTasks() { + return this.chores.clone(); + } + + public Task getTask(int index) { + return this.chores[index]; + } + +} \ No newline at end of file diff --git a/src/tp03/UseToDoList.java b/src/tp03/UseToDoList.java new file mode 100644 index 0000000..6b24cd5 --- /dev/null +++ b/src/tp03/UseToDoList.java @@ -0,0 +1,41 @@ +package tp03.ex02; + +import java.time.LocalDate; + +public class UseToDoList { + + public static void main(String[] args) { + ToDoList Alice = new ToDoList(); + ToDoList Bruno = new ToDoList(); + + Alice.addTask(new Task("a1", LocalDate.now(), LocalDate.now().plusDays(3), 1)); + Alice.addTask(new Task("a2", LocalDate.now(), LocalDate.now().plusDays(4), 1)); + Alice.addTask(new Task("a3", LocalDate.now(), LocalDate.now().plusDays(5), 1)); + + Bruno.addTask(new Task("b1", LocalDate.now(), LocalDate.now().plusDays(1), 1)); + Bruno.addTask(new Task("b2", LocalDate.now(), LocalDate.now().plusDays(5), 1)); + Bruno.addTask(new Task("b3", LocalDate.now(), LocalDate.now().plusDays(9), 1)); + + System.out.println("Alice: " + Alice); + System.out.println("Bruno: " + Bruno); + + Task[] newAliceTasks = Bruno.getTasks(); + + for (int i = 0; i < newAliceTasks.length; i++) { + Alice.addTask(newAliceTasks[i]); + Bruno.removeTask(i); + } + + System.out.println("Alice: " + Alice); + System.out.println("Bruno: " + Bruno); + + Alice.durationSort(); + Alice.getTask(Alice.getNbTask() - 1).delay(30); + Alice.senioritySort(); + Alice.getTask(Alice.getNbTask() - 1).delay(30); + + System.out.println("Alice: " + Alice); + + } + +} \ No newline at end of file -- GitLab