写一个java的购物管理模块其中包括显示所有商品信息,查询商品详细信息,添加购物车,清空购物车,购物车结算
时间: 2023-11-12 14:09:39 浏览: 80
以下是一个简单的 Java 购物管理模块的代码示例:
```java
import java.util.ArrayList;
import java.util.Scanner;
// 商品类
class Product {
private String name;
private double price;
private String description;
public Product(String name, double price, String description) {
this.name = name;
this.price = price;
this.description = description;
}
public String getName() {
return name;
}
public double getPrice() {
return price;
}
public String getDescription() {
return description;
}
public String toString() {
return name + " - " + price + " - " + description;
}
}
// 购物车类
class ShoppingCart {
private ArrayList<Product> products;
public ShoppingCart() {
products = new ArrayList<Product>();
}
public void addProduct(Product product) {
products.add(product);
}
public void clear() {
products.clear();
}
public double getTotalPrice() {
double totalPrice = 0.0;
for (Product product : products) {
totalPrice += product.getPrice();
}
return totalPrice;
}
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append("Shopping Cart:\n");
for (Product product : products) {
sb.append(product.toString()).append("\n");
}
sb.append("Total price: ").append(getTotalPrice());
return sb.toString();
}
}
// 商品管理类
class ProductManager {
private ArrayList<Product> products;
public ProductManager() {
products = new ArrayList<Product>();
}
public void addProduct(Product product) {
products.add(product);
}
public void showProducts() {
System.out.println("Product list:");
for (Product product : products) {
System.out.println(product.toString());
}
}
public void showProductDetails(String name) {
for (Product product : products) {
if (product.getName().equals(name)) {
System.out.println(product.toString());
return;
}
}
System.out.println("Product not found.");
}
}
// 主程序
public class Main {
public static void main(String[] args) {
ProductManager productManager = new ProductManager();
productManager.addProduct(new Product("iPhone", 999.99, "Apple iPhone"));
productManager.addProduct(new Product("Samsung Galaxy", 899.99, "Samsung Galaxy"));
productManager.addProduct(new Product("Google Pixel", 799.99, "Google Pixel"));
ShoppingCart shoppingCart = new ShoppingCart();
Scanner scanner = new Scanner(System.in);
while (true) {
System.out.println("1. Show all products");
System.out.println("2. Show product details");
System.out.println("3. Add to cart");
System.out.println("4. Clear cart");
System.out.println("5. Checkout");
System.out.println("0. Exit");
System.out.print("Enter your choice: ");
int choice = scanner.nextInt();
switch (choice) {
case 1:
productManager.showProducts();
break;
case 2:
System.out.print("Enter product name: ");
String name = scanner.next();
productManager.showProductDetails(name);
break;
case 3:
System.out.print("Enter product name: ");
name = scanner.next();
for (Product product : productManager.getProducts()) {
if (product.getName().equals(name)) {
shoppingCart.addProduct(product);
System.out.println("Added to cart.");
break;
}
}
break;
case 4:
shoppingCart.clear();
System.out.println("Cart cleared.");
break;
case 5:
System.out.println(shoppingCart.toString());
System.out.println("Thank you for shopping!");
System.exit(0);
break;
case 0:
System.exit(0);
break;
default:
System.out.println("Invalid choice.");
break;
}
}
}
}
```
该示例代码包括了商品类、购物车类和商品管理类三个类,并在主程序中进行了调用。用户可以通过主程序进行以下操作:
1. 显示所有商品信息
2. 查询商品详细信息
3. 添加购物车
4. 清空购物车
5. 购物车结算
用户输入对应的数字即可进行操作。示例代码中提供了三种商品,可以根据需要添加更多商品。
阅读全文