So I'm currently writing a program that basically asks for user input and calculates total course load and fees based on that output.
I'm a bit stuck, however. I'm supposed to prevent the user from entering the same course number more than once and I can't seem to figure it out. Any help would be appreciated!
This is a small piece of my code:
while (option == JOptionPane.YES_OPTION) {
if (!(typeOfStudent.equals("Online") || typeOfStudent.equals("On campus"))) {
JOptionPane.showMessageDialog(null, "Enter either 'Online' or 'On campus'");
System.exit(0);
}
courseNumber = JOptionPane.showInputDialog("Enter the Course Number for the class you are taking(100/150/250/300): " + "\n");
courseNum = Integer.parseInt(courseNumber);
if (!(courseNum == 100 || courseNum == 150 || courseNum == 250 || courseNum == 300)) {
JOptionPane.showMessageDialog(null, "Enter a valid course number (100/150/250/300)");
System.exit(0);
}
if (courseNum == 100 && (typeOfStudent.equals("Online"))) {
totalNumCredits += credits100;
totalStudentFee += onlineStudentFee100;
}
Maintain a list of courses that are already selected by the user:
List<Integer> courses = new ArrayList<>();
Then modify your current code to check if the course is already selected or not.
if (!(courseNum == 100 || courseNum == 150 || courseNum == 250 || courseNum == 300)) {
JOptionPane.showMessageDialog(null, "Enter a valid course number (100/150/250/300)");
System.exit(0);
}
if(courses.contains(courseNum) {
JOptionPane.showMessageDialog(null, "It is an already selected course number");
return;
}
courses.add(courseNum);
if (courseNum == 100 && (typeOfStudent.equals("Online"))) {
totalNumCredits += credits100;
totalStudentFee += onlineStudentFee100;
}