Skip to content

Commit 5c30377

Browse files
authored
Merge branch 'master' into fix-pr-22
2 parents 38cfe25 + 8f6745c commit 5c30377

21 files changed

+250
-158
lines changed
File renamed without changes.

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

+1-1
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
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/Average.java

-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
package com.codefortomorrow.advanced.chapter14.practice;
22

3-
import java.io.*;
4-
53
/*
64
The file “numbers.txt” has 100 random numbers
75
(one on each line). Use file i/o to find the

src/com/codefortomorrow/advanced/chapter14/practice/Lexico.java

-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
package com.codefortomorrow.advanced.chapter14.practice;
22

3-
import java.io.*;
4-
53
/*
64
Practice: Use File I/O to compare two files lexicographically.
75
Lexicographical order is very similar to alphabetical order,

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

-3
This file was deleted.

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

-3
This file was deleted.

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

-3
This file was deleted.

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

-36
This file was deleted.

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

-3
This file was deleted.
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/Bank.java

+15-14
Original file line numberDiff line numberDiff line change
@@ -34,43 +34,44 @@ public static void main(String[] args) {
3434

3535
account1.deposit(200);
3636
withdraw(account1, 300);
37-
System.out.println("Account 1: " + account1.getBalance());
37+
System.out.printf("Account 1 Balance: $%.2f\n", account1.getBalance());
3838

3939
account2.deposit(200);
4040
withdraw(account2, 1500);
41-
System.out.println("Account 2: " + account2.getBalance());
41+
System.out.printf("Account 2 Balance: $%.2f\n", account2.getBalance());
4242
}
4343

44-
public static void withdraw(BankAccount account, int amount) {
44+
public static void withdraw(BankAccount account, double amount) {
4545
try {
4646
account.withdraw(amount);
4747
} catch (NotEnoughMoneyException e) {
48-
int diff = Math.abs(amount - account.getBalance());
49-
System.out.println(e.toString());
48+
System.out.println(e.getMessage());
5049
}
5150
}
5251
}
5352

5453
class BankAccount {
5554

56-
private int balance;
55+
private double balance;
5756

58-
public BankAccount(int balance) {
57+
public BankAccount(double balance) {
5958
this.balance = balance;
6059
}
6160

62-
public void deposit(int amount) {
63-
balance += amount;
61+
public void deposit(double amount) {
62+
if (amount > 0) {
63+
balance += amount;
64+
}
6465
}
6566

66-
public void withdraw(int amount) throws NotEnoughMoneyException {
67-
if (amount > balance) throw new NotEnoughMoneyException(
68-
"Bank balance is short $" + Math.abs(balance - amount)
69-
);
67+
public void withdraw(double amount) throws NotEnoughMoneyException {
68+
if (amount > balance) {
69+
throw new NotEnoughMoneyException(String.format("Bank balance is short $%.2f", Math.abs(balance - amount)));
70+
}
7071
balance -= amount;
7172
}
7273

73-
public int getBalance() {
74+
public double getBalance() {
7475
return balance;
7576
}
7677
}

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

-3
This file was deleted.

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

-3
This file was deleted.

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

-3
This file was deleted.

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

-57
This file was deleted.

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

-3
This file was deleted.

src/com/codefortomorrow/advanced/chapter16/solutions/Access.java

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.codefortomorrow.advanced.chapter16.solutions;
22

3-
import java.util.*;
3+
import java.util.ArrayList;
4+
import java.util.LinkedList;
45

56
/*
67
Using the methods outlined in the “Benefits of LinkedLists” section,
@@ -15,6 +16,7 @@
1516

1617
public class Access {
1718

19+
@SuppressWarnings("unused")
1820
public static void main(String[] args) {
1921
long a, b;
2022

0 commit comments

Comments
 (0)