Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class LoginController {
- @FXML AnchorPane mainPane;
- @FXML AnchorPane secondaryPane;
- @FXML Button loginButton;
- @FXML Button toSignUp;
- @FXML PasswordField passwordField;
- @FXML TextField usernameField;
- static String file = "info.txt";
- static Scanner scanFile = new Scanner(file);
- static Stage secondaryStage;
- public void buttonClickedHandler (ActionEvent evt) {
- Button clickedButton = (Button) evt.getTarget();
- String buttonLabel = clickedButton.getText();
- if(loginButton.getText().equals(buttonLabel)) {
- String username = usernameField.getText();
- String password = passwordField.getText();
- boolean found = verifyLogin(username,password,file);
- if (found) {
- openWindow("loginSuccess", 350, 150);
- }
- }
- if(toSignUp.getText().equals(buttonLabel)) {
- openWindow("SignUp", 600, 400);
- }
- }
- void openWindow(String name, int width, int height) {
- try {
- // load the pop up you created
- Pane pane = (Pane)FXMLLoader.load(getClass().getResource(name + ".fxml"));
- // create a new scene
- Scene scene = new Scene(pane,width,height);
- //select corresponding stylesheet to correct window
- scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
- //create new stage to put scene in
- secondaryStage = new Stage();
- secondaryStage.setScene(scene);
- secondaryStage.setResizable(false);
- secondaryStage.showAndWait();
- } catch(Exception e) {
- e.printStackTrace();
- }
- }
- boolean verifyLogin(String username, String password, String filepath) {
- boolean found = false;
- String tempUsername = "";
- String tempPassword = "";
- try {
- scanFile = new Scanner(new File(filepath));
- scanFile.useDelimiter("/");
- //While loop read each line from designated file for the records of usernames and passwords
- //and save each password and username in temp strings below
- while (scanFile.hasNext() && !found) {
- //System.out.println("ok");
- tempUsername = scanFile.next();
- tempPassword = scanFile.next();
- if (tempUsername.equals(username) && tempPassword .equals(password)) {
- found = true;
- }
- }
- scanFile.close();
- System.out.println(found);
- }
- catch(Exception e) {
- System.out.println("Error");
- }
- return found;
- }
- }
- *********************************************SIGNUP************************************
- public class SignUpController {
- @FXML Button SignUpButton;
- @FXML TextField EnterUsername;
- @FXML PasswordField EnterPassword;
- @FXML PasswordField reEnterPassword;
- @FXML Text PassError;
- @FXML Text PassError1;
- @FXML Text UserExists;
- static String file = "info.txt";
- static Scanner scanFile = new Scanner(file);
- static Stage secondaryStage;
- public void buttonClickedHandler (ActionEvent evt) throws IOException {
- Button clickedButton = (Button) evt.getTarget();
- String buttonLabel = clickedButton.getText();
- if(SignUpButton.getText().equals(buttonLabel)) {
- String newUsername = EnterUsername.getText();
- String newPassword = EnterPassword.getText();
- String checkPassword = reEnterPassword.getText();
- if (newPassword.equals(checkPassword)) {
- boolean requiredPassword = checkPassword(newPassword);
- if (requiredPassword) {
- boolean done = checkCredentials(newUsername, newPassword, checkPassword, file);
- if (!done) {
- closeCurrentWindow(evt);
- }
- }
- }
- }
- }
- private boolean checkPassword(String password) {
- boolean meetsRequirments = false;
- char ch;
- for (int i = 0; i < password.length(); i++) {
- ch = password.charAt(i);
- if (Character.isUpperCase(ch)) {
- meetsRequirments = true;
- }
- }
- if (!meetsRequirments) {
- PassError.setVisible(true);
- PassError1.setVisible(true);
- }
- return meetsRequirments;
- }
- boolean checkCredentials(String newUsername, String newPassword, String checkPassword, String filepath) throws IOException {
- boolean userExists = false;
- try {
- String tempUsername = "";
- String hold = "";
- scanFile = new Scanner(new File(filepath));
- scanFile.useDelimiter("/");
- //While loop read each line from designated file for the records of usernames and passwords
- //and save each password and username in temp strings below
- while (scanFile.hasNext() && !userExists) {
- //System.out.println("ok");
- tempUsername = scanFile.next();
- hold = scanFile.next();
- if (tempUsername.equals(newUsername)) {
- userExists = true;
- }
- }
- scanFile.close();
- if (userExists) {
- UserExists.setVisible(true);
- }
- }
- catch(Exception e) {
- System.out.println("Error");
- }
- if (!userExists) {
- addCredentials(newUsername, newPassword, file);
- }
- return userExists;
- }
- private void addCredentials(String newUsername, String newPassword, String file) throws IOException {
- FileWriter fw = new FileWriter(file, true);
- fw.write("/"+newUsername+"/"+newPassword);
- fw.close();
- }
- private void closeCurrentWindow(ActionEvent evt) {
- final Node source = (Node) evt.getSource();
- final Stage stage =(Stage)source.getScene().getWindow();
- stage.close();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement