Ajout du Gestionnaire de Tâches en Java
This commit is contained in:
BIN
TaskManagerCLI/Task.class
Normal file
BIN
TaskManagerCLI/Task.class
Normal file
Binary file not shown.
BIN
TaskManagerCLI/TaskManager.class
Normal file
BIN
TaskManagerCLI/TaskManager.class
Normal file
Binary file not shown.
133
TaskManagerCLI/TaskManager.java
Normal file
133
TaskManagerCLI/TaskManager.java
Normal file
@@ -0,0 +1,133 @@
|
|||||||
|
import java.io.*;
|
||||||
|
import java.util.*;
|
||||||
|
|
||||||
|
// Classe interne, non publique
|
||||||
|
class Task implements Serializable {
|
||||||
|
private static final long serialVersionUID = 1L;
|
||||||
|
private String description;
|
||||||
|
private boolean done;
|
||||||
|
|
||||||
|
public Task(String description) {
|
||||||
|
this.description = description;
|
||||||
|
this.done = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void markDone() {
|
||||||
|
this.done = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean isDone() {
|
||||||
|
return done;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getDescription() {
|
||||||
|
return description;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return (done ? "[X] " : "[ ] ") + description;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Classe principale, publique
|
||||||
|
public class TaskManager {
|
||||||
|
private static final String FILE_NAME = "tasks.dat";
|
||||||
|
private List<Task> tasks;
|
||||||
|
|
||||||
|
public TaskManager() {
|
||||||
|
tasks = loadTasks();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void addTask(String description) {
|
||||||
|
tasks.add(new Task(description));
|
||||||
|
saveTasks();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void listTasks() {
|
||||||
|
if (tasks.isEmpty()) {
|
||||||
|
System.out.println("Aucune tâche.");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
for (int i = 0; i < tasks.size(); i++) {
|
||||||
|
System.out.println((i + 1) + ". " + tasks.get(i));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void markTaskDone(int index) {
|
||||||
|
if (index >= 1 && index <= tasks.size()) {
|
||||||
|
tasks.get(index - 1).markDone();
|
||||||
|
saveTasks();
|
||||||
|
} else {
|
||||||
|
System.out.println("Index invalide.");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void deleteTask(int index) {
|
||||||
|
if (index >= 1 && index <= tasks.size()) {
|
||||||
|
tasks.remove(index - 1);
|
||||||
|
saveTasks();
|
||||||
|
} else {
|
||||||
|
System.out.println("Index invalide.");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void saveTasks() {
|
||||||
|
try (ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(FILE_NAME))) {
|
||||||
|
out.writeObject(tasks);
|
||||||
|
} catch (IOException e) {
|
||||||
|
System.out.println("Erreur lors de la sauvegarde des tâches.");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@SuppressWarnings("unchecked")
|
||||||
|
private List<Task> loadTasks() {
|
||||||
|
try (ObjectInputStream in = new ObjectInputStream(new FileInputStream(FILE_NAME))) {
|
||||||
|
return (List<Task>) in.readObject();
|
||||||
|
} catch (IOException | ClassNotFoundException e) {
|
||||||
|
return new ArrayList<>();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
Scanner scanner = new Scanner(System.in);
|
||||||
|
TaskManager manager = new TaskManager();
|
||||||
|
|
||||||
|
System.out.println("=== Gestionnaire de Tâches ===");
|
||||||
|
|
||||||
|
while (true) {
|
||||||
|
System.out.println("\n1. Ajouter une tâche");
|
||||||
|
System.out.println("2. Marquer une tâche comme terminée");
|
||||||
|
System.out.println("3. Supprimer une tâche");
|
||||||
|
System.out.println("4. Lister les tâches");
|
||||||
|
System.out.println("5. Quitter");
|
||||||
|
System.out.print("Choix: ");
|
||||||
|
String choice = scanner.nextLine();
|
||||||
|
|
||||||
|
switch (choice) {
|
||||||
|
case "1":
|
||||||
|
System.out.print("Description de la tâche: ");
|
||||||
|
String desc = scanner.nextLine();
|
||||||
|
manager.addTask(desc);
|
||||||
|
break;
|
||||||
|
case "2":
|
||||||
|
manager.listTasks();
|
||||||
|
System.out.print("Numéro de la tâche à marquer comme terminée: ");
|
||||||
|
manager.markTaskDone(Integer.parseInt(scanner.nextLine()));
|
||||||
|
break;
|
||||||
|
case "3":
|
||||||
|
manager.listTasks();
|
||||||
|
System.out.print("Numéro de la tâche à supprimer: ");
|
||||||
|
manager.deleteTask(Integer.parseInt(scanner.nextLine()));
|
||||||
|
break;
|
||||||
|
case "4":
|
||||||
|
manager.listTasks();
|
||||||
|
break;
|
||||||
|
case "5":
|
||||||
|
System.out.println("Au revoir !");
|
||||||
|
return;
|
||||||
|
default:
|
||||||
|
System.out.println("Choix invalide.");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
BIN
TaskManagerCLI/tasks.dat
Normal file
BIN
TaskManagerCLI/tasks.dat
Normal file
Binary file not shown.
Reference in New Issue
Block a user