实现一个模拟送餐系统
内容
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