Advertisement
Guest User

Untitled

a guest
Apr 1st, 2018
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.64 KB | None | 0 0
  1. package labjava.p0072;
  2.  
  3. import java.io.IOException;
  4. import java.io.UnsupportedEncodingException;
  5. import java.security.MessageDigest;
  6. import java.security.NoSuchAlgorithmException;
  7. import java.text.DateFormat;
  8. import java.text.ParseException;
  9. import java.text.SimpleDateFormat;
  10. import java.util.*;
  11. public class Main {
  12. public static void main(String[] args) throws IOException {
  13. while(true){
  14. String userName="";
  15. Scanner sc = new Scanner(System.in);
  16. int choice;
  17. List<Account> account = new ArrayList<>();
  18. account.add(new Account("admin"));
  19. System.out.println("Choose Option Below");
  20. System.out.println("1.Add Acount");
  21. System.out.println("2.Login");
  22. choice = Integer.parseInt(sc.nextLine());
  23. switch (choice) {
  24. case 1:
  25. System.out.println("Add Account Infomation");
  26. System.out.println("Enter Your User Name");
  27. userName=sc.nextLine();
  28. if(checkValid(account,userName)==false){
  29. account.add(new Account(userName));
  30. }
  31. break;
  32. case 2:
  33. for(Account x:account){
  34. System.out.println("List Account: "+x.getUserName());
  35. }
  36. /*System.out.println("Enter Your Password");
  37. String Password=sc.nextLine();
  38. while(checkPassword(Password)==true){
  39. System.out.println("Re-Enter Your Password");
  40. Password=sc.nextLine();
  41. }
  42. System.out.println("Enter Your Phone Number");
  43. String Phone = sc.nextLine();
  44. while(checkPhone(Phone)==false){
  45. System.out.println("RE-Enter Your Phone Number");
  46. Phone = sc.nextLine();
  47. }
  48. System.out.println("Enter Your Email");
  49. String email = sc.nextLine();
  50. while(checkEmail(email)==false){
  51. System.out.println("RE-Enter Your Email");
  52. email=sc.nextLine();
  53. }
  54. System.out.println("Enter Your Date of Birth");
  55. String DoB= sc.nextLine();
  56. while(checkDate(DoB)==false){
  57. System.out.println("Re-Enter Your Date of Birth");
  58. DoB= sc.nextLine();
  59. }
  60. System.out.println("Enter Your Address");
  61. String Address=sc.nextLine();
  62. String MD5=generatorMD5psw(Password);
  63. */
  64. }
  65.  
  66. }
  67. }
  68. //check valid Password
  69. public static boolean checkPassword(String password){
  70. return password.isEmpty()||password==null;
  71. }
  72. //check valid userName
  73. public static boolean checkValid( List<Account> x,String userName) {
  74. for(Account a:x){
  75. if(userName.matches(a.getUserName())||userName.isEmpty()||userName==null)
  76. return true;
  77. }
  78. return false;
  79. }
  80. //generator MD5 password
  81. public static String generatorMD5psw(String message) {
  82. String digest = null;
  83. try {
  84. MessageDigest md5 = MessageDigest.getInstance("MD5");
  85. byte[] hash = md5.digest(message.getBytes("UTF-8"));
  86. StringBuilder sb = new StringBuilder(2 * hash.length);
  87. for (byte b : hash) {
  88. sb.append(String.format("%02x", b & 0xff));
  89. }
  90. digest = sb.toString();
  91. } catch (UnsupportedEncodingException | NoSuchAlgorithmException error) {}
  92. return digest;
  93. }
  94. public static boolean checkPhone(String phone) {
  95. return phone.length() == 10 || phone.length() == 11;
  96. }
  97. public static boolean checkEmail(String email) {
  98. String regex = "^[\\w!#$%&’*+/=?`{|}~^-]+(?:\\.[\\w!#$%&’*+/=?`{|}~^-]+)*@(?:[a-zA-Z0-9_]+\\.)+[\\w]{2,7}$";
  99. return email.matches(regex);
  100. }
  101. public static boolean checkDate(String inDate) {
  102. DateFormat date = new SimpleDateFormat("dd/MM/yyyy");
  103. date.setLenient(false);
  104. try {
  105. date.parse(inDate.trim());
  106. } catch (ParseException err) {
  107. System.out.println("re-Enter Your Date of Birth");
  108. return false;
  109. }
  110. return true;
  111. }
  112.  
  113.  
  114. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement