Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: adds lesson_14 homework and lesson_15 pre-work #450

Merged
merged 4 commits into from
Oct 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions .github/workflows/check_lesson_14_java_pr.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
name: Check Lesson 14 Java Pull Request

on:
pull_request:
branches: [ "main" ]
paths:
- "lesson_14/exceptions/**"

jobs:
build:

runs-on: ubuntu-latest
permissions:
contents: read

steps:
- uses: actions/checkout@v4

- name: Set up JDK 17
uses: actions/setup-java@v4
with:
java-version: '17'
distribution: 'temurin'

- name: Build Lesson 13 with Java
working-directory: ./lesson_14/exceptions
run: ./gradlew check
5 changes: 5 additions & 0 deletions .github/workflows/check_push.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ on:
- "lesson_12/structs_ts/**"
- "lesson_13/maps_java/**"
- "lesson_13/maps_ts/**"
- "lesson_14/exceptions/**"

jobs:
build:
Expand Down Expand Up @@ -117,3 +118,7 @@ jobs:
run: |
npm ci
npm run compile

- name: Build Lesson 14 with Java
working-directory: ./lesson_14/exceptions
run: ./gradlew assemble
11 changes: 10 additions & 1 deletion lesson_14/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,13 @@ Please review the following resources before lecture:

## Homework

