Use an ArrayList to maintain a to-do list. Each entry in the list should contain
ID: 3746911 • 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:
1. To create a new to-do list
2. To load a to-do list from a file
3. To create a new entry, and add it into the list
4. To find an entry in the list by name, and display it
5. To find an entry in the list by name, and remove it
6. To display the entry with the earliest due date
7. To display all entries for a specific due date
8. To display all entries in the list
9. To save the list into a file
10. To exit
Write a main program to test and demonstrate the functions of the class.
(Java)
Explanation / Answer
package magicball;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.List;
class Records {//a new to do list has been created as a class
String name;
String task_description;
String due_date;
Records(String name, String task_description, String due_date) {//constructor for to do list to put all its //items into file
this.name = name;
this.task_description = task_description;
this.due_date = due_date;
}
public String getDue_date() {//getters for Due_Date
return due_date;
}
public void setDue_date(String due_date) {//Setters for DueDate
this.due_date = due_date;
}
}
public class MyClass {
static void sort(ArrayList<Records> al) {//function to sort array in order of ascending due_date
Collections.sort(al, new Comparator<Records>() {// inbuild sort function in java
public int compare(Records s1, Records s2) {
try {
Date sld = new SimpleDateFormat("yyyy-MM-dd").parse(s1.getDue_date());
Date sld2 = new SimpleDateFormat("yyyy-MM-dd").parse(s2.getDue_date());
return sld.compareTo(sld2);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return 0;
}
});
}
public static void write (String filename,ArrayList<Records> al) throws IOException{//Writing an //arrayList al into filename fileName
BufferedWriter outputWriter = null;
outputWriter = new BufferedWriter(new FileWriter(filename));
for (int i = 0; i < al.size(); i++) {
outputWriter.write(al.get(i).name+" "+al.get(i).task_description+ " "+ al.get(i).due_date );
outputWriter.newLine();
}
outputWriter.flush();
outputWriter.close();
}
static void add(Records al) throws Exception {//add a to to list in an arraylist
al1.add(al);
sort(al1);// after adding sort the array
}
static ArrayList<Records> al1 = new ArrayList<>();// An arrayList to record all the records
public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int choice=Integer.parseInt(br.readLine());
if(choice==1){
//create a new to do list
Records ph = new Records("abc", "def", "2018-08-08");
Records ph2 = new Records("abc", "def", "2018-08-11");
Records ph3 = new Records("abc", "def", "2018-08-10");
}
else if(choice==2){
BufferedReader br2 = new BufferedReader( //1.adding a new to do list from the file
new InputStreamReader(new FileInputStream(filepath)));// give filepathname from where you //want to load data
try {
String line;
while ((line = br2.readLine()) != null) {
String[] s1=line.trim().split("\s+");// reading everyline from the file and splitting it on the basis
String name=s1[0];//of spaces
String task_desc=s1[1];
String due_date=s1[2];
add(new Records(name, task_desc, due_date));
}
} finally {
br.close();
}
}
else if(choice==3)
{// create a new to do list and add to the arrayList
Records ph = new Records("abc", "def", "2018-08-08");
Records ph2 = new Records("abc", "def", "2018-08-11");
Records ph3 = new Records("abc", "def", "2018-08-10");
add(ph);
add(ph2);
add(ph3);
}
else if(choice==4)
{//To find an entry in the list by name, and display it
String nameToBeSearched=br.readLine();
for(int i=0;i<al1.size();i++){
if(al1.get(i).name.equals(nameToBeSearched))
System.out.println(al1.get(i).name+" "+ al1.get(i).task_description+" "+al1.get(i).due_date);
}}
else if(choice==5){
String nameToDeleted=br.readLine();
for(int i=0;i<al1.size();i++){
if(al1.get(i).name.equals(nameToDeleted))
al1.remove(i);
}sort(al1);// after delete resort the array
}
//
else if(choice==6){//To display the entry with the earliest due date
System.out.println(al1.get(0).name+" "+ al1.get(0).task_description+" "+al1.get(0).due_date);}
else if(choice==7){//To display all entries for a specific due date
String specifiedDueDate = br.readLine();
for(int i=0;i<al1.size();i++){
if(al1.get(i).due_date.equals(specifiedDueDate))
System.out.println(al1.get(i).name+" "+ al1.get(i).task_description+" "+al1.get(i).due_date);
}}
else if(choice==8){//To display all entries in the list
for(int i=0;i<al1.size();i++){
System.out.println(al1.get(i).name+" "+ al1.get(i).task_description+" "+al1.get(i).due_date);
}
}
else if(choice==9){//To save the list into a file
String filePath=filepath;//create a file and give your file path in specified format
write(filePath,al1);
}
else {
//Exit
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.