I have the following code which is located within a case, in a switch statement. Its goal is to add a Pupil to a Subject using the Collections in the respective class.
I've tried to make the code as readable as possible, but I'm sure there's a better approach.
System.out.println("Adding a Pupil...");
String AddPupilSubj;
do {
UserInput.prompt("\nEnter the Subject Code to add the Pupil to, or enter 'list' to list all Subjects: ");
AddPupilSubj = UserInput.readString();
if(AddPupilSubj.equals("list"))
school.listSubjects();
} while(AddPupilSubj.isEmpty() || AddPupilSubj.equals("list"));
Boolean found = false;
// Locate the Subject
for(Object obj : school.getSubjects()) {
Subject subj = (Subject) obj;
if(subj.getCode().equals(AddPupilSubj)) {
found = true;
// Get the ID of the Pupil
UserInput.prompt("Enter Pupil ID: ");
int id = UserInput.readInt();
// Get the Pupil's Name
UserInput.prompt("Enter Pupil Name: ");
String name = UserInput.readString();
// Get the Address of the Pupil
UserInput.prompt("Enter Pupil Address: ");
String address = UserInput.readString();
String date; Boolean valid;
do {
// Get the Pupil's Date of Birth
UserInput.prompt("Enter Pupil Date of Birth (dd/mm/yyyy): ");
date = UserInput.readString();
valid = validDOB(date);
} while(!valid);
// Get the Pupil's email address
UserInput.prompt("Enter Pupil Email: ");
String email = UserInput.readString();
DateTime dob = DateTime.forDateOnly(Integer.parseInt(date.substring(6, 10)),
Integer.parseInt(date.substring(3, 5)),
Integer.parseInt(date.substring(0, 2)));
DateTime admitted = DateTime.today(TimeZone.getDefault());
Pupil Pupil = new Pupil(id, name, address, dob, email, admitted);
if(subj.addPupil(Pupil)) {
System.out.println(
String.format("\nPupil, %s, has been added to the %s Subject.",
Pupil.getName(),
subj.getName()));
} else {
System.out.println(
String.format("Pupil #%s is already in the Subject!",
Pupil.getID()));
}
break;
}
}
if(!found) {
System.out.println(
String.format("A Subject with code, %s, doesn't exist!",
AddPupilSubj));
}
Boolean
instead ofboolean
? \$\endgroup\$