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...
Binary Search Tree Code in Java Write a program that prompts user to enter however many...
Binary Search Tree Code in Java Write a program that prompts user to enter however many numbers they want to add to the binary search tree. Then have the user input numbers until the tree contains that many elements. If the user enters a number that already exists in the tree, then a message should be displayed "Number is already in the tree". Once all elements are added to the tree print its contents in sorted order. Assume all user...
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 an application that prompt user to choose Sorting Algorithm (Insertion, Merge, or Heap) in GUI...
Write an application that prompt user to choose Sorting Algorithm (Insertion, Merge, or Heap) in GUI input message, then prompt user to enter numbers in GUI input message also, sort these numbers in descending order using Selected sorting algorithm, and show the result in message dialog as shown in the following figure. The program contains two classes: 1- “Sort” Class which contains 3 methods named as follow: a. InsertionSort(int[] numbers), returns sorted numbers from the passed array using Insertion sort...
Write a Java program that prompts the user to input a word (String). The program must...
Write a Java program that prompts the user to input a word (String). The program must print the reversed word with all consecutive duplicate characters removed. The program must contain the following classes: - The StackX class (you can use the Java Stack class). - The Reverse class which must contain a private data field called “word” of type string, a constructor, and a void method called revNoDup(). The revNoDup() method must reverse the word and remove the consecutive duplicate...
**JAVA LANGUAGE** Write a program that models an employee. An employee has an employee number, a...
**JAVA LANGUAGE** Write a program that models an employee. An employee has an employee number, a name, an address, and a hire date. A name consists of a first name and a last name. An address consists of a street, a city, a state (2 characters), and a 5-digit zip code. A date consists of an integer month, day and year. All fields are required to be non-blank. The Date fields should be reasonably valid values (ex. month 1-12, day...
(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...
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
Objective: Write a Java program that will use a JComboBox from which the user will select...
Objective: Write a Java program that will use a JComboBox from which the user will select to convert a temperature from either Celsius to Fahrenheit, or Fahrenheit to Celsius. The user will enter a temperature in a text field from which the conversion calculation will be made. The converted temperature will be displayed in an uneditable text field with an appropriate label. Specifications Structure your file name and class name on the following pattern: The first three letters of your...