I am writing a GUI interface program for a user to register for courses. There will be 1 textfield for the student name, 1 textfield for year they will be entering, one textfield for season they are registering for, 1 text field to enter their past courses, 5 more textfields where each one will be for each course they are registering for (the user can register up to 5 courses). I wrote the code, but the program is not doing what I want it to do. It is supposed to check for prerequisites. There are 3 arrays. One is for courses, one is for credits, and one is for the prerequisites. Some courses have two prerequisites (some have two but only one of the two needs to be taken), so I had to write some if statements to see if the user has either one of the prerequisites entered in the past courses textfield (and some require both prerequisites). My program is only printing out the receipt of registration. It is not validating and is not showing the errors I want. First of all, I have an if statement saying that if all 5 course registration textfields are left blank, then an error will return to the user, but that error is not working. My program is not validating to see if the user is missing prerequisites. It is printing out the receipt anyways even if they don't have a prerequisite. The code below is only the code used for when the user hits the register button. What is wrong with my code? I will show you my code, but only the necessary parts of it. If you need my entire program, let me know.
register.setOnAction(e -> {
String student = student_name.getText();
String y = year.getText();
String season = semester.getText();
String past = old_courses.getText();
String course1 = registration1.getText();
String course2 = registration2.getText();
String course3 = registration3.getText();
String course4 = registration4.getText();
String course5 = registration5.getText();
//Print an error dialogue if there is no customer name.
if (student.isEmpty())
{
Alert error1 = new Alert(AlertType.ERROR);
error1.setTitle("Need a student name");
error1.setHeaderText("Need student name");
error1.setContentText("Please enter your name.");
error1.showAndWait();
}
//Prints an error dialogue if table number is empty.
if (y.isEmpty())
{
Alert error2 = new Alert(AlertType.ERROR);
error2.setTitle("Need a year");
error2.setHeaderText("Need year");
error2.setContentText("Please enter a year");
error2.showAndWait();
}
if (season.isEmpty())
{
Alert error3 = new Alert(AlertType.ERROR);
error3.setTitle("Need a semester season");
error3.setHeaderText("Need semester season");
error3.setContentText("Please enter a semester season");
error3.showAndWait();
}
if (course1.isEmpty() && course2.isEmpty() && course3.isEmpty() && course4.isEmpty() && course5.isEmpty()){
Alert error5 = new Alert(AlertType.ERROR);
error5.setTitle("Need at least 1-5 courses");
error5.setHeaderText("Need 1-5 courses");
error5.setContentText("Please enter at least 1 or more courses.");
}
String [] courses = {"IT 101", "CS 100", "MATH 138", "HUM 101", "CS 107", "CS 113", "IT 120", "MATH 105", "HUM 102",
"IT 114", "IT 201", "IT 202", "IS 207", "IS 331", "IT 340", "IT 420", "IT 490", "IT 491", "CS 407"};
int [] credits = {3, 3, 3, 3, 1, 3, 3, 3, 3, 3, 3, 3, 1, 3, 3, 3, 3, 3, 1};
String [] prerequisites = {null, null, null, null, null, "CS 100", null, null, "HUM 101", "CS 113", "IT 101", null,
"CS 107", "IT 202", "IT 120", null, "IT 420", null, null};
for(int i = 0; i < courses.length; i++)
{
if(i == courses.length - 1 && !course1.contains(courses[i]) || !course2.contains(courses[i])
|| !course3.contains(courses[i]) || !course4.contains(courses[i]) || !course5.contains(courses[i]))
{
Alert error6 = new Alert(AlertType.ERROR);
error6.setTitle("Invalid course detected.");
error6.setHeaderText("Invalid course detected.");
error6.setContentText("You have tried to register for an invalid course.");
break;
}
if(i == 17 && !y.contains("senior") || !y.contains("Senior") && course1.contains(courses[i]) || course2.contains(courses[i])
|| course3.contains(courses[i]) || course4.contains(courses[i]) || course5.contains(courses[i]))
{
Alert error8 = new Alert(AlertType.ERROR);
error8.setTitle("Not senior standing");
error8.setHeaderText("Not entering senior year");
error8.setContentText("You must be entering senior year to have IT 491!");
continue;
}
if(i == 11 && !past.contains("CS 100") || !past.contains("CS 113") && course1.contains(courses[i]) || course2.contains(courses[i])
|| course3.contains(courses[i]) || course4.contains(courses[i]) || course5.contains(courses[i]))
{
Alert error9 = new Alert(AlertType.ERROR);
error9.setTitle("Missing prerequisite");
error9.setHeaderText("You are missing a prerequisite");
error9.setContentText("You have not taken neither CS 100 nor CS 113 yet. You must take at least one of the two.");
continue;
}
if(i == 15 && !past.contains("IT 120") && !past.contains("CS 113") && course1.contains(courses[i]) || course2.contains(courses[i])
|| course3.contains(courses[i]) || course4.contains(courses[i]) || course5.contains(courses[i]))
{
Alert error10 = new Alert(AlertType.ERROR);
error10.setTitle("Missing prerequisite");
error10.setHeaderText("You are missing a prerequisite");
error10.setContentText("You have not taken either IT 120 or CS 113 yet. Both courses must be taken.");
continue;
}
if(i == 18 && !past.contains("CS 107") && !past.contains("CS 207") && course1.contains(courses[i]) || course2.contains(courses[i])
|| course3.contains(courses[i]) || course4.contains(courses[i]) || course5.contains(courses[i]))
{
Alert error11 = new Alert(AlertType.ERROR);
error11.setTitle("Missing prerequisite");
error11.setHeaderText("You are missing a prerequisite");
error11.setContentText("You have not taken either CS 107 or CS 207. Both courses must be taken.");
continue;
}
if(!past.contains(prerequisites[i]) && course1.contains(courses[i]) || course2.contains(courses[i])
|| course3.contains(courses[i]) || course4.contains(courses[i]) || course5.contains(courses[i]))
{
Alert error7 = new Alert(AlertType.ERROR);
error7.setTitle("Missing prerequisite");
error7.setHeaderText("You are missing a prerequisite.");
error7.setContentText("Missing prerequisite: " + prerequisites[i]);
}
}
execute_registration();});
Aucun commentaire:
Enregistrer un commentaire