System Implementation (Phase 3) The user interface has been written for you in W
ID: 3694312 • Letter: S
Question
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.
DO NOT modify WebRegApp.java or your Period, Course, and Student objects. If needed, you may add helper methods and fields 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, ifcourseName 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.
WebReg.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]);
}
}
}
}
Course.java
class Course{
int department;
int courseNum;
String name;
int credits;
Student students[] = new Student[20];
Period period;
public Course(int department, int courseNum, String name, char day, int timeSlot, int credits) {
this.department = department;
this.courseNum = courseNum;
this.name = name;
this.credits = credits;
this.period = new Period(day,timeSlot);
}
public int getDepartment(){
return department;
}
public int getCourseNumber(){
return courseNum;
}
public String getName(){
return name;
}
public Period getPeriod(){
return period;
}
public int getCredits(){
return credits;
}
public Student[] getRoster(){
return students;
}
public String toString(){
return ""+ this.department+":"+this.courseNum+" "+"["+this.name+"]"+" "+this.period +" " + "credits:"+this.credits;
}
public boolean equals(Course other){
return courseNum==other.courseNum && department==other.department;
}
}
Student.java
public class Student {
private String firstName;
private String lastName;
private int id;
private int gradYear;
Course courses[] = new Course[6];
public Student(String firstName, String lastName, int id, int gradYear){
this.firstName = firstName;
this.lastName = lastName;
this.id = id;
this.gradYear = gradYear;
}
public String getFirstName() {
return firstName;
}
public String getLastName() {
return lastName;
}
public int getId() {
return id;
}
public int getGradYear() {
return gradYear;
}
public Course[] getSchedule() {
return courses;
}
public void setSchedule(Course[] courses) {
this.courses = courses;
}
public String toString() {
return ""+ this.id + ": " +this.lastName +", " + this.firstName + " - " +this.gradYear;
}
public boolean equals(Student other) {
if(this.id == other.id){
return true;
}
else{
return false;
}
}
}
Period.java
public class Period{
private char day;
private int timeSlot;
public static void main(String[] args){
Period p1 = new Period('M', 2);
Period p2 = new Period('M', 2);
Period p3 = new Period('F', 4);
int status = p1.compareTo(p2);
if(status == 0){
System.out.println(p1 + " is equal to " + p2);
}
else if(status == -1){
System.out.println(p1 + " comes before " + p2);
}
else{
System.out.println(p1 + " comes after " + p2);
}
status = p1.compareTo(p3);
if(status == 0){
System.out.println(p1 + " is equal to " + p3);
}
else if(status == -1){
System.out.println(p1 + " comes before " + p3);
}
else{
System.out.println(p1 + " comes after " + p3);
}
}
public Period(char day, int timeSlot){
if(day >= 'a' && day <= 'z'){
day = (char) (day + (int)'A' - (int)'a'); // converting to uppercase here
}
this.day = day;
this.timeSlot = timeSlot;
}
public char getDay(){
return this.day;
}
public int getTimeSlot(){
return this.timeSlot;
}
public String toString(){
return ""+this.day + this.timeSlot;
}
public boolean equals(Period p){
return day == p.day && timeSlot == p.timeSlot;
}
public int compareTo(Period other){
if(equals(other)){
return 0;
}
else{
if(other.day == day){
if(timeSlot < other.timeSlot){
return -1;
}
else{
return 1;
}
}
else{
String days = "SMTWF";
int ind1 = days.indexOf(day);
int ind2 = days.indexOf(other.day);
if(ind1 < ind2){
return -1;
}
else{
return 1;
}
}
}
}
public void setTimeSlot(char newSlot){
int count = 1;
while (count !=0){
if ((newSlot < 1) || (newSlot > 9)){
System.out.println("Time Slot must be between 1-9");
}
else {
this.timeSlot = newSlot;
count = 0;
}
}
}
}
Explanation / Answer
Hello there ,
Please find below class which contains 5 methods.
Do I need to implement menu item 3/4 ? As You have not asked for.
Let me know if you have any queries . Thanks.
import java.util.Arrays;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
public class WebReg {
public static Course lookupCourseByName(Course[] catalog, String courseName) {
Course course = null;
// iterate the String array
for (int i = 0; i < catalog.length; i++) {
// check if course array contains the string
if (catalog[i].getName().toLowerCase().equals(courseName.toLowerCase())) {
// course found
course = catalog[i];
break;
}
}
return course;
}
public static Course[] lookupCoursesByDept(Course[] catalog, int dept) {
Course[] courses1 = new Course[catalog.length];
int index = 0;
// iterate the String array
for (int i = 0; i < catalog.length; i++) {
// check if course array contains the dept
if (catalog[i].getDepartment() == dept) {
// course found
courses1[index] = catalog[i];
index++;
}
}
Course[] courses = new Course[index];
System.arraycopy(courses1, 0, courses, 0, index);
return courses;
}
public static int countCredits(Student student) {
Course[] courses = student.getSchedule();
int countOfCredits = 0;
for (int i = 0; i < courses.length; i++) {
countOfCredits = countOfCredits + courses[i].getCredits();
}
return countOfCredits;
}
public static boolean addCourse(Student student, Course c) {
Course[] coursesRegistered = student.getSchedule();
if (!Arrays.asList(coursesRegistered).contains(c)) {
for (int i = 0; i < coursesRegistered.length; i++) {
if (coursesRegistered[i] == null) {
coursesRegistered[i] = c;
return true;
}
}
} else {
return false;
}
return false;
}
public static boolean dropCourse(Student student, Course c) {
Course[] coursesRegistered = student.getSchedule();
Course[] coursesRegisteredUpdated = new Course[coursesRegistered.length - 1];
boolean changed = false;
if (Arrays.asList(coursesRegistered).contains(c)) {
int index = 0;
for (int i = 0; i < coursesRegistered.length; i++) {
if (coursesRegistered[i] == c) {
changed = true;
continue;
} else {
coursesRegisteredUpdated[index] = coursesRegistered[i];
index++;
}
}
student.setSchedule(coursesRegisteredUpdated);
} else {
return false;
}
return changed;
}
}
===catalog.txt===
'198:112 [Data Structure] M5 credits:4
560:101 [Elementary Italian] M2 credits:4
400:105 [Facets of Food Science] H1 credits:1
198:111 [Introduction to Computer Science] T7 credits:4
560:232 [Italian Culture] M5 credits:3
220:301 [Money and Banking] F5 credits:3
965:300 [New York Theater Experience] W6 credits:3
105:343 [Observational Radio Astronomy] M2 credits:3
776:310 [Plant Propagation] H2 credits:3
965:315 [Playwriting] W4 credits:3
105:342 [Principles of Astrophysics] T5 credits:3
080:201 [Seminar in Contemporary Art] F4 credits:3
=====O/P=====
Menu:
1. Look up course by name
2. Look up courses by department
3. View catalog, sorted by department/course number
4. View catalog, sorted by scheduled day/period
5. View my course schedule
6. View my current credit load
7. Register for a course
8. Withdraw from a course
9. View a courses' roster
10. Quit
Choice (1-10)?
7
Enter course name:
Playwriting
Successfully registered.
Menu:
1. Look up course by name
2. Look up courses by department
3. View catalog, sorted by department/course number
4. View catalog, sorted by scheduled day/period
5. View my course schedule
6. View my current credit load
7. Register for a course
8. Withdraw from a course
9. View a courses' roster
10. Quit
Choice (1-10)?
5
Current schedule:
965:315 [Playwriting] W4 credits:3
null entry in array
null entry in array
null entry in array
null entry in array
null entry in array
Menu:
1. Look up course by name
2. Look up courses by department
3. View catalog, sorted by department/course number
4. View catalog, sorted by scheduled day/period
5. View my course schedule
6. View my current credit load
7. Register for a course
8. Withdraw from a course
9. View a courses' roster
10. Quit
Choice (1-10)?
7
Enter course name:
Principles of Astrophysics
Successfully registered.
Menu:
1. Look up course by name
2. Look up courses by department
3. View catalog, sorted by department/course number
4. View catalog, sorted by scheduled day/period
5. View my course schedule
6. View my current credit load
7. Register for a course
8. Withdraw from a course
9. View a courses' roster
10. Quit
Choice (1-10)?
5
Current schedule:
965:315 [Playwriting] W4 credits:3
105:342 [Principles of Astrophysics] T5 credits:3
null entry in array
null entry in array
null entry in array
null entry in array
Menu:
1. Look up course by name
2. Look up courses by department
3. View catalog, sorted by department/course number
4. View catalog, sorted by scheduled day/period
5. View my course schedule
6. View my current credit load
7. Register for a course
8. Withdraw from a course
9. View a courses' roster
10. Quit
Choice (1-10)?
8
Enter course name:
Principles of Astrophysics
Successfully withdrawn.
Menu:
1. Look up course by name
2. Look up courses by department
3. View catalog, sorted by department/course number
4. View catalog, sorted by scheduled day/period
5. View my course schedule
6. View my current credit load
7. Register for a course
8. Withdraw from a course
9. View a courses' roster
10. Quit
Choice (1-10)?
5
Current schedule:
965:315 [Playwriting] W4 credits:3
null entry in array
null entry in array
null entry in array
null entry in array
Menu:
1. Look up course by name
2. Look up courses by department
3. View catalog, sorted by department/course number
4. View catalog, sorted by scheduled day/period
5. View my course schedule
6. View my current credit load
7. Register for a course
8. Withdraw from a course
9. View a courses' roster
10. Quit
Choice (1-10)?
2
Enter number of department:
105
105:343 [Observational Radio Astronomy] M2 credits:3
105:342 [Principles of Astrophysics] T5 credits:3
Menu:
1. Look up course by name
2. Look up courses by department
3. View catalog, sorted by department/course number
4. View catalog, sorted by scheduled day/period
5. View my course schedule
6. View my current credit load
7. Register for a course
8. Withdraw from a course
9. View a courses' roster
10. Quit
Choice (1-10)?
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.