Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- mport javax.swing.*;
- import java.io.*;
- import java.util.Scanner;
- public class Account
- {
- public static int attempts;
- public static String adminNum;
- /** createAccount() method allows the user to create a new account
- * it stores the admin number, username and password in a file
- *
- *
- */
- public static void createAccount() throws IOException
- {
- FileWriter aFileWriter = new FileWriter("Admins.txt",true);
- PrintWriter out = new PrintWriter(aFileWriter);
- String adminUserName,adminPassword;
- adminNum = JOptionPane.showInputDialog(null,"Enter administrator number");
- adminUserName = JOptionPane.showInputDialog(null,"Enter username");
- String msg1 = "Welcome " + adminUserName + " your administrator account has been created";
- adminPassword = JOptionPane.showInputDialog(null,"Enter password");
- String errorM = "Invalid entry";
- if((adminUserName != null) || (adminPassword != null))
- {
- JOptionPane.showMessageDialog(null, msg1);
- out.print(adminNum + "," + adminUserName + "," + adminPassword + "\n");
- out.close();
- aFileWriter.close();
- }
- else
- JOptionPane.showMessageDialog(null,errorM);
- }
- /** adminLogin() method checks the username and password the user inputs and compares it to
- * those stored in the file. If it is found the user can access their account , else an
- * error message is displayed
- *
- *
- */
- public static void adminLogin() throws IOException
- {
- File aFile = new File("Admins.txt");
- String adminUserName,adminPassword;
- Scanner in;
- String fileElements[];
- boolean found;
- if (aFile.exists())
- {
- adminUserName = JOptionPane.showInputDialog(null,"Enter your username");
- adminPassword = JOptionPane.showInputDialog(null,"Enter your password");
- in = new Scanner(aFile);
- found = false;
- while(in.hasNext() && !found)
- {
- fileElements = (in.nextLine()).split(",");
- if (adminUserName.equals(fileElements[1]) && adminPassword.equals(fileElements[2]))
- found = true;
- else
- attempts++;
- }
- in.close();
- if(found)
- JOptionPane.showMessageDialog(null,"Welcome " + adminUserName);
- else
- if (attempts >= 3)
- JOptionPane.showMessageDialog(null, "You have exceeded the limit of login attempts");
- else
- {
- JOptionPane.showMessageDialog(null, "Incorrect username and/or incorrect password");
- adminLogin();
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement