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


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

public class ShoppingReceiptSimple {

    public static void main(String[] args) {
        File file = new File("products.txt");

        double totalSum = 0;
        int itemCount = 0;
        int over500Count = 0;

        try (Scanner scanner = new Scanner(file)) {
            System.out.println("=== СПИСОК ПОКУПОК ===");

            while (scanner.hasNextLine()) {
                String line = scanner.nextLine().trim();
                if (line.isEmpty()) continue;

                String[] words = line.split("\\s+");
                if (words.length < 2) continue;

                String priceText = words[words.length - 1].replace(',', '.');
                
                StringBuilder nameBuilder = new StringBuilder();
                for (int i = 0; i < words.length - 1; i++) {
                    nameBuilder.append(words[i]).append(" ");
                }
                String productName = nameBuilder.toString().trim();

                try {
                    double price = Double.parseDouble(priceText);
                    totalSum += price;
                    itemCount++;

                    if (price > 500) {
                        over500Count++;
                    }

                    System.out.printf("%d. %-32s | %8.2f руб.%n", itemCount, productName, price);
                } catch (NumberFormatException ignored) {
                }
            }

            if (itemCount > 0) {
                double averagePrice = totalSum / itemCount;
                double vat = totalSum * 20 / 120;
                double discount = totalSum >= 1500 ? totalSum * 0.05 : 0;
                double finalPrice = totalSum - discount;
                int bonusPoints = (int) (finalPrice * 0.03);

                System.out.println("\n=== КАССОВЫЙ ЧЕК ===");
                System.out.printf("Позиций в чеке:        %d шт.%n", itemCount);
                System.out.printf("Товаров дороже 500 р.: %d шт.%n", over500Count);
                System.out.printf("Средняя цена товара:   %.2f руб.%n", averagePrice);
                System.out.printf("Сумма до скидки:       %.2f руб.%n", totalSum);
                System.out.printf("Скидка (5%% от 1500р):  %.2f руб.%n", discount);
                System.out.printf("В т.ч. НДС (20%%):      %.2f руб.%n", vat);
                System.out.printf("ИТОГО К ОПЛАТЕ:        %.2f руб.%n", finalPrice);
                System.out.printf("Начислено бонусов:     %d балл(ов)%n", bonusPoints);
            } else {
                System.out.println("Чек пуст.");
            }

        } catch (FileNotFoundException e) {
            System.out.println("Файл products.txt не найден.");
        }
    }
}