I am working on a small app that will allow a student to join a class when selected. I want to have the user be able to select a student by ID, select a class by ID and have the user be added to that class. This works, but after the student is added, I want the program to ask if the student wants to join another class, instead of resetting the whole program and having to go back.
I think I am close to a solution, but do not know how to get my method (signUp()) to go back to halfway through the method. Does this make sense? Once a student is entered in a class and "join another class" is selected I want to return to the bold comment in the code.
Sorry if this is long-winded and confusing, I'm new to this, so I appreciate your patience!
static void signUp() {
System.out.println("\nSign Up For a Class\n");
try {
Scanner input = new Scanner(System.in);
System.out.println("Enter Student ID: ");
String user_entered_student_id = input.nextLine();
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/ClassSelector?autoReconnect=true&useSSL=false", "", "");
Statement myStmt = con.createStatement();
Statement myStmt2 = con.createStatement();
Statement myStmt3 = con.createStatement();
ResultSet rs;
rs = myStmt.executeQuery("SELECT student_name FROM ClassSelector.students WHERE student_id = " + user_entered_student_id);
while (rs.next()) {
String userEnterId = rs.getString("student_name");
System.out.println("Is " + userEnterId + " the correct student? (Y/N)");
String confirm = input.nextLine();
if (confirm.equals("Y") || confirm.equals("y")) {
ResultSet rs2 = myStmt2.executeQuery("SELECT * FROM ClassSelector.classes");
while (rs2.next()) {
String avlClasses = rs2.getString("class_id") + "\t" + rs2.getString("classname") + "\t" + rs2.getString("description");
System.out.println(avlClasses);
}
} else if (confirm.equals("N") || confirm.equals("n")) {
System.out.println("Oops, let start over");
return;
}
**//RETURN TO THIS SECTION OF CODE AND PROCEEED**
System.out.println("Enter Class ID from Classes Listed Above to Join: ");
String selectedClass = input.nextLine();
ResultSet rs3 = myStmt3.executeQuery("SELECT * FROM ClassSelector.classes WHERE class_id = " + selectedClass);
while (rs3.next()) {
String innerJoin = (userEnterId + " has been added to " + rs3.getString("classname") + " " + rs3.getString("class_id"));
System.out.println(innerJoin);
String student_classJoin = "INSERT IGNORE INTO student_x_class" + "(student_id,student_name, class_id, classname)" + "VALUES (?, ?, ?, ?)";
PreparedStatement pStmt = con.prepareStatement(student_classJoin);
pStmt.setString(1, user_entered_student_id);
pStmt.setString(2, userEnterId);
pStmt.setString(3, rs3.getString("class_id"));
pStmt.setString(4, rs3.getString("classname"));
pStmt.executeUpdate();
System.out.println("Would you like to enroll " + userEnterId + " into another class? (Y/N)");
String additionalClass = input.nextLine();
if(additionalClass.equals("Y") || additionalClass.equals("y")){
signUp();
}
}
}
} catch (java.sql.SQLException SQL) {
SQL.printStackTrace();
} catch (Exception EXC) {
EXC.printStackTrace();
}
}
This code is very confusing. But let's take a step back and look at some pseudo -code which may represent the sort of structure you're looking for.
Let's start with the operation being performed. Something like this:
studentID = prompt("enter a student ID");
student = get_from_data(studentID);
courseID = prompt("enter a course ID");
course = get_from_data(courseID);
insert_in_data(studentID, courseID);
The details are very implementation-specific, but in general your primary logical approach should semantically look very similar to that. In fact, it's often a very good idea to extract code into small individual methods where the method name describes the operation being performed. Then the overall sequence of steps reads like a short story which describes the business process.
Now, you want to repeat that structure? Well, that's what loops are for. Something like this:
while(!someTerminatingCondition) {
// the steps above
}
Now we just need to define that condition. It's based on user input, right? So something like this:
shouldContinue = true;
while(shouldContinue) {
// the steps above
shouldContinue = prompt("continue?");
}
To give you something to Google, what you're describing is often called a "game loop". The idea is that the entire process should repeat, over and over, until some specific condition is met. (In a game this condition would be the determination of a winner.)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.