/
FreddDEAGLE
/
sds
Обзор
Документация
Войти
/
FreddDEAGLE
/
sds
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
master
GroceryBasket.java
57 строк
2 KB
FreddDEAGLE
upload files
21 апр 2025, 20:51
21 апр 2025, 20:51
8d42ace
Код
Авторство
О чём код?
import java.util.Scanner; public class GroceryBasket { public static void main(String[] args) { String[] products = {"Хлеб", "Яблоки", "Молоко"}; int[] prices = {100, 200, 300}; int[] counters = new int[products.length]; showMenu(products, prices); dialogCustomer(counters); showCart(products, prices, counters); } private static void showCart(String[] products, int[] prices, int[] counters) { System.out.println("\nВаша корзина:"); int total = 0; for (int i = 0; i < products.length; i++) { if (counters[i] > 0) { int currentPrice = prices[i] * counters[i]; total += currentPrice; System.out.printf("%s %d шт %d руб/шт %d р в сумме\n", products[i], counters[i], prices[i], currentPrice); } } System.out.printf("Итого %d руб\n", total); } private static void dialogCustomer(int[] counters) { Scanner sc = new Scanner(System.in); while (true) { System.out.println("Выберите товар и количество или введите 'end'"); String answer = sc.nextLine(); if ("end".equals(answer)) { break; } String[] positionAndCounter = answer.split(" "); int position = Integer.parseInt(positionAndCounter[0]) - 1; int counter = Integer.parseInt(positionAndCounter[1]) - 1; counters[position] += counter; } } private static void showMenu(String[] products, int[] prices) { System.out.println("Cписок товара для покупок"); for (int i = 0; i < products.length; i++) { System.out.printf("%d. %s %d руб/шт\n", i + 1, products[i], prices[i]); } System.out.println(" "); } }