In: Computer Science
need to add these functions to my code: • show only important tasks • show all completed tasks
My code in java:
import java.util.ArrayList;
import java.util.*;
public class TodoList {
String date="";
String work="";
boolean completed=false;
boolean important=false;
public TodoList(String a,String b,boolean c,boolean d){
this.date=a;
this.work=b;
this.completed=c;
this.important=d;
}
public boolean isCompleted(){
return this.completed;
}
public boolean isImportant(){
return this.important;
}
public String getDate(){
return this.date;
}
public String getTask(){
return this.work;
}
}
class Main{
public static void main(String[] args) {
ArrayList<TodoList> t1=new ArrayList<TodoList>();
TodoList t2=null;
Scanner s=new Scanner(System.in);
int a;
String b="",c="";
boolean d,e;
char g;
do{
System.out.println("1 Add a Task\n2 Show All Task\n3 Show Completed Task\n4 Delete a task");
a=s.nextInt();
switch(a){
case 1:
System.out.println("enter the date");
b=s.next();
System.out.println("enter the task");
c=s.next();
System.out.println("enter the task is important true/false");
e=s.nextBoolean();
System.out.println("enter the task is completed true/false");
d=s.nextBoolean();
t2=new TodoList(b,c,d,e);
t1.add(t2);
break;
case 2:
for(int i=0;i<t1.size();i++)
{
b=t1.get(i).getDate();
c=t1.get(i).getTask();
d=t1.get(i).isCompleted();
e=t1.get(i).isImportant();
System.out.println("Date: "+b+"\tTask: "+c+"\tCompleted: "+d+"\tImportant: "+e);
}
break;
case 3:
for(int i=0;i<t1.size();i++)
{
b=t1.get(i).getDate();
c=t1.get(i).getTask();
d=t1.get(i).isCompleted();
e=t1.get(i).isImportant();
if(d){
System.out.println("Date: "+b+"\tTask: "+c+"\tCompleted: "+d+"\tImportant: "+e);
}
}
break;
case 4:
System.out.println("Enter task number to delete");
int f=s.nextInt();
for(int i=f;i<t1.size()-1;i++){
t1.set(i,t1.get(i+1));
}
break;
default:
System.out.println("Error");
break;
}
System.out.println("Continue: Y/N");
g=s.next().charAt(0);
}while((g=='y')||(g=='Y'));
}
}
//I have made some improvments in code along with requirements............please review it....
//Java Code
import java.util.ArrayList;
import java.util.*;
public class TodoList {
private String date="";
private String work="";
private boolean completed=false;
private boolean important=false;
public TodoList(String a,String b,boolean c,boolean d){
this.date=a;
this.work=b;
this.completed=c;
this.important=d;
}
public boolean isCompleted(){
return this.completed;
}
public boolean isImportant(){
return this.important;
}
public String getDate(){
return this.date;
}
public String getTask(){
return this.work;
}
@Override
public String toString() {
return "Date: "+date+"\tTask: "+work+"\tCompleted: "+completed+"\tImportant: "+important;
}
}
class Main{
public static void main(String[] args) {
ArrayList<TodoList> t1=new ArrayList<TodoList>();
TodoList t2=null;
Scanner s=new Scanner(System.in);
int a;
String b="",c="";
boolean d,e;
char g='Y';
do{
try {
System.out.println("1 Add a Task\n2 Show All Task\n3 Show Completed Task\n4. Show Only Important Tasks \n5. Delete a task");
a = s.nextInt();
s.nextLine();
switch (a) {
case 1:
System.out.println("enter the date");
b = s.nextLine();
System.out.println("enter the task");
c = s.nextLine();
System.out.println("enter the task is important true/false");
e = s.nextBoolean();
System.out.println("enter the task is completed true/false");
d = s.nextBoolean();
s.nextLine();
t2 = new TodoList(b, c, d, e);
t1.add(t2);
System.out.println("task added...");
break;
case 2:
//Show all tasks
for (TodoList todoList : t1) {
System.out.println(todoList);
}
break;
case 3:
boolean found1 = false;
//Show completed Tasks
for (TodoList todo : t1) {
if (todo.isCompleted()) {
found1 = true;
System.out.println(todo);
}
}
if (!found1) {
System.out.println("No completed task to show...");
}
break;
case 4:
//show only important tasks
boolean found = false;
for (TodoList todo : t1
) {
if (todo.isImportant()) {
found = true;
System.out.println(todo);
}
}
if (!found) {
System.out.println("No important task to show...");
}
break;
case 5:
System.out.println("Enter task number to delete");
int f = s.nextInt();
s.nextLine();
if (f >= 0 && f < t1.size()) {
System.out.println(t1.get(f).getTask() + " deleted...");
t1.remove(f);
} else {
System.out.println("No task to delete...");
}
break;
default:
System.out.println("Error");
break;
}
}
catch (InputMismatchException|ArrayIndexOutOfBoundsException ex)
{
System.out.println(ex.getMessage());
s.nextLine();
}
System.out.println("Continue: Y/N");
g = s.nextLine().toUpperCase().charAt(0);
}while(g=='Y');
}
}
//Output

//If you need any help regarding this solutionn...... please leave a comment........ thanks