/
helga95
/
Products
Обзор
Документация
Войти
/
helga95
/
Products
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
Аналитика
Безопасность
master
Main.java
62 строки
2 KB
helga95
upload files
20 мар 2025, 21:11
20 мар 2025, 21:11
63ab781
Код
Авторство
О чём код?
import java.util.Arrays; import java.util.List; import java.util.Scanner; public class Main { public static void main(String[] args) { String[] products = {"Хлеб", "Яблоки", "Молоко"}; int[] prices = {100, 200, 300}; int[] counters = new int[products.length]; showMenu(products, prices); dialogWithCustomer(counters); calcResults(products, prices, counters); } private static void calcResults(String[] products, int[] prices, int[] counters) { int totalPrice = 0; System.out.println("Ваша корзина:"); for (int i = 0; i < products.length; i++) { if (counters[i] > 0) { int currentPrice = prices[i] * counters[i]; totalPrice += currentPrice; System.out.printf("%s %d шт %d руб/шт %d руб в сумме\n", products[i], counters[i], prices[i], currentPrice); } } System.out.printf("Итого %d руб\n", totalPrice); } private static void dialogWithCustomer(int[] counters) { Scanner sc = new Scanner(System.in); while (true) { System.out.println("Выберите товар и количество или введите `end`"); String answer = sc.nextLine(); if ("end".equals(answer)) { break; } String[] splitedAnswer = answer.split(" "); int position = Integer.parseInt(splitedAnswer[0]) - 1; int count = Integer.parseInt(splitedAnswer[1]); counters[position] += count; } } private static void showMenu(String[] products, int[] prices) { System.out.println("Список возможных товаров для покупки"); for (int i = 0; i < products.length; i++) { System.out.printf("%d.%s %d руб/шт\n", i + 1, products[i], prices[i]); } } }