Skip to content

Commit 69fde1b

Browse files
committed
Feat: adds Custom Exception
1 parent c601e7f commit 69fde1b

File tree

4 files changed

+39
-7
lines changed

4 files changed

+39
-7
lines changed

lesson_14/exceptions/exceptions_app/src/main/java/com/codedifferently/lesson14/ecommerce/Order.java

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,25 @@ public Order(String orderId, Product product, int quantity) {
1111
this.quantity = quantity;
1212
}
1313

14-
public String getOrderId() {
14+
public String getOrderId() throws OrderNotFoundException {
15+
if (orderId == null) {
16+
throw new OrderNotFoundException("This order does not match any in our records");
17+
}
1518
return orderId;
1619
}
1720

18-
public Product getProduct() {
21+
public Product getProduct() throws OrderNotFoundException {
22+
if (product == null) {
23+
throw new OrderNotFoundException("This Product is not in our system.");
24+
}
1925
return product;
2026
}
2127

22-
public int getQuantity() {
28+
public int getQuantity() throws OrderNotFoundException {
29+
if (quantity == 0) {
30+
throw new OrderNotFoundException("Out of Stock.");
31+
}
32+
2333
return quantity;
2434
}
2535
}

lesson_14/exceptions/exceptions_app/src/main/java/com/codedifferently/lesson14/ecommerce/OrderNotFoundException.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,8 @@
55

66
package com.codedifferently.lesson14.ecommerce;
77

8-
class OrderNotFoundException {}
8+
class OrderNotFoundException extends Exception {
9+
public OrderNotFoundException(String message) {
10+
super(message);
11+
}
12+
}

lesson_14/exceptions/exceptions_app/src/main/java/com/codedifferently/lesson14/ecommerce/Product.java

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,25 @@ public Product(String productId, String name) {
99
this.name = name;
1010
}
1111

12-
public String getProductId() {
12+
public String getProductId() /*throws ProductNotFoundException*/ {
13+
if (productId == null) {
14+
try {
15+
throw new ProductNotFoundException("The product ID can not be found.");
16+
} catch (ProductNotFoundException e) {
17+
System.out.println(e);
18+
}
19+
}
1320
return productId;
1421
}
1522

16-
public String getName() {
23+
public String getName() /*throws ProductNotFoundException*/ {
24+
if (name == null) {
25+
try {
26+
throw new ProductNotFoundException("Nothing matches that name");
27+
} catch (ProductNotFoundException e) {
28+
System.out.println(e);
29+
}
30+
}
1731
return name;
1832
}
1933
}

lesson_14/exceptions/exceptions_app/src/main/java/com/codedifferently/lesson14/ecommerce/ProductNotFoundException.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,8 @@
55

66
package com.codedifferently.lesson14.ecommerce;
77

8-
class ProductNotFoundException {}
8+
class ProductNotFoundException extends Exception {
9+
public ProductNotFoundException(String message) {
10+
super(message);
11+
}
12+
}

0 commit comments

Comments
 (0)