diff --git a/documentation/html_template/index.html b/documentation/html_template/index.html new file mode 100644 index 0000000..217cce2 --- /dev/null +++ b/documentation/html_template/index.html @@ -0,0 +1,16 @@ + + + + + Hello + + +

Hello my friend!

+

abra
+ kadabrak
+ 11111 kkjlf© + yyyyyyuuu + wuyeiuwbw + df

+ + \ No newline at end of file diff --git a/pom.xml b/pom.xml index fe65b87..7596340 100644 --- a/pom.xml +++ b/pom.xml @@ -11,6 +11,41 @@ 11 11 + 5.3.12 + + + org.junit.jupiter + junit-jupiter + 5.8.1 + test + + + org.springframework + spring-core + ${spring.version} + + + org.springframework + spring-context + ${spring.version} + + + org.springframework + spring-beans + ${spring.version} + + + + + + + org.apache.maven.plugins + maven-surefire-plugin + 3.0.0-M5 + + + + \ No newline at end of file diff --git a/src/main/java/com/epam/brest/Main.java b/src/main/java/com/epam/brest/Main.java index ae354df..e3b2df9 100644 --- a/src/main/java/com/epam/brest/Main.java +++ b/src/main/java/com/epam/brest/Main.java @@ -1,30 +1,38 @@ package com.epam.brest; -import com.epam.brest.calc.CalcImpl; +import com.epam.brest.files.CSVFileReader; +import com.epam.brest.files.FileReader; +import com.epam.brest.model.ReadDataState; +import com.epam.brest.model.Status; +import com.epam.brest.model.StatusType; +import org.springframework.context.ApplicationContext; +import org.springframework.context.support.ClassPathXmlApplicationContext; +import java.io.IOException; import java.math.BigDecimal; +import java.util.Map; import java.util.Scanner; public class Main { - public static void main(String[] args) { - BigDecimal weight, pricePerKg, length, pricePerKm; + public static void main(String[] arg) throws IOException { + //result = weight*pricePerKg + km*pricePerKm; + + ApplicationContext applicationContext + = new ClassPathXmlApplicationContext("spring-config.xml"); + FileReader fileReader = (FileReader) applicationContext.getBean("fileReader"); + + Map pricePerKgMap = fileReader.readData("price_weight.csv"); + Map pricePerKmMap = fileReader.readData("price_distance.csv"); + try (Scanner scanner = new Scanner(System.in)) { - do { - weight = getValueFromCon(scanner, "Enter weight:"); - pricePerKg = getValueFromCon(scanner, "Enter pricePerKg:"); - length = getValueFromCon(scanner, "Enter length:"); - pricePerKm = getValueFromCon(scanner, "Enter pricePerKm:"); - System.out.println("Result:" + new CalcImpl().handle(weight, pricePerKg, length, pricePerKm)); - System.out.println("Enter 'q' for exit or 'c' to continue:"); - } while (scanner.next().equals("c")); + Status currentStatus = new ReadDataState(scanner, pricePerKgMap, pricePerKmMap); + while (currentStatus.getType() != StatusType.EXIT) { + System.out.println("Current system status: " + currentStatus.getType()); + currentStatus = currentStatus.handle(); + } } } - private static BigDecimal getValueFromCon(Scanner scanner, String outputMessage) { - BigDecimal enteredValue; - System.out.print(outputMessage); - enteredValue = scanner.nextBigDecimal(); - return enteredValue; - } } + diff --git a/src/main/java/com/epam/brest/calc/CalcImpl.java b/src/main/java/com/epam/brest/calc/CalcImpl.java index b7b0f9d..593af55 100644 --- a/src/main/java/com/epam/brest/calc/CalcImpl.java +++ b/src/main/java/com/epam/brest/calc/CalcImpl.java @@ -6,6 +6,9 @@ public class CalcImpl implements Calc { @Override public BigDecimal handle(BigDecimal weight, BigDecimal pricePerKg, BigDecimal length, BigDecimal pricePerKm) { + if (weight == null || pricePerKg == null || length == null || pricePerKm == null) { + throw new IllegalArgumentException("Parameter should not be NULL"); + } return weight.multiply(pricePerKg).add(length.multiply(pricePerKm)); } } diff --git a/src/main/java/com/epam/brest/files/CSVFileReader.java b/src/main/java/com/epam/brest/files/CSVFileReader.java new file mode 100644 index 0000000..a87ded7 --- /dev/null +++ b/src/main/java/com/epam/brest/files/CSVFileReader.java @@ -0,0 +1,29 @@ +package com.epam.brest.files; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.math.BigDecimal; +import java.util.Map; +import java.util.TreeMap; + +public class CSVFileReader implements FileReader { + + @Override + public Map readData(String filePath) throws IOException { + Map resultMap = new TreeMap<>(); + InputStream inputStream = getClass().getResourceAsStream("/" + filePath); + try (InputStreamReader inputStreamReader = new InputStreamReader(inputStream); + BufferedReader bufferedReader = new BufferedReader(inputStreamReader)) { + String line; + String[] values; + while ((line = bufferedReader.readLine()) != null) { + values = line.split(","); + resultMap.put(Integer.valueOf(values[0]), new BigDecimal(values[1])); + } + } + return resultMap; + } +} + diff --git a/src/main/java/com/epam/brest/files/FileReader.java b/src/main/java/com/epam/brest/files/FileReader.java new file mode 100644 index 0000000..3b12516 --- /dev/null +++ b/src/main/java/com/epam/brest/files/FileReader.java @@ -0,0 +1,10 @@ +package com.epam.brest.files; + +import java.io.IOException; +import java.math.BigDecimal; +import java.util.Map; + +public interface FileReader { + + Map readData(String filePath) throws IOException; +} diff --git a/src/main/java/com/epam/brest/model/AbstractStatus.java b/src/main/java/com/epam/brest/model/AbstractStatus.java new file mode 100644 index 0000000..75366d2 --- /dev/null +++ b/src/main/java/com/epam/brest/model/AbstractStatus.java @@ -0,0 +1,13 @@ +package com.epam.brest.model; + +import java.math.BigDecimal; +import java.util.Map; +import java.util.Scanner; + +public abstract class AbstractStatus implements Status { + + Scanner scanner; + Map pricePerKgMap; + Map pricePerKmMap; + +} diff --git a/src/main/java/com/epam/brest/model/CalcState.java b/src/main/java/com/epam/brest/model/CalcState.java new file mode 100644 index 0000000..bf86466 --- /dev/null +++ b/src/main/java/com/epam/brest/model/CalcState.java @@ -0,0 +1,39 @@ +package com.epam.brest.model; + +import com.epam.brest.calc.CalcImpl; +import com.epam.brest.selector.PriceSelector; + +import java.math.BigDecimal; +import java.util.Map; +import java.util.Scanner; + +import static com.epam.brest.model.StatusType.CALC; + +public class CalcState extends AbstractStatus { + + public CalcState(Scanner scanner, Map pricePerKgMap, Map pricePerKmMap) { + this.scanner = scanner; + this.pricePerKgMap = pricePerKgMap; + this.pricePerKmMap = pricePerKmMap; + } + + @Override + public Status handle() { + + try { + BigDecimal pricePerKg = new PriceSelector().selectPriceValue(pricePerKgMap, userData.get(0)); + BigDecimal pricePerKm = new PriceSelector().selectPriceValue(pricePerKmMap, userData.get(1)); + BigDecimal result = new CalcImpl().handle(pricePerKg, userData.get(0), pricePerKm, userData.get(1)); + System.out.println("Result: " + result); + } finally { + userData.clear(); + } + + return new ReadDataState(scanner, pricePerKgMap, pricePerKmMap); + } + + @Override + public StatusType getType() { + return CALC; + } +} diff --git a/src/main/java/com/epam/brest/model/ExitState.java b/src/main/java/com/epam/brest/model/ExitState.java new file mode 100644 index 0000000..98018d0 --- /dev/null +++ b/src/main/java/com/epam/brest/model/ExitState.java @@ -0,0 +1,16 @@ +package com.epam.brest.model; + +import static com.epam.brest.model.StatusType.*; + +public class ExitState implements Status { + + @Override + public Status handle() { + return null; + } + + @Override + public StatusType getType() { + return EXIT; + } +} diff --git a/src/main/java/com/epam/brest/model/ReadDataState.java b/src/main/java/com/epam/brest/model/ReadDataState.java new file mode 100644 index 0000000..1c6f28f --- /dev/null +++ b/src/main/java/com/epam/brest/model/ReadDataState.java @@ -0,0 +1,49 @@ +package com.epam.brest.model; + +import java.math.BigDecimal; +import java.util.Map; +import java.util.Scanner; + +import static com.epam.brest.model.StatusType.READ_DATA; + +public class ReadDataState extends AbstractStatus { + + public static final int NUMBER_OF_USER_DATA = 2; + + public ReadDataState(Scanner scanner, Map pricePerKgMap, Map pricePerKmMap) { + this.scanner = scanner; + this.pricePerKgMap = pricePerKgMap; + this.pricePerKmMap = pricePerKmMap; + } + + @Override + public Status handle() { + if (userData.size() < NUMBER_OF_USER_DATA) { + System.out.println(messages.get(userData.size())); + String inputValue = scanner.next(); + if (inputValue.equalsIgnoreCase("q")) { + return new ExitState(); + } else if (isCorrectValue(inputValue)) { + userData.add(new BigDecimal(inputValue)); + } + } else { + return new CalcState(scanner, pricePerKgMap, pricePerKmMap); + } + return this; + } + + private boolean isCorrectValue(String inputValue) { + try { + BigDecimal enteredValue = new BigDecimal(inputValue); + return enteredValue.doubleValue() > 0; + } catch (NumberFormatException nfe) { + System.out.println("Incorrect value: " + inputValue); + } + return false; + } + + @Override + public StatusType getType() { + return READ_DATA; + } +} diff --git a/src/main/java/com/epam/brest/model/Status.java b/src/main/java/com/epam/brest/model/Status.java new file mode 100644 index 0000000..08ec672 --- /dev/null +++ b/src/main/java/com/epam/brest/model/Status.java @@ -0,0 +1,17 @@ +package com.epam.brest.model; + +import java.math.BigDecimal; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + +public interface Status { + + List messages = new ArrayList<>(Arrays.asList("Please enter weight:", "Please enter distance:")); + List userData = new ArrayList<>(); + + Status handle(); + + StatusType getType(); + +} diff --git a/src/main/java/com/epam/brest/model/StatusType.java b/src/main/java/com/epam/brest/model/StatusType.java new file mode 100644 index 0000000..1b4911c --- /dev/null +++ b/src/main/java/com/epam/brest/model/StatusType.java @@ -0,0 +1,5 @@ +package com.epam.brest.model; + +public enum StatusType { + READ_DATA, CALC, EXIT +} diff --git a/src/main/java/com/epam/brest/selector/PriceSelector.java b/src/main/java/com/epam/brest/selector/PriceSelector.java new file mode 100644 index 0000000..2a0368c --- /dev/null +++ b/src/main/java/com/epam/brest/selector/PriceSelector.java @@ -0,0 +1,24 @@ +package com.epam.brest.selector; + +import java.math.BigDecimal; +import java.util.Map; +import java.util.SortedSet; +import java.util.TreeSet; + +public class PriceSelector { + + public BigDecimal selectPriceValue(Map valueMap, BigDecimal targetValue) { + SortedSet sortedKeys = new TreeSet<>(valueMap.keySet()); + Integer resultKey = sortedKeys.first(); + for (Integer key : sortedKeys) { + if (resultKey >= targetValue.doubleValue()) { + break; + } + resultKey = key; + } + + BigDecimal resultValue = valueMap.get(resultKey); + System.out.println("Value: " + targetValue + " -> " + resultValue); + return resultValue; + } +} diff --git a/src/main/resources/config.properties b/src/main/resources/config.properties new file mode 100644 index 0000000..9968269 --- /dev/null +++ b/src/main/resources/config.properties @@ -0,0 +1,7 @@ +pricePerKg_10=1 +pricePerKg_20=2 +pricePerKg_default=3 + +pricePerKm_10=4 +pricePerKm_20=5 +pricePerKm_default=6 \ No newline at end of file diff --git a/src/main/resources/price_distance.csv b/src/main/resources/price_distance.csv new file mode 100644 index 0000000..561331c --- /dev/null +++ b/src/main/resources/price_distance.csv @@ -0,0 +1,4 @@ +100,100 +200,190 +500,150 +1000,100 \ No newline at end of file diff --git a/src/main/resources/price_weight.csv b/src/main/resources/price_weight.csv new file mode 100644 index 0000000..9f5b87d --- /dev/null +++ b/src/main/resources/price_weight.csv @@ -0,0 +1,3 @@ +1000,10 +2000,20 +3000,300 \ No newline at end of file diff --git a/src/main/resources/spring-config.xml b/src/main/resources/spring-config.xml new file mode 100644 index 0000000..d825d68 --- /dev/null +++ b/src/main/resources/spring-config.xml @@ -0,0 +1,10 @@ + + + + + + + \ No newline at end of file diff --git a/src/test/java/com/epam/brest/calc/CalcImplTest.java b/src/test/java/com/epam/brest/calc/CalcImplTest.java new file mode 100644 index 0000000..d53c397 --- /dev/null +++ b/src/test/java/com/epam/brest/calc/CalcImplTest.java @@ -0,0 +1,30 @@ +package com.epam.brest.calc; + +import org.junit.jupiter.api.Test; + +import java.math.BigDecimal; + +import static org.junit.jupiter.api.Assertions.*; + +class CalcImplTest { + + Calc calc = new CalcImpl(); + + @Test + void testHandleMethod() { + BigDecimal weight = new BigDecimal("10"); + BigDecimal weightPrice = new BigDecimal("10"); + BigDecimal length = new BigDecimal("10"); + BigDecimal lengthPrice = new BigDecimal("10"); + + assertNotNull(calc); + assertEquals(new BigDecimal(200), calc.handle(weight, weightPrice, length, lengthPrice)); + } + + @Test + void testHandleMethodWithNullParameters() { + assertNotNull(calc); + assertThrows(IllegalArgumentException.class, () -> + calc.handle(null, null, null, null)); + } +} \ No newline at end of file