3
3
import java .util .Scanner ;
4
4
5
5
/*
6
- Create the following classes that simply extend the `Exception` class:
6
+ Create the following classes that simply extend the `Exception` class:
7
7
8
8
- `InvalidUsernameException`
9
9
- `InvalidPasswordException`
10
10
- `InvalidAgeException`
11
11
- `PasswordMismatchException`
12
12
13
13
In the `Account` class, code a method called `createAccount` that takes a `username`, `age`,
14
- `password`, and `confirmPassword` as parameters.
14
+ `password`, and `confirmPassword` as parameters.
15
15
16
16
In `createAccount`, throw an:
17
17
@@ -53,39 +53,34 @@ public static void main(String[] args) {
53
53
54
54
System .out .println ("Welcome to Account Creation!" );
55
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
- }
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
79
}
80
80
}
81
81
82
- public static void createAccount (
83
- String username ,
84
- int age ,
85
- String password ,
86
- String confirmPassword
87
- )
88
- throws InvalidAgeException , InvalidPasswordException , InvalidUsernameException , PasswordMismatchException {
82
+ public static void createAccount (String username , int age , String password , String confirmPassword )
83
+ throws InvalidAgeException , InvalidPasswordException , InvalidUsernameException , PasswordMismatchException {
89
84
if (username .length () < 4 || username .length () > 10 ) {
90
85
throw new InvalidUsernameException ();
91
86
}
@@ -101,7 +96,7 @@ public static void createAccount(
101
96
if (!password .equals (confirmPassword )) {
102
97
throw new PasswordMismatchException ();
103
98
}
104
- }
99
+ }
105
100
}
106
101
107
102
class InvalidUsernameException extends Exception {}
0 commit comments