In: Computer Science
Create a new project |
Purpose:
To demonstrate your ability to use a linear structure to insert, contain, change, and remove data elements (Strings). Also to show that you can perform the detailed work in functions with the main function containing only the major program logic.
Specifications:
Create a new project called ToDoList and save it in the appropriate Lab folder.
Step 1: To begin the main program, use an ArrayList or LinkedList, to declare a structure to hold String values. Add at least five (5) tasks to the structure(this may be done in main or in a separate function (preferred).
Add a function to print a numbered list of the tasks (numbering must start at 1). The list should display all the elements with the assigned number (1, 2, 3, … n). The numbers are not permanently associated with a tasks but are reassigned each time the list is displayed. The listed number should identify the current position the task occupies in the list.
Step 2: Provide a simple vertical menu to allow the user the option to add, change, or delete a task from the list. Also include the option to terminate the program. The menu options should be listed vertically. The menu display and its selection validation may be done as a separate class in its own file or via a function called by the main program.
Step 3: In the main program logic, use the menu selection to determine the next operation.
Hint: using a switch to handle the menu options provides a neat and orderly structure in the main function.
When the user opts to add a task , add it to the end of the list.
When the user opts to change or delete a task, require the user to supply the assigned task number. If the task number entered is not in the list, display an error, pause the screen to allow the user to see the error, and end the operation.
When the user opts to change a task, obtain the new task description and replace the old description with the new.
When the user opts to delete a task remove it.
After each operation (with or without an error), clear the screen and redisplay the list followed by the menu.
Note: each of the operations (add, change, delete) should be coded in its own function and called from the main function.
Provide a main function loop (while or do...while) to allow the user to continue processing until the user selects the terminate menu option.
Step 4: Using the JAVADOC format, document the program and every function (main is a function, not a constructor) as displayed in the sample programs found in the downloaded files for Self Study Activity. Completeness, accuracy, and spelling count.
import java.util.*;
import java.util.Map.Entry;
class TestClass {
public static void main(String args[] ) throws Exception {
ArrayList<String> al = new ArrayList<>();
Scanner sc = new Scanner(System.in);
int option;
do {
System.out.println("Enter choice");
System.out.println("1. Add");
System.out.println("2. Change");
System.out.println("3. Delete");
System.out.println("4. Exit");
option = sc.nextInt();
switch(option) {
case 1:
add(al);
print(al);
break;
case 2:
change(al);
print(al);
break;
case 3:
delete(al);
print(al);
break;
case 4:
System.out.println("System terminates");
break;
default:
System.out.println("Invalid choice");
}
//System.out.print("\033[H\033[2J"); // To clear the screen.
System.out.flush();
}while(option!=4);
//sc.close();
}
static void print(ArrayList<String> al) {
System.out.println("List :");
for(int i=0;i<al.size();i++) {
System.out.println((i+1)+" "+al.get(i));
}
}
static void add(ArrayList<String> al) {
Scanner scanner = new Scanner(System.in);
System.out.println("Enter task");
String str = scanner.nextLine();
al.add(str);
//scanner.close();
}
static void change(ArrayList<String> al) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter task number to change");
int task_number = sc.nextInt();
if(task_number>al.size()) {
System.out.println("Entered task number is not present in the list");
//sc.close();
return;
}
System.out.println("Enter new task description");
sc.nextLine();
String str = sc.nextLine();
al.set(task_number-1, str);
//sc.close();
}
static void delete(ArrayList<String> al) {
System.out.println("Enter task number to remove");
Scanner sc = new Scanner(System.in);
int task_number = sc.nextInt();
if(task_number>al.size()) {
System.out.println("Entered task number is not present in the list");
//sc.close();
return;
}
al.remove((task_number-1));
//sc.close();
}
}