Witam, jestem w trakcie tworzenia pierwszej aplikacji, dotyczy ona rezerwacji stolików w restauracji. Mam problem dotyczący sposobu sprawdzenia czy stolik, który wpisał użytkownik istnieje w bazie. To co napisałam działa w ten sposób, że jeżeli mam do wyboru tylko stolik nr 2 to jeżeli wpiszę cyfrę większą od 2 lub litery to ponownie pyta o podanie stolika, gdyż takiego w bazie nie ma, ale gdy wpiszę 1 to przechodzi dalej nie prosząc o poprawną liczbę.
Poniżej wklejam istotny fragment kodu, najprawdopodobniej z metodą checkIfTableExists jest coś nie tak, ale nie mam innego pomysłu.
private static String getTableNumber(Database database, Scanner scan, int restaurantId, String hallNumber) throws SQLException {
boolean incorrectSign;
String tableNumber;
do {
incorrectSign= false;
System.out.println("Wybierz numer stolika: ");
database.showTablesForHall(restaurantId, hallNumber);
tableNumber = scan.nextLine();
if (database.checkIfTableExists(database, tableNumber)){
incorrectSign=true;
System.out.println("Stolik o podanym numerze nie istnieje");
}
}while(incorrectSign);
return tableNumber;
}
public void showTablesForHall(int restaurantId, String hallNumber) throws SQLException {
resultSet = statement.executeQuery("SELECT * FROM tables where hall_id = (SELECT id from halls where number='" + hallNumber + "' AND restaurant_id = (SELECT id from restaurants where id=" + restaurantId + "))");
while (resultSet.next()) {
System.out.println(resultSet.getString("number"));
}
closeResultSet(resultSet);
}
public boolean checkIfTableExists(Database database, String tableNumber) throws SQLException {
resultSet = statement.executeQuery("SELECT number from tables where EXISTS (SELECT number from tables where number='" + tableNumber + "')");
if (resultSet.isBeforeFirst()) {
return false;
} else {
return true;
}
}