In: Computer Science
In java. Using a linked list only, iterator and scanner.
Develop a program to maintain a Linked List of homework assignments name and due date. When an assignment is assigned, add it to the list, and when it is completed, remove it. You should keep track of the due date. Your program should provide the following services each contained within its own method:
code in java with explanation in comments
import java.util.*;
import java.text.*;
class Test{
//static helper method to print menu
public static void print_menu(){
System.out.println("Select an option.");
System.out.println("1. Add a new assignment");
System.out.println("2. Remove an assignment");
System.out.println("3. List assignment in order they were assigned");
System.out.println("4. Assignment with earliest due date");
System.out.println("0. Exit");
}
public static void main(String[] args) throws Exception{
//linked list to store assignment name
LinkedList<String> homeworkAssignmentsName = new LinkedList<String>();
//linked list to store assignment due date
LinkedList<Date> homeworkAssignmentsDueDate = new LinkedList<Date>();
//Scanner class to take input
Scanner sc = new Scanner(System.in);
//declare variable to read from user
String name, dateString;
Date date;
//Dateformatter to convert entered date to date class
DateFormat formatter = new SimpleDateFormat("ddMMyyyy");
int op;
//Menu driven program
do{
//print menu
print_menu();
//take input from user
op = sc.nextInt();
//check for invalid input
if(op<0||op>4){
System.out.println("Invalid Input\nPlease select an option from 0 to 4 only");
}
//perform different operations depending on input
switch(op){
case 1: {
//take input for new assignment to add
System.out.print("Enter name of assignment: ");
name=sc.next();
System.out.print("Enter due date (ddMMyyyy): ");
dateString=sc.next();
date = formatter.parse(dateString);
//add new assignment to our linked list
homeworkAssignmentsName.add(name);
homeworkAssignmentsDueDate.add(date);
break;
}
case 2: {
//take input for assignment to remove
System.out.print("Enter name of assignment to remove: ");
name=sc.next();
//search for the assignment in our list
int index = homeworkAssignmentsName.indexOf(name);
if(index==-1){
//display message if not found
System.out.println("Assignment not found!");
}else{
//delete both assignment and due date if found
homeworkAssignmentsName.remove(index);
homeworkAssignmentsDueDate.remove(index);
System.out.println("Assignment deleted.");
}
break;
}
case 3: {
//declare iterators to traverse in the linked list
Iterator it1 = homeworkAssignmentsName.iterator();
Iterator it2 = homeworkAssignmentsDueDate.iterator();
System.out.println("Assignments: Name - Date");
//traverse both list and print on console
while(it1.hasNext()){
System.out.println(it1.next() + " - " + it2.next());
}
break;
}
case 4:{
//declare iterators to traverse in the linked list
Iterator it = homeworkAssignmentsDueDate.iterator();
//check if there are no assignments and display error
if(!it.hasNext()){
System.out.println("No assignments added!");
}
else{
//initialize d with first value to compare
Date d = (Date)it.next();
//compare d with all other dates and store least date
while(it.hasNext()){
Date x = (Date)it.next();
if(d.after(x)){
d=x;
}
}
//search for assignment with this date
int index = homeworkAssignmentsDueDate.indexOf(d);
//display in console
System.out.println("Assignments with least due date is : " + homeworkAssignmentsName.get(index) + " - " + d);
}
break;
}
}
}while(op!=0);
}
}
Code Screenshot
Console Output Screenshot
Let me know in the comments if you have any doubts.
Do leave a thumbs up if this was helpful.