Skip to content

Commit 297dd21

Browse files
committed
Add Module 9
1 parent 22425c0 commit 297dd21

File tree

72 files changed

+44213
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

72 files changed

+44213
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#### 9.9. Домашняя работа 9.3
2+
3+
**Цель задания**
4+
5+
Научиться читать файл CSV и анализировать его.
6+
7+
**Что нужно сделать**
8+
9+
1. Создать новый проект, который будет читать файл csv банковской выписки movementsList.csv и парсить полученные строки. Путь к файлу выписки храните в константе.
10+
2. Код должен выводить сводную информацию по этой выписке: общий приход, общий расход и разбивку расходов.
11+
3. Примеры работы программы
12+
13+
Сумма расходов: 398 563.39 руб.
14+
Сумма доходов: 289 890.06 руб.
15+
16+
Суммы расходов по организациям:
17+
RUSMOSKVA56 SHLOVE REPUBLIC 1 081.53 руб.
18+
RUSMOSCOW42 SHCL ETOILE 126.34 руб.
19+
RUSPUSHKINO105ZOOMAGAZIN 4 217.65 руб.
20+
21+
**Критерии оценки**
22+
23+
«Зачёт» — после запуска программы в консоль выводятся суммы и расходы по организациям.
24+
«Незачёт» — задание не выполнено.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
7+
<groupId>ru.radmitr</groupId>
8+
<artifactId>bank-statement</artifactId>
9+
<version>1.0-SNAPSHOT</version>
10+
11+
<properties>
12+
<!-- compiler -->
13+
<maven.compiler.source>17</maven.compiler.source>
14+
<maven.compiler.target>17</maven.compiler.target>
15+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
16+
<!-- dependencies -->
17+
<junit.version>5.9.3</junit.version>
18+
<log4j.version>2.20.0</log4j.version>
19+
<lombok.version>1.18.28</lombok.version>
20+
<commons.csv.version>1.10.0</commons.csv.version>
21+
<!-- plugins -->
22+
<compiler.plugin.version>3.11.0</compiler.plugin.version>
23+
<surefire.plugin.version>3.1.2</surefire.plugin.version>
24+
<assembly.plugin.version>3.6.0</assembly.plugin.version>
25+
</properties>
26+
27+
<dependencies>
28+
<!-- https://mvnrepository.com/artifact/org.junit.jupiter/junit-jupiter-engine -->
29+
<dependency>
30+
<groupId>org.junit.jupiter</groupId>
31+
<artifactId>junit-jupiter-engine</artifactId>
32+
<version>${junit.version}</version>
33+
<scope>test</scope>
34+
</dependency>
35+
<!-- https://mvnrepository.com/artifact/org.junit.jupiter/junit-jupiter-api -->
36+
<dependency>
37+
<groupId>org.junit.jupiter</groupId>
38+
<artifactId>junit-jupiter-api</artifactId>
39+
<version>${junit.version}</version>
40+
<scope>test</scope>
41+
</dependency>
42+
<!-- https://mvnrepository.com/artifact/org.apache.logging.log4j/log4j-core -->
43+
<dependency>
44+
<groupId>org.apache.logging.log4j</groupId>
45+
<artifactId>log4j-core</artifactId>
46+
<version>${log4j.version}</version>
47+
</dependency>
48+
<!-- https://mvnrepository.com/artifact/org.projectlombok/lombok -->
49+
<dependency>
50+
<groupId>org.projectlombok</groupId>
51+
<artifactId>lombok</artifactId>
52+
<version>${lombok.version}</version>
53+
<scope>provided</scope>
54+
</dependency>
55+
<!-- https://mvnrepository.com/artifact/org.apache.commons/commons-csv -->
56+
<dependency>
57+
<groupId>org.apache.commons</groupId>
58+
<artifactId>commons-csv</artifactId>
59+
<version>${commons.csv.version}</version>
60+
</dependency>
61+
</dependencies>
62+
63+
<build>
64+
<plugins>
65+
<!-- https://mvnrepository.com/artifact/org.apache.maven.plugins/maven-compiler-plugin -->
66+
<plugin>
67+
<artifactId>maven-compiler-plugin</artifactId>
68+
<version>${compiler.plugin.version}</version>
69+
</plugin>
70+
<!-- https://mvnrepository.com/artifact/org.apache.maven.plugins/maven-surefire-plugin -->
71+
<plugin>
72+
<artifactId>maven-surefire-plugin</artifactId>
73+
<version>${surefire.plugin.version}</version>
74+
</plugin>
75+
<!-- https://mvnrepository.com/artifact/org.apache.maven.plugins/maven-assembly-plugin -->
76+
<plugin>
77+
<artifactId>maven-assembly-plugin</artifactId>
78+
<version>${assembly.plugin.version}</version>
79+
<executions>
80+
<execution>
81+
<phase>package</phase>
82+
<goals>
83+
<goal>single</goal>
84+
</goals>
85+
</execution>
86+
</executions>
87+
<configuration>
88+
<archive>
89+
<manifest>
90+
<addClasspath>true</addClasspath>
91+
<mainClass>Main</mainClass>
92+
</manifest>
93+
</archive>
94+
<descriptorRefs>
95+
<descriptorRef>jar-with-dependencies</descriptorRef>
96+
</descriptorRefs>
97+
</configuration>
98+
</plugin>
99+
</plugins>
100+
</build>
101+
102+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
package ru.radmitr;
2+
3+
import org.apache.commons.csv.CSVFormat;
4+
import org.apache.commons.csv.CSVRecord;
5+
import ru.radmitr.pojo.BankAccount;
6+
import ru.radmitr.pojo.Transaction;
7+
8+
import java.io.BufferedReader;
9+
import java.io.FileReader;
10+
import java.io.IOException;
11+
import java.time.LocalDate;
12+
import java.time.format.DateTimeFormatter;
13+
import java.util.ArrayList;
14+
import java.util.List;
15+
import java.util.TreeMap;
16+
import java.util.stream.Collectors;
17+
18+
import static ru.radmitr.Utility.*;
19+
20+
public class MainUsingCommonsCsv {
21+
22+
private static final String CSV_FILE_PATH = "src/main/resources/movementList.csv";
23+
private static final DateTimeFormatter CSV_DATE_FORMATTER = DateTimeFormatter.ofPattern("dd.MM.yy");
24+
25+
public static void main(String[] args) throws IOException {
26+
List<Transaction> transactions = loadTransactionsFromFile(CSV_FILE_PATH);
27+
28+
System.out.println("Сумма доходов: " + transactions.stream()
29+
.mapToDouble(Transaction::getIncome).sum());
30+
System.out.println("Сумма расходов: " + transactions.stream()
31+
.mapToDouble(Transaction::getExpense).sum());
32+
System.out.println("--------------------------------------------------------");
33+
34+
System.out.println("Сумма доходов по организациям: ");
35+
transactions.stream()
36+
.collect(Collectors.groupingBy(Transaction::getRecipient,
37+
TreeMap::new, Collectors.toSet()))
38+
.entrySet().stream()
39+
.forEach(group -> {
40+
double sum = group.getValue().stream().mapToDouble(Transaction::getIncome).sum();
41+
if (sum == 0) {
42+
return;
43+
}
44+
System.out.format("\t %-30s %.2f руб.\n", group.getKey(), sum);
45+
});
46+
47+
System.out.println("\nСумма расходов по организациям: ");
48+
transactions.stream()
49+
.collect(Collectors.groupingBy(Transaction::getRecipient,
50+
TreeMap::new, Collectors.toSet()))
51+
.entrySet().stream()
52+
.forEach(group -> {
53+
double sum = group.getValue().stream().mapToDouble(Transaction::getExpense).sum();
54+
if (sum == 0) {
55+
return;
56+
}
57+
System.out.format("\t %-30s %.2f руб.\n", group.getKey(), sum);
58+
});
59+
}
60+
61+
/**
62+
* Load transactions from CSV file using commons CSV
63+
*/
64+
private static List<Transaction> loadTransactionsFromFile(String path) throws IOException {
65+
List<Transaction> tranList = new ArrayList<>();
66+
67+
BufferedReader br = null;
68+
try {
69+
br = new BufferedReader(new FileReader(path), 1000);
70+
Iterable<CSVRecord> csvRecords = CSVFormat.DEFAULT.builder()
71+
.setHeader().setSkipHeaderRecord(true).build().parse(br);
72+
73+
for (CSVRecord record : csvRecords) {
74+
tranList.add(new Transaction(
75+
new BankAccount(record.get("Номер счета"), record.get("Тип счёта"), record.get("Валюта")),
76+
LocalDate.parse(record.get("Дата операции"), CSV_DATE_FORMATTER),
77+
record.get("Референс проводки"),
78+
record.get("Описание операции"),
79+
extractRecipientName(record.get("Описание операции")),
80+
getDouble(record.get("Приход")),
81+
getDouble(record.get("Расход"))
82+
)
83+
);
84+
}
85+
} catch (Exception e) {
86+
e.printStackTrace();
87+
} finally {
88+
br.close();
89+
}
90+
91+
return tranList;
92+
}
93+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
package ru.radmitr;
2+
3+
import ru.radmitr.pojo.BankAccount;
4+
import ru.radmitr.pojo.Transaction;
5+
6+
import java.nio.file.Files;
7+
import java.nio.file.Paths;
8+
import java.time.LocalDate;
9+
import java.time.format.DateTimeFormatter;
10+
import java.util.ArrayList;
11+
import java.util.List;
12+
import java.util.TreeSet;
13+
import java.util.stream.Collectors;
14+
import static ru.radmitr.Utility.*;
15+
16+
public class MainUsingCycles {
17+
18+
private static final String CSV_FILE_PATH = "src/main/resources/movementList.csv";
19+
private static final DateTimeFormatter CSV_DATE_FORMATTER = DateTimeFormatter.ofPattern("dd.MM.yy");
20+
21+
public static void main(String[] args) {
22+
ArrayList<Transaction> transactions = loadTransactionsFromFile(CSV_FILE_PATH);
23+
24+
System.out.println("Сумма доходов: " + transactions.stream()
25+
.mapToDouble(Transaction::getIncome).sum());
26+
System.out.println("Сумма расходов: " + transactions.stream()
27+
.mapToDouble(Transaction::getExpense).sum());
28+
System.out.println("--------------------------------------------------------");
29+
30+
TreeSet<String> recipients = transactions.stream()
31+
.map(Transaction::getRecipient)
32+
.collect(Collectors.toCollection(TreeSet::new));
33+
34+
System.out.println("Сумма доходов по организациям: ");
35+
for (String rec : recipients) {
36+
double sum = 0;
37+
for (Transaction tran : transactions) {
38+
if (tran.getRecipient().equals(rec) && (tran.getIncome() > 0)) {
39+
sum += tran.getIncome();
40+
}
41+
}
42+
if (sum > 0) {
43+
System.out.format("\t %-30s %.2f руб.\n", rec, sum);
44+
}
45+
}
46+
47+
System.out.println("\nСумма расходов по организациям: ");
48+
for (String rec : recipients) {
49+
double sum = 0;
50+
for (Transaction tran : transactions) {
51+
if (tran.getRecipient().equals(rec) && (tran.getExpense() > 0)) {
52+
sum += tran.getExpense();
53+
}
54+
}
55+
if (sum > 0) {
56+
System.out.format("\t %-30s %.2f руб.\n", rec, sum);
57+
}
58+
}
59+
}
60+
61+
/**
62+
* Load transactions from CSV file
63+
*/
64+
private static ArrayList<Transaction> loadTransactionsFromFile(String path) {
65+
ArrayList<Transaction> tranList = new ArrayList<>();
66+
try {
67+
List<String> lines = Files.readAllLines(Paths.get(path));
68+
lines.remove(0);
69+
for (String line : lines) {
70+
// in numbers, replace the comma with a dot and divide the string into columns by comma
71+
String[] columnArr = line.replaceAll("\"([\\d&&[^\"]]*),([\\d&&[^\"]]+)\"", "$1.$2")
72+
.split(",");
73+
if (columnArr.length != 8) {
74+
System.out.println("Wrong line: " + line);
75+
continue;
76+
}
77+
tranList.add(new Transaction(
78+
new BankAccount(columnArr[1], columnArr[0], columnArr[2]),
79+
LocalDate.parse(columnArr[3], CSV_DATE_FORMATTER),
80+
columnArr[4],
81+
columnArr[5],
82+
extractRecipientName(columnArr[5]),
83+
Double.parseDouble(columnArr[6]),
84+
Double.parseDouble(columnArr[7])
85+
)
86+
);
87+
}
88+
} catch (Exception e) {
89+
e.printStackTrace();
90+
}
91+
return tranList;
92+
}
93+
}

0 commit comments

Comments
 (0)