TODO(anthonydmays): Complete this
- [ ] Complete [Exception Handling](#exception-handling) assignment.
- [ ] Do pre-work for [lesson 15](/lesson_15/).

### Exception Handling

For this assignment, you will update existing code to add exceptions for unsupported operations in an e-commerce system.

To complete this assignment, [use the tests][test-link] to understand what you need to implement in order to get the tests to pass.

[test-link]: ./exceptions/exceptions_app/src/test/java/com/codedifferently/lesson14/ecommerce/EcommerceSystemTest.java
9 changes: 9 additions & 0 deletions lesson_14/exceptions/.gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#
# https://help.github.com/articles/dealing-with-line-endings/
#
# Linux start script should use lf
/gradlew text eol=lf

# These are Windows script files and should use crlf
*.bat text eol=crlf

5 changes: 5 additions & 0 deletions lesson_14/exceptions/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Ignore Gradle project-specific cache directory
.gradle

# Ignore Gradle build output directory
build
64 changes: 64 additions & 0 deletions lesson_14/exceptions/exceptions_app/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
plugins {
// Apply the application plugin to add support for building a CLI application in Java.
application
eclipse
id("com.diffplug.spotless") version "6.25.0"
id("org.springframework.boot") version "3.2.2"
id("com.adarshr.test-logger") version "4.0.0"
}

apply(plugin = "io.spring.dependency-management")

repositories {
// Use Maven Central for resolving dependencies.
mavenCentral()
}

dependencies {
// Use JUnit Jupiter for testing.
testImplementation("com.codedifferently.instructional:instructional-lib")
testImplementation("org.junit.jupiter:junit-jupiter:5.9.1")
testImplementation("org.springframework.boot:spring-boot-starter-test")
testImplementation("org.assertj:assertj-core:3.25.1")
testImplementation("at.favre.lib:bcrypt:0.10.2")

// This dependency is used by the application.
implementation("com.codedifferently.instructional:instructional-lib")
implementation("com.google.guava:guava:31.1-jre")
implementation("com.google.code.gson:gson:2.10.1")
implementation("org.projectlombok:lombok:1.18.30")
implementation("org.springframework.boot:spring-boot-starter")
}

application {
// Define the main class for the application.
mainClass.set("com.codedifferently.lesson12.Lesson12")
}

tasks.named<Test>("test") {
// Use JUnit Platform for unit tests.
useJUnitPlatform()
}


configure<com.diffplug.gradle.spotless.SpotlessExtension> {

format("misc", {
// define the files to apply `misc` to
target("*.gradle", ".gitattributes", ".gitignore")

// define the steps to apply to those files
trimTrailingWhitespace()
indentWithTabs() // or spaces. Takes an integer argument if you don't like 4
endWithNewline()
})

java {
// don't need to set target, it is inferred from java

// apply a specific flavor of google-java-format
googleJavaFormat()
// fix formatting of type annotations
formatAnnotations()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.codedifferently.lesson14;

public class Lesson14 {

public static void main(String[] args) {
System.out.println("Hello World");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package com.codedifferently.lesson14.ecommerce;

import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

public class EcommerceSystem {
private Map<String, Product> products;
private Map<String, Order> orders;

public EcommerceSystem() {
products = new HashMap<>();
orders = new HashMap<>();
}

public void addProduct(String productId, String name) {
products.put(productId, new Product(productId, name));
}

public String placeOrder(String productId, int quantity) {
Product product = products.get(productId);
String orderId = UUID.randomUUID().toString();
orders.put(orderId, new Order(orderId, product, quantity));
return orderId;
}

public void cancelOrder(String orderId) {
orders.remove(orderId);
}

public String checkOrderStatus(String orderId) {
Order order = orders.get(orderId);
return "Order ID: "
+ orderId
+ ", Product: "
+ order.getProduct().getName()
+ ", Quantity: "
+ order.getQuantity();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.codedifferently.lesson14.ecommerce;

public class Order {
private String orderId;
private Product product;
private int quantity;

public Order(String orderId, Product product, int quantity) {
this.orderId = orderId;
this.product = product;
this.quantity = quantity;
}

public String getOrderId() {
return orderId;
}

public Product getProduct() {
return product;
}

public int getQuantity() {
return quantity;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
*/

package com.codedifferently.lesson14.ecommerce;

class OrderNotFoundException {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.codedifferently.lesson14.ecommerce;

public class Product {
private String productId;
private String name;

public Product(String productId, String name) {
this.productId = productId;
this.name = name;
}

public String getProductId() {
return productId;
}

public String getName() {
return name;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
*/

package com.codedifferently.lesson14.ecommerce;

class ProductNotFoundException {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.codedifferently.lesson14;

import static org.assertj.core.api.Assertions.assertThat;

import org.junit.jupiter.api.Test;

class Lesson14Test {

@Test
public void testLesson14() {
var solution = new Lesson14();

assertThat(solution).isNotNull();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package com.codedifferently.lesson14.ecommerce;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

class EcommerceSystemTest {

EcommerceSystem ecommerceSystem;

@BeforeEach
void setUp() {
ecommerceSystem = new EcommerceSystem();
}

@Test
void testAddProduct() {
// Act
ecommerceSystem.addProduct("1", "Laptop");
}

@Test
void testPlaceOrder() throws Exception {
// Arrange
ecommerceSystem.addProduct("1", "Laptop");

// Act
String orderId = ecommerceSystem.placeOrder("1", 1);

// Assert
String actual = ecommerceSystem.checkOrderStatus(orderId);
assertThat(actual).isEqualTo("Order ID: " + orderId + ", Product: Laptop, Quantity: 1");
}

@Test
void testPlaceOrder_productDoesNotExist() throws Exception {
// Act
assertThatThrownBy(() -> ecommerceSystem.placeOrder("1", 1))
.isInstanceOf(ProductNotFoundException.class)
.hasMessage("Product with ID 1 not found");
}

@Test
void testCancelOrder() throws Exception {
// Arrange
ecommerceSystem.addProduct("1", "Laptop");
String orderId = ecommerceSystem.placeOrder("1", 1);

// Act
ecommerceSystem.cancelOrder(orderId);
}

@Test
void testCheckOrderStatus_orderDoesNotExist() {
// Act
assertThatThrownBy(() -> ecommerceSystem.checkOrderStatus("1"))
.isInstanceOf(OrderNotFoundException.class)
.hasMessage("Order with ID 1 not found");
}

@Test
void testCheckOrderStatus_orderCancelled() throws Exception {
// Arrange
ecommerceSystem.addProduct("1", "Laptop");
String orderId = ecommerceSystem.placeOrder("1", 1);
ecommerceSystem.cancelOrder(orderId);

// Act
assertThatThrownBy(() -> ecommerceSystem.checkOrderStatus("1"))
.isInstanceOf(OrderNotFoundException.class)
.hasMessage("Order with ID 1 not found");
}
}
Binary file not shown.
6 changes: 6 additions & 0 deletions lesson_14/exceptions/gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-8.0.2-bin.zip
networkTimeout=10000
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
Loading
Loading