/
spirzen
/
it-code-examples
Обзор
Документация
Войти
/
spirzen
/
it-code-examples
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
Аналитика
Безопасность
master
examples/java/java-503-18-037/main.java
55 строк
1 KB
Spirzen
OOP
09 июн 2026, 20:58
09 июн 2026, 20:58
1dca232
Код
Авторство
О чём код?
import java.util.ArrayList; class Product { String name; int price; Product(String name, int price) { this.name = name; this.price = price; } } class Cart { ArrayList<Product> items = new ArrayList<>(); void add(Product product) { items.add(product); System.out.println("В корзину добавлено: " + product.name + " (" + product.price + " ₽)"); } int total() { int sum = 0; for (Product p : items) sum += p.price; return sum; } } class Order { ArrayList<Product> items; int total; Order(Cart cart) { this.items = new ArrayList<>(cart.items); this.total = cart.total(); } void checkout() { System.out.println("Оформление заказа..."); for (Product item : items) { System.out.println(" — " + item.name + ": " + item.price + " ₽"); } System.out.println("Итого: " + total + " ₽"); System.out.println("Заказ оформлен!"); } } public class Main { public static void main(String[] args) { Cart cart = new Cart(); cart.add(new Product("Книга", 500)); cart.add(new Product("Ручка", 50)); Order order = new Order(cart); order.checkout(); } }