基于用户输入实现简单的在线购物车功能
现代在线购物系统需要以用户为中心,提供便捷的操作体验。购物车是其中一个核心功能。今天我们将探讨如何用Java实现一个简单的购物车功能,涵盖功能需求、代码设计以及实现方案。
功能需求:
- 商品管理:
- 用户可以查看所有可选商品。
- 用户可根据商品ID选择并添加商品到购物车。
- 购物车操作:
- 用户可以查看购物车的商品及总价。
- 用户可以从购物车中移除某件商品。
- 用户可以清空购物车。
- 订单确认:
- 用户可以完成结算,系统计算应付金额并清空购物车。
技术方案设计:
我们将基于面向对象的设计原则,分为以下几部分:
- 数据结构设计:
Product类:表示商品信息,例如ID、名称、价格。Cart类:保存已加入购物车的商品列表及其数量。
- 逻辑功能设计: 每个功能点将作为单一方法实现,并通过控制台接口交互。
实现代码:
以下是功能的核心代码实现:
import java.util.*;
class Product {
private int id;
private String name;
private double price;
public Product(int id, String name, double price) {
this.id = id;
this.name = name;
this.price = price;
}
public int getId() {
return id;
}
public String getName() {
return name;
}
public double getPrice() {
return price;
}
@Override
public String toString() {
return "ID: " + id + ", Name: " + name + ", Price: $" + price;
}
}
class Cart {
private Map<Integer, Integer> items = new HashMap<>(); // Key: Product ID, Value: Quantity
public void addProduct(int productId) {
items.put(productId, items.getOrDefault(productId, 0) + 1);
}
public void removeProduct(int productId) {
if (items.containsKey(productId)) {
items.put(productId, items.get(productId) - 1);
if (items.get(productId) <= 0) {
items.remove(productId);
}
}
}
public double calculateTotal(Map<Integer, Product> productCatalog) {
double total = 0.0;
for (Map.Entry<Integer, Integer> entry : items.entrySet()) {
int productId = entry.getKey();
int quantity = entry.getValue();
Product product = productCatalog.get(productId);
total += product.getPrice() * quantity;
}
return total;
}
public void clearCart() {
items.clear();
}
public void displayCart(Map<Integer, Product> productCatalog) {
if (items.isEmpty()) {
System.out.println("Your cart is empty.");
} else {
System.out.println("Your cart contains:");
for (Map.Entry<Integer, Integer> entry : items.entrySet()) {
Product product = productCatalog.get(entry.getKey());
System.out.println(product.getName() + " x" + entry.getValue() + " - $" + (product.getPrice() * entry.getValue()));
}
System.out.println("Total Price: $" + calculateTotal(productCatalog));
}
}
}
public class ShoppingCartSystem {
private static Map<Integer, Product> productCatalog = new HashMap<>();
static {
// Sample products
productCatalog.put(1, new Product(1, "Laptop", 999.99));
productCatalog.put(2, new Product(2, "Smartphone", 499.99));
productCatalog.put(3, new Product(3, "Headphones", 149.99));
}
public static void main(String[] args) {
Cart cart = new Cart();
Scanner scanner = new Scanner(System.in);
while (true) {
System.out.println("\nMenu:");
System.out.println("1. View Products");
System.out.println("2. Add Product to Cart");
System.out.println("3. Remove Product from Cart");
System.out.println("4. View Cart");
System.out.println("5. Checkout");
System.out.println("6. Exit");
System.out.print("Enter your choice: ");
int choice = scanner.nextInt();
switch (choice) {
case 1:
System.out.println("Available Products:");
for (Product product : productCatalog.values()) {
System.out.println(product);
}
break;
case 2:
System.out.print("Enter Product ID to add: ");
int addId = scanner.nextInt();
if (productCatalog.containsKey(addId)) {
cart.addProduct(addId);
System.out.println("Product added to cart.");
} else {
System.out.println("Invalid Product ID.");
}
break;
case 3:
System.out.print("Enter Product ID to remove: ");
int removeId = scanner.nextInt();
cart.removeProduct(removeId);
System.out.println("Product removed from cart.");
break;
case 4:
cart.displayCart(productCatalog);
break;
case 5:
System.out.println("Order total: $" + cart.calculateTotal(productCatalog));
System.out.println("Thank you for shopping!");
cart.clearCart();
break;
case 6:
System.out.println("Goodbye!");
scanner.close();
return;
default:
System.out.println("Invalid choice.");
}
}
}
}
核心实现说明:
- 商品管理:
Product类用于对商品信息进行封装,商品数据存储在productCatalog中。 - 购物车操作:
Cart类提供添加、移除、显示及清空购物车的功能。 - 系统交互: 在
main方法中,通过控制台菜单让用户选择操作,方便直观。
可能的改进:
- 提供持久化存储功能,例如将商品数据和购物记录保存在数据库中。
- 引入用户管理系统,让不同用户可以操作自己的购物车。
- 使用Web框架(如Spring Boot)构建一个真正的在线购物应用。
结论:
通过以上代码,我们实现了一个基础的购物车功能。该案例演示了Java在小型系统开发中的强大优势,易于扩展和维护。希望能帮助大家更深入理解Java编程的实际应用场景。