Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package account;
- import account.exceptions.NameTooShortException;
- import account.exceptions.NameTooLongException;
- import account.exceptions.NameIncompleteException;
- import account.exceptions.PasswordTooLongException;
- import account.exceptions.PasswordTooShortException;
- import account.exceptions.PasswordWithTwoParticlesException;
- import account.exceptions.UsernameAlredyExistsException;
- import account.exceptions.UsernameNotFoundException;
- import account.exceptions.UsernameTooLongException;
- import account.exceptions.UsernameTooShortException;
- import account.exceptions.UsernameWithTwoParticlesException;
- import java.io.BufferedWriter;
- import java.io.File;
- import java.io.FileWriter;
- import java.io.IOException;
- /**
- *
- * @author Ramon
- */
- public class Account {
- private String name;
- private String email;
- private String username;
- private String password;
- public Account(String name, String email, String username, String password)
- throws NameTooLongException, NameTooShortException, NameIncompleteException,
- UsernameWithTwoParticlesException, UsernameTooLongException, UsernameTooShortException, UsernameAlredyExistsException,
- PasswordWithTwoParticlesException, PasswordTooLongException, PasswordTooShortException {
- setName(name);
- this.email = email;
- setUsername(username);
- setPassword(password);
- }
- public void setName(String name) throws NameTooLongException, NameTooShortException, NameIncompleteException {
- int size = name.length();
- if (size < 10 || size > 40) {
- if (size < 10) {
- throw new NameTooShortException();
- }
- throw new NameTooLongException();
- }
- String[] splitted = name.split(" ");
- if (splitted.length < 2) {
- throw new NameIncompleteException();
- }
- this.name = name;
- }
- public void setEmail(String email) {
- //...
- }
- public void setUsername(String username) throws UsernameTooLongException, UsernameTooShortException, UsernameWithTwoParticlesException, UsernameAlredyExistsException {
- int size = username.length();
- if (size < 5 || size > 15) {
- if (size < 5) {
- throw new UsernameTooShortException();
- }
- throw new UsernameTooLongException();
- }
- String[] splitted = username.split(" ");
- if (splitted.length > 1) {
- throw new UsernameWithTwoParticlesException();
- }
- File file = new File("accounts\\" + username + ".txt");
- if (file.exists()) {
- throw new UsernameAlredyExistsException();
- }
- this.username = username;
- }
- public void setPassword(String password) throws PasswordWithTwoParticlesException, PasswordTooLongException, PasswordTooShortException {
- int size = password.length();
- if (size < 5 || size > 15) {
- if (size < 5) {
- throw new PasswordTooShortException();
- }
- throw new PasswordTooLongException();
- }
- String[] splitted = password.split(" ");
- if (splitted.length > 1) {
- throw new PasswordWithTwoParticlesException();
- }
- this.password = password;
- }
- public void saveAccount() throws IOException {
- File file = new File("accounts\\" + getUsername() + ".txt");
- file.createNewFile();
- BufferedWriter out = new BufferedWriter(new FileWriter(file));
- out.write(name + "\t" + email + "\t" + username + "\t" + password);
- out.close();
- }
- public void removeAccount() throws UsernameNotFoundException {
- File file = new File("accounts\\" + getUsername() + ".txt");
- if (!file.exists()) {
- throw new UsernameNotFoundException();
- }
- file.delete();
- }
- public String getName() {
- return name;
- }
- public String getEmail() {
- return email;
- }
- public String getUsername() {
- return username;
- }
- public String getPassword() {
- return password;
- }
- }
Add Comment
Please, Sign In to add comment