面试题:实现一个模拟送餐系统

本文介绍如何设计并实现一个模拟送餐系统,包括客户下单、商家制作、商品上架、骑手配送等环节。系统使用4x4网格表示货架,商品制作时间为3s,骑手配送时长在10-20s之间随机。当货架满载时暂停接单,有空位则重新开启。每单商品需在2分钟内由骑手取走。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

实现一个模拟送餐系统

内容

1、客户在平台下单,下单后商家开始制作,制作完成后自动将商品放置到货架等待骑手取走
2、用一个4x4的16宫格UI界面表示货架;商品制作完成后放到货架,骑手取走后从货架删除商品
3、订单产生频率在5-15s内随机分布;
4、商品制作时间是3s钟
5、系统内线上骑手人数为20个
6、每个骑手的配送一单的时间是10-20s随机
7、货架装满后停止接单,货架有空位开启接单
8、每单商品最长派送等待时长不超过2分钟(商品下单后2分钟内必须有骑手取走

点击开始下单后的效果图

代码

//activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/rv"
        app:layout_constraintTop_toTopOf="parent"
        android:layout_width="match_parent"
        android:layout_height="400dp"/>


    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/rv_msg"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        app:layout_constraintTop_toBottomOf="@id/rv"
        app:layout_constraintBottom_toTopOf="@id/but_start"
        />


    <Button
        android:id="@+id/but_start"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="开始下单"
        app:layout_constraintBottom_toTopOf="@id/but_stop"
        />

    <Button
        android:id="@+id/but_stop"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="停止接单"
        app:layout_constraintBottom_toTopOf="@id/but_clear_msg"
        />

    <Button
        android:id="@+id/but_clear_msg"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="清理快递员送餐信息"
        app:layout_constraintBottom_toBottomOf="parent"
        />

</androidx.constraintlayout.widget.ConstraintLayout>
//goods_item_msg_page.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:orientation="vertical"
    android:layout_height="wrap_content">

    <TextView
        android:id="@+id/goods_item"
        android:background="@color/teal_200"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:textSize="15dp"
        android:gravity="center"
        />

</FrameLayout>

//goods_item_page.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:orientation="vertical"
    android:layout_height="60dp">

    <TextView
        android:id="@+id/goods_item"
        android:background="@color/teal_200"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:textSize="25dp"
        android:gravity="center"
        />

</FrameLayout>
//MainActivity.kt
package com.example.myapplication

import android.os.Bundle
import android.util.Log
import android.widget.Button
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import java.lang.ref.WeakReference
import java.util.concurrent.CopyOnWriteArrayList

class MainActivity : AppCompatActivity() {
    private lateinit var adapter: OrderAdapter
    private lateinit var orderSystemCenterManager: OrderSystemCenterManager
    private var stringBuilder: StringBuilder = StringBuilder()
    private var msgAdapter = OrderMsgAdapter()


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        val goodsList = findViewById<RecyclerView>(R.id.rv)
        val msgList = findViewById<RecyclerView>(R.id.rv_msg)

        adapter = OrderAdapter()

        goodsList.layoutManager = GridLayoutManager(this, 4)
        goodsList.adapter = adapter
        goodsList.addItemDecoration(OrderAdapter.SpacesItemDecoration(5))

        msgList.layoutManager = LinearLayoutManager(this).apply {
            orientation = LinearLayoutManager.VERTICAL
        }
        msgList.adapter = msgAdapter


        orderSystemCenterManager = OrderSystemCenterManager()
        orderSystemCenterManager.addSystemCenterListener(OrderSystemCenterEvent(WeakReference(this)))


        findViewById<Button>(R.id.but_start).setOnClickListener {
            Toast.makeText(this, "请等待8s 到 18s", Toast.LENGTH_SHORT).show()
            orderSystemCenterManager.startPlaceOrder()
        }

        findViewById<Button>(R.id.but_stop).setOnClickListener {
            orderSystemCenterManager.stopPlaceOrder()
        }

        findViewById<Button>(R.id.but_clear_msg).setOnClickListener {
            msgAdapter.clearMsg()
        }
    }

    fun notifyOrder(goods: CopyOnWriteArrayList<Goods>) {
        adapter.list = goods
        adapter.notifyDataSetChanged()
    }

    fun deliveryStoryComplete(deliveryStory: DeliveryStory) {
        dealDeliveryStoryGoodsMsg(deliveryStory, false)
    }

    fun deliveryStoryTakeGoods(deliveryStory: DeliveryStory) {
        dealDeliveryStoryGoodsMsg(deliveryStory, true)
    }

    /**
     * @param deliveryStory 快递员信息
     * @param isTake 是否是取商品
     */
    private fun dealDeliveryStoryGoodsM
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值