import java.nio.file.*;
import java.util.*;
record Product(String name, double price) {}
public class ShoppingReport {
public static void main(String[] args) {
Locale.setDefault(Locale.US);
try {
List<Product> products = new ArrayList<>();
for (String line : Files.readAllLines(Path.of("shopping.txt"))) {
line = line.trim();
int i = line.lastIndexOf(' ');
if (i < 0) continue;
try {
products.add(new Product(
line.substring(0, i).trim(),
Double.parseDouble(line.substring(i + 1).trim())
));
} catch (NumberFormatException e) {
System.out.println("Ошибка: " + line);
}
}
if (products.isEmpty()) {
System.out.println("Список товаров пуст!");
return;
}
double total = products.stream()
.mapToDouble(Product::price).sum();
double vat = total * 0.20;
double average = total / products.size();
System.out.println("=== Отчёт о покупках ===");
System.out.printf("Всего товаров: %d%n", products.size());
System.out.printf("Общая сумма: %.2f руб.%n", total);
System.out.printf("НДС (20%%): %.2f руб.%n", vat);
System.out.printf("Итого с НДС: %.2f руб.%n", total + vat);
System.out.printf("Средняя цена: %.2f руб.%n", average);
System.out.println("\nДетализация:");
products.forEach(p ->
System.out.printf("- %s: %.2f руб.%n", p.name(), p.price())
);
} catch (Exception e) {
System.out.println("Ошибка чтения файла: " + e.getMessage());
}
}
}