/
Kmaxim
/
DZ
Обзор
Документация
Войти
/
Kmaxim
/
DZ
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
Аналитика
Безопасность
master
ShoppingCart.java
43 строки
2 KB
Kmaxim
shopping_cart
16 фев 2025, 20:18
16 фев 2025, 20:18
e8f205c
Код
Авторство
О чём код?
//TIP To <b>Run</b> code, press <shortcut actionId="Run"/> or // click the <icon src="AllIcons.Actions.Execute"/> icon in the gutter. import java.util.Scanner; public class ShoppingCart { public static void main(String[] args) { String[] products = {"Хлеб", "Яблоки", "Молоко"}; int[] prices = {100, 200, 300}; int[] cart = new int[products.length]; int sumProducts = 0; Scanner scanner = new Scanner(System.in); System.out.println("Список возможных товаров для покупки:"); for (int i = 0; i < products.length; i++) { System.out.println((i + 1) + ". " + products[i] + " " + prices[i] + " руб/шт"); } while (true) { System.out.println("Выберите товар и количество или введите `end`"); String input = scanner.nextLine(); if (input.equals("end")) { break; } String[] parts = input.split(" "); int productNumber = Integer.parseInt(parts[0]) - 1; int productCount = Integer.parseInt(parts[1]); cart[productNumber] += productCount; sumProducts += prices[productNumber] * productCount; } System.out.println("Ваша корзина:"); for (int i = 0; i < products.length; i++) { if (cart[i] > 0) { System.out.println(products[i] + " " + cart[i] + " шт " + prices[i] + " руб/шт " + (cart[i] * prices[i]) + " руб в сумме"); } } System.out.println("Итого " + sumProducts + " руб"); scanner.close(); } }