I have this rather ghastly if
statement and I'm sure there is a way to condense it, but I don't really know how. I was told a case
statement would work but I've never used a case
statement and I'm not sure.
public void add(int p, Course c)
{
if (p == 1){
if (course1.isFree() == true) {
course1 = c;
System.out.println("Course " + c + "added to period " + p +".\n");
} else {
System.out.println("Unable to add class to a nonfree period.");
}
} else if (p == 2){
if (course2.isFree() == true) {
course2 = c;
System.out.println("Course " + c + "added to period " + p +".\n");
} else {
System.out.println("Unable to add class to a nonfree period.");
}
} else if (p == 3){
if (course3.isFree() == true) {
course3 = c;
System.out.println("Course " + c + "added to period " + p +".\n");
} else {
System.out.println("Unable to add class to a nonfree period.");
}
} else if (p == 4){
if (course4.isFree() == true) {
course4 = c;
System.out.println("Course " + c + "added to period " + p +".\n");
} else {
System.out.println("Unable to add class to a nonfree period.");
}
} else if (p == 5){
if (course5.isFree() == true) {
course5 = c;
System.out.println("Course " + c + "added to period " + p +".\n");
} else {
System.out.println("Unable to add class to a nonfree period.");
}
} else if (p == 6){
if (course6.isFree() == true) {
course6 = c;
System.out.println("Course " + c + "added to period " + p +".\n");
} else {
System.out.println("Unable to add class to a nonfree period.");
}
} else if (p == 7){
if (course7.isFree() == true) {
course7 = c;
System.out.println("Course " + c + "added to period " + p +".\n");
} else {
System.out.println("Unable to add class to a nonfree period.");
}
} else if (p == 8){
if (course8.isFree() == true) {
course8 = c;
System.out.println("Course " + c + "added to period " + p +".\n");
} else {
System.out.println("Unable to add class to a nonfree period.");
}
}
totalcredits();
}