第15章 综合项目——网上订餐系统

📘 第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 初学者非常经典的综合项目,能有效提升实践能力。

相关推荐
千里马学框架3 天前
一起学 Android 14:ShellTransition 屏幕旋转过程深度剖析
android·智能手机·性能优化·framework·性能·屏幕旋转·rotation
美狐美颜SDK开放平台3 天前
开发直播APP时如何接入视频美颜SDK?开发流程与注意事项
android·人工智能·计算机视觉·音视频·直播美颜sdk
AFinalStone3 天前
Android7 SystemUI源码解析(七)Keyguard锁屏模块深度解析
android·systemui
致远ccc3 天前
Google Play 上架前如何测试 App?多国家 Android 环境测试
android·app测试·googleplay·多国家应用测试
ttyyttemo3 天前
Kotlin 协程中的 Job 结构化并发与取消
android
sun0077003 天前
tbox 4g/5g切换,导致wan ip 改变,导致车机旧网络不可用。需要重启车机才行
android
其实防守也摸鱼3 天前
内网穿透与反向代理:原理、工具与实战指南
android·大数据·运维·安全·网络安全·自动化·渗透
AFinalStone3 天前
Android7 SystemUI 源码解析(四)NavigationBar 导航栏与 SystemBars
android·systemui
JMchen3 天前
属性动画原理与高级动画实现
android·kotlin·canvas
AFinalStone3 天前
Android7 SystemUI 源码解析(二)启动流程深度解析
android·systemui