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

Тестовое задание КРОК [Попов Вячеслав] #34

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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
8 changes: 8 additions & 0 deletions .idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 15 additions & 0 deletions .idea/artifacts/TestTaskCrock_jar.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 13 additions & 0 deletions .idea/compiler.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions .idea/encodings.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 20 additions & 0 deletions .idea/jarRepositories.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 13 additions & 0 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions META-INF/MANIFEST.MF
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Manifest-Version: 1.0
Main-Class: Main

19 changes: 19 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,26 @@
4. Найденный в соответствии с условием задачи месяц должен выводиться на английском языке в нижнем регистре. Если месяцев несколько, то на вывод они все подаются на английском языке в нижнем регистре в порядке их следования в течение года.

## Автор решения
Попов Вячеслав Игоревич

telegram: @Lava210

mail: [email protected]

## Описание реализации


1. В Функцию getReport передается путь до файла, затем запускается функция Deserialize, которая считывает данные
и заполняет ими List<InputData>, который в свою очередь и возвращает
2. После того как мы получили List от функции Deserialize мы проходимся по каждому элементу и ищем в каких экземплярах
status = completed. Если мы находим такой, то добавляем месяц оформления заказа как ключ в массив, а в значение пррибавляем
стоимость
3. Проходим по массиву, где выбираем самые прибыльные месяца, которые в дальнейшем преобразуем в строку.
Затем идет возврат этой строки.


## Инструкция по сборке и запуску решения
1. Зайти в дирректорию проекта (school2024-test-task1)
запустить эту команду в терминале java -jar out/artifacts/TestTaskCrock_jar/TestTaskCrock.jar
2. Запустить Main файл в IDEA.

38 changes: 38 additions & 0 deletions input.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
[
{
"user_id": "3acfb0b7-04bd-4978-be4c-3929372277c1",
"ordered_at": "2023-01-16T13:56:39.492",
"status": "COMPLETED",
"total": "1917.00"
},
{
"user_id": "25b003b9-ab22-4a24-a616-dd0303f983d8",
"ordered_at": "2023-03-05T08:34:21.123",
"status": "COMPLETED",
"total": "13990.00"
},
{
"user_id": "e1470ada-fcbb-4424-8c46-065b6409ca4b",
"ordered_at": "2016-03-16T13:56:39.492",
"status": "COMPLETED",
"total": "215.50"
},
{
"user_id": "081a47a5-b7bf-462c-a11a-68002a179152",
"ordered_at": "2023-12-08T21:36:59.281",
"status": "COMPLETED",
"total": "49499.00"
},
{
"user_id": "0999c6aa-1bac-4ded-9a54-92fff4f34d69",
"ordered_at": "2023-12-14T11:10:29.408",
"status": "CANCELED",
"total": "13650.00"
},
{
"user_id": "0999c6aa-1bac-4ded-9a54-92fff4f34d69",
"ordered_at": "2023-12-14T11:15:31.108",
"status": "COMPLETED",
"total": "14760.00"
}
]
Binary file not shown.
53 changes: 53 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>CrockTest</groupId>
<artifactId>TestTaskCrock</artifactId>
<version>1.0-SNAPSHOT</version>

<properties>
<maven.compiler.source>21</maven.compiler.source>
<maven.compiler.target>21</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<dependencies>
<!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.17.0</version>
</dependency>

</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>3.7.1</version>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<archive>
<manifest>
<mainClass>Main</mainClass>
</manifest>
</archive>
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
7 changes: 7 additions & 0 deletions src/main/java/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import java.util.Arrays;
public class Main {
public static void main(String[] args) {
Solution sl = new Solution();
System.out.println(sl.GetReport("input.json"));
}
}
92 changes: 92 additions & 0 deletions src/main/java/Solution.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
import com.fasterxml.jackson.databind.ObjectMapper;
import java.io.File;
import java.io.IOException;
import java.time.LocalDateTime;
import java.time.Month;
import java.time.format.DateTimeFormatter;
import java.util.*;

public class Solution {

private HashMap<Integer, Float> map = new HashMap<>();

//Главный метод, который возвращает отчет в виде json строки
public String GetReport(String path){
InputData[] input = Deserialize(path);
List<Integer> months = new ArrayList<>(12);

//Заполнение HashMap
// key = month , value = total
Integer month;
for (InputData obj: input){
if (obj.status.equals("COMPLETED")){
month = LocalDateTime.parse(obj.ordered_at,
DateTimeFormatter.ISO_LOCAL_DATE_TIME).getMonthValue();
if (map.containsKey(month)){
map.put(month, map.get(month)+Float.parseFloat(obj.total));
} else {
map.put(month, Float.parseFloat(obj.total));
}
}
}

//Поиск маскимальных значений в HashMap
Integer maxKey = 0;
for (Integer key :map.keySet()){
if (maxKey == 0){
maxKey = key;
} else {
if (map.get(key) > map.get(maxKey)) {
maxKey = key;
}
}
}
months.add(maxKey);
for (Integer key : map.keySet()){
if(key != maxKey){
if (map.get(key) - map.get(maxKey) == 0){
months.add(key);
}
}
}

//Сборка итоговой строки
Collections.sort(months);
String output = "{\"months\": [";
int i = 0;
for(int number : months){
i++;
output += "«" + Month.of(number).toString().toLowerCase() + "»";
if (i!=months.size()){
output +=", ";
}
}
output+="]}";

return output;
}



//Read from Json
private InputData[] Deserialize(String path){
ObjectMapper objectMapper = new ObjectMapper();
InputData[] inputList;
try {
inputList = objectMapper.readValue(new File(path), InputData[].class);
return inputList;
} catch (IOException e) {
e.printStackTrace();
System.out.println("Error in read file");
return null;
}

}
}
class InputData {
public String user_id;
public String ordered_at;
public String status;
public String total;
}

Binary file added target/classes/InputData.class
Binary file not shown.
Binary file added target/classes/Main.class
Binary file not shown.
Binary file added target/classes/Solution.class
Binary file not shown.