import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class Shopping {
public static void main(String[] args) {
File file = new File("products.txt");
double total = 0;
int count = 0;
System.out.println("===== СПИСОК ПОКУПОК =====");
try (Scanner scanner = new Scanner(file)) {
while (scanner.hasNextLine()) {
String line = scanner.nextLine().trim();
if (line.isEmpty()) continue;
int lastSpace = line.lastIndexOf(' ');
if (lastSpace == -1) continue;
String name = line.substring(0, lastSpace);
String priceText = line.substring(lastSpace + 1);
double price = Double.parseDouble(priceText);
total += price;
count++;
System.out.println(name + " — " + price + " руб.");
}
} catch (FileNotFoundException e) {
System.out.println("Файл products.txt не найден");
return;
} catch (NumberFormatException e) {
System.out.println("Ошибка в цене");
return;
}
if (count == 0) {
System.out.println("Файл пустой");
return;
}
double average = total / count;
double vat = total * 0.20;
double withVat = total + vat;
System.out.println("-------------------------");
System.out.println("Товаров: " + count);
System.out.printf("Итого: %.2f руб.%n", total);
System.out.printf("Средняя цена: %.2f руб.%n", average);
System.out.printf("НДС 20%%: %.2f руб.%n", vat);
System.out.printf("К оплате с НДС: %.2f руб.%n", withVat);
}
}