I am trying to code a program that has conditions and asks these questions for 3 courses:
- what is the name of the course?
- How many students are enrolled in the course?
- What is student capacity for this course?
- When is the final exam date for the third course?
The condition is the final exam date for course 2 can't be the same as final exam date for course 1 and final exam date for course 3 can't be the same as course 1 or course 2. It checks for course one but terminates before checking for course 2.
Here is the code:
import java.util.Scanner;
public class CourseTest2 {
public static void main(String[] args) {
Scanner scanoption = new Scanner(System.in);
Scanner in = new Scanner(System. in);
String name1;
int enroll1;
int cap1;
String final1;
String answer;
//This is for the first course
System.out.println("What is the name of the course?");
name1 = in. nextLine();
System.out.println("How many students are enrolled in the course?");
enroll1 = in. nextInt();
System.out.println("What is student capacity for this course?");
cap1 = in. nextInt(); in.nextLine();
while (enroll1 > cap1)
{
System.out.println("Amount of Enrolled Students cannot exceed Student Capacity.Press Enter");
in.nextLine();
System.out.println("How many students are enrolled in the course?");
enroll1 = in. nextInt();
System.out.println("What is student capacity for this course?");
cap1 = in. nextInt(); in.nextLine();
}
System.out.println("When is the final exam date for this course?");
final1 = in. nextLine();
Course course1 = new Course (name1);
System.out.println("Do you want to add another course? (Y/N)"); // this is to repeat the questions so the use can enter information for the next class obejct
answer = in.nextLine();
course1.setName(name1);
course1.setEnrolledStudentNum(enroll1);
course1.setFinalExamDate(final1);
//2nd Course Object
String name2;
int enroll2;
int cap2;
String final2;
switch (answer)
{
case "Y":
case "y": System.out.println("What is the name of the second course?");
name2 = in. nextLine();
System.out.println("How many students are enrolled in the course?");
enroll2 = in. nextInt();
System.out.println("What is student capacity for this course?");
cap2 = in. nextInt();in.nextLine();
while (enroll2 > cap2)
{
System.out.println("Amount of Enrolled Students cannot exceed Student Capacity.Press Enter");
in.nextLine();
System.out.println("How many students are enrolled in the course?");
enroll2 = in. nextInt();
System.out.println("What is student capacity for this course?");
cap2 = in. nextInt(); in.nextLine();
}
do
{
System.out.println("When is the final exam date for this course?");
final2 = in. nextLine();
}
while ( final2 == final1);
{
System.out.println("Final Exam Date for Second Course cannot be the same as the first course. Press enter and try again");
in.nextLine();
System.out.println("When is the final exam date for this course?");
final2 = in. nextLine();
}
Course course2 = new Course (name2);
System.out.println("Do you want to add another course? (Y/N)");// this is to repeat the questions so the use can enter information for the next class obejct
answer = in.nextLine();
course2.setName(name2);
course2.setEnrolledStudentNum(enroll2);
course2.setFinalExamDate(final2);
break;
case "N":
case "n":System.exit(0);
break;
default :
System.out.println("invalid choice") ;
break;
}
//3rd Class Object
String final3 = null;
switch (answer)
{
case "Y":
case "y": System.out.println("What is the name of the third course?");
String name3 = in. nextLine();
System.out.println("How many students are enrolled in the course?");
int enroll3 = in. nextInt();
System.out.println("What is student capacity for this course?");
int cap3 = in. nextInt();in.nextLine();
while (enroll3 > cap3)
{
System.out.println("Amount of Enrolled Students cannot exceed Student Capacity. Press Enter");
in.nextLine();
System.out.println("How many students are enrolled in the course?");
enroll3 = in. nextInt();
System.out.println("What is student capacity for this course?");
cap3 = in. nextInt(); in.nextLine();
}
do
{
System.out.println("When is the final exam date for the third course?");
final3 = in. nextLine();
}
while ( final3 == final1 && final3 == final2);
{
System.out.println("Final Exam Date for third Course cannot be the same as the first or second course. Press enter and try again");
in.nextLine();
System.out.println("When is the final exam date for this course?");
final3 = in. nextLine();
in.nextLine();
}
Course course3 = new Course (name3);
course3.setName(name3);
course3.setEnrolledStudentNum(enroll3);
course3.setFinalExamDate(final3); `
This is what it prints out:
What is the name of the course? a How many students are enrolled in the course? 5 What is student capacity for this course? 20 When is the final exam date for this course? 05/05 Do you want to add another course? (Y/N) y What is the name of the second course? b How many students are enrolled in the course? 10 What is student capacity for this course? 5 Amount of Enrolled Students cannot exceed Student Capacity.Press Enter How many students are enrolled in the course? 10 What is student capacity for this course? 20 When is the final exam date for this course? 05/05 Final Exam Date for Second Course cannot be the same as the first course. Press enter and try again When is the final exam date for this course? 10/10 Do you want to add another course? (Y/N) y What is the name of the third course? c How many students are enrolled in the course? 15 What is student capacity for this course? 10 Amount of Enrolled Students cannot exceed Student Capacity. Press Enter How many students are enrolled in the course? 15 What is student capacity for this course? 20 When is the final exam date for the third course? 05/05 Final Exam Date for third Course cannot be the same as the first or second course. Press enter and try again When is the final exam date for this course? 10/10
As you can see the second attempt to change the final exam date meets the condition so it should say "Final Exam Date for third Course cannot be the same as the first or second course. Press enter and try again" but it just terminates after the user presses enter