Chapter 12, Programming Project 15 Java Project Name: IC26_MyOCC In this in-clas
ID: 3838416 • Letter: C
Question
Chapter 12, Programming Project 15 Java Project Name: IC26_MyOCC
In this in-class assignment, we will be replicating the functionality of course registration (e.g. MyOCC) utilizing a HashMap to store each student and the courses in which they are enrolled.
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 A105, THEA A107
Total students enrolled: 3
Total courses enrolled: 7
Student with most courses: mpaulding
HELP :)
Explanation / Answer
Hi, Please find my implementation.
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
public class IC26_MyOCC {
public static void main(String[] args) throws IOException {
BufferedReader keyboard = new BufferedReader (new InputStreamReader(System.in));
HashMap<String, ArrayList<String>> studentEnrollments = new HashMap<String, ArrayList<String>>( );
String name, course;
while(true){
System.out.print("Please enter student username (or "quit" to exit):");
name = keyboard.readLine().toLowerCase();
// stopping criteria check
if(name.trim().equalsIgnoreCase("quit"))
break;
ArrayList<String> courseList = null;
if(!studentEnrollments.containsKey(name)){
courseList = new ArrayList<>();
studentEnrollments.put(name, courseList);
}else{
courseList = studentEnrollments.get(name);
}
System.out.print("Please enter course to enroll in: ");
course = keyboard.readLine();
courseList.add(course);
}
System.out.println("~~~~~~~~~~~~~~~Spring 2017 Enrollment~~~~~~~~~~~~~~~ ");
int totalStudent = 0;
int totalCourse = 0;
String studentMostCourse = null;
int courseNumber = 0;
for(Map.Entry<String, ArrayList<String>> entry : studentEnrollments.entrySet()){
totalStudent++;
System.out.println("Student: "+entry.getKey());
if(studentMostCourse == null){
studentMostCourse = entry.getKey();
courseNumber = entry.getValue().size();
}
else if(courseNumber < entry.getValue().size()){
studentMostCourse = entry.getKey();
courseNumber = entry.getValue().size();
}
System.out.print("Courses: ");
ArrayList<String> courseList = entry.getValue();
totalCourse += courseList.size();
for(int i=0; i<courseList.size(); i++){
if(i != courseList.size()-1)
System.out.print(courseList.get(i)+", ");
else
System.out.print(courseList.get(i));
}
System.out.println();
}
System.out.println("Total students enrolled: "+totalStudent);
System.out.println("Total courses enrolled: "+totalCourse);
System.out.println("Student with most courses: "+studentMostCourse);
keyboard.close();
}
}
/*
Sample run:
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
*/
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.