Skip to content

Commit 1bc9537

Browse files
authored
Merge pull request #25 from code4tomorrow/fix-account
Fix Ch. 14 Account practice problem, fix package name in Ch. 13 Negative Sum practice problem
2 parents 1915441 + 49d5566 commit 1bc9537

13 files changed

+171
-118
lines changed

src/com/codefortomorrow/advanced/chapter13/solutions/NegativeSum.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.codefortomorrow.advanced.chapter13.practice;
1+
package com.codefortomorrow.advanced.chapter13.solutions;
22

33
/*
44
Implement the iterative code as a recursive
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
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(
54+
String username,
55+
int age,
56+
String password,
57+
String confirmPassword
58+
) {
59+
// Write your code here
60+
}
61+
}
62+
// 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: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
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 {}

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.

0 commit comments

Comments
 (0)