Hello everyone,
I am doing an assignment that requires me to create an application that manages a student records database. They gave me the option to use a GUI or console application so I am making it a console app. The task requires that we use the abstract class pattern. The problem i am having is controlling flow when I call the add() method which is an abstract method in a super class named Student. I call the add() method from the starter class with the main method when I create a new Student object of type Undergraduate which is a class that extends the Student class. The add method in each subclass also contains a database connection with statements that are unique to that subclass. ie (Undergraduate, Graduate, Part-time). The specific problem that I am having is when using a Scanner to get the student information from the user the program skips whatever System out is last and falls straight down to the database connection. I know I have some kind of problem with my try catch. Here is what I got:
` public void add()
{
try {
Integer statusValue = 0;// for casting scanner values
this.level = getLevel(); // call getLevel member method
setLevel(this.level); // set local variable level
studentID = getNewSID(); // call super class Student's "student ID generator" method
setStudentID(super.studentID); //set the super class studentID instance field
// Scanner for getting student information
Scanner getStudentInfo = new Scanner(System.in);
// Get student's first name
System.out.println("Please enter student first name");
firstName = getStudentInfo.nextLine();
setFirstName(super.firstName); //set the super class first name instance field
// Get student's last name
System.out.println("Please enter student lastname");
lastName = getStudentInfo.nextLine();
setLastName(super.lastName); // set the super class last name instance field
// Get student's Residency status
System.out.println("Please enter Student Status (1 = Resident 2 = Non-resident)");
statusValue = getStudentInfo.nextInt();
if (statusValue.equals(1))
{
super.status = "Resident"; // if user choose 1
setStatus(super.status); // set super class status instance field
}
else
{
super.status = "Resident";// if user choose 2
setStatus(super.status); // set super class status instance field
}
// Get student GPA
System.out.println("Please enter student GPA");
gpa = getStudentInfo.nextDouble();
setGpa(super.gpa);
System.out.println("Please enter student mentor's name (20 char max) >");
mentor = getStudentInfo.nextLine();
setMentor(super.mentor);
//////////////////////////////////need to control flow here//////////////////
//////////////////////////////need to control flow here//////////////////
Statement stmt;
DBConnect conn = new DBConnect();
stmt = conn.makeStatement();
stmt.execute("Insert Into Student (studentID,firstName,lastName,status,gpa,mentor,level,thesisTitle,thesisAdvisor,company,type)"
+ " Values ('"
+ studentID + "', '"
+ firstName + "', '"
+ lastName + "', '"
+ status + "', '"
+ gpa + "', '"
+ mentor + "', '"
+ level + "', '"
+ "NA', '"
+ "NA', '"
+ "NA', '"
+ type + "')");
stmt.close();
conn.close();
} catch (SQLException | InputMismatchException e) {
System.err.println(e + "Could not insert");
}
System.out.println("Successfully added student to database");
System.out.println("from Undergraduate class");
}`
Any help is appreciated very much.