From 18a718d36ff786e607ef3e5fc1dc9cf9616b242f Mon Sep 17 00:00:00 2001 From: simon Date: Sun, 3 Mar 2019 16:40:14 +0100 Subject: [PATCH 1/2] Add PG with tests --- .../ch/heigvd/res/chill/domain/Bartender.java | 26 +++++++-------- .../ch/heigvd/res/chill/domain/IProduct.java | 5 +-- .../heigvd/res/chill/domain/sjaubain/PG.java | 21 ++++++++++++ .../res/chill/domain/BartenderTest.java | 14 ++++---- .../res/chill/domain/sjaubain/PGTest.java | 32 +++++++++++++++++++ 5 files changed, 76 insertions(+), 22 deletions(-) create mode 100644 src/main/java/ch/heigvd/res/chill/domain/sjaubain/PG.java create mode 100644 src/test/java/ch/heigvd/res/chill/domain/sjaubain/PGTest.java diff --git a/src/main/java/ch/heigvd/res/chill/domain/Bartender.java b/src/main/java/ch/heigvd/res/chill/domain/Bartender.java index e669136da..2b73e5348 100644 --- a/src/main/java/ch/heigvd/res/chill/domain/Bartender.java +++ b/src/main/java/ch/heigvd/res/chill/domain/Bartender.java @@ -9,21 +9,21 @@ public class Bartender { - public GreetingsResponse greet(GreetingsRequest request) { - return new GreetingsResponse("hello, how can I help you?"); - } + public GreetingsResponse greet(GreetingsRequest request) { + return new GreetingsResponse("hello, how can I help you?"); + } - public OrderResponse order(OrderRequest request) { - String productName = request.getProductName(); + public OrderResponse order(OrderRequest request) { + String productName = request.getProductName(); - try { - // let Java reflection do its magic - IProduct product = (IProduct) Class.forName(productName).newInstance(); - BigDecimal totalPrice = product.getPrice().multiply(new BigDecimal(request.getQuantity())); - return new OrderResponse(totalPrice); - } catch (Exception e) { - return null; + try { + // let Java reflection do its magic + IProduct product = (IProduct) Class.forName(productName).newInstance(); + BigDecimal totalPrice = product.getPrice().multiply(new BigDecimal(request.getQuantity())); + return new OrderResponse(totalPrice); + } catch (Exception e) { + return null; + } } - } } diff --git a/src/main/java/ch/heigvd/res/chill/domain/IProduct.java b/src/main/java/ch/heigvd/res/chill/domain/IProduct.java index d11e0f9ed..6894f2491 100644 --- a/src/main/java/ch/heigvd/res/chill/domain/IProduct.java +++ b/src/main/java/ch/heigvd/res/chill/domain/IProduct.java @@ -4,7 +4,8 @@ public interface IProduct { - String getName(); - BigDecimal getPrice(); + String getName(); + + BigDecimal getPrice(); } diff --git a/src/main/java/ch/heigvd/res/chill/domain/sjaubain/PG.java b/src/main/java/ch/heigvd/res/chill/domain/sjaubain/PG.java new file mode 100644 index 000000000..2e54b6a9a --- /dev/null +++ b/src/main/java/ch/heigvd/res/chill/domain/sjaubain/PG.java @@ -0,0 +1,21 @@ +package ch.heigvd.res.chill.domain.sjaubain; + +import ch.heigvd.res.chill.domain.IProduct; + +import java.math.BigDecimal; + +public class PG implements IProduct { + + public final static String NAME = "Biere Prix Garantie"; + public final static BigDecimal PRICE = new BigDecimal(0.5); + + @Override + public String getName() { + return NAME; + } + + @Override + public BigDecimal getPrice() { + return PRICE; + } +} diff --git a/src/test/java/ch/heigvd/res/chill/domain/BartenderTest.java b/src/test/java/ch/heigvd/res/chill/domain/BartenderTest.java index 052a63f93..13155e3bb 100644 --- a/src/test/java/ch/heigvd/res/chill/domain/BartenderTest.java +++ b/src/test/java/ch/heigvd/res/chill/domain/BartenderTest.java @@ -8,12 +8,12 @@ class BartenderTest { - @Test - void aBartenderShouldReplyToGreetings() { - Bartender john = new Bartender(); - GreetingsRequest request = new GreetingsRequest("Hi there"); - GreetingsResponse response = john.greet(request); - assertEquals("hello, how can I help you?", response.getText()); - } + @Test + void aBartenderShouldReplyToGreetings() { + Bartender john = new Bartender(); + GreetingsRequest request = new GreetingsRequest("Hi there"); + GreetingsResponse response = john.greet(request); + assertEquals("hello, how can I help you?", response.getText()); + } } \ No newline at end of file diff --git a/src/test/java/ch/heigvd/res/chill/domain/sjaubain/PGTest.java b/src/test/java/ch/heigvd/res/chill/domain/sjaubain/PGTest.java new file mode 100644 index 000000000..fd50c2cf7 --- /dev/null +++ b/src/test/java/ch/heigvd/res/chill/domain/sjaubain/PGTest.java @@ -0,0 +1,32 @@ +package ch.heigvd.res.chill.domain.sjaubain; + +import ch.heigvd.res.chill.domain.Bartender; +import ch.heigvd.res.chill.domain.wasadigi.Boxer; +import ch.heigvd.res.chill.protocol.OrderRequest; +import ch.heigvd.res.chill.protocol.OrderResponse; +import org.junit.jupiter.api.Test; + +import java.math.BigDecimal; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +class PGTest { + + @Test + void thePriceAndNameForGrimbergenShouldBeCorrect() { + PG beer = new PG(); + assertEquals(beer.getName(), PG.NAME); + assertEquals(beer.getPrice(), PG.PRICE); + } + + @Test + void aBartenderShouldAcceptAnOrderForPG() { + Bartender jane = new Bartender(); + String productName = "ch.heigvd.res.chill.domain.sjaubain.PG"; + OrderRequest request = new OrderRequest(3, productName); + OrderResponse response = jane.order(request); + BigDecimal expectedTotalPrice = PG.PRICE.multiply(new BigDecimal(3)); + assertEquals(expectedTotalPrice, response.getTotalPrice()); + } + +} \ No newline at end of file From e1715c9825c135a9954053a49c685c5acc629526 Mon Sep 17 00:00:00 2001 From: simon Date: Sun, 3 Mar 2019 17:02:04 +0100 Subject: [PATCH 2/2] added only the production and test class --- src/main/java/ch/heigvd/res/chill/domain/sjaubain/PG.java | 1 + src/test/java/ch/heigvd/res/chill/domain/sjaubain/PGTest.java | 1 + 2 files changed, 2 insertions(+) diff --git a/src/main/java/ch/heigvd/res/chill/domain/sjaubain/PG.java b/src/main/java/ch/heigvd/res/chill/domain/sjaubain/PG.java index 2e54b6a9a..192e60aa8 100644 --- a/src/main/java/ch/heigvd/res/chill/domain/sjaubain/PG.java +++ b/src/main/java/ch/heigvd/res/chill/domain/sjaubain/PG.java @@ -18,4 +18,5 @@ public String getName() { public BigDecimal getPrice() { return PRICE; } + } diff --git a/src/test/java/ch/heigvd/res/chill/domain/sjaubain/PGTest.java b/src/test/java/ch/heigvd/res/chill/domain/sjaubain/PGTest.java index fd50c2cf7..7a3dd3bf7 100644 --- a/src/test/java/ch/heigvd/res/chill/domain/sjaubain/PGTest.java +++ b/src/test/java/ch/heigvd/res/chill/domain/sjaubain/PGTest.java @@ -21,6 +21,7 @@ void thePriceAndNameForGrimbergenShouldBeCorrect() { @Test void aBartenderShouldAcceptAnOrderForPG() { + Bartender jane = new Bartender(); String productName = "ch.heigvd.res.chill.domain.sjaubain.PG"; OrderRequest request = new OrderRequest(3, productName);