|
| 1 | +package com.codefortomorrow.advanced.chapter14.solutions; |
| 2 | + |
| 3 | +import java.util.Scanner; |
| 4 | + |
| 5 | +/* |
| 6 | +Create the following classes that simply extend the `Exception` class: |
| 7 | +
|
| 8 | +- `InvalidUsernameException` |
| 9 | +- `InvalidPasswordException` |
| 10 | +- `InvalidAgeException` |
| 11 | +- `PasswordMismatchException` |
| 12 | +
|
| 13 | +In the `Account` class, code a method called `createAccount` that takes a `username`, `age`, |
| 14 | +`password`, and `confirmPassword` as parameters. |
| 15 | +
|
| 16 | +In `createAccount`, throw an: |
| 17 | +
|
| 18 | +- `InvalidUsernameException` - if the given username is <4 or >10 characters. |
| 19 | +- `InvalidPasswordException` - if the given password is <4 or >10 characters. |
| 20 | +- `InvalidAgeException` - if the given age is <18. |
| 21 | +- `PasswordMismatchException` - if the given password does not match the given confirm password. |
| 22 | +
|
| 23 | +In your `main` method (within the `Account` class): |
| 24 | +
|
| 25 | +- To simulate the creation of an account online, prompt the user to enter their username, age, |
| 26 | + password, and their password again (to confirm). |
| 27 | +- Call the `createAccount` method continuously with the user's input and use `try`/`catch` to |
| 28 | + handle the possible exceptions and print the appropriate error message. (You can assume that |
| 29 | + the user input will only throw at most 1 exception.) |
| 30 | +- Keep asking the user for input until the account is created successfully. |
| 31 | +
|
| 32 | +Example output: |
| 33 | +
|
| 34 | +``` |
| 35 | +Welcome to Account Creation! |
| 36 | +Enter username (4 to 10 characters): hi |
| 37 | +Enter age: 18 |
| 38 | +Enter password (4 to 10 characters): thisislong |
| 39 | +Confirm password (4 to 10 characters): thisislong |
| 40 | +Invalid username. |
| 41 | +Enter username (4 to 10 characters): four |
| 42 | +Enter age: 18 |
| 43 | +Enter password (4 to 10 characters): thisislong |
| 44 | +Confirm password (4 to 10 characters): thisislong |
| 45 | +Account created successfully! |
| 46 | +``` |
| 47 | +*/ |
| 48 | + |
| 49 | +public class Account { |
| 50 | + |
| 51 | + public static void main(String[] args) { |
| 52 | + Scanner sc = new Scanner(System.in); |
| 53 | + |
| 54 | + System.out.println("Welcome to Account Creation!"); |
| 55 | + while (true) { |
| 56 | + System.out.print("Enter username (4 to 10 characters): "); |
| 57 | + String username = sc.next(); |
| 58 | + System.out.print("Enter age: "); |
| 59 | + int age = sc.nextInt(); |
| 60 | + System.out.print("Enter password (4 to 10 characters): "); |
| 61 | + String password = sc.next(); |
| 62 | + System.out.print("Confirm password (4 to 10 characters): "); |
| 63 | + String confirmPassword = sc.next(); |
| 64 | + |
| 65 | + try { |
| 66 | + createAccount(username, age, password, confirmPassword); |
| 67 | + System.out.println("Account created successfully!"); |
| 68 | + sc.close(); |
| 69 | + break; |
| 70 | + } catch (InvalidUsernameException e) { |
| 71 | + System.out.println("Invalid username."); |
| 72 | + } catch (InvalidPasswordException e) { |
| 73 | + System.out.println("Invalid password."); |
| 74 | + } catch (InvalidAgeException e) { |
| 75 | + System.out.println("Must be 18 or older to create an account."); |
| 76 | + } catch (PasswordMismatchException e) { |
| 77 | + System.out.println("Passwords don't match!"); |
| 78 | + } |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + public static void createAccount(String username, int age, String password, String confirmPassword) |
| 83 | + throws InvalidAgeException, InvalidPasswordException, InvalidUsernameException, PasswordMismatchException { |
| 84 | + if (username.length() < 4 || username.length() > 10) { |
| 85 | + throw new InvalidUsernameException(); |
| 86 | + } |
| 87 | + |
| 88 | + if (age < 18) { |
| 89 | + throw new InvalidAgeException(); |
| 90 | + } |
| 91 | + |
| 92 | + if (password.length() < 4 || password.length() > 10) { |
| 93 | + throw new InvalidPasswordException(); |
| 94 | + } |
| 95 | + |
| 96 | + if (!password.equals(confirmPassword)) { |
| 97 | + throw new PasswordMismatchException(); |
| 98 | + } |
| 99 | + } |
| 100 | +} |
| 101 | + |
| 102 | +class InvalidUsernameException extends Exception {} |
| 103 | + |
| 104 | +class InvalidPasswordException extends Exception {} |
| 105 | + |
| 106 | +class InvalidAgeException extends Exception {} |
| 107 | + |
| 108 | +class PasswordMismatchException extends Exception {} |
0 commit comments