To-do list is a set of operations performed by the choice of user. This list includes four operations.
- · Insert a task
- · Mark a task as done
- · Display the task status
- · Exit the application.
Insert a task: add a task to the list
Mark a task as done: If the task is done, it is marked as
done.
Display the task status: it shows the tasks.
Exit the application: close the task.
Java Simple project implementation – To-Do list:
Steps:
Step1:
- · First, include three built in packages for list,ArrayList and scanner.
- · Create a class “Task” with two member variables(String Description and Boolean workDone).
- · In the constructor, initialise the values.
- · Add three member functions getDescript(), workdone() ans markAsDone() and override toString().
Step2: write the code for To Do list class.
- · Include the code for implementing addTask(),markTaskAsdone(), showTask().
Step3 :
- · Write the main function() with switch case. Get the options from user and call the functions depends on the choice of the user.
Here is the program.
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
class Task {
private String
description;
private boolean
workDone;
this.description = description;
this.workDone
= false;
}
return
description;
}
return
workDone;
}
this.workDone
= true;
}
public String
toString() {
return
(workDone ? "[X] " : "[ ] ") + description;
}
}
class ToDoList {
private
List<Task> tasks;
public ToDoList()
{
tasks = new
ArrayList<>();
}
public void
addTask(String description) {
tasks.add(new
Task(description));
}
public void
markTaskAsDone(int ind) {
if (ind >=
0 && ind < tasks.size()) {
tasks.get(ind).markAsDone();
} else {
System.out.println("You have entered an Invalid task
number.");
}
}
public void
showTask() {
for (int i =
0; i < tasks.size(); i++) {
System.out.println(i + 1 + ". " + tasks.get(i));
}
}
}
public class Main25 {
public static void
main(String[] args) {
ToDoList
toDoList1 = new ToDoList();
Scanner
scanner = new Scanner(System.in);
String cmd;
System.out.println("\n1. Insert a Task");
System.out.println("2. Choose to Mark a Task as Done");
System.out.println("3. Display the Tasks");
System.out.println("4. Exit");
System.out.print("Please Enter your choice: ");
cmd =
scanner.nextLine();
case
"1":
System.out.print("Enter the task description: ");
String descript = scanner.nextLine();
toDoList1.addTask(descript);
break;
case
"2":
System.out.print("Enter the task number to mark as done: ");
int taskNo = Integer.parseInt(scanner.nextLine());
toDoList1.markTaskAsDone(taskNo - 1);
break;
case
"3":
toDoList1.showTask();
break;
case
"4":
System.out.println("Thank you for using this Application");
scanner.close();
System.exit(0);
break;
default:
System.out.println("Invalid. Please Enter the correct
choice.");
}
}
}
}
Executing this program, the output is displayed as follows…
C:\raji\blog>javac Main25.java
C:\raji\blog>java Main25
To-Do List Application in java
1. Insert a Task
2. Choose to Mark a Task as Done
3. Display the Tasks
4. Exit
Please Enter your choice: 1
Enter the task description: Read
1. Insert a Task
2. Choose to Mark a Task as Done
3. Display the Tasks
4. Exit
Please Enter your choice: 3
1. [ ] Read
1. Insert a Task
2. Choose to Mark a Task as Done
3. Display the Tasks
4. Exit
Please Enter your choice: 2
Enter the task number to mark as done: 1
1. Insert a Task
2. Choose to Mark a Task as Done
3. Display the Tasks
4. Exit
Please Enter your choice: 3
1. [X] Read
1. Insert a Task
2. Choose to Mark a Task as Done
3. Display the Tasks
4. Exit
Please Enter your choice: 1
Enter the task description: Write
1. Insert a Task
2. Choose to Mark a Task as Done
3. Display the Tasks
4. Exit
Please Enter your choice: 3
1. [X] Read
2. [ ] Write
1. Insert a Task
2. Choose to Mark a Task as Done
3. Display the Tasks
4. Exit
Please Enter your choice: 2
Enter the task number to mark as done: 2
1. Insert a Task
2. Choose to Mark a Task as Done
3. Display the Tasks
4. Exit
Please Enter your choice: 3
1. [X] Read
2. [X] Write
1. Insert a Task
2. Choose to Mark a Task as Done
3. Display the Tasks
4. Exit
Please Enter your choice: 4
Thank you for using this Application
This is the way of implementing a simple ToDo list in list
.Keep Coding!!!
No comments:
Post a Comment