Skip to content

Commit 87dc112

Browse files
committed
Fix Account exception handling problem
- Consolidate into 1 class called Account.java - Clarify instructions and post instructions in the file - Close Scanner object when done
1 parent bc785e4 commit 87dc112

12 files changed

+168
-117
lines changed
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package com.codefortomorrow.advanced.chapter14.practice;
2+
3+
/*
4+
Create the following classes that simply extend the `Exception` class:
5+
6+
- `InvalidUsernameException`
7+
- `InvalidPasswordException`
8+
- `InvalidAgeException`
9+
- `PasswordMismatchException`
10+
11+
In the `Account` class, code a method called `createAccount` that takes a `username`, `age`,
12+
`password`, and `confirmPassword` as parameters.
13+
14+
In `createAccount`, throw an:
15+
16+
- `InvalidUsernameException` - if the given username is <4 or >10 characters.
17+
- `InvalidPasswordException` - if the given password is <4 or >10 characters.
18+
- `InvalidAgeException` - if the given age is <18.
19+
- `PasswordMismatchException` - if the given password does not match the given confirm password.
20+
21+
In your `main` method (within the `Account` class):
22+
23+
- To simulate the creation of an account online, prompt the user to enter their username, age,
24+
password, and their password again (to confirm).
25+
- Call the `createAccount` method continuously with the user's input and use `try`/`catch` to
26+
handle the possible exceptions and print the appropriate error message. (You can assume that
27+
the user input will only throw at most 1 exception.)
28+
- Keep asking the user for input until the account is created successfully.
29+
30+
Example output:
31+
32+
```
33+
Welcome to Account Creation!
34+
Enter username (4 to 10 characters): hi
35+
Enter age: 18
36+
Enter password (4 to 10 characters): thisislong
37+
Confirm password (4 to 10 characters): thisislong
38+
Invalid username.
39+
Enter username (4 to 10 characters): four
40+
Enter age: 18
41+
Enter password (4 to 10 characters): thisislong
42+
Confirm password (4 to 10 characters): thisislong
43+
Account created successfully!
44+
```
45+
*/
46+
47+
public class Account {
48+
49+
public static void main(String[] args) {
50+
// Write your code here
51+
}
52+
53+
public static void createAccount(String username, int age, String password, String confirmPassword) {
54+
// Write your code here
55+
}
56+
}
57+
58+
// Write exception classes here

src/com/codefortomorrow/advanced/chapter14/practice/account/InvalidAgeException.java

Lines changed: 0 additions & 3 deletions
This file was deleted.

src/com/codefortomorrow/advanced/chapter14/practice/account/InvalidPasswordException.java

Lines changed: 0 additions & 3 deletions
This file was deleted.

src/com/codefortomorrow/advanced/chapter14/practice/account/InvalidUsernameException.java

Lines changed: 0 additions & 3 deletions
This file was deleted.

src/com/codefortomorrow/advanced/chapter14/practice/account/Main.java

Lines changed: 0 additions & 36 deletions
This file was deleted.

src/com/codefortomorrow/advanced/chapter14/practice/account/PasswordMismatchException.java

Lines changed: 0 additions & 3 deletions
This file was deleted.
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
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(
83+
String username,
84+
int age,
85+
String password,
86+
String confirmPassword
87+
)
88+
throws InvalidAgeException, InvalidPasswordException, InvalidUsernameException, PasswordMismatchException {
89+
if (username.length() < 4 || username.length() > 10) {
90+
throw new InvalidUsernameException();
91+
}
92+
93+
if (age < 18) {
94+
throw new InvalidAgeException();
95+
}
96+
97+
if (password.length() < 4 || password.length() > 10) {
98+
throw new InvalidPasswordException();
99+
}
100+
101+
if (!password.equals(confirmPassword)) {
102+
throw new PasswordMismatchException();
103+
}
104+
}
105+
}
106+
107+
class InvalidUsernameException extends Exception {}
108+
class InvalidPasswordException extends Exception {}
109+
class InvalidAgeException extends Exception {}
110+
class PasswordMismatchException extends Exception {}

src/com/codefortomorrow/advanced/chapter14/solutions/account/InvalidAgeException.java

Lines changed: 0 additions & 3 deletions
This file was deleted.

src/com/codefortomorrow/advanced/chapter14/solutions/account/InvalidPasswordException.java

Lines changed: 0 additions & 3 deletions
This file was deleted.

src/com/codefortomorrow/advanced/chapter14/solutions/account/InvalidUsernameException.java

Lines changed: 0 additions & 3 deletions
This file was deleted.

0 commit comments

Comments
 (0)