Advertisement
Guest User

vasko

a guest
Nov 18th, 2019
180
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.08 KB | None | 0 0
  1. package xx;
  2.  
  3. import java.io.BufferedWriter;
  4. import java.io.File;
  5. import java.io.FileWriter;
  6. import java.io.IOException;
  7. import java.util.Scanner;
  8.  
  9. public class FileHandler {
  10.  
  11. public static void main(String[] args) {
  12. // TODO Auto-generated method stub
  13.  
  14. String username;
  15. String password;
  16. Scanner scanner = new Scanner(System.in);
  17.  
  18. BufferedWriter buffWriter= null;
  19. FileWriter fileWriter = null;
  20. for (int i=0;i<3;i++) {
  21.  
  22. try {
  23. username = scanner.next();
  24. password = scanner.next();
  25.  
  26. User user = new User(username, password);
  27.  
  28. File file = new File("someNewFile.txt");
  29. fileWriter = new FileWriter(file.getAbsoluteFile());
  30. buffWriter= new BufferedWriter(fileWriter);
  31. buffWriter.write(user.getUsername());
  32. buffWriter.write(user.getPassword());
  33. buffWriter.flush();
  34. System.out.println("Ready!");
  35. } catch (IOException e) {
  36. e.printStackTrace();
  37. }finally{
  38. if(buffWriter!=null ){
  39. try {
  40. buffWriter.close();
  41. } catch (IOException e) {
  42. e.printStackTrace();
  43. }
  44. }if(fileWriter != null){
  45. try {
  46. fileWriter.close();
  47. } catch (IOException e) {
  48. e.printStackTrace();
  49. }
  50. }
  51. }
  52. }
  53.  
  54. }
  55. }
  56.  
  57.  
  58.  
  59. package xx;
  60.  
  61. public class MyException extends Exception{
  62.  
  63. @Override
  64. public String getMessage() {
  65. return "Bad";
  66. }
  67.  
  68. }
  69.  
  70.  
  71.  
  72.  
  73.  
  74.  
  75.  
  76. package xx;
  77.  
  78. import java.util.regex.Matcher;
  79. import java.util.regex.Pattern;
  80.  
  81. public class UserValidation {
  82.  
  83. private static Pattern pattern;
  84. private static Pattern passwordPattern;
  85.  
  86. private static Matcher matcher;
  87.  
  88. private static final String USERNAME_PATTERN = "^[a-z]{3,10}$";
  89. private static final String PASSWORD_PATTERN = "^[A-Z]{5,10}%";
  90.  
  91. public UserValidation(){
  92.  
  93. pattern = Pattern.compile(USERNAME_PATTERN);
  94. passwordPattern = Pattern.compile(PASSWORD_PATTERN);
  95.  
  96. }
  97.  
  98. public static boolean usernameValidator(String username) {
  99.  
  100. matcher = pattern.matcher(username);
  101. return matcher.matches();
  102.  
  103. }
  104.  
  105. public static boolean passwordValidator(String password) {
  106.  
  107. matcher = passwordPattern.matcher(password);
  108. return matcher.matches();
  109.  
  110. }
  111. }
  112.  
  113.  
  114.  
  115.  
  116.  
  117.  
  118.  
  119.  
  120.  
  121.  
  122.  
  123.  
  124. package xx;
  125.  
  126. public class User {
  127.  
  128. public User(String username, String password) {
  129. super();
  130. this.username = username;
  131. this.password = password;
  132. }
  133. String username;
  134. String password;
  135.  
  136. public String getUsername() {
  137. return username;
  138. }
  139. public void setUsername(String username) throws MyException {
  140. if (!UserValidation.usernameValidator(username))throw new MyException();
  141. this.username = username;
  142. }
  143. public String getPassword() {
  144. return password;
  145. }
  146. public void setPassword(String password)throws MyException {
  147. if(!UserValidation.passwordValidator(password))throw new MyException();
  148. this.password = password;
  149. }
  150.  
  151. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement