Last active
September 29, 2016 19:47
-
-
Save Tandrial/125bbeacf5eacbf85eda727b39480de0 to your computer and use it in GitHub Desktop.
[Java] Using a nested for loop or nested while loop fails to iterate through all elements REFACTOR
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public boolean detectConflict(Course aCourse) { //FIXME | |
boolean foundConflict = false; | |
for (Course existingCourse : schedule) { | |
for (Integer timeofNewCourse : aCourse.getSchedule()) { | |
for (Integer timeofExistingCourse : existingCourse.getSchedule()) { | |
if (timeofExistingCourse.equals(timeofNewCourse)) { | |
System.out.print(String.format("%s%d course cannot be added to %s's schedule. ", | |
aCourse.getDepartment().getDepartmentName(), | |
aCourse.getCourseNumber(), | |
this.getName())); | |
System.out.println(String.format("%s%d conflicts with %s%d. Conflicting time slot is : %s %s", | |
aCourse.getDepartment().getDepartmentName(), | |
aCourse.getCourseNumber(), | |
existingCourse.getDepartment().getDepartmentName(), | |
existingCourse.getCourseNumber(), | |
convertCourseNumToDayofWeek(aCourse.getCourseNumber()), | |
convertCourseNumToTime(aCourse.getCourseNumber()))); | |
foundConflict = true; | |
} | |
} | |
} | |
} | |
return foundConflict; | |
} | |
private String convertCourseNumToTime(int courseNum) { | |
switch (courseNum % 100) { | |
case 1: | |
return "8:00am to 9:15am."; | |
case 2: | |
return "9:30am to 10:45am."; | |
case 3: | |
return "11:00am to 12:15pm."; | |
case 4: | |
return "12:30pm to 1:45pm."; | |
case 5: | |
return "2:00pm to 3:15pm."; | |
case 6: | |
return "3:30pm to 4:45pm."; | |
} | |
return ""; | |
} | |
private String convertCourseNumToDayofWeek(int courseNum) { | |
switch (courseNum / 100) { | |
case 1: | |
return "Mon"; | |
case 2: | |
return "Tue"; | |
case 3: | |
return "Wed"; | |
case 4: | |
return "Thu"; | |
case 5: | |
return "Fri"; | |
} | |
return ""; | |
} | |
public void printSchedule() { | |
for (Course course : schedule) { | |
for (Integer courseNumber : course.getSchedule()) { | |
System.out.println(String.format("%s %s %s%d %s", | |
convertCourseNumToDayofWeek(courseNumber), | |
convertCourseNumToTime(courseNumber), | |
course.getDepartment().getDepartmentName(), | |
course.getCourseNumber(), | |
course.getName())); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment