Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package labjava.p0072;
- import java.io.IOException;
- import java.io.UnsupportedEncodingException;
- import java.security.MessageDigest;
- import java.security.NoSuchAlgorithmException;
- import java.text.DateFormat;
- import java.text.ParseException;
- import java.text.SimpleDateFormat;
- import java.util.*;
- public class Main {
- public static void main(String[] args) throws IOException {
- while(true){
- String userName="";
- Scanner sc = new Scanner(System.in);
- int choice;
- List<Account> account = new ArrayList<>();
- account.add(new Account("admin"));
- System.out.println("Choose Option Below");
- System.out.println("1.Add Acount");
- System.out.println("2.Login");
- choice = Integer.parseInt(sc.nextLine());
- switch (choice) {
- case 1:
- System.out.println("Add Account Infomation");
- System.out.println("Enter Your User Name");
- userName=sc.nextLine();
- if(checkValid(account,userName)==false){
- account.add(new Account(userName));
- }
- break;
- case 2:
- for(Account x:account){
- System.out.println("List Account: "+x.getUserName());
- }
- /*System.out.println("Enter Your Password");
- String Password=sc.nextLine();
- while(checkPassword(Password)==true){
- System.out.println("Re-Enter Your Password");
- Password=sc.nextLine();
- }
- System.out.println("Enter Your Phone Number");
- String Phone = sc.nextLine();
- while(checkPhone(Phone)==false){
- System.out.println("RE-Enter Your Phone Number");
- Phone = sc.nextLine();
- }
- System.out.println("Enter Your Email");
- String email = sc.nextLine();
- while(checkEmail(email)==false){
- System.out.println("RE-Enter Your Email");
- email=sc.nextLine();
- }
- System.out.println("Enter Your Date of Birth");
- String DoB= sc.nextLine();
- while(checkDate(DoB)==false){
- System.out.println("Re-Enter Your Date of Birth");
- DoB= sc.nextLine();
- }
- System.out.println("Enter Your Address");
- String Address=sc.nextLine();
- String MD5=generatorMD5psw(Password);
- */
- }
- }
- }
- //check valid Password
- public static boolean checkPassword(String password){
- return password.isEmpty()||password==null;
- }
- //check valid userName
- public static boolean checkValid( List<Account> x,String userName) {
- for(Account a:x){
- if(userName.matches(a.getUserName())||userName.isEmpty()||userName==null)
- return true;
- }
- return false;
- }
- //generator MD5 password
- public static String generatorMD5psw(String message) {
- String digest = null;
- try {
- MessageDigest md5 = MessageDigest.getInstance("MD5");
- byte[] hash = md5.digest(message.getBytes("UTF-8"));
- StringBuilder sb = new StringBuilder(2 * hash.length);
- for (byte b : hash) {
- sb.append(String.format("%02x", b & 0xff));
- }
- digest = sb.toString();
- } catch (UnsupportedEncodingException | NoSuchAlgorithmException error) {}
- return digest;
- }
- public static boolean checkPhone(String phone) {
- return phone.length() == 10 || phone.length() == 11;
- }
- public static boolean checkEmail(String email) {
- String regex = "^[\\w!#$%&’*+/=?`{|}~^-]+(?:\\.[\\w!#$%&’*+/=?`{|}~^-]+)*@(?:[a-zA-Z0-9_]+\\.)+[\\w]{2,7}$";
- return email.matches(regex);
- }
- public static boolean checkDate(String inDate) {
- DateFormat date = new SimpleDateFormat("dd/MM/yyyy");
- date.setLenient(false);
- try {
- date.parse(inDate.trim());
- } catch (ParseException err) {
- System.out.println("re-Enter Your Date of Birth");
- return false;
- }
- return true;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement