Advertisement
Guest User

Untitled

a guest
Apr 24th, 2018
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.86 KB | None | 0 0
  1. mport javax.swing.*;
  2. import java.io.*;
  3. import java.util.Scanner;
  4.  
  5. public class Account
  6. {
  7.  
  8. public static int attempts;
  9.  
  10. public static String adminNum;
  11.  
  12. /** createAccount() method allows the user to create a new account
  13. * it stores the admin number, username and password in a file
  14. *
  15. *
  16. */
  17. public static void createAccount() throws IOException
  18. {
  19. FileWriter aFileWriter = new FileWriter("Admins.txt",true);
  20. PrintWriter out = new PrintWriter(aFileWriter);
  21. String adminUserName,adminPassword;
  22. adminNum = JOptionPane.showInputDialog(null,"Enter administrator number");
  23. adminUserName = JOptionPane.showInputDialog(null,"Enter username");
  24. String msg1 = "Welcome " + adminUserName + " your administrator account has been created";
  25. adminPassword = JOptionPane.showInputDialog(null,"Enter password");
  26. String errorM = "Invalid entry";
  27. if((adminUserName != null) || (adminPassword != null))
  28. {
  29. JOptionPane.showMessageDialog(null, msg1);
  30. out.print(adminNum + "," + adminUserName + "," + adminPassword + "\n");
  31. out.close();
  32. aFileWriter.close();
  33. }
  34. else
  35. JOptionPane.showMessageDialog(null,errorM);
  36. }
  37.  
  38. /** adminLogin() method checks the username and password the user inputs and compares it to
  39. * those stored in the file. If it is found the user can access their account , else an
  40. * error message is displayed
  41. *
  42. *
  43. */
  44. public static void adminLogin() throws IOException
  45. {
  46. File aFile = new File("Admins.txt");
  47. String adminUserName,adminPassword;
  48. Scanner in;
  49. String fileElements[];
  50. boolean found;
  51.  
  52. if (aFile.exists())
  53. {
  54. adminUserName = JOptionPane.showInputDialog(null,"Enter your username");
  55. adminPassword = JOptionPane.showInputDialog(null,"Enter your password");
  56. in = new Scanner(aFile);
  57. found = false;
  58.  
  59. while(in.hasNext() && !found)
  60. {
  61. fileElements = (in.nextLine()).split(",");
  62. if (adminUserName.equals(fileElements[1]) && adminPassword.equals(fileElements[2]))
  63. found = true;
  64. else
  65. attempts++;
  66. }
  67. in.close();
  68. if(found)
  69. JOptionPane.showMessageDialog(null,"Welcome " + adminUserName);
  70. else
  71. if (attempts >= 3)
  72. JOptionPane.showMessageDialog(null, "You have exceeded the limit of login attempts");
  73. else
  74. {
  75. JOptionPane.showMessageDialog(null, "Incorrect username and/or incorrect password");
  76. adminLogin();
  77. }
  78. }
  79.  
  80. }
  81. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement