购物系统主要包括以下几个部分:
1、商品类(Product)
2、购物车类(ShoppingCart)
3、用户类(User)
4、订单类(Order)
5、主程序(Main)
下面是详细的代码实现:
// 1. 商品类(Product) class Product { private String id; private String name; private double price; public Product(String id, String name, double price) { this.id = id; this.name = name; this.price = price; } public String getId() { return id; } public String getName() { return name; } public double getPrice() { return price; } } // 2. 购物车类(ShoppingCart) class ShoppingCart { private Map<Product, Integer> cartItems; public ShoppingCart() { cartItems = new HashMap<>(); } public void addItem(Product product, int quantity) { cartItems.put(product, quantity); } public void removeItem(Product product) { cartItems.remove(product); } public double getTotalPrice() { double totalPrice = 0; for (Map.Entry<Product, Integer> entry : cartItems.entrySet()) { totalPrice += entry.getKey().getPrice() * entry.getValue(); } return totalPrice; } } // 3. 用户类(User) class User { private String id; private String name; private ShoppingCart shoppingCart; public User(String id, String name) { this.id = id; this.name = name; this.shoppingCart = new ShoppingCart(); } public String getId() { return id; } public String getName() { return name; } public ShoppingCart getShoppingCart() { return shoppingCart; } } // 4. 订单类(Order) class Order { private String id; private User user; private List<Product> products; private double totalPrice; public Order(String id, User user, List<Product> products) { this.id = id; this.user = user; this.products = products; this.totalPrice = calculateTotalPrice(); } private double calculateTotalPrice() { double totalPrice = 0; for (Product product : products) { totalPrice += product.getPrice(); } return totalPrice; } public String getId() { return id; } public User getUser() { return user; } public List<Product> getProducts() { return products; } public double getTotalPrice() { return totalPrice; } } // 5. 主程序(Main) public class Main { public static void main(String[] args) { // 创建商品 Product product1 = new Product("1", "商品1", 100); Product product2 = new Product("2", "商品2", 200); Product product3 = new Product("3", "商品3", 300); // 创建用户 User user = new User("1", "张三"); // 添加商品到购物车 user.getShoppingCart().addItem(product1, 1); user.getShoppingCart().addItem(product2, 2); user.getShoppingCart().addItem(product3, 3); // 创建订单 List<Product> orderProducts = new ArrayList<>(user.getShoppingCart().cartItems.keySet()); Order order = new Order("1", user, orderProducts); // 输出订单信息 System.out.println("订单ID:" + order.getId()); System.out.println("用户名:" + order.getUser().getName()); System.out.println("订单总价:" + order.getTotalPrice()); System.out.println("购买的商品:"); for (Product product : order.getProducts()) { System.out.println("商品名称:" + product.getName() + ",价格:" + product.getPrice()); } } }
这个购物系统实现了商品、购物车、用户、订单等基本功能,用户可以将商品添加到购物车,然后创建一个订单,订单会计算购物车中所有商品的总价。
原创文章,作者:未希,如若转载,请注明出处:https://www.kdun.com/ask/641561.html
本网站发布或转载的文章及图片均来自网络,其原创性以及文中表达的观点和判断不代表本网站。如有问题,请联系客服处理。
发表回复