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

相关推荐
阿巴斯甜13 小时前
Android 报错:Zip file '/Users/lyy/develop/repoAndroidLapp/l-app-android-ble/app/bu
android
Kapaseker14 小时前
实战 Compose 中的 IntrinsicSize
android·kotlin
xq952715 小时前
Andorid Google 登录接入文档
android
黄林晴16 小时前
告别 Modifier 地狱,Compose 样式系统要变天了
android·android jetpack
冬奇Lab1 天前
Android触摸事件分发、手势识别与输入优化实战
android·源码阅读
城东米粉儿1 天前
Android MediaPlayer 笔记
android
Jony_1 天前
Android 启动优化方案
android
阿巴斯甜1 天前
Android studio 报错:Cause: error=86, Bad CPU type in executable
android
张小潇1 天前
AOSP15 Input专题InputReader源码分析
android
_小马快跑_2 天前
Kotlin | 协程调度器选择:何时用CoroutineScope配置,何时用launch指定?
android