Question

Write a program in Java that: 1. will prompt user with a menu that contains options...

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!

Homework Answers

Answer #1

// 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;

   }

}

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Write a program that utilizes a DO-WHILE loop to redisplay a menu to the user. Ask...
Write a program that utilizes a DO-WHILE loop to redisplay a menu to the user. Ask the user to input their favorite SuperHero and display a positive or funny comment about the chosen SuperHero. If RBG is chosen, display "You can't spell TRUTH without RUTH." Utilize a Do-While Loop to redisplay the menu to the user after item 1, 2 or 3 is executed. If item 4 is chosen, end the program. If the user chooses 4, end the program....
Create a Python program that: Allows the user to enter a phrase or sentence. The program...
Create a Python program that: Allows the user to enter a phrase or sentence. The program should then take the phrase or sentence entered Separate out the individual words entered Each individual word should then be added to a list After all of the words have been place in a list Sort the contents of the list Display the contents of the sorted list with each individual word displayed on a separate line Display a message to the user indicating...
Write a java program that repeatedly prompts the user to input a string starting with letters...
Write a java program that repeatedly prompts the user to input a string starting with letters from the English alphabet. The program must stop getting input when the user inputs the string “STOOOOP”. Then the program must display the words starting with each letter from the alphabet in a separate line (e.g. you need to make a new line after all words starting with a specific letter are finished.
Write a Java program that reads words from a text file and displays all the words...
Write a Java program that reads words from a text file and displays all the words (duplicates allowed) in ascending alphabetical order. The words must start with a letter. 1. You must use one of following Concrete class to store data from the input.txt file. Vector, Stack, ArrayList, LinkedList 2. To sort those words, you should use one of existing interface methods available in Collection or List class.
Write a program in python to display all the consonant in the user input. E.g. user...
Write a program in python to display all the consonant in the user input. E.g. user input: Hello Good Day to you. Output: consonant H = 1 consonant l = 2 consonant G = 1   consonant d = 1 consonant D = 1 etc
(8 marks) Write a program to ask user to input an integer and display the special...
Write a program to ask user to input an integer and display the special pattern accordingly. REQUIREMENTS The user input is always correct (input verification is not required). Your code must use loop statements (for, while or do-while). Your program should use only the following 3 output statements, one of EACH of the followings: System.out.print("-"); // print # System.out.print("+"); // print + System.out.println(); // print a newline Your code must work exactly like the following example (the text in bold...
JAVA: Write a program that takes in a line of text as input, and outputs that...
JAVA: Write a program that takes in a line of text as input, and outputs that line of text in reverse. The program repeats, ending when the user enters “done” (case-insensitive) for the line of text. Note: You are not supposed to use any in-build methods to reverse the text. Example: Enter a series of strings: Hello there Hey done the output is: ereht olleH yeH
Write a program that sorts the content of a map by Values. Before we start, we...
Write a program that sorts the content of a map by Values. Before we start, we need to check the explanation of Java Map Interface and Java LinkedHashMap. This program can be implemented as follows: 1. Create a LinkedHashMap class <String (Country), String(Capital)> that stores Capitals of FIVE countries. 2. Use sortMap() for receiving content of the created class of LinkedHashMap and reorder it in a sorted Map. (sort in natural order). 3. You need to transfer the map into...
Write a program (also write its pseudocode and draw its flowchart) to prompt the user to...
Write a program (also write its pseudocode and draw its flowchart) to prompt the user to input the variables to solve the following problem: X2 + X×Y/Z – W*V3 Note 1: You should ask the user to input 5 number to be able to do this calculation. Note 2: You can use multiplication instead of power if you do not want to use it (as we have not officially covered it).
TO BE DONE IN JAVA Your task is to complete the AddTime program so that it...
TO BE DONE IN JAVA Your task is to complete the AddTime program so that it takes multiple sets of hours, minutes and seconds from the user in hh:mm:ss format and then computes the total time elapsed in hours, minutes and seconds. This can be done using the Time object already given to you. Tasks: Complete addTimeUnit() in AddTime so that it processes a string input in hh:mm:ss format and adds a new Time unit to the ArrayList member variable....
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT