Skip to content

Commit 7d7ac9f

Browse files
committed
added low difficulty exception handling practice problem
1 parent 90d76dd commit 7d7ac9f

File tree

10 files changed

+115
-0
lines changed

10 files changed

+115
-0
lines changed
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
package com.codefortomorrow.advanced.chapter14.practice.account;
2+
3+
public class InvalidAgeException {
4+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
package com.codefortomorrow.advanced.chapter14.practice.account;
2+
3+
public class InvalidPasswordException {
4+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
package com.codefortomorrow.advanced.chapter14.practice.account;
2+
3+
public class InvalidUsernameException {
4+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package com.codefortomorrow.advanced.chapter14.practice.account;
2+
3+
import java.util.Scanner;
4+
5+
public class Main {
6+
public static void main(String[]args) {
7+
Scanner sc = new Scanner(System.in);
8+
9+
System.out.println("Welcome to Account Creation!");
10+
while (true) {
11+
System.out.print("Enter username (4 to 10 characters): ");
12+
String username = sc.next();
13+
System.out.print("Enter age: ");
14+
int age = sc.nextInt();
15+
System.out.print("Enter password (4 to 10 characters): ");
16+
String password = sc.next();
17+
System.out.print("Confirm password (4 to 10 characters): ");
18+
String confirmPassword = sc.next();
19+
20+
// TODO: try and catch to handle exceptions
21+
createAccount(username, password, age, confirmPassword);
22+
System.out.println("Account created successfully!");
23+
}
24+
25+
}
26+
27+
public static void createAccount(String username, String password, int age, String confirmPassword) throws
28+
InvalidAgeException, InvalidPasswordException, InvalidUsernameException, PasswordMismatchException {
29+
// TODO: complete
30+
}
31+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
package com.codefortomorrow.advanced.chapter14.practice.account;
2+
3+
public class PasswordMismatchException {
4+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
package com.codefortomorrow.advanced.chapter14.solutions.account;
2+
3+
public class InvalidAgeException extends Exception{
4+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
package com.codefortomorrow.advanced.chapter14.solutions.account;
2+
3+
public class InvalidPasswordException extends Exception {
4+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
package com.codefortomorrow.advanced.chapter14.solutions.account;
2+
3+
public class InvalidUsernameException extends Exception {
4+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package com.codefortomorrow.advanced.chapter14.solutions.account;
2+
3+
import java.util.Scanner;
4+
5+
public class Main {
6+
public static void main(String[]args) {
7+
Scanner sc = new Scanner(System.in);
8+
9+
System.out.println("Welcome to Account Creation!");
10+
while (true) {
11+
System.out.print("Enter username (4 to 10 characters): ");
12+
String username = sc.next();
13+
System.out.print("Enter age: ");
14+
int age = sc.nextInt();
15+
System.out.print("Enter password (4 to 10 characters): ");
16+
String password = sc.next();
17+
System.out.print("Confirm password (4 to 10 characters): ");
18+
String confirmPassword = sc.next();
19+
20+
try {
21+
createAccount(username, password, age, confirmPassword);
22+
System.out.println("Account created successfully!");
23+
break;
24+
} catch (InvalidUsernameException e) {
25+
System.out.println("Invalid username.");
26+
} catch (InvalidPasswordException e) {
27+
System.out.println("Invalid password.");
28+
} catch (InvalidAgeException e) {
29+
System.out.println("Must be 18 or older to create an account.");
30+
} catch (PasswordMismatchException e) {
31+
System.out.println("Passwords don't match!");
32+
}
33+
}
34+
35+
}
36+
37+
public static void createAccount(String username, String password, int age, String confirmPassword) throws
38+
InvalidAgeException, InvalidPasswordException, InvalidUsernameException, PasswordMismatchException {
39+
if (username.length() < 4 || username.length() > 10) {
40+
throw new InvalidUsernameException();
41+
}
42+
if (password.length() < 4 || password.length() > 10) {
43+
throw new InvalidPasswordException();
44+
}
45+
if (age < 18) {
46+
throw new InvalidAgeException();
47+
}
48+
if (!password.equals(confirmPassword)) {
49+
throw new PasswordMismatchException();
50+
}
51+
}
52+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
package com.codefortomorrow.advanced.chapter14.solutions.account;
2+
3+
public class PasswordMismatchException extends Exception {
4+
}

0 commit comments

Comments
 (0)