2ое задание

mail@pastecode.io avatar
unknown
java
a year ago
4.0 kB
1
Indexable
Never
import java.io.FileReader;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import com.opencsv.CSVReader;

public class CsvParserExample {

    public static void main(String[] args) {
        String csvFile = "movementList.csv";
        List<Transaction> transactions = parseCsvFile(csvFile);

        System.out.println("Total expenses: " + getExpenseSum(transactions));
        System.out.println("Total income: " + getIncomeSum(transactions));

        List<String> expensesByOrganization = getListOfExpenses(transactions);
        System.out.println("Expenses by organization:");
        for (String expense : expensesByOrganization) {
            System.out.println(expense);
        }
    }

    private static List<Transaction> parseCsvFile(String csvFile) {
        List<Transaction> transactions = new ArrayList<>();
        try (CSVReader reader = new CSVReader(new FileReader(csvFile))) {
            String[] line;
            reader.readNext();
            while ((line = reader.readNext()) != null) {
                Transaction transaction = new Transaction();
                transaction.setAccountType(line[0]);
                transaction.setAccountNumber(line[1]);
                transaction.setCurrency(line[2]);
                transaction.setOperationDate(line[3]);
                transaction.setReference(line[4]);
                transaction.setDescription(line[5]);
                transaction.setIncome(new BigDecimal(line[6]));
                transaction.setExpense(new BigDecimal(line[7]));
                transactions.add(transaction);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return transactions;
    }

    private static BigDecimal getExpenseSum(List<Transaction> transactions) {
        BigDecimal sum = BigDecimal.ZERO;
        for (Transaction transaction : transactions) {
            sum = sum.add(transaction.getExpense());
        }
        return sum;
    }

    private static BigDecimal getIncomeSum(List<Transaction> transactions) {
        BigDecimal sum = BigDecimal.ZERO;
        for (Transaction transaction : transactions) {
            sum = sum.add(transaction.getIncome());
        }
        return sum;
    }

    private static List<String> getListOfExpenses(List<Transaction> transactions) {
        List<String> expensesByOrganization = new ArrayList<>();
        for (Transaction transaction : transactions) {
            if (transaction.getExpense().compareTo(BigDecimal.ZERO) > 0) {
                String expenseStr = transaction.getAccountNumber() + ": " + transaction.getExpense();
                expensesByOrganization.add(expenseStr);
            }
        }
        return expensesByOrganization;
    }

    private static class Transaction {
        private String accountType;
        private String accountNumber;
        private String currency;
        private String operationDate;
        private String reference;
        private String description;
        private BigDecimal income;
        private BigDecimal expense;


        public String getAccountType() {
            return accountType;
        }

        public void setAccountType(String accountType) {
            this.accountType = accountType;
        }

        public String getAccountNumber() {
            return accountNumber;
        }

        public void setAccountNumber(String accountNumber) {
            this.accountNumber = accountNumber;
        }

        public String getCurrency() {
            return currency;
        }

        public void setCurrency(String currency) {
            this.currency = currency;
        }

        public String getOperationDate() {
            return operationDate;
        }

        public void setOperationDate(String operationDate) {
            this.operationDate = operationDate;