Загрузка данных


import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        File inputFile = new File("C:\\Users\\USER1\\shopitem\\items.txt");

        ArrayList<String> names = new ArrayList<>();
        ArrayList<Float> prices = new ArrayList<>();

         try {
            Scanner scanner = new Scanner(inputFile);
            
            while (scanner.hasNextLine()) {
                String line = scanner.nextLine(); //.trim()
                if (line == null) continue;

                String[] parts = line.split(" ");


                if (parts.length < 2) continue;

                String priceStr = parts[parts.length - 1].replace(',', '.');
                float price = Float.parseFloat(priceStr);

                String name = "";
                for (int i = 0; i < parts.length - 1; i++) {
                    name += parts[i] + " ";
                }
                name = name.trim(); 

                names.add(name);
                prices.add(price);
            }
            scanner.close();
        } catch (FileNotFoundException e) {
            System.out.println("products.txt не найден");
            return;
        }
    
        if (names.isEmpty()) {
            System.out.println("список пустой");
            return;
        }

        float total = 0;
        float maxPrice = prices.get(0);
        float minPrice = prices.get(0);
        String maxName = names.get(0);
        String minName = names.get(0);

        for (int i = 0; i < prices.size(); i++) {
            float currentPrice = prices.get(i);
            total += currentPrice;

            if (currentPrice > maxPrice) {
                maxPrice = currentPrice;
                maxName = names.get(i);
            }
            if (currentPrice < minPrice) {
                minPrice = currentPrice;
                minName = names.get(i);
            }
        }

        float average = total / prices.size();                
        float finalTotal = total;
        float vat = finalTotal * 0.20f; // НДС 20%

            System.out.println("Список товаров");

            for (int i = 0; i < names.size(); i++) {
                String itemLine = String.format("%d. %-25s %.2f руб.", (i + 1), names.get(i), prices.get(i));
                System.out.println(itemLine);           
            }

            String list = "----------------------------------\n" +
                    String.format("Всего позиций:           %d шт.\n", names.size()) +                                  
                    String.format("ИТОГО К ОПЛАТЕ:          %.2f руб.\n", finalTotal) +
                    String.format("В т.ч. НДС (20%%):        %.2f руб.\n", vat) +
                    "----------------------------------\n" +
                    String.format("Средняя цена за товар:   %.2f руб.\n", average) +
                    String.format("Самый дорогой:           %s (%.2f руб.)\n", maxName, maxPrice) +
                    String.format("Самый дешевый:           %s (%.2f руб.)\n", minName, minPrice) +
                    "==================================";

            System.out.println(list);
        }
}