Guest User

Untitled

a guest
Jan 25th, 2018
319
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.20 KB | None | 0 0
  1. package account;
  2.  
  3. import account.exceptions.NameTooShortException;
  4. import account.exceptions.NameTooLongException;
  5. import account.exceptions.NameIncompleteException;
  6. import account.exceptions.PasswordTooLongException;
  7. import account.exceptions.PasswordTooShortException;
  8. import account.exceptions.PasswordWithTwoParticlesException;
  9. import account.exceptions.UsernameAlredyExistsException;
  10. import account.exceptions.UsernameNotFoundException;
  11. import account.exceptions.UsernameTooLongException;
  12. import account.exceptions.UsernameTooShortException;
  13. import account.exceptions.UsernameWithTwoParticlesException;
  14. import java.io.BufferedWriter;
  15. import java.io.File;
  16. import java.io.FileWriter;
  17. import java.io.IOException;
  18.  
  19. /**
  20.  *
  21.  * @author Ramon
  22.  */
  23. public class Account {
  24.     private String name;
  25.     private String email;
  26.     private String username;
  27.     private String password;
  28.    
  29.     public Account(String name, String email, String username, String password)
  30.             throws NameTooLongException, NameTooShortException, NameIncompleteException,
  31.             UsernameWithTwoParticlesException, UsernameTooLongException, UsernameTooShortException, UsernameAlredyExistsException,
  32.             PasswordWithTwoParticlesException, PasswordTooLongException, PasswordTooShortException {
  33.        
  34.         setName(name);
  35.         this.email = email;
  36.         setUsername(username);
  37.         setPassword(password);
  38.     }
  39.    
  40.     public void setName(String name) throws NameTooLongException, NameTooShortException, NameIncompleteException {
  41.         int size = name.length();
  42.         if (size < 10 || size > 40) {
  43.             if (size < 10) {
  44.                 throw new NameTooShortException();
  45.             }
  46.             throw new NameTooLongException();
  47.         }
  48.         String[] splitted = name.split(" ");
  49.         if (splitted.length < 2) {
  50.             throw new NameIncompleteException();
  51.         }
  52.         this.name = name;
  53.     }
  54.    
  55.     public void setEmail(String email) {
  56.         //...
  57.     }
  58.    
  59.     public void setUsername(String username) throws UsernameTooLongException, UsernameTooShortException, UsernameWithTwoParticlesException, UsernameAlredyExistsException {
  60.         int size = username.length();
  61.         if (size < 5 || size > 15) {
  62.             if (size < 5) {
  63.                 throw new UsernameTooShortException();
  64.             }
  65.             throw new UsernameTooLongException();
  66.         }
  67.         String[] splitted = username.split(" ");
  68.         if (splitted.length > 1) {
  69.             throw new UsernameWithTwoParticlesException();
  70.         }
  71.         File file = new File("accounts\\" + username + ".txt");
  72.         if (file.exists()) {
  73.             throw new UsernameAlredyExistsException();
  74.         }
  75.         this.username = username;
  76.     }
  77.    
  78.     public void setPassword(String password) throws PasswordWithTwoParticlesException, PasswordTooLongException, PasswordTooShortException {
  79.         int size = password.length();
  80.         if (size < 5 || size > 15) {
  81.             if (size < 5) {
  82.                 throw new PasswordTooShortException();
  83.             }
  84.             throw new PasswordTooLongException();
  85.         }
  86.         String[] splitted = password.split(" ");
  87.         if (splitted.length > 1) {
  88.             throw new PasswordWithTwoParticlesException();
  89.         }
  90.         this.password = password;
  91.     }
  92.    
  93.     public void saveAccount() throws IOException {
  94.         File file = new File("accounts\\" + getUsername() + ".txt");
  95.         file.createNewFile();
  96.         BufferedWriter out = new BufferedWriter(new FileWriter(file));
  97.         out.write(name + "\t" + email + "\t" + username + "\t" + password);
  98.         out.close();
  99.     }
  100.    
  101.     public void removeAccount() throws UsernameNotFoundException {
  102.         File file = new File("accounts\\" + getUsername() + ".txt");
  103.         if (!file.exists()) {
  104.             throw new UsernameNotFoundException();
  105.         }
  106.         file.delete();
  107.     }
  108.  
  109.     public String getName() {
  110.         return name;
  111.     }
  112.  
  113.     public String getEmail() {
  114.         return email;
  115.     }
  116.  
  117.     public String getUsername() {
  118.         return username;
  119.     }
  120.  
  121.     public String getPassword() {
  122.         return password;
  123.     }
  124.    
  125.    
  126. }
Add Comment
Please, Sign In to add comment