When I run the code I do not get my desired result for the setDepartment method. I'm trying to trap the user if they entered a wrong selection. When I execute the code and put in a value (string) that should without a doubt execute, it does not execute. I have tested to see where the problem could lay. The only thing that worked is when I tried hardcoding the correct value into the method. If I put in an incorrect value and trap the user, and put in the correct value, the user is still trapped in the loop. I checked to see if there are any logical errors in my loop, but I could not find a problem. I am at a complete loss, I can't wait to find what I messed up on.
Please ignore the courseCost instance variable I haven't worked on that yet, and please disregard the system.out.println(department) after the department = department.toUpperCase in the setDepartment method. I was using the system.out.println(department) in the setDepartment method to rule out any possible errors that may have been on the department = department.toUpperCase line of code. Also, you can ignore everything else, everything else works just fine. Thank you for taking the time to take a look.
import java.util.Scanner;
public class Course {
static Scanner keyboard = new Scanner(System.in);
private String department;
private int courseNumber;
private int courseCredits;
private double courseCost;
public Course () {
department = "unknown";
courseNumber = 0;
courseCost = 0;
courseCredits = 0;
}
public Course(String department, int courseNumber, int courseCredits) {
setDepartment(department);
setCourseNumber(courseNumber);
setCourseCredits(courseCredits);
//courseCost = no value will be passed to the constructor, courseCost will be calculated as courseCredits/2 * $500, see setter below (if the course is a Lab Course add $100 to the cost)
}
public String getDepartment() {
return department;
}
public int getCourseNumber() {
return courseNumber;
}
public int getCourseCredits() {
return courseCredits;
}
public double getCourseCost() {
return courseCost;
}
public void setDepartment(String department) {
boolean enteredCorrectly = false;
//department = department.toUpperCase();
do{
department = department.toUpperCase();
System.out.println(department);
//ENGL, MATH, COMP, HIST, HUMN, SCIE, LANG, PHYS
if( (department == "ENGL") || (department == "MATH") || (department == "COMP") ||
(department == "HIST") || (department == "HUMN") || (department == "SCIE") ||
(department == "LANG") || (department == "PHYS") ) {
this.department = department;
enteredCorrectly = true;
} else {
System.out.println("Please re-enter a valid department.");
department = keyboard.nextLine();
}
} while(!(enteredCorrectly));
}
public void setCourseNumber(int courseNumber) {
boolean enterCorrectly = false;
do{
if(1 <= courseNumber && courseNumber <= 399) {
this.courseNumber = courseNumber;
enterCorrectly = true;
} else {
System.out.println("Please re-enter a valid course number.");
courseNumber = keyboard.nextInt();
keyboard.nextLine();
}
} while(!(enterCorrectly));
}
public void setCourseCredits(int courseCredits) {
boolean enterCorrectly = false;
do{
if(courseCredits == 3 || courseCredits == 4 || courseCredits == 6) {
this.courseCredits = courseCredits;
enterCorrectly = true;
} else {
System.out.println("Please re-enter a valid course credits.");
courseCredits = keyboard.nextInt();
keyboard.nextLine();
}
} while(!(enterCorrectly));
}
public void setCourseCost(double courseCost) {
this.courseCost = courseCost;
}
public static void main(String[] args) {
Course c = new Course ("engl", 991, 1);
}
}