import java.util.ArrayList;
import java.util.List;
class Product {
String name;
int price;
int num;
public Product(String name, int price, int num) {
this.name = name;
this.price = price;
this.num = num;
}
public int getPrice() {
return price;
}
public int getNum() {
return num;
}
public void setNum(int num) {
this.num = num;
}
}
class ShoppingCart {
int total;
List<Product> products;
public ShoppingCart() {
this.total = 0;
this.products = new ArrayList<>();
}
public void add(Product product) {
products.add(product);
total += product.getPrice();
product.setNum(product.getNum() - 1);
}
public void remove(Product product) {
products.remove(product);
total -= product.getPrice();
product.setNum(product.getNum() + 1);
}
public void displayCart() {
System.out.println("购物车中的商品信息:");
for (Product product : products) {
System.out.println("名称:" + product.name + ",价格:" + product.price);
}
System.out.println("总金额:" + total);
}
}
public class Main {
public static void main(String[] args) {
Product product1 = new Product("苹果", 5, 10);
Product product2 = new Product("香蕉", 3, 5);
ShoppingCart cart = new ShoppingCart();
cart.add(product1);
cart.add(product2);
cart.displayCart();
cart.remove(product1);
cart.displayCart();
}
}