Question
How do I have ALL attempts to login in to the application below logged in the Log.txt file. Currently, only the last attempt is logged,
How do I have ALL attempts to login in to the application below logged in the Log.txt file. Currently, only the last attempt is logged, not all the attempts.
import java.io.BufferedWriter; import java.io.FileWriter; import java.io.IOException; import java.text.SimpleDateFormat; import java.util.Date; import javafx.application.Application; import javafx.event.ActionEvent; import javafx.event.EventHandler; import javafx.geometry.Pos; import javafx.scene.Scene; import javafx.scene.control.Button; import javafx.scene.control.Label; import javafx.scene.control.PasswordField; import javafx.scene.control.TextField; import javafx.scene.layout.GridPane; import javafx.scene.paint.Color; import javafx.scene.text.Text; import javafx.stage.Stage;
public class loginAuthLog extends Application {
//counter for login attempts int loginAttempts = 0; //maximum number of login attempts final int numAttempts = 3;
@Override public void start(Stage primaryStage) { primaryStage.setTitle("SDEV460 Login & Unit Testing"); GridPane grid = new GridPane(); grid.setAlignment(Pos.CENTER); grid.setHgap(10); grid.setVgap(10);
// Create some text to place in the scene Text scenetitle = new Text("Login Screen");
grid.add(scenetitle, 0, 0, 2, 1); try { // Create Label Label userName = new Label("User Name:"); // Add label to grid 0,1 grid.add(userName, 0, 1); // Create Textfield TextField userTextField = new TextField(); // Add textfield to grid 1,1 grid.add(userTextField, 1, 1); // Create Label Label pw = new Label("Password:"); // Add label to grid 0,2 grid.add(pw, 0, 2); // Create Passwordfield PasswordField pwBox = new PasswordField(); // Add Password field to grid 1,2 grid.add(pwBox, 1, 2); // Create Login Button Button btn = new Button("Login"); // Add button to grid 1,5 grid.add(btn, 1, 7);
final Text actiontarget = new Text(); grid.add(actiontarget, 1, 8);
// Set the Action when button is clicked btn.setOnAction(new EventHandler
@Override
public void handle(ActionEvent e) { // Authenticate the user boolean isValid = authenticate(userTextField.getText(), pwBox.getText()); String username = userTextField.getText(); accessLog(username, isValid); //if valid clear the grid and Welcome the user //if statement to instruct what action to take when the numAttempts //equal the maximum number of loginAttempts
if (loginAttempts == numAttempts) { grid.setVisible(false); GridPane grid3 = new GridPane(); grid3.setAlignment(Pos.CENTER); grid3.setHgap(10); grid3.setVgap(10); Text exitTitle = new Text("Account locked after 4 failed login attempts"); grid3.add(exitTitle, 0, 0, 2, 1); Scene scene2 = new Scene(grid3, 500, 400); primaryStage.setScene(scene2); primaryStage.show();
} else if (isValid) { grid.setVisible(false); GridPane grid2 = new GridPane(); grid2.setAlignment(Pos.CENTER); grid2.setHgap(10); grid2.setVgap(10); Text scenetitle = new Text("Login Successful"); grid2.add(scenetitle, 0, 0, 2, 1); Scene scene3 = new Scene(grid2, 500, 400); primaryStage.setScene(scene3); primaryStage.show(); // If Invalid Ask user to try again } else { //loginAttempts counter loginAttempts++; final Text actiontarget = new Text(); grid.add(actiontarget, 1, 9); actiontarget.setFill(Color.FIREBRICK); actiontarget.setText("Please try again."); } } });
} catch (Exception ex) { ex.printStackTrace(); } // Set the size of Scene Scene scene = new Scene(grid, 500, 400); primaryStage.setScene(scene); primaryStage.show(); }
/** * @param args the command line arguments */ public static void main(String[] args) { launch(args); }
/** * @param user the username entered * @param pword the password entered * @return isValid true for authenticated */ public boolean authenticate(String user, String pword) { boolean isValid = false; if (user.equalsIgnoreCase("Test") && pword.equals("password")) { isValid = true; } return isValid; }
public static void accessLog(String username, boolean isValid) { String timestamp = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss").format(new Date()); // declaring variables of log and initializing the buffered writer String log = "Username: " + username + " Attempted login timestamp: " + timestamp + " Login: " + isValid + " "; BufferedWriter writer = null;
//write the log variable using the bufferedWriter to log.txt try { writer = new BufferedWriter(new FileWriter("Log.txt")); writer.write(log); } //print error message if there is one catch (IOException io) { System.out.println("File IO Exception" + io.getMessage()); } //close the file finally { try { if (writer != null) { writer.close(); } } //print error message if there is one catch (IOException io) { System.out.println("Issue closing the file." + io.getMessage()); } } } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access with AI-Powered Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started