use std::io;
use std::io::Write; // Required for io::stdout().flush()
struct Task {
id: usize,
description: String,
completed: bool,
}
struct TodoList {
tasks: Vec<Task>,
next_id: usize,
}
impl TodoList {
fn new() -> Self {
TodoList {
tasks: Vec::new(),
next_id: 1,
}
}
fn add_task(&mut self, description: String) {
let new_task = Task {
id: self.next_id,
description,
completed: false,
};
self.tasks.push(new_task);
println!("Task '{}' added with ID: {}", new_task.description, self.next_id);
self.next_id += 1;
}
fn list_tasks(&self) {
if self.tasks.is_empty() {
println!("\n--- No tasks in the list. ---");
return;
}
println!("\n--- Your Tasks ---");
for task in &self.tasks {
let status = if task.completed { "[X]" } else { "[ ]" };
println!("{} ID:{} - {}", status, task.id, task.description);
}
println!("------------------\n");
}
fn complete_task(&mut self, id: usize) {
let mut found = false;
for task in &mut self.tasks {
if task.id == id {
if !task.completed {
task.completed = true;
println!("Task ID {} ('{}') marked as completed.", id, task.description);
} else {
println!("Task ID {} ('{}') was already completed.", id, task.description);
}
found = true;
break;
}
}
if !found {
println!("Task with ID {} not found.", id);
}
}
fn delete_task(&mut self, id: usize) {
let initial_len = self.tasks.len();
// Retain only tasks whose ID is not the one we want to delete
self.tasks.retain(|task| task.id != id);
if self.tasks.len() < initial_len {
println!("Task ID {} deleted successfully.", id);
} else {
println!("Task with ID {} not found.", id);
}
}
}
fn main() {
let mut todo_list = TodoList::new();
loop {
println!("--- Todo List Menu ---");
println!("1. Add Task");
println!("2. List Tasks");
println!("3. Complete Task");
println!("4. Delete Task");
println!("5. Exit");
print!("Enter your choice: ");
// Flush stdout to ensure the prompt is displayed before reading input
io::stdout().flush().expect("Could not flush stdout");
let mut choice = String::new();
io::stdin()
.read_line(&mut choice)
.expect("Failed to read line");
let choice = choice.trim().parse::<u8>();
match choice {
Ok(1) => {
print!("Enter task description: ");
io::stdout().flush().expect("Could not flush stdout");
let mut description = String::new();
io::stdin()
.read_line(&mut description)
.expect("Failed to read line");
todo_list.add_task(description.trim().to_string());
}
Ok(2) => {
todo_list.list_tasks();
}
Ok(3) => {
print!("Enter ID of task to complete: ");
io::stdout().flush().expect("Could not flush stdout");
let mut id_str = String::new();
io::stdin()
.read_line(&mut id_str)
.expect("Failed to read line");
match id_str.trim().parse::<usize>() {
Ok(id) => todo_list.complete_task(id),
Err(_) => println!("Invalid ID. Please enter a number."),
}
}
Ok(4) => {
print!("Enter ID of task to delete: ");
io::stdout().flush().expect("Could not flush stdout");
let mut id_str = String::new();
io::stdin()
.read_line(&mut id_str)
.expect("Failed to read line");
match id_str.trim().parse::<usize>() {
Ok(id) => todo_list.delete_task(id),
Err(_) => println!("Invalid ID. Please enter a number."),
}
}
Ok(5) => {
println!("Exiting Todo List. Goodbye!");
break; // Exit the loop
}
_ => {
println!("Invalid choice. Please enter a number between 1 and 5.");
}
}
println!(""); // Add a newline for better readability between menu interactions
}
}