Write a program in Java that:
1. will prompt user with a menu that contains options to:
a. Add a To-Do Item to a todo list. A To-Do Item contains:
i. An arbitrary reference number (4; 44,004; etc.)
ii. A text description of the item ("Pick up dry cleaning", etc.)
iii. A priority level (1, 2, 3, 4, or 5)
b. View an item in the list (based on its reference number)
c. Delete an item from the list (based on its reference number)
d. Display all items in the list, sorted by reference number
e. Display all items in the list, sorted by priority level
2. Program must be implemented in an object-oriented fashion. You must create 2 classes:
a. A To-Do List class that stores the list itself, and has all the methods in it needed to interact with the list. All methods for adding, deleting, sorting, and viewing the list must be implemented in this class, not in main().
b. A To-Do Item class that contains the reference number, text, and priority, and any other methods as needed.
3. You may use any method you like to actually store the data, but I would humbly suggest that an ArrayList might be a good way.
4. You must use TRY … CATCH statements to check user input and catch common errors. YOUR PROGRAM SHOULD NOT CRASH BECAUSE OF BAD USER INPUT AT ANY POINT!
// ToDoList.java
package todo;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
public class ToDoList
{
List<ToDoItem> toDoList=new ArrayList<ToDoItem>();
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
void addItem()
{
ToDoItem toDoObj=new ToDoItem();
long refNo=0;
String dec=null;
int priority=0;
try {
System.out.println("arbitrary reference number :");
refNo=Long.parseLong(br.readLine());
System.out.println("arbitrary Description:");
dec=br.readLine();
System.out.println("arbitrary Priority Level :");
priority=Integer.parseInt(br.readLine());
toDoObj.setArbitraryReferenceNumber(refNo);
toDoObj.setTextDescription(dec);
toDoObj.setPriorityLevel(priority);
toDoList.add(toDoObj);
} catch (NumberFormatException e)
{
System.out.println("Enter Valid Number");
} catch (IOException e) {
System.out.println("Enter Valid input");
}
}
void viewItem()
{
System.out.println("Enter reference Number:");
long refNumber=0;
boolean flag=false;
try
{
refNumber = Long.parseLong(br.readLine());
}
catch (NumberFormatException | IOException e)
{
System.out.println("Enter a valid Input");
}
for(ToDoItem todo:toDoList)
{
if(todo.getArbitraryReferenceNumber()==refNumber)
{
flag=true;
System.out.println("Reference Number"+todo.getArbitraryReferenceNumber());
System.out.println("Description:"+todo.getTextDescription());
System.out.println("Priority Level:"+todo.getPriorityLevel());
}
}
if(!flag)
{
System.out.println("There is no Record Found");
}
}
void deleteItem()
{
System.out.println("Enter reference Number:");
long refNumber=0;
boolean flag=false;
try
{
refNumber = Long.parseLong(br.readLine());
}
catch (NumberFormatException | IOException e)
{
System.out.println("Enter a valid Input");
}
Iterator<ToDoItem> it = toDoList.iterator();
while (it.hasNext()) {
ToDoItem user = it.next();
if (user.getArbitraryReferenceNumber().equals(refNumber)) {
flag=true;
it.remove();
}
}
if(!flag)
{
System.out.println("There is no Record Found");
}
}
void displayByRefNumber()
{
List<Long>temp=new ArrayList<>();
for(ToDoItem todo:toDoList)
{
temp.add(todo.getArbitraryReferenceNumber());
}
Collections.sort(temp);
List<ToDoItem>tempList=new ArrayList<>(toDoList);
for(Long l:temp)
{
Iterator<ToDoItem> it = toDoList.iterator();
while (it.hasNext()) {
ToDoItem user = it.next();
if(user.getArbitraryReferenceNumber()==l)
{
tempList.remove(user);
System.out.println("Reference Number"+user.getArbitraryReferenceNumber());
System.out.println("Description:"+user.getTextDescription());
System.out.println("Priority Level:"+user.getPriorityLevel());
}
}
}
}
void displayByPriority()
{
List<Integer>temp=new ArrayList<>();
for(ToDoItem todo:toDoList)
{
temp.add(todo.getPriorityLevel());
}
List<ToDoItem>tempList=new ArrayList<>(toDoList);
Collections.sort(temp);
for(Integer l:temp)
{
Iterator<ToDoItem> it = toDoList.iterator();
while (it.hasNext()) {
ToDoItem user = it.next();
if(user.getPriorityLevel()==l)
{
tempList.remove(user);
System.out.println("Reference Number"+user.getArbitraryReferenceNumber());
System.out.println("Description:"+user.getTextDescription());
System.out.println("Priority Level:"+user.getPriorityLevel());
}
}
}
}
//main methode
public static void main(String s[])
{
ToDoList todolistObj=new ToDoList();
do
{
System.out.println("Menu");
System.out.println("1. Add an Item");
System.out.println("2. View an Item");
System.out.println("3. Delete an Item");
System.out.println("4. Display all Item sorted on reference number");
System.out.println("5. Display all Item sorted on Priority level");
System.out.println("choose Options:");
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int opt=0;
try
{
opt=Integer.parseInt(br.readLine());
}
catch(Exception e)
{
System.out.println("Enter A valid option");
}
if(opt!=0)
{
switch(opt)
{
case 1:
todolistObj.addItem();
break;
case 2:
todolistObj.viewItem();
break;
case 3:
todolistObj.deleteItem();
break;
case 4:
todolistObj.displayByRefNumber();
break;
case 5:
todolistObj.displayByPriority();
break;
default:
System.out.println("Enter A valid option");
}
}
}
while(true);
}
}
//ToDoItem.java
package todo;
public class ToDoItem
{
private Long arbitraryReferenceNumber;
private String textDescription;
private int priorityLevel;
public Long getArbitraryReferenceNumber()
{
return arbitraryReferenceNumber;
}
public void setArbitraryReferenceNumber(Long arbitraryReferenceNumber)
{
this.arbitraryReferenceNumber = arbitraryReferenceNumber;
}
public String getTextDescription()
{
return textDescription;
}
public void setTextDescription(String textDescription)
{
this.textDescription = textDescription;
}
public int getPriorityLevel()
{
return priorityLevel;
}
public void setPriorityLevel(int priorityLevel)
{
this.priorityLevel = priorityLevel;
}
}
Get Answers For Free
Most questions answered within 1 hours.