Skip to content
Snippets Groups Projects
Commit cbc575ec authored by Ethan Robert's avatar Ethan Robert
Browse files

Added ToDoList (unfinished)

parent 4cc9e43d
No related branches found
No related tags found
No related merge requests found
......@@ -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
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
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
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment