Skip to content
Open
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
16 changes: 16 additions & 0 deletions documentation/html_template/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Hello</title>
</head>
<body>
<h1>Hello my friend!</h1>
<p>ab<b>ra</b><br/>
ka<strong>dab</strong>rak<br/>
11111&nbsp;kkjlf&copy;
<i>yyy<strike>yyyu</strike>uu</i>
<em>wuy<sup>eiu</sup>wbw</em>
df</p>
</body>
</html>
35 changes: 35 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,41 @@
<properties>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
<spring.version>5.3.12</spring.version>
</properties>

<dependencies>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>5.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>${spring.version}</version>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.0.0-M5</version>
</plugin>
</plugins>
</build>

</project>
42 changes: 25 additions & 17 deletions src/main/java/com/epam/brest/Main.java
Original file line number Diff line number Diff line change
@@ -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<Integer, BigDecimal> pricePerKgMap = fileReader.readData("price_weight.csv");
Map<Integer, BigDecimal> 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;
}
}

3 changes: 3 additions & 0 deletions src/main/java/com/epam/brest/calc/CalcImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
}
29 changes: 29 additions & 0 deletions src/main/java/com/epam/brest/files/CSVFileReader.java
Original file line number Diff line number Diff line change
@@ -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<Integer, BigDecimal> readData(String filePath) throws IOException {
Map<Integer, BigDecimal> 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;
}
}

10 changes: 10 additions & 0 deletions src/main/java/com/epam/brest/files/FileReader.java
Original file line number Diff line number Diff line change
@@ -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<Integer, BigDecimal> readData(String filePath) throws IOException;
}
13 changes: 13 additions & 0 deletions src/main/java/com/epam/brest/model/AbstractStatus.java
Original file line number Diff line number Diff line change
@@ -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<Integer, BigDecimal> pricePerKgMap;
Map<Integer, BigDecimal> pricePerKmMap;

}
39 changes: 39 additions & 0 deletions src/main/java/com/epam/brest/model/CalcState.java
Original file line number Diff line number Diff line change
@@ -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<Integer, BigDecimal> pricePerKgMap, Map<Integer, BigDecimal> 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;
}
}
16 changes: 16 additions & 0 deletions src/main/java/com/epam/brest/model/ExitState.java
Original file line number Diff line number Diff line change
@@ -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;
}
}
49 changes: 49 additions & 0 deletions src/main/java/com/epam/brest/model/ReadDataState.java
Original file line number Diff line number Diff line change
@@ -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<Integer, BigDecimal> pricePerKgMap, Map<Integer, BigDecimal> 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;
}
}
17 changes: 17 additions & 0 deletions src/main/java/com/epam/brest/model/Status.java
Original file line number Diff line number Diff line change
@@ -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<String> messages = new ArrayList<>(Arrays.asList("Please enter weight:", "Please enter distance:"));
List<BigDecimal> userData = new ArrayList<>();

Status handle();

StatusType getType();

}
5 changes: 5 additions & 0 deletions src/main/java/com/epam/brest/model/StatusType.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.epam.brest.model;

public enum StatusType {
READ_DATA, CALC, EXIT
}
24 changes: 24 additions & 0 deletions src/main/java/com/epam/brest/selector/PriceSelector.java
Original file line number Diff line number Diff line change
@@ -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<Integer, BigDecimal> valueMap, BigDecimal targetValue) {
SortedSet<Integer> 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;
}
}
7 changes: 7 additions & 0 deletions src/main/resources/config.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
pricePerKg_10=1
pricePerKg_20=2
pricePerKg_default=3

pricePerKm_10=4
pricePerKm_20=5
pricePerKm_default=6
4 changes: 4 additions & 0 deletions src/main/resources/price_distance.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
100,100
200,190
500,150
1000,100
3 changes: 3 additions & 0 deletions src/main/resources/price_weight.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
1000,10
2000,20
3000,300
10 changes: 10 additions & 0 deletions src/main/resources/spring-config.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd">

<bean name="fileReader" class="com.epam.brest.files.CSVFileReader"/>
<bean name="calc" class="com.epam.brest.calc.CalcImpl"/>

</beans>
Loading