Reputation: 33
So I'm having an unusual issue where code is detecting the current user with the method getCurrentUser()
but when I use it later to check which user borrowed a book it returns null.
I also attempted changing line 3 of borrowBook method to the currentUser variable, and also tried getting the info through usernameTxtField.getText()
public class Controller {
private Book HFJ = new Book("HFJ", "SI & BA","5678",3, 1);
@FXML
private void attemptLogin(ActionEvent event) throws IOException {
String usernameInput = usernameTxtField.getText();
String passwordInput = passwordTxtField.getText();
boolean loginSuccessful = false;
switch (usernameInput.toLowerCase()) {
case "user1":
if (passwordInput.equals("Password1")) {
System.out.println("Login Successful");
informationLabel.setText("Login Successful");
loginSuccessful = true;
currentUser = usernameInput.toLowerCase();
}
break;
case "user2":
if (passwordInput.equals("Password2")) {
System.out.println("Login Successful");
informationLabel.setText("Login Successful");
loginSuccessful = true;
currentUser = usernameInput.toLowerCase();
}
break;
case "user3":
if (passwordInput.equals("Password3")) {
System.out.println("Login Successful");
informationLabel.setText("Login Successful");
loginSuccessful = true;
currentUser = usernameInput.toLowerCase();
}
break;
case "user4":
if (passwordInput.equals("Password4")) {
System.out.println("Login Successful");
informationLabel.setText("Login Successful");
loginSuccessful = true;
currentUser = usernameInput.toLowerCase();
}
break;
default:
System.out.println("Incorrect username or Password");
informationLabel.setText("Incorrect username or Password");
break;
}
if (loginSuccessful == true) {
Parent home = FXMLLoader.load(getClass().getResource("home.fxml"));
Scene home_scene = new Scene(home);
Stage primaryStage = (Stage) ((Node) event.getSource()).getScene().getWindow();
primaryStage.setScene(home_scene);
primaryStage.show();
currentUser = usernameInput.toLowerCase();
System.out.println(getCurrentUser());
homeLoginInfoLabel.setText("Login Successful");
}
}
@FXML private Label HFJQuantity = new Label();
@FXML private Button HFJBorrowBtn = new Button();
@FXML
private void borrowBook(ActionEvent event) throws IOException{
if(HFJ.getQuantity()>=1){
HFJ.setQuantity(HFJ.getQuantity() -1);
System.out.println("Book Borrowed By: " + getCurrentUser());//CONSOLE PRINT
HFJQuantity.setText("" + HFJ.getQuantity());
Parent borrowSuccessful = FXMLLoader.load(getClass().getResource("borrowSuccess.fxml"));
Scene success_scene = new Scene(borrowSuccessful);
Stage primaryStage = (Stage) ((Node) event.getSource()).getScene().getWindow();
primaryStage.setScene(success_scene);
primaryStage.show();
}else{
System.out.println("Not enough books");
HFJBorrowBtn.setDisable(true);
}
}
private String getCurrentUser() {
return currentUser;
}
When I run the code and login this is what the console prints:
Login Successful
user1
Book Borrowed By: null
Why isn't it recognising the current user? Any help would be great
Upvotes: 0
Views: 53
Reputation: 11
I think the problem you are having is down to scope. I can't see in this class where you have declared currentUser outside of the switch statement or the if/else. Try initialising currentUser at the top with your other class variable.
Upvotes: 1