要用Java编写一个购物系统,首先需要创建一个商品类(Product),包含商品的基本信息,如名称、价格等。然后创建一个购物车类(ShoppingCart),用于存储用户添加到购物车的商品。接下来,创建一个用户界面类(UserInterface),用于与用户进行交互,如显示商品列表、接收用户输入等。创建一个主类(Main),用于启动整个购物系统。
购物系统可以分为以下几个部分:
1、商品类(Product)
2、购物车类(ShoppingCart)
3、用户类(User)
4、主程序(Main)
我们创建一个商品类(Product):
public 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 void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public double getPrice() { return price; } public void setPrice(double price) { this.price = price; } @Override public String toString() { return "Product{" + "id=" + id + ", name='" + name + ''' + ", price=" + price + '}'; } }
接下来,我们创建一个购物车类(ShoppingCart):
import java.util.ArrayList; import java.util.List; public class ShoppingCart { private List<Product> products; public ShoppingCart() { products = new ArrayList<>(); } public void addProduct(Product product) { products.add(product); } public void removeProduct(Product product) { products.remove(product); } public double getTotalPrice() { double totalPrice = 0; for (Product product : products) { totalPrice += product.getPrice(); } return totalPrice; } public void showCart() { System.out.println("购物车中的商品:"); for (Product product : products) { System.out.println(product); } System.out.println("总价:" + getTotalPrice()); } }
我们创建一个用户类(User):
public class User { private String username; private String password; private ShoppingCart shoppingCart; public User(String username, String password) { this.username = username; this.password = password; shoppingCart = new ShoppingCart(); } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public ShoppingCart getShoppingCart() { return shoppingCart; } }
我们创建一个主程序(Main):
import java.util.Scanner; public class Main { public static void main(String[] args) { User user = new User("张三", "123456"); Scanner scanner = new Scanner(System.in); boolean exit = false; while (!exit) { System.out.println("请选择操作:1.添加商品 2.删除商品 3.查看购物车 4.退出"); int operation = scanner.nextInt(); switch (operation) { case 1: System.out.println("请输入商品ID:"); int productId = scanner.nextInt(); System.out.println("请输入商品名称:"); String productName = scanner.next(); System.out.println("请输入商品价格:"); double productPrice = scanner.nextDouble(); Product product = new Product(productId, productName, productPrice); user.getShoppingCart().addProduct(product); break; case 2: System.out.println("请输入要删除的商品ID:"); int deleteProductId = scanner.nextInt(); Product deleteProduct = null; for (Product p : user.getShoppingCart().getProducts()) { if (p.getId() == deleteProductId) { deleteProduct = p; break; } } if (deleteProduct != null) { user.getShoppingCart().removeProduct(deleteProduct); } else { System.out.println("商品不存在!"); } break; case 3: user.getShoppingCart().showCart(); break; case 4: exit = true; break; default: System.out.println("无效的操作,请重新选择!"); } } scanner.close(); } }
这个简单的购物系统可以实现商品的添加、删除和查看购物车功能,你可以根据需要进一步完善和优化这个系统。
原创文章,作者:未希,如若转载,请注明出处:https://www.kdun.com/ask/636066.html
本网站发布或转载的文章及图片均来自网络,其原创性以及文中表达的观点和判断不代表本网站。如有问题,请联系客服处理。
发表回复