Skip to content

Commit d3fa849

Browse files
authored
fix: updates build config and adjust tests (#458)
* fix: updates build config and adjust tests Signed-off-by: Anthony D. Mays <[email protected]> * chore: fix formattitng --------- Signed-off-by: Anthony D. Mays <[email protected]>
1 parent 2fbb684 commit d3fa849

File tree

5 files changed

+31
-10
lines changed

5 files changed

+31
-10
lines changed

lesson_13/maps_java/maps_app/build.gradle.kts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ dependencies {
3232

3333
application {
3434
// Define the main class for the application.
35-
mainClass.set("com.codedifferently.lesson12.Lesson12")
35+
mainClass.set("com.codedifferently.lesson13.Lesson13")
3636
}
3737

3838
tasks.named<Test>("test") {

lesson_14/exceptions/exceptions_app/build.gradle.kts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ dependencies {
3232

3333
application {
3434
// Define the main class for the application.
35-
mainClass.set("com.codedifferently.lesson12.Lesson12")
35+
mainClass.set("com.codedifferently.lesson14.Lesson14")
3636
}
3737

3838
tasks.named<Test>("test") {

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

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import java.util.UUID;
66

77
public class EcommerceSystem {
8+
89
private Map<String, Product> products;
910
private Map<String, Order> orders;
1011

@@ -17,8 +18,11 @@ public void addProduct(String productId, String name) {
1718
products.put(productId, new Product(productId, name));
1819
}
1920

20-
public String placeOrder(String productId, int quantity) {
21+
public String placeOrder(String productId, int quantity) throws ProductNotFoundException {
2122
Product product = products.get(productId);
23+
if (product == null) {
24+
throw new ProductNotFoundException("Product with ID " + productId + " not found");
25+
}
2226
String orderId = UUID.randomUUID().toString();
2327
orders.put(orderId, new Order(orderId, product, quantity));
2428
return orderId;
@@ -28,8 +32,11 @@ public void cancelOrder(String orderId) {
2832
orders.remove(orderId);
2933
}
3034

31-
public String checkOrderStatus(String orderId) {
35+
public String checkOrderStatus(String orderId) throws OrderNotFoundException {
3236
Order order = orders.get(orderId);
37+
if (order == null) {
38+
throw new OrderNotFoundException("Order with ID " + orderId + " not found");
39+
}
3340
return "Order ID: "
3441
+ orderId
3542
+ ", Product: "

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,11 @@
22
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
33
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
44
*/
5-
65
package com.codedifferently.lesson14.ecommerce;
76

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

lesson_14/exceptions/exceptions_app/src/test/java/com/codedifferently/lesson14/ecommerce/EcommerceSystemTest.java

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,11 @@ void testPlaceOrder_productDoesNotExist() throws Exception {
4040
assertThatThrownBy(() -> ecommerceSystem.placeOrder("1", 1))
4141
.isInstanceOf(ProductNotFoundException.class)
4242
.hasMessage("Product with ID 1 not found");
43+
44+
// Act
45+
assertThatThrownBy(() -> ecommerceSystem.placeOrder("34", 1))
46+
.isInstanceOf(ProductNotFoundException.class)
47+
.hasMessage("Product with ID 34 not found");
4348
}
4449

4550
@Test
@@ -58,18 +63,23 @@ void testCheckOrderStatus_orderDoesNotExist() {
5863
assertThatThrownBy(() -> ecommerceSystem.checkOrderStatus("1"))
5964
.isInstanceOf(OrderNotFoundException.class)
6065
.hasMessage("Order with ID 1 not found");
66+
67+
// Act
68+
assertThatThrownBy(() -> ecommerceSystem.checkOrderStatus("33"))
69+
.isInstanceOf(OrderNotFoundException.class)
70+
.hasMessage("Order with ID 33 not found");
6171
}
6272

6373
@Test
6474
void testCheckOrderStatus_orderCancelled() throws Exception {
6575
// Arrange
66-
ecommerceSystem.addProduct("1", "Laptop");
67-
String orderId = ecommerceSystem.placeOrder("1", 1);
76+
ecommerceSystem.addProduct("58", "Laptop");
77+
String orderId = ecommerceSystem.placeOrder("58", 1);
6878
ecommerceSystem.cancelOrder(orderId);
6979

7080
// Act
71-
assertThatThrownBy(() -> ecommerceSystem.checkOrderStatus("1"))
81+
assertThatThrownBy(() -> ecommerceSystem.checkOrderStatus("58"))
7282
.isInstanceOf(OrderNotFoundException.class)
73-
.hasMessage("Order with ID 1 not found");
83+
.hasMessage("Order with ID 58 not found");
7484
}
7585
}

0 commit comments

Comments
 (0)