Use an ArrayList to maintain a to-do list. Each entry in the list should contain
ID: 3749660 • Letter: U
Question
Use an ArrayList to maintain a to-do list. Each entry in the list should contain a task name (a String), a task description (a String), and an optional due date (a String in the format of yyyy-mm-dd). The entries in the list are ordered according to their due dates (with the earliest at the beginning), though they are not necessarily entered in that order. Entries without a due date are put at the end of the list.
The program uses a simple user interface (either text-only or graphical) to allow the user to choose the following operations:
To create a new to-do list
To load a to-do list from a file
To create a new entry, and add it into the list
To find an entry in the list by name, and display it
To find an entry in the list by name, and remove it
To display the entry with the earliest due date
To display all entries for a specific due date
To display all entries in the list
To save the list into a file
To exit
Write a main program to test and demonstrate the functions of the class.
Explanation / Answer
// Task.java
import java.util.Date;
public class Task {
private String taskname;
private String description;
private String date;
Task(String taskname,String description,String date){
this.taskname = taskname;
this.description = description;
if(date == null){
}else{
this.date = date;
}
}
public String getTaskname() {
return taskname;
}
public String getDescription() {
return description;
}
public String getDate() {
return date;
}
}
//Todo.java
// pleaee give proper inputfilepath
import java.io.*;
import java.util.*;
public class Todo {
public static final List<Task> Todolist = new ArrayList<Task>();
public static String filename = "inputfilepath";
public static void main(String args[]) throws IOException {
Scanner s = new Scanner(System.in);
/*
Prompting user to input values
*/
System.out.println("1. create a new to-do list ");
System.out.println("2. load a to-do list from a file");
System.out.println("3. create a new entry, and add it into the list");
System.out.println("4. find an entry in the list by name, and display it");
System.out.println("5. find an entry in the list by name, and remove it");
System.out.println("6. display the entry with the earliest due date");
System.out.println("7. display all entries for a specific due date");
System.out.println("8. display all entries in the list");
System.out.println("9. save the list into a file ");
System.out.println("10. exit ");
System.out.println("Please select your choice and input your number");
int choice = s.nextInt();
switch (choice) {
case 1:
System.out.println("1. create a new to-do list ");
createList();
case 2:
System.out.println("2. load a to-do list from a file");
loadlist();
case 3:
System.out.println("3. create a new entry, and add it into the list");
addtolist();
case 4:
System.out.println("4. find an entry in the list by name, and display it");
String taskid = "23";
findentry(taskid);
case 5:
System.out.println("5. find an entry in the list by name, and remove it");
String task = "23";
findentryremove(task);
case 6:
System.out.println("6. display the entry with the earliest due date");
displayearliest();
case 7:
System.out.println("7. display all entries for a specific due date");
String duedate = "23-10-2018";
displayentries(duedate);
case 8:
System.out.println("8. display all entries in the list");
displayallentries();
case 9:
System.out.println("9. save the list into a file ");
savelisttofile();
case 10:
System.out.println("You have entered 10 which is to exit ");
System.exit(0);
}
}
public static void createList() {
Todolist.add(new Task("task1", "this a new task description", "12-2-2018"));
}
public static void loadlist() throws IOException {
BufferedReader r;
PrintWriter w;
r = new BufferedReader(new FileReader("B:\myfile.txt"));
String contentLine = r.readLine();
/*
Task name , Task Description and Date is being seperated by white spaces in the file.
*/
String[] g = contentLine.split("\s+");
for (int i = 0; i < g.length; i++) {
System.out.println(" Printing the file contents " + g[i]);
Todolist.add(new Task(g[0], g[1], g[2]));
}
}
public static void savelisttofile() throws IOException {
BufferedWriter r;
PrintWriter w;
r = new BufferedWriter(new FileWriter("B:\myfile.txt"));
/*
Task name , Task Description and Date is being seperated by white spaces in the file.
*/
for (Task t : Todolist) {
System.out.println(" Writing to the file contents " + t.getDate());
r.write(t.getTaskname());
r.write(t.getDescription());
r.write(t.getDate());
}
r.flush();
r.close();
}
public static void addtolist(){
Todolist.add(new Task("task2", "this a new task description", "12-2-2018"));
System.out.println("Done adding");
}
public static void findentry(String key){
boolean check = false;
for(Task task: Todolist){
if(task.getTaskname().equalsIgnoreCase(key)){
check = true;
System.out.println("Entry found");
System.out.println(" Task id " + task.getTaskname() + " Desc " + task.getDescription() + " Date is " + task.getDate());
}
}
if(!check){
System.out.println(" Sorry no entry found here ");
}
}
public static void findentryremove(String key){
boolean check = false;
for(Task task: Todolist){
if(task.getTaskname().equalsIgnoreCase(key)){
check = true;
System.out.println("Entry found");
System.out.println(" Task id " + task.getTaskname() + " Desc " + task.getDescription() + " Date is " + task.getDate());
/*
Removing it
*/
Todolist.remove(task);
System.out.println("Removed Bingo");
}
}
if(!check){
System.out.println(" Sorry no entry found here ");
}
}
public static void displayearliest(){
Collections.sort(Todolist, (o1, o2) -> (o1.getDate().compareTo(o2.getDate())));
Task task = Todolist.get(0);
System.out.println(" Earliest due date is ");
System.out.println(" Task id " + task.getTaskname() + " Desc " + task.getDescription() + " Date is " + task.getDate());
}
public static void displayentries(String duedate){
boolean check = false;
for(Task task: Todolist){
if(task.getDate().equalsIgnoreCase(duedate)){
check = true;
System.out.println("Entry found");
System.out.println(" Task id " + task.getTaskname() + " Desc " + task.getDescription() + " Date is " + task.getDate());
}
}
if(!check){
System.out.println(" Sorry no entry found here for the given due date");
}
}
public static void displayallentries(){
for(Task task: Todolist){
System.out.println("Displaying entries");
System.out.println(" Task id " + task.getTaskname() + " Desc " + task.getDescription() + " Date is " + task.getDate());
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.