📘 第15章 综合项目------网上订餐系统
本章将通过一个完整的「网上订餐 App」项目,综合运用前面学习的界面布局、事件处理、数据存储、网络编程、多媒体等知识,完成一个可运行的订餐应用。
📑 目录
一、项目分析
1.1 项目概述
这是一个典型的移动互联网应用:
用户可以在手机端查看附近店铺、浏览菜品、添加购物车、提交订单。
系统整体流程:
首页 → 店铺列表 → 店铺详情 → 菜品详情 → 加入购物车 → 提交订单 → 展示订单
涉及技术:
-
Activity + Fragment 页面切换
-
RecyclerView 列表展示
-
SharedPreferences / SQLite 本地存储购物车
-
HTTP 请求加载店铺和菜品数据
-
JSON 数据解析
-
Intent 页面跳转与参数传递
1.2 开发环境
| 工具 | 版本 |
|---|---|
| Android Studio | Electric Eel / 或以上 |
| Gradle | 自动生成即可 |
| SDK | Android 8.0 以上 |
| 后端 | 简单 Node.js / PHP / Mock 数据接口 |
1.3 模块说明
本项目包含 4 大模块:
| 模块 | 功能 |
|---|---|
| 店铺模块 | 展示店铺列表,进入店铺详情 |
| 店铺详情模块 | 展示菜品列表,点击查看详情 |
| 菜品详情模块 | 展示菜品详情,加入购物车 |
| 订单模块 | 显示订单商品、提交订单 |
二、效果展示
(以下为占位,你可以替换成自己 App 的截图)
2.1 店铺页面
展示附近店铺:
---------------------------------
| 🍔 麦当劳 ⭐4.8 起送¥20 |
---------------------------------
| 🍜 兰州拉面 ⭐4.5 起送¥15 |
---------------------------------
| 🍕 必胜客 ⭐4.7 起送¥25 |
---------------------------------
2.2 店铺详情界面
店铺名称:麦当劳
店铺简介:全球知名快餐品牌
菜品列表:
[汉堡套餐] ¥22
[薯条] ¥10
[可乐] ¥5
2.3 菜品详情界面
🍔 巨无霸汉堡
介绍:双层牛肉加芝士
价格:¥18.00
[加入购物车按钮]
2.4 订单界面
已选择商品:
汉堡 x 1 ¥18
可乐 x 1 ¥5
---------------------
总价:¥23
[提交订单按钮]
三、服务器数据准备
可自建一个简单 JSON 接口,例如:
店铺列表(/shops)
[
{
"id": 1,
"name": "麦当劳",
"score": 4.8,
"startPrice": 20,
"logo": "http://xxx/mcdonald.png"
}
]
菜品列表(/menus?shopId=1)
[
{
"id": 101,
"name": "巨无霸汉堡",
"price": 18,
"desc": "双层牛肉加芝士",
"img": "http://xxx/burger.png"
}
]
四、功能实现
4.1 店铺功能业务实现
(1)布局 RecyclerView
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/rvShop"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
(2)加载网络数据
new Thread(() -> {
String json = HttpUtils.get("/shops");
runOnUiThread(() -> parseShopList(json));
}).start();
(3)解析 JSON
JSONArray array = new JSONArray(json);
for (int i = 0; i < array.length(); i++) {
JSONObject obj = array.getJSONObject(i);
Shop shop = new Shop(
obj.getInt("id"),
obj.getString("name"),
obj.getDouble("score"),
obj.getInt("startPrice")
);
list.add(shop);
}
(4)点击店铺,跳转详情页
Intent intent = new Intent(this, ShopDetailActivity.class);
intent.putExtra("shopId", shop.id);
startActivity(intent);
4.2 店铺详情功能业务实现
(1)接收参数
int shopId = getIntent().getIntExtra("shopId", 0);
(2)加载菜品列表
String url = "/menus?shopId=" + shopId;
String json = HttpUtils.get(url);
parseMenuList(json);
(3)点击菜品 → 跳转详情
Intent intent = new Intent(this, DishDetailActivity.class);
intent.putExtra("dishId", dish.id);
startActivity(intent);
4.3 菜品详情功能业务实现
(1)显示菜品信息
布局包含:
-
菜品图片
-
名称
-
价格
-
简介
-
加入购物车按钮
(2)加入购物车(SharedPreferences / SQLite)
SharedPreferences 简单示例:
SharedPreferences sp = getSharedPreferences("cart", MODE_PRIVATE);
SharedPreferences.Editor editor = sp.edit();
editor.putInt("dish_" + dishId, count);
editor.apply();
4.4 订单功能业务实现
(1)读取购物车数据
Map<String, ?> all = sp.getAll();
for(String key : all.keySet()){
if(key.startsWith("dish_")){
int dishId = Integer.parseInt(key.substring(5));
int count = (int) all.get(key);
// 添加到订单列表
}
}
(2)计算总价
total += dish.price * count;
(3)提交订单
一般向后端发 POST:
JSONObject order = new JSONObject();
order.put("total", total);
order.put("items", items);
HttpUtils.post("/order", order.toString());
五、总结
本章实现了一个完整的网上订餐项目,涉及:
-
Activity 页面跳转
-
RecyclerView 列表
-
Intent 数据传递
-
SharedPreferences 购物车
-
HTTP 网络请求
-
JSON 数据解析
-
多页面业务逻辑联动
这是一个 Android 初学者非常经典的综合项目,能有效提升实践能力。