Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

need help with this app in java Write a Java program that reads in a student use

ID: 3838793 • Letter: N

Question

need help with this app in java

Write a Java program that reads in a student username (e.g. mpaulding) and a course in which they would like to enroll (e.g. CS170). Use a HashMap to store the username and an ArrayList of all the courses (Strings) for which that student has enrolled. Specifically, we will create a HashMap with the following definition:

HashMap<String, ArrayList<String>> studentEnrollments = new HashMap<String, ArrayList<String>>( );

In a loop, prompt the user to enter their username and the course they want to enroll in. The loop will continue as long as the user does not input the word "quit".

After the loop completes, display a listing of each student's username and the courses in which they are enrolled, followed by statistics about enrollment, including the total number of students, total number of courses enrolled and the student with the most number of courses.

For example, a sample transaction with the user might be:

~~~~~~~~~~~~~~~~~Welcome to MyOCC ~~~~~~~~~~~~~~~~~

Please enter student username (or "quit" to exit): mpaulding
Please enter course to enroll in: CS A170

Please enter student username (or "quit" to exit): mpaulding
Please enter course to enroll in: CS A150

Please enter student username (or "quit" to exit): mpaulding
Please enter course to enroll in: SPAN A180

Please enter student username (or "quit" to exit): dkeaton123
Please enter course to enroll in: THEA A105

Please enter student username (or "quit" to exit): dkeaton123
Please enter course to enroll in: THEA A107

Please enter student username (or "quit" to exit): sseagal77
Please enter course to enroll in: THEA A103

Please enter student username (or "quit" to exit): sseagal77
Please enter course to enroll in: THEA A104

Please enter student username (or "quit" to exit): quit

~~~~~~~~~~~~~~~Spring 2017 Enrollment~~~~~~~~~~~~~~~

Student: mpaulding
Courses: CS A170, CS A150, SPAN A180

Student: dkeaton123
Courses: THEA A105, THEA A107

Student: sseagal77
Courses: THEA A103, THEA A104

Total students enrolled: 3
Total courses enrolled: 7
Student with most courses: mpaulding

Explanation / Answer

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Map.Entry;
import java.util.Scanner;

public class StudentCoures {

   /**
   * @param args
   */
   public static void main(String[] args) {

       // declaration
       Scanner scanner = null;
       try {
           // initialization
           scanner = new Scanner(System.in);
           HashMap<String, ArrayList<String>> studentEnrollments = new HashMap<String, ArrayList<String>>();
           int count = 0;

           System.out
                   .println("~~~~~~~~~~~~~~~~~Welcome to MyOCC ~~~~~~~~~~~~~~~~~");

           do {
               // prompt to enter input
               System.out
                       .print("Please enter student username (or "quit" to exit):");
               String name = scanner.nextLine();

               // break if enters quit
               if (name.equals("quit"))
                   break;

               System.out.print("Please enter course to enroll in: ");
               String course = scanner.nextLine();

               ArrayList<String> courses = new ArrayList<String>();

               // if name contains in the map
               if (studentEnrollments.containsKey(name)) {
                   courses = studentEnrollments.get(name);
                   courses.add(course);

                   studentEnrollments.put(name, courses);
               } else {
                   courses.add(course);
                   studentEnrollments.put(name, courses);
               }
               // count the number of hikes
               count++;
               System.out.println();
           } while (true);

           // get the key max number of course from map
           String keyOfMaxValue = Collections.max(
                   studentEnrollments.entrySet(),
                   new Comparator<Entry<String, ArrayList<String>>>() {
                       @Override
                       public int compare(Entry<String, ArrayList<String>> o1,
                               Entry<String, ArrayList<String>> o2) {
                           return o1.getValue().size() > o2.getValue().size() ? 1
                                   : -1;
                       }
                   }).getKey();
           System.out
                   .println("~~~~~~~~~~~~~~~Spring 2017 Enrollment~~~~~~~~~~~~~~~");

           // Itrate through hashmap
           for (Entry<String, ArrayList<String>> entry : studentEnrollments
                   .entrySet()) {

               System.out.println("Student: " + entry.getKey());
               System.out.println("Courses: " + entry.getValue());

           }

           System.out.println("Total students enrolled:"
                   + studentEnrollments.size());
           System.out.println("Total courses enrolled: " + count);
           System.out.print("Student with most courses: " + keyOfMaxValue);

       } catch (Exception e) {
           // TODO: handle exception
       }

   }
}

OUTPUT:

~~~~~~~~~~~~~~~~~Welcome to MyOCC ~~~~~~~~~~~~~~~~~
Please enter student username (or "quit" to exit):Srinivas
Please enter course to enroll in: CS A170

Please enter student username (or "quit" to exit):Rajesh
Please enter course to enroll in: CS A171

Please enter student username (or "quit" to exit):Pavan
Please enter course to enroll in: CS A170

Please enter student username (or "quit" to exit):Srinivas
Please enter course to enroll in: CS A172

Please enter student username (or "quit" to exit):Srinivas
Please enter course to enroll in: CS A173

Please enter student username (or "quit" to exit):quit
~~~~~~~~~~~~~~~Spring 2017 Enrollment~~~~~~~~~~~~~~~
Student: Pavan
Courses: [CS A170]
Student: Srinivas
Courses: [CS A170, CS A172, CS A173]
Student: Rajesh
Courses: [CS A171]
Total students enrolled:3
Total courses enrolled: 5
Student with most courses: Srinivas