该项目核心是用Java基础语法实现购物清单功能,包含商品添加、查看、删除和结算。通过Product类表示商品信息并计算单项总价,ShoppingCart类管理商品列表与用户交互,使用Scanner接收输入,结合List集合存储数据,利用循环和条件判断实现菜单选择。程序提供清晰的操作界面,支持持续添加商品、查看清单、按编号删除及统计总金额,适合初学者掌握类与对象、集合、流式计算等基础知识,后续可扩展数据持久化或分类管理功能。

做Java在线购物清单小项目,核心是用基础语法实现用户管理商品的添加、查看、删除和结算功能。适合初学者练手,能巩固类、对象、集合、循环和条件判断等知识点。
这个小项目可以包含以下基本功能:
定义两个主要类:
Product 类:表示商品
立即学习“Java免费学习笔记(深入)”;
class Product {
String name;
double price;
int quantity;
<pre class='brush:java;toolbar:false;'>public Product(String name, double price, int quantity) {
this.name = name;
this.price = price;
this.quantity = quantity;
}
// 计算单项总价
public double getTotal() {
return price * quantity;
}
@Override
public String toString() {
return "名称:" + name + ",单价:" + price + "元,数量:" + quantity + ",小计:" + getTotal() + "元";
}}
ShoppingCart 类:管理购物清单
import java.util.*;
<p>public class ShoppingCart {
private List<Product> items = new ArrayList<>();
private Scanner scanner = new Scanner(System.in);</p><pre class='brush:java;toolbar:false;'>public void start() {
while (true) {
System.out.println("\n--- 在线购物清单 ---");
System.out.println("1. 添加商品");
System.out.println("2. 查看清单");
System.out.println("3. 删除商品");
System.out.println("4. 计算总价");
System.out.println("5. 退出");
System.out.print("请选择操作:");
int choice = scanner.nextInt();
scanner.nextLine(); // 消费换行符
switch (choice) {
case 1 -> addProduct();
case 2 -> viewCart();
case 3 -> removeProduct();
case 4 -> calculateTotal();
case 5 -> {
System.out.println("感谢使用!");
return;
}
default -> System.out.println("无效选择,请重试。");
}
}
}
private void addProduct() {
System.out.print("输入商品名称:");
String name = scanner.nextLine();
System.out.print("输入单价(元):");
double price = scanner.nextDouble();
System.out.print("输入数量:");
int quantity = scanner.nextInt();
scanner.nextLine();
items.add(new Product(name, price, quantity));
System.out.println("✅ " + name + " 已添加到购物车。");
}
private void viewCart() {
if (items.isEmpty()) {
System.out.println("购物车为空。");
return;
}
System.out.println("\n? 当前购物清单:");
for (int i = 0; i < items.size(); i++) {
System.out.println((i + 1) + ". " + items.get(i));
}
}
private void removeProduct() {
viewCart();
if (items.isEmpty()) return;
System.out.print("输入要删除的商品编号:");
int index = scanner.nextInt() - 1;
if (index >= 0 && index < items.size()) {
Product removed = items.remove(index);
System.out.println("?️ 已删除:" + removed.name);
} else {
System.out.println("❌ 编号无效!");
}
}
private void calculateTotal() {
double total = items.stream().mapToDouble(Product::getTotal).sum();
System.out.printf("? 当前总金额:%.2f 元\n", total);
}}
创建主类运行程序:
public class Main {
public static void main(String[] args) {
new ShoppingCart().start();
}
}
用户交互类似:
--- 在线购物清单 --- 1. 添加商品 2. 查看清单 3. 删除商品 4. 计算总价 5. 退出 请选择操作:1 输入商品名称:苹果 输入单价(元):5.5 输入数量:3 ✅ 苹果 已添加到购物车。
这个项目不复杂但容易忽略细节,比如输入验证、Scanner换行处理、索引越界检查。可以后续扩展加入用户登录、文件保存数据、商品分类等功能。
以上就是Java中在线购物清单小项目的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号