Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public void actionPerformed(ActionEvent e) {
- boolean userCheck = false;
- PrintWriter userWrite;
- int createUser;
- String username = addressLogOnUsernameText.getText();
- char[] pass = addressLogOnPasswordText.getPassword();
- String password = String.valueOf(pass);
- boolean user = false;
- try {
- File usernamesFile = new File("Resources//usernames.txt");
- ArrayList<String> usernames = new ArrayList<String>();
- File passwordsFile = new File("Resources//passwords.txt");
- ArrayList<String> passwords = new ArrayList<String>();
- fileScanner = new Scanner(usernamesFile);
- fileScanner.useDelimiter(",");
- do {
- usernames.add(fileScanner.next());
- } while (fileScanner.hasNext());
- fileScanner = new Scanner(passwordsFile);
- fileScanner.useDelimiter(",");
- do {
- passwords.add(fileScanner.next());
- } while (fileScanner.hasNext());
- for (int i = 0; i < usernames.size(); i++) {
- if ((usernames.get(i).equals(username)) && (passwords.get(i).equals(password))) {
- addressLogOnWindow.setVisible(false);
- new GUI(username);
- user = true;
- }
- }
- if (user == false) {
- createUser = JOptionPane.showConfirmDialog(null,
- "A user with these credentials could not be found.\n" + "Would you like to create one?",
- "Incorrect Username or Password", JOptionPane.YES_NO_OPTION, JOptionPane.ERROR_MESSAGE);
- if (createUser == JOptionPane.YES_OPTION) {
- for (int j = 0; j < usernames.size(); j++) {
- if (username.equals(usernames.get(j))) {
- userCheck = true;
- }
- }
- if (userCheck == true) {
- JOptionPane.showMessageDialog(null, "A user with these credentials already exists.",
- "User Could Not Be Created", JOptionPane.ERROR_MESSAGE);
- } else {
- try {
- userWrite = new PrintWriter(
- new BufferedWriter(new FileWriter("Resources//usernames.txt", true)));
- userWrite.print(username + ",");
- userWrite.close();
- userWrite = new PrintWriter(
- new BufferedWriter(new FileWriter("Resources//passwords.txt", true)));
- userWrite.print(password + ",");
- userWrite.close();
- JOptionPane.showMessageDialog(null,
- "Please log on with your new credentials for: " + username, "User Created",
- JOptionPane.INFORMATION_MESSAGE);
- new LogOnGUI();
- addressLogOnWindow.dispose();
- } catch (IOException ioe) {
- JOptionPane.showMessageDialog(null, "Could not create user", "User Creation Error",
- JOptionPane.ERROR_MESSAGE);
- }
- }
- } else {
- addressLogOnUsernameText.setText("");
- addressLogOnPasswordText.setText("");
- }
- }
- }
- catch (FileNotFoundException ex) {
- JOptionPane.showMessageDialog(addressLogOnWindow,
- "Unable to connect to file.\n" + "Please replace the file and try again.",
- "File Connect Error", JOptionPane.ERROR_MESSAGE);
- fileScanner.close();
- } catch (NoSuchElementException nsee) {
- }
- }
- });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement