云主机测评网云主机测评网云主机测评网

云主机测评网
www.yunzhuji.net

java编写购物系统

使用Java编写购物系统,可以实现商品展示、购物车管理、订单处理等功能,提高用户体验和购物效率。

购物系统主要包括以下几个部分:

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());
        }
    }
}

这个购物系统实现了商品、购物车、用户、订单等基本功能,用户可以将商品添加到购物车,然后创建一个订单,订单会计算购物车中所有商品的总价。

打赏
版权声明:主机测评不销售、不代购、不提供任何支持,仅分享信息/测评(有时效性),自行辨别,请遵纪守法文明上网。
文章名称:《java编写购物系统》
文章链接:https://www.yunzhuji.net/internet/178128.html

评论

  • 验证码