Project Description You will build part of a college\'s course registration syst
ID: 3695125 • Letter: P
Question
Project Description
You will build part of a college's course registration system (similar to WebReg, though considerably less complex). The system will keep track of a catalog of courses that students can take. It will allow the user (a student) to view and search the course catalog, and to add (register for) and drop (withdraw from) courses.
The following restrictions apply:
All courses meet only once per week, for one class period.
Days will be denoted by the following letters:
M - Monday, T - Tuesday, W - Wednesday, H - Thursday, F - Friday, S - Saturday. There are no Sunday courses.
Class periods (time slots) are numbered from 1 to 9.
A student cannot register for a course that is not in the catalog.
There is a limit on the number of courses a student may take in one semester.
There is NO explicit limit on the number of credits a student may carry in one semester.
A student cannot register for two courses that meet on the same day and time.
System Implementation (Phase 3)
The user interface has been written for you in WebRegApp.java. This program handles all keyboard and file input, and all screen output. You will complete the following methods in WebReg.java, which are designed to perform actions such as searching and sorting the catalog.
WebRegApp.java first reads the catalog (a list of courses) from catalog.txt. The catalog will be represented as an array of Course objects. WebRegApp reads the catalog from the file catalog.txt - you may edit this file if you wish. If you add entries to it, be sure they follow the same format as the entries already in the file. It then shows the user a menu from which they can choose various options. In most cases, WebRegApp will call methods from WebReg to execute the option chosen by the user. *ignore options 3 & 4 – you do not need to implement those methods until the next milestone.
Courses for which the student registers should be placed into the student’s schedule array starting at index 0. Empty (unused) entries in the array should remain null; all empty entries should be on the right side of the array (there should be no "gaps" between used entries). For example, since the maximum number of courses allowed is 6 and if the student is currently taking 3 courses, the schedule array would be of size 6, entries 0 through 2 would contain Course objects, and entries 3 through 5 would contain null. This should be the case regardless of the sequence of adds and drops the student performed in order to create his or her current schedule. The same concept applies for the roster of students for a specific course.
Implement the following methods in WebReg.java.
public static Course lookupCourseByName(Course[] catalog, String courseName)
search for the course with the given name in the catalog, and return the corresponding Course object.
if the course is not found, return null.
*you may ignore the letter casing of the course name – in other words, if courseName is all uppercase but the name of the course in the catalog is all lowercase, it would still be a match.
public static Course[] lookupCoursesByDept(Course[] catalog, int department)
search for all courses offered by the given department in the catalog, and return the corresponding Course objects in a new array (in any order).
the returned array should be just large enough to hold the results, and no larger; it should not contain any empty (null) entries.
if no courses are found with that department number, return null.
public static int countCredits(Student s)
compute the student's current total credit load.
public static boolean addCourse(Student s, Course c)
attempt to add a course, c, to the student's schedule.
put the Course object into the student’s schedule in the first free slot (null entry), update the courses’ roster, and return true.
if the student is not allowed to register for this course (for any reason), return false and do not alter the student’s schedule or the courses’ roster.
public static boolean dropCourse(Student s, Course c)
attempt to remove a course, c, from the student's schedule.
search for the Course object in the student's schedule.
if you find the Course object in the student’s schedule, remove it from the student’s schedule, update the courses’ roster, making sure not to create any "gaps" (null entries between used entries), and return true.
if you cannot find the course in the student's schedule or the student is not in the courses’ roster, return false.
DO NOT modify WebRegApp.java or your Period, Course, and Student objects.
Student.java
Period.java
Course.java
catalog.txt
WebRegApp.java
import java.io.*;
public class WebRegApp{
public static void main(String[] args){
Course[] catalog;
Student me = new Student("Joe", "Smith", 123456789, 2020);
Course[] results;
Course course;
String catalogFilename, courseName;
int dept, numCredits;
boolean success;
catalogFilename = "catalog.txt";
catalog = readFile(catalogFilename);
if (catalog == null){
System.err.println("Error reading " + catalogFilename + ":");
return;
}
while (true){
int choice = getMenuChoice();
if (choice == 1){
System.out.println("Enter name of course:");
courseName = IO.readString();
System.out.println();
course = WebReg.lookupCourseByName(catalog, courseName);
if (course != null){
System.out.println(course);
}
else{
System.out.println("No course found by that name.");
}
}
else if (choice == 2){
System.out.println("Enter number of department:");
dept = IO.readInt();
System.out.println();
results = WebReg.lookupCoursesByDept(catalog, dept);
if (results != null){
printArray(results);
}
else{
System.out.println("No courses found in that department.");
}
}
else if (choice == 3){
//WebReg.sortByNumber(catalog);
//printArray(catalog);
}
else if (choice == 4){
//WebReg.sortByTime(catalog);
//printArray(catalog);
}
else if (choice == 5){
System.out.println("Current schedule:");
printArray(me.getSchedule());
}
else if (choice == 6){
numCredits = WebReg.countCredits(me);
System.out.println("Credits being taken: " + numCredits);
}
else if (choice == 7){
System.out.println("Enter course name:");
courseName = IO.readString();
Course c = WebReg.lookupCourseByName(catalog, courseName);
System.out.println();
if(c != null){
success = WebReg.addCourse(me, c);
if (success){
System.out.println("Successfully registered.");
}
else{
System.out.println("Attempt to register failed.");
}
}
else{
System.out.println("No course found by that name.");
}
}
else if (choice == 8){
System.out.println("Enter course name:");
courseName = IO.readString();
Course c = WebReg.lookupCourseByName(catalog, courseName);
System.out.println();
if(c != null){
success = WebReg.dropCourse(me, c);
if (success){
System.out.println("Successfully withdrawn.");
}
else{
System.out.println("Attempt to withdraw failed.");
}
}
else{
System.out.println("No course found by that name.");
}
}
else if (choice == 9){
System.out.println("Enter course name:");
courseName = IO.readString();
Course c = WebReg.lookupCourseByName(catalog, courseName);
System.out.println();
if(c != null){
printArray(c.getRoster());
}
else{
System.out.println("No course found by that name.");
}
}
else{
return;
}
}
}
private static final String[] menuChoices = {
"Look up course by name",
"Look up courses by department",
"View catalog, sorted by department/course number",
"View catalog, sorted by scheduled day/period",
"View my course schedule",
"View my current credit load",
"Register for a course",
"Withdraw from a course",
"View a courses' roster"
};
private static int getMenuChoice(){
int numChoices = menuChoices.length+1;
System.out.println();
System.out.println("Menu:");
for (int i = 0 ; i < menuChoices.length ; i++){
System.out.println((i+1) + ". " + menuChoices[i]);
}
System.out.println(numChoices + ". Quit");
System.out.println();
System.out.println("Choice (1-" + numChoices + ")?");
int choice = IO.readInt();
while (choice < 1 || choice > numChoices){
System.out.println();
System.out.print("That is not a valid menu option. Pick 1-" + numChoices + ". ");
choice = IO.readInt();
}
System.out.println();
return choice;
}
private static Course[] readFile(String filename){
BufferedReader file;
int numEntries;
Course[] entries;
try{
file = new BufferedReader(new FileReader(filename));
numEntries = parseFile(file, false, null);
entries = new Course[numEntries];
file.close();
file = new BufferedReader(new FileReader(filename));
parseFile(file, true, entries);
file.close();
return entries;
}
catch (IOException e){
e.printStackTrace();
return null;
}
catch (IllegalArgumentException e){
e.printStackTrace();
return null;
}
}
private static int parseFile(BufferedReader file, boolean readEntries, Course[] catalog) throws IOException{
int numEntries;
String line;
numEntries = 0;
while((line = file.readLine()) != null){
line = line.trim();
if(line.equals("")){
continue;
}
if(readEntries){
catalog[numEntries] = parseCourseLine(line);
}
numEntries++;
}
return numEntries;
}
private static Course parseCourseLine(String line){
String remaining, part;
int index;
int dept, courseNum, timeSlot, credits;
String name;
char day;
remaining = line.trim();
// read dept
index = remaining.indexOf(':');
if (index == -1){
throw new IllegalArgumentException("badly formatted line in catalog file: " + line);
}
part = remaining.substring(0, index);
try{
dept = Integer.parseInt(part);
if (dept < 0 || dept > 999){
throw new IllegalArgumentException("invalid department number on line: " + line);
}
}
catch (NumberFormatException e){
throw new IllegalArgumentException("missing department number on line: " + line);
}
remaining = remaining.substring(index + 1);
// read number
index = remaining.indexOf(' ');
if (index == -1){
throw new IllegalArgumentException("badly formatted line in catalog file: " + line);
}
part = remaining.substring(0, index);
try{
courseNum = Integer.parseInt(part);
if (courseNum < 0 || courseNum > 999){
throw new IllegalArgumentException("invalid course number on line: " + line);
}
}
catch (NumberFormatException e){
throw new IllegalArgumentException("missing course number on line: " + line);
}
remaining = remaining.substring(index + 1);
// read name
index = remaining.indexOf('[');
if (index == -1){
throw new IllegalArgumentException("badly formatted line in catalog file: " + line);
}
remaining = remaining.substring(index + 1);
index = remaining.indexOf(']');
if (index == -1){
throw new IllegalArgumentException("badly formatted line in catalog file: " + line);
}
name = remaining.substring(0, index);
remaining = remaining.substring(index + 1);
// read day
remaining = remaining.trim();
if (remaining.length() < 2){
throw new IllegalArgumentException("badly formatted line in catalog file: " + line);
}
day = Character.toUpperCase(remaining.charAt(0));
if (day != 'M' && day != 'T' && day != 'W' && day != 'H' && day != 'F' && day != 'S'){
throw new IllegalArgumentException("invalid day on line: " + line);
}
try{
timeSlot = Integer.parseInt(remaining.substring(1,2));
if (timeSlot < 1 || timeSlot > 9){
throw new IllegalArgumentException("invalid class time slot on line: " + line);
}
}
catch (NumberFormatException e){
throw new IllegalArgumentException("missing class time slot on line: " + line);
}
remaining = remaining.substring(2);
// read credits
remaining = remaining.trim();
if(!remaining.startsWith("credits:")){
throw new IllegalArgumentException("badly formatted line in catalog file: " + line);
}
remaining = remaining.substring("credits:".length());
try{
credits = Integer.parseInt(remaining);
if (credits < 1){
throw new IllegalArgumentException("invalid number of credits on line: " + line);
}
}
catch (NumberFormatException e){
throw new IllegalArgumentException("missing or invalid number of credits on line: " + line);
}
Course c = new Course(dept, courseNum, name, day, timeSlot, credits);
return c;
}
private static void printArray(Object[] array){
for (int i = 0 ; i < array.length ; i++){
if (array[i] == null){
System.out.println("null entry in array");
}
else{
System.out.println(array[i]);
}
}
}
}
Remember to test each of your methods thoroughly by running WebRegApp!
Explanation / Answer
Ans;
As the implementation of the classes Course, Student is not provided, I can do only these following methods. For rest of the methods, the required classes structure need to be know.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.