From 34d694c162033b7462afcf91f101bbd28f130aab Mon Sep 17 00:00:00 2001 From: Eric Douglas Date: Sat, 26 Dec 2020 13:42:16 +0100 Subject: [PATCH] Build tool maven, spring boot framework in combination with a database mysql added --- pom.xml | 84 ++++++++++++++++--- .../com/ApplicationStartDrinkMachine.java | 12 +++ .../java/com/controller/ViewsController.java | 28 +++++++ .../exceptions/InputContainsException.java | 11 +++ .../interfaces_and_enum/AbstractAutomat.java} | 10 +-- .../AbstractProductType.java} | 18 +++- .../interfaces_and_enum/EnumCoinValue.java} | 8 +- src/main/java/com/model/Coin.java | 23 +++++ .../java/{ => com}/model/CoinAndQuantity.java | 12 +-- .../java/{ => com}/model/Compartment.java | 8 +- src/main/java/com/model/Drink.java | 53 ++++++++++++ .../java/{ => com}/model/DrinkAndChange.java | 17 ++-- .../java/{ => com}/model/DrinkMachine.java | 34 ++++---- .../java/com/repository/DrinkRepository.java | 11 +++ src/main/java/com/service/DrinkService.java | 20 +++++ src/main/java/com/service/IDrinkService.java | 9 ++ src/main/java/com/service/InputService.java | 24 ++++++ src/main/java/common/Drink.java | 7 -- src/main/java/model/Coin.java | 23 ----- .../start_the_machine/StartDrinkMachine.java | 6 -- src/main/resources/application.properties | 12 +++ src/main/resources/static/index.html | 11 +++ src/main/resources/templates/showDrinks.ftlh | 27 ++++++ src/test/java/TestDrinkMachine.java | 53 ++++++------ src/test/java/com/model/DrinkMachineTest.java | 10 +++ web/WEB-INF/applicationContext.xml | 6 ++ web/WEB-INF/dispatcher-servlet.xml | 6 ++ web/WEB-INF/web.xml | 22 +++++ web/index.jsp | 16 ++++ 29 files changed, 462 insertions(+), 119 deletions(-) create mode 100644 src/main/java/com/ApplicationStartDrinkMachine.java create mode 100644 src/main/java/com/controller/ViewsController.java create mode 100644 src/main/java/com/exceptions/InputContainsException.java rename src/main/java/{common/Automat.java => com/interfaces_and_enum/AbstractAutomat.java} (71%) rename src/main/java/{common/ProductType.java => com/interfaces_and_enum/AbstractProductType.java} (57%) rename src/main/java/{common/CoinValue.java => com/interfaces_and_enum/EnumCoinValue.java} (75%) create mode 100644 src/main/java/com/model/Coin.java rename src/main/java/{ => com}/model/CoinAndQuantity.java (79%) rename src/main/java/{ => com}/model/Compartment.java (81%) create mode 100644 src/main/java/com/model/Drink.java rename src/main/java/{ => com}/model/DrinkAndChange.java (60%) rename src/main/java/{ => com}/model/DrinkMachine.java (92%) create mode 100644 src/main/java/com/repository/DrinkRepository.java create mode 100644 src/main/java/com/service/DrinkService.java create mode 100644 src/main/java/com/service/IDrinkService.java create mode 100644 src/main/java/com/service/InputService.java delete mode 100644 src/main/java/common/Drink.java delete mode 100644 src/main/java/model/Coin.java delete mode 100644 src/main/java/start_the_machine/StartDrinkMachine.java create mode 100644 src/main/resources/application.properties create mode 100644 src/main/resources/static/index.html create mode 100644 src/main/resources/templates/showDrinks.ftlh create mode 100644 src/test/java/com/model/DrinkMachineTest.java create mode 100644 web/WEB-INF/applicationContext.xml create mode 100644 web/WEB-INF/dispatcher-servlet.xml create mode 100644 web/WEB-INF/web.xml create mode 100644 web/index.jsp diff --git a/pom.xml b/pom.xml index 8f33167..227155c 100644 --- a/pom.xml +++ b/pom.xml @@ -8,12 +8,83 @@ drinkAutomat 1.0-SNAPSHOT + + org.springframework.boot + spring-boot-starter-parent + 2.3.5.RELEASE + + + Demo project for Spring Drink machine + org.junit.jupiter junit-jupiter-api 5.4.2 + + + org.springframework.ws + spring-ws-core + 3.0.1.RELEASE + + + + org.springframework.boot + spring-boot-starter-actuator + 2.3.5.RELEASE + + + + + org.springframework.boot + spring-boot + 2.3.5.RELEASE + + + + org.springframework + spring-web + 5.1.5.RELEASE + + + + mysql + mysql-connector-java + 8.0.22 + + + + org.springframework.boot + spring-boot-starter-freemarker + 2.3.5.RELEASE + + + + org.springframework.boot + spring-boot-starter-data-jpa + 2.3.1.RELEASE + provided + + + + org.springframework.data + spring-data-jpa + 2.3.5.RELEASE + + + + org.springframework.boot + spring-boot-starter-test + 2.3.5.RELEASE + test + + + org.junit.vintage + junit-vintage-engine + + + @@ -26,20 +97,13 @@ - org.apache.maven.plugins - maven-jar-plugin - 3.1.2 + org.springframework.boot + spring-boot-maven-plugin - - - start_the_machine.StartDrinkMachine - - + com.ApplicationStartDrinkMachine - - \ No newline at end of file diff --git a/src/main/java/com/ApplicationStartDrinkMachine.java b/src/main/java/com/ApplicationStartDrinkMachine.java new file mode 100644 index 0000000..90cfe21 --- /dev/null +++ b/src/main/java/com/ApplicationStartDrinkMachine.java @@ -0,0 +1,12 @@ +package com; + + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +@SpringBootApplication +public class ApplicationStartDrinkMachine { + public static void main(String[] args) { + SpringApplication.run(ApplicationStartDrinkMachine.class, args); + } +} \ No newline at end of file diff --git a/src/main/java/com/controller/ViewsController.java b/src/main/java/com/controller/ViewsController.java new file mode 100644 index 0000000..485e549 --- /dev/null +++ b/src/main/java/com/controller/ViewsController.java @@ -0,0 +1,28 @@ +package com.controller; + +import com.service.IDrinkService; +import com.model.Drink; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Controller; +import org.springframework.ui.Model; +import org.springframework.web.bind.annotation.GetMapping; + +import java.util.List; + +@Controller +public class ViewsController { + + @Autowired + private IDrinkService drinkService; + + @GetMapping("/drinks") + public String findDrinks(Model model) { + + List drinks = (List) drinkService.findAll(); + + model.addAttribute("drinks", drinks); + + return "showDrinks"; + } +} \ No newline at end of file diff --git a/src/main/java/com/exceptions/InputContainsException.java b/src/main/java/com/exceptions/InputContainsException.java new file mode 100644 index 0000000..31867d2 --- /dev/null +++ b/src/main/java/com/exceptions/InputContainsException.java @@ -0,0 +1,11 @@ +package com.exceptions; + +public class InputContainsException extends Exception { + + public InputContainsException() { + } + + public InputContainsException(String message) { + super(message); + } +} \ No newline at end of file diff --git a/src/main/java/common/Automat.java b/src/main/java/com/interfaces_and_enum/AbstractAutomat.java similarity index 71% rename from src/main/java/common/Automat.java rename to src/main/java/com/interfaces_and_enum/AbstractAutomat.java index 730f4cc..172e12e 100644 --- a/src/main/java/common/Automat.java +++ b/src/main/java/com/interfaces_and_enum/AbstractAutomat.java @@ -1,11 +1,11 @@ -package common; +package com.interfaces_and_enum; -import model.Coin; -import model.CoinAndQuantity; +import com.model.Coin; +import com.model.CoinAndQuantity; import java.util.List; -public abstract class Automat { +public abstract class AbstractAutomat { protected abstract void fillInWithCoin(List coins); @@ -16,4 +16,4 @@ public abstract class Automat { protected abstract void fillOut(List restCoinToGiveBack); protected abstract List changeCoin(int sum); -} +} \ No newline at end of file diff --git a/src/main/java/common/ProductType.java b/src/main/java/com/interfaces_and_enum/AbstractProductType.java similarity index 57% rename from src/main/java/common/ProductType.java rename to src/main/java/com/interfaces_and_enum/AbstractProductType.java index e9cab22..4d5ac5e 100644 --- a/src/main/java/common/ProductType.java +++ b/src/main/java/com/interfaces_and_enum/AbstractProductType.java @@ -1,6 +1,13 @@ -package common; +package com.interfaces_and_enum; -public abstract class ProductType { +import javax.persistence.GeneratedValue; +import javax.persistence.GenerationType; +import javax.persistence.Id; +import java.util.Objects; + +import static javax.persistence.GenerationType.*; + +public abstract class AbstractProductType { protected String name; protected double price; @@ -8,11 +15,14 @@ public abstract class ProductType { * @param name general name for drink product. * @param price general price of drink. */ - public ProductType(String name, double price) { + public AbstractProductType(String name, double price) { this.name = name; this.price = price; } + public AbstractProductType() { + } + public double getPrice() { return price; } @@ -28,4 +38,4 @@ public String getName() { public void setName(String name) { this.name = name; } -} +} \ No newline at end of file diff --git a/src/main/java/common/CoinValue.java b/src/main/java/com/interfaces_and_enum/EnumCoinValue.java similarity index 75% rename from src/main/java/common/CoinValue.java rename to src/main/java/com/interfaces_and_enum/EnumCoinValue.java index ac574d4..7daee9e 100644 --- a/src/main/java/common/CoinValue.java +++ b/src/main/java/com/interfaces_and_enum/EnumCoinValue.java @@ -1,9 +1,9 @@ -package common; +package com.interfaces_and_enum; /** * The coin is initialised in cents. */ -public enum CoinValue { +public enum EnumCoinValue { ONE_EURO(100), TWO_EURO(200), FIFTY_CENT(50), @@ -16,7 +16,7 @@ public int getValue() { return this.coinValue; } - CoinValue(int value) { + EnumCoinValue(int value) { this.coinValue = value; } -} +} \ No newline at end of file diff --git a/src/main/java/com/model/Coin.java b/src/main/java/com/model/Coin.java new file mode 100644 index 0000000..9f3147b --- /dev/null +++ b/src/main/java/com/model/Coin.java @@ -0,0 +1,23 @@ +package com.model; + +import com.interfaces_and_enum.EnumCoinValue; + +public class Coin { + + EnumCoinValue coinValue; + + /** + * @param coinValue represents the real coin value. + */ + public Coin(EnumCoinValue coinValue) { + this.coinValue = coinValue; + } + + public EnumCoinValue getCoinValue() { + return coinValue; + } + + public void setCoinValue(EnumCoinValue coinValue) { + this.coinValue = coinValue; + } +} \ No newline at end of file diff --git a/src/main/java/model/CoinAndQuantity.java b/src/main/java/com/model/CoinAndQuantity.java similarity index 79% rename from src/main/java/model/CoinAndQuantity.java rename to src/main/java/com/model/CoinAndQuantity.java index 0869a98..fc0ead1 100644 --- a/src/main/java/model/CoinAndQuantity.java +++ b/src/main/java/com/model/CoinAndQuantity.java @@ -1,20 +1,20 @@ -package model; +package com.model; -import common.CoinValue; +import com.interfaces_and_enum.EnumCoinValue; /** * This class is used to store change coins. */ public class CoinAndQuantity implements Comparable { - protected CoinValue type; + protected EnumCoinValue type; protected int quantity; /** * @param coin * @param quantity gives the number of coins for type coin */ - public CoinAndQuantity(CoinValue coin, int quantity) { + public CoinAndQuantity(EnumCoinValue coin, int quantity) { this.type = coin; this.quantity = quantity; @@ -32,7 +32,7 @@ public void reduceCoinNumber(int quantity) { this.quantity -= quantity; } - public CoinValue getType() { + public EnumCoinValue getType() { return type; } @@ -43,4 +43,4 @@ public int compareTo(CoinAndQuantity o) { return this.type.getValue() - f.type.getValue(); } -} +} \ No newline at end of file diff --git a/src/main/java/model/Compartment.java b/src/main/java/com/model/Compartment.java similarity index 81% rename from src/main/java/model/Compartment.java rename to src/main/java/com/model/Compartment.java index 6416ef7..5eab98b 100644 --- a/src/main/java/model/Compartment.java +++ b/src/main/java/com/model/Compartment.java @@ -1,6 +1,6 @@ -package model; +package com.model; -import common.ProductType; +import com.interfaces_and_enum.AbstractProductType; /** * Generic class for compartment in the drink machine. @@ -8,7 +8,7 @@ * * @param */ -public class Compartment { +public class Compartment { private T common; int quantity; @@ -33,4 +33,4 @@ public int getQuantity() { public void setQuantity(int quantity) { this.quantity = quantity; } -} +} \ No newline at end of file diff --git a/src/main/java/com/model/Drink.java b/src/main/java/com/model/Drink.java new file mode 100644 index 0000000..ce3870d --- /dev/null +++ b/src/main/java/com/model/Drink.java @@ -0,0 +1,53 @@ +package com.model; + +import com.interfaces_and_enum.AbstractProductType; + +import javax.persistence.*; + +import java.util.Objects; + +import static javax.persistence.GenerationType.AUTO; + +@Entity +@Table(name = "drinks") +public class Drink extends AbstractProductType { + @Id + @GeneratedValue(strategy = AUTO) + protected long id; + + public Drink(long id, String name, double price) { + super(name, price); + this.id = id; + } + + public Drink() { + super(); + } + + public long getId() { + return id; + } + + public void setId(long id) { + this.id = id; + } + + @Override + public int hashCode() { + int hash = 7; + hash = 79 * hash + Objects.hashCode(this.id); + hash = 79 * hash + Objects.hashCode(this.name); + hash = (int) ((79 * hash) + this.price); + return hash; + } + + @Override + public String toString() { + final StringBuilder sb = new StringBuilder("drink{"); + sb.append("id=").append(id); + sb.append(", name='").append(name).append('\''); + sb.append(", price=").append(price); + sb.append('}'); + return sb.toString(); + } +} \ No newline at end of file diff --git a/src/main/java/model/DrinkAndChange.java b/src/main/java/com/model/DrinkAndChange.java similarity index 60% rename from src/main/java/model/DrinkAndChange.java rename to src/main/java/com/model/DrinkAndChange.java index e38e75a..4314a05 100644 --- a/src/main/java/model/DrinkAndChange.java +++ b/src/main/java/com/model/DrinkAndChange.java @@ -1,17 +1,16 @@ -package model; +package com.model; -import common.CoinValue; -import common.Drink; +import com.interfaces_and_enum.EnumCoinValue; import java.util.HashMap; import java.util.List; /** - * This is used to return the drink and the change for the user. + * This is used to return the drink and the change for the com.user. */ public class DrinkAndChange { - HashMap change; + HashMap change; Drink drink; /** @@ -20,7 +19,7 @@ public class DrinkAndChange { */ public DrinkAndChange(Drink drink, List changes) { this.drink = drink; - this.change = new HashMap(); + this.change = new HashMap(); for (CoinAndQuantity change : changes) { @@ -33,11 +32,11 @@ public String getDrinkName() { return drink.getName(); } - public HashMap getChange() { + public HashMap getChange() { return change; } - public void setChange(HashMap change) { + public void setChange(HashMap change) { this.change = change; } -} +} \ No newline at end of file diff --git a/src/main/java/model/DrinkMachine.java b/src/main/java/com/model/DrinkMachine.java similarity index 92% rename from src/main/java/model/DrinkMachine.java rename to src/main/java/com/model/DrinkMachine.java index 96f543c..7a97bd8 100644 --- a/src/main/java/model/DrinkMachine.java +++ b/src/main/java/com/model/DrinkMachine.java @@ -1,8 +1,7 @@ -package model; +package com.model; -import common.Automat; -import common.CoinValue; -import common.Drink; +import com.interfaces_and_enum.AbstractAutomat; +import com.interfaces_and_enum.EnumCoinValue; import java.util.ArrayList; import java.util.Collections; @@ -13,15 +12,17 @@ /** * This class is an implementation of a drink machine, with the principal procedure. */ -public class DrinkMachine extends Automat { +public class DrinkMachine extends AbstractAutomat { public static final String NOT_MORE_AVAILABLE = "The drink name you choose is not more available"; public static final String NOT_ENOUGH_MONEY = "You have not given enough coins for the drink you need."; public static final String NOT_ENOUGH_COINS_FOR_CHANGE = "Not enough coins for change"; + HashMap compartments; List availableCoinForChange; List returnMoney; + /** * @param compartments: Each compartment contents only one drink type. */ @@ -31,13 +32,16 @@ public DrinkMachine(HashMap compartments) { this.compartments = compartments; } + public DrinkMachine() { + } + /** * This method manages the purchase process and in the best case returns * an object containing the order and the exchange coin. * - * @param drink: Contains the drink the user choose. - * @param coins: Contains the list of coins given for the user. - * @return if success the user's oder. + * @param drink: Contains the drink the com.user choose. + * @param coins: Contains the list of coins given for the com.user. + * @return if success the com.user's oder. */ public DrinkAndChange buy(Drink drink, List coins) { int rest; @@ -181,15 +185,17 @@ public void fillInWithCoin(List coinsToFillIn) { } public List getAvailableCoinForChange() { - return availableCoinForChange; + } + public HashMap getCompartments() { + return compartments; } /** * This method removes input coins from the available change coins list. * - * @param inputCoins the coin giving from the user. + * @param inputCoins the coin giving from the com.user. */ public void fillOutInputCoin(List inputCoins) { @@ -217,9 +223,9 @@ public void fillOutInputCoin(List inputCoins) { /** * This method removes changes coins in save before delivering the order. - * This occurs when the machine releases the remaining coins to the current user. + * This occurs when the machine releases the remaining coins to the current com.user. * - * @param restCoinToGiveBack is a list of change coins to give back to the current user. + * @param restCoinToGiveBack is a list of change coins to give back to the current com.user. */ public void fillOut(List restCoinToGiveBack) { @@ -252,7 +258,7 @@ public void fillOut(List restCoinToGiveBack) { * @param coinValue to find. * @return founded CoinAndQuantity object */ - private CoinAndQuantity getCoinAndQuantityByType(final CoinValue coinValue) { + private CoinAndQuantity getCoinAndQuantityByType(final EnumCoinValue coinValue) { for (CoinAndQuantity coinAndQuantity : this.availableCoinForChange) { @@ -284,4 +290,4 @@ private boolean isNotEnoughCoinsGiven(Drink drink, final List coins) { return drink.getPrice() * 100 > getCoinSum(coins); } -} +} \ No newline at end of file diff --git a/src/main/java/com/repository/DrinkRepository.java b/src/main/java/com/repository/DrinkRepository.java new file mode 100644 index 0000000..95aec1c --- /dev/null +++ b/src/main/java/com/repository/DrinkRepository.java @@ -0,0 +1,11 @@ +package com.repository; + +import com.model.Drink; + +import org.springframework.data.repository.CrudRepository; +import org.springframework.stereotype.Repository; + +@Repository +public interface DrinkRepository extends CrudRepository { + +} \ No newline at end of file diff --git a/src/main/java/com/service/DrinkService.java b/src/main/java/com/service/DrinkService.java new file mode 100644 index 0000000..44f0c42 --- /dev/null +++ b/src/main/java/com/service/DrinkService.java @@ -0,0 +1,20 @@ +package com.service; + +import com.model.Drink; +import com.repository.DrinkRepository; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +import java.util.List; + +@Service +public class DrinkService implements IDrinkService { + @Autowired + private DrinkRepository repository; + + @Override + public List findAll() { + return (List) repository.findAll(); + } +} + diff --git a/src/main/java/com/service/IDrinkService.java b/src/main/java/com/service/IDrinkService.java new file mode 100644 index 0000000..319f812 --- /dev/null +++ b/src/main/java/com/service/IDrinkService.java @@ -0,0 +1,9 @@ +package com.service; + +import com.model.Drink; + +import java.util.List; + +public interface IDrinkService { + List findAll(); +} diff --git a/src/main/java/com/service/InputService.java b/src/main/java/com/service/InputService.java new file mode 100644 index 0000000..d1b1e32 --- /dev/null +++ b/src/main/java/com/service/InputService.java @@ -0,0 +1,24 @@ +package com.service; + +import com.model.DrinkMachine; + +public class InputService { + + public InputService() { + System.out.println("Hello! Please choose the drink you want to buy ans gives "); + } + + public void getUserOrder(DrinkMachine drinkMachine) { + + if (!drinkMachine.getCompartments().isEmpty()) { + System.out.println("Here is the list of the available drinks."); + for (String drinkName : drinkMachine.getCompartments().keySet()) { + System.out.print(drinkName + " : "); + } + } else { + System.out.println("Sorry! The machine is out of service for the moment."); + } + } +} + + diff --git a/src/main/java/common/Drink.java b/src/main/java/common/Drink.java deleted file mode 100644 index 704d8e3..0000000 --- a/src/main/java/common/Drink.java +++ /dev/null @@ -1,7 +0,0 @@ -package common; - -public class Drink extends ProductType { - public Drink(String name, double price) { - super(name, price); - } -} diff --git a/src/main/java/model/Coin.java b/src/main/java/model/Coin.java deleted file mode 100644 index bed403f..0000000 --- a/src/main/java/model/Coin.java +++ /dev/null @@ -1,23 +0,0 @@ -package model; - -import common.CoinValue; - -public class Coin { - - CoinValue coinValue; - - /** - * @param coinValue represents the real coin value. - */ - public Coin(CoinValue coinValue) { - this.coinValue = coinValue; - } - - public CoinValue getCoinValue() { - return coinValue; - } - - public void setCoinValue(CoinValue coinValue) { - this.coinValue = coinValue; - } -} diff --git a/src/main/java/start_the_machine/StartDrinkMachine.java b/src/main/java/start_the_machine/StartDrinkMachine.java deleted file mode 100644 index 4857a90..0000000 --- a/src/main/java/start_the_machine/StartDrinkMachine.java +++ /dev/null @@ -1,6 +0,0 @@ -package start_the_machine; -public class StartDrinkMachine { - public static void main(String[] args) { - System.out.println("Hello world"); - } -} diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties new file mode 100644 index 0000000..5aa0fad --- /dev/null +++ b/src/main/resources/application.properties @@ -0,0 +1,12 @@ +spring.main.banner-mode=off +logging.level.org.springframework=ERROR +spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver +spring.datasource.url=jdbc:mysql://localhost:3306/testdb?useSSL=false&serverTimezone=UTC&allowPublicKeyRetrieval=true +spring.datasource.username=user12 +spring.datasource.password=madeleine +## Hibernate Properties +# The SQL dialect makes Hibernate generate better SQL for the chosen database +spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5InnoDBDialect +# Hibernate ddl auto (create, create-drop, validate, update) +spring.jpa.hibernate.ddl-auto=update + diff --git a/src/main/resources/static/index.html b/src/main/resources/static/index.html new file mode 100644 index 0000000..dfdbde3 --- /dev/null +++ b/src/main/resources/static/index.html @@ -0,0 +1,11 @@ + + + + Home page + + + + +Show drinks + + \ No newline at end of file diff --git a/src/main/resources/templates/showDrinks.ftlh b/src/main/resources/templates/showDrinks.ftlh new file mode 100644 index 0000000..17ea742 --- /dev/null +++ b/src/main/resources/templates/showDrinks.ftlh @@ -0,0 +1,27 @@ + + + + Drinks + + + + +

List of drinks

+ + + + + + + + + <#list drinks as drink> + + + + + + +
IdNamePrice
${drink.id}${drink.name}${drink.price}
+ + \ No newline at end of file diff --git a/src/test/java/TestDrinkMachine.java b/src/test/java/TestDrinkMachine.java index b4215a1..a132bda 100644 --- a/src/test/java/TestDrinkMachine.java +++ b/src/test/java/TestDrinkMachine.java @@ -1,7 +1,6 @@ -import common.CoinValue; -import common.Drink; -import model.*; +import com.interfaces_and_enum.EnumCoinValue; +import com.model.*; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; @@ -28,8 +27,8 @@ public class TestDrinkMachine { */ @BeforeEach public void init() { - cola = new Drink(COLA, 1.20); - beer = new Drink(BEER, 2); + cola = new Drink(00, COLA, 1.20); + beer = new Drink(01, BEER, 2); colaCompartment = new Compartment<>(cola, 2); beerCompartment = new Compartment<>(beer, 2); compartments = new HashMap<>(); @@ -38,7 +37,7 @@ public void init() { } /** - * A user orders a beer and gives exactly three coins (50, 50 and 1 euros). + * A com.user orders a beer and gives exactly three coins (50, 50 and 1 euros). * The test checks if he gets exactly one beer and no change, because he gave exactly 2 euros. */ @Test @@ -46,13 +45,13 @@ public void testReturnDrink() { init(); DrinkMachine drinkMachine = new DrinkMachine(compartments); List inputCoins = new ArrayList<>(); - inputCoins.add(new Coin(CoinValue.FIFTY_CENT)); - inputCoins.add(new Coin(CoinValue.FIFTY_CENT)); - inputCoins.add(new Coin(CoinValue.ONE_EURO)); + inputCoins.add(new Coin(EnumCoinValue.FIFTY_CENT)); + inputCoins.add(new Coin(EnumCoinValue.FIFTY_CENT)); + inputCoins.add(new Coin(EnumCoinValue.ONE_EURO)); - CoinAndQuantity coin_50 = new CoinAndQuantity(CoinValue.FIFTY_CENT, 6); - CoinAndQuantity coin_20 = new CoinAndQuantity(CoinValue.TWENTY_CENT, 2); - CoinAndQuantity coin_10 = new CoinAndQuantity(CoinValue.TEN_CENT, 2); + CoinAndQuantity coin_50 = new CoinAndQuantity(EnumCoinValue.FIFTY_CENT, 6); + CoinAndQuantity coin_20 = new CoinAndQuantity(EnumCoinValue.TWENTY_CENT, 2); + CoinAndQuantity coin_10 = new CoinAndQuantity(EnumCoinValue.TEN_CENT, 2); drinkMachine.addAvailableCoinForChange(coin_10); drinkMachine.addAvailableCoinForChange(coin_20); drinkMachine.addAvailableCoinForChange(coin_50); @@ -66,7 +65,7 @@ public void testReturnDrink() { } /** - * A user ordered a cola and gives 2 euro. The cola cost is 1.20 euro. The rest will be 80 cent. + * A com.user ordered a cola and gives 2 euro. The cola cost is 1.20 euro. The rest will be 80 cent. * Test if the machine gives back a cola and the following coins 1x50 cent, 1x20 cent and 1x10 cent. * Test further to see if there are no more 50 cents in the reserve, and also shows that * the 2 euro is now also in the reserve. @@ -75,16 +74,16 @@ public void testReturnDrink() { public void testReturnChange() { init(); List inputCoins = new ArrayList<>(); - inputCoins.add(new Coin(CoinValue.TWO_EURO)); + inputCoins.add(new Coin(EnumCoinValue.TWO_EURO)); DrinkMachine drinkMachine = new DrinkMachine(compartments); /** * Set coins for change */ - CoinAndQuantity coin_50 = new CoinAndQuantity(CoinValue.FIFTY_CENT, 1); - CoinAndQuantity coin_20 = new CoinAndQuantity(CoinValue.TWENTY_CENT, 4); - CoinAndQuantity coin_10 = new CoinAndQuantity(CoinValue.TEN_CENT, 8); + CoinAndQuantity coin_50 = new CoinAndQuantity(EnumCoinValue.FIFTY_CENT, 1); + CoinAndQuantity coin_20 = new CoinAndQuantity(EnumCoinValue.TWENTY_CENT, 4); + CoinAndQuantity coin_10 = new CoinAndQuantity(EnumCoinValue.TEN_CENT, 8); drinkMachine.addAvailableCoinForChange(coin_10); drinkMachine.addAvailableCoinForChange(coin_20); drinkMachine.addAvailableCoinForChange(coin_50); @@ -92,19 +91,19 @@ public void testReturnChange() { DrinkAndChange drinkAndChange = drinkMachine.buy(cola, inputCoins); assertEquals(cola.getName(), drinkAndChange.getDrinkName()); - assertEquals(1, drinkAndChange.getChange().get(CoinValue.FIFTY_CENT)); - assertEquals(1, drinkAndChange.getChange().get(CoinValue.TWENTY_CENT)); - assertEquals(1, drinkAndChange.getChange().get(CoinValue.TEN_CENT)); + assertEquals(1, drinkAndChange.getChange().get(EnumCoinValue.FIFTY_CENT)); + assertEquals(1, drinkAndChange.getChange().get(EnumCoinValue.TWENTY_CENT)); + assertEquals(1, drinkAndChange.getChange().get(EnumCoinValue.TEN_CENT)); assertEquals(3, drinkMachine.getAvailableCoinForChange().size()); for (CoinAndQuantity coinAndQuantity : drinkMachine.getAvailableCoinForChange()) { - assertNotEquals(CoinValue.FIFTY_CENT, coinAndQuantity.getType()); + assertNotEquals(EnumCoinValue.FIFTY_CENT, coinAndQuantity.getType()); } } /** - * A user orders a cola. As the ordered drink is no longer available, + * A com.user orders a cola. As the ordered drink is no longer available, * the purchase is not made. This test checks or no cola is given. */ @Test @@ -113,7 +112,7 @@ public void testNoExistingDrink() { HashMap compartments = new HashMap<>(); List inputCoins = new ArrayList<>(); - inputCoins.add(new Coin(CoinValue.TWO_EURO)); + inputCoins.add(new Coin(EnumCoinValue.TWO_EURO)); DrinkMachine drinkMachine = new DrinkMachine(compartments); @@ -122,14 +121,14 @@ public void testNoExistingDrink() { } /** - * A user orders a cola and gives 2 euros. But a cola costs 1.20 euro. + * A com.user orders a cola and gives 2 euros. But a cola costs 1.20 euro. * As the machine has no more change he should not place the order. */ @Test public void testNotEnoughCoinForDrink() { init(); List inputCoins = new ArrayList<>(); - inputCoins.add(new Coin(CoinValue.ONE_EURO)); + inputCoins.add(new Coin(EnumCoinValue.ONE_EURO)); DrinkMachine drinkMachine = new DrinkMachine(compartments); @@ -148,7 +147,7 @@ public void testFillInChangeCoins() { init(); List inputCoins = new ArrayList<>(); - inputCoins.add(new Coin(CoinValue.TEN_CENT)); + inputCoins.add(new Coin(EnumCoinValue.TEN_CENT)); DrinkMachine drinkMachine = new DrinkMachine(compartments); @@ -158,6 +157,6 @@ public void testFillInChangeCoins() { assertEquals(1, drinkMachine.getAvailableCoinForChange().size()); - assertEquals(CoinValue.TEN_CENT, drinkMachine.getAvailableCoinForChange().get(0).getType()); + assertEquals(EnumCoinValue.TEN_CENT, drinkMachine.getAvailableCoinForChange().get(0).getType()); } } \ No newline at end of file diff --git a/src/test/java/com/model/DrinkMachineTest.java b/src/test/java/com/model/DrinkMachineTest.java new file mode 100644 index 0000000..d01b677 --- /dev/null +++ b/src/test/java/com/model/DrinkMachineTest.java @@ -0,0 +1,10 @@ +package com.model; + +import org.junit.jupiter.api.Test; + +class DrinkMachineTest { + + @Test + void buy() { + } +} \ No newline at end of file diff --git a/web/WEB-INF/applicationContext.xml b/web/WEB-INF/applicationContext.xml new file mode 100644 index 0000000..142def2 --- /dev/null +++ b/web/WEB-INF/applicationContext.xml @@ -0,0 +1,6 @@ + + + + \ No newline at end of file diff --git a/web/WEB-INF/dispatcher-servlet.xml b/web/WEB-INF/dispatcher-servlet.xml new file mode 100644 index 0000000..142def2 --- /dev/null +++ b/web/WEB-INF/dispatcher-servlet.xml @@ -0,0 +1,6 @@ + + + + \ No newline at end of file diff --git a/web/WEB-INF/web.xml b/web/WEB-INF/web.xml new file mode 100644 index 0000000..7483b1f --- /dev/null +++ b/web/WEB-INF/web.xml @@ -0,0 +1,22 @@ + + + + contextConfigLocation + /WEB-INF/applicationContext.xml + + + org.springframework.web.context.ContextLoaderListener + + + dispatcher + org.springframework.web.servlet.DispatcherServlet + 1 + + + dispatcher + *.form + + \ No newline at end of file diff --git a/web/index.jsp b/web/index.jsp new file mode 100644 index 0000000..1746268 --- /dev/null +++ b/web/index.jsp @@ -0,0 +1,16 @@ +<%-- + Created by IntelliJ IDEA. + User: eric + Date: 25.12.20 + Time: 02:30 + To change this template use File | Settings | File Templates. +--%> +<%@ page contentType="text/html;charset=UTF-8" language="java" %> + + + $Title$ + + + $END$ + +