Guest User

Untitled

a guest
Feb 20th, 2018
236
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.70 KB | None | 0 0
  1. import lombok.AllArgsConstructor;
  2. import lombok.Getter;
  3.  
  4. import java.text.ParseException;
  5. import java.text.SimpleDateFormat;
  6. import java.util.*;
  7.  
  8. public class MemberCertification {
  9. public static final String EXPECTED_EMAIL = "oshima@gmail.com";
  10. public static final String EXPECTED_BIRTHDAY = "1990/01/01";
  11. public static final String EXPECTED_NAME = "大嶋 一哉";
  12. public static final String EXPECTED_TEL = "090-1111-2222";
  13.  
  14. @AllArgsConstructor
  15. @Getter
  16. public enum CertifiedItems {
  17. MAIL("mail"),
  18. BIRTH_DATE("birthDate"),
  19. NAME("name"),
  20. TEL("tel");
  21.  
  22. private final String value;
  23. }
  24.  
  25. public static final String AUTHENTICATED = "正しく認証されました。";
  26. public static final String UNAUTHORIZED = "認証情報が正しくありません。";
  27.  
  28. private static Map<String, String> messages = new LinkedHashMap<String, String>() {
  29. {
  30. put(CertifiedItems.MAIL.getValue(),"メールアドレスを入力してください。");
  31. put(CertifiedItems.BIRTH_DATE.getValue(),"生年月日を入力してください。");
  32. put(CertifiedItems.NAME.getValue(),"名前を入力してください。");
  33. put(CertifiedItems.TEL.getValue(),"電話番号を入力してください。");
  34. }
  35. };
  36.  
  37. public static void main(String args[]) {
  38. System.out.println("再認証を行います。");
  39. Map<String, String> inputs = new LinkedHashMap<>();
  40. try {
  41. messages.forEach((k, v) -> {
  42. System.out.println(v);
  43. Scanner sc = new Scanner(System.in);
  44. inputs.put(k, sc.nextLine());
  45. });
  46. String msgResult = executeAuthentication(inputs) ? AUTHENTICATED : UNAUTHORIZED;
  47. System.out.println(msgResult);
  48. } catch (ParseException e) {
  49. System.out.println(UNAUTHORIZED);
  50. e.printStackTrace();
  51. }
  52. }
  53.  
  54. static boolean executeAuthentication(Map<String, String> input) throws ParseException {
  55. Boolean[] results = {
  56. checkEmail(input.get(CertifiedItems.MAIL.getValue())),
  57. checkBirthday(input.get(CertifiedItems.BIRTH_DATE.getValue())),
  58. checkName(input.get(CertifiedItems.NAME.getValue())),
  59. checkTel(input.get(CertifiedItems.TEL.getValue()))
  60. };
  61. return Arrays.stream(results).allMatch(Boolean::valueOf);
  62. }
  63.  
  64. static boolean checkEmail(String email) {
  65. return EXPECTED_EMAIL.equals(email);
  66. }
  67.  
  68. static boolean checkBirthday(String birthDate) throws ParseException {
  69. SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");
  70. return (sdf.parse(EXPECTED_BIRTHDAY).compareTo(sdf.parse(birthDate)) == 0);
  71. }
  72.  
  73. static boolean checkName(String name) {
  74. return EXPECTED_NAME.equals(name);
  75. }
  76.  
  77. static boolean checkTel(String tel) {
  78. return EXPECTED_TEL.replaceAll("-","").equals(tel.replaceAll("-",""));
  79. }
  80. }
Add Comment
Please, Sign In to add comment