# OrderingSystemDemo **Repository Path**: xiongwg/ordering-system-demo ## Basic Information - **Project Name**: OrderingSystemDemo - **Description**: OrderingSystemDemo分布式点餐系统 - **Primary Language**: Unknown - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 1 - **Forks**: 0 - **Created**: 2021-10-26 - **Last Updated**: 2021-12-22 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # OrderingSystemDemo ## **#HarmonyOS技术训练营#第三期#基于分布式文件的点餐功能** ### 项目简介 基于分布式文件服务,实现多设备点餐功能。同一桌号,设备A点餐,设备B能实时看到,同理,设备B点餐,设备A也能实时更新。 ### 实现效果 ![](./screenshort/screen.gif) ### 实现过程解析 实现原理很简单,基于鸿蒙[分布式文件服务](https://developer.harmonyos.com/cn/docs/documentation/doc-guides/database-dfs-guidelines-0000000000030159)功能,对同一文件进行读写,同时为了达到及时更新的效果,这里简单的采用了Timer定时轮询去读取分布式文件。 步骤大致分为以下几步: 1、进入点餐页面,首先去读取该分布式文件,没有则创建文件 ```java public class DistributedDataManager { private static final String FILE_NAME = "orderInfo.txt"; private final String filePath; public DistributedDataManager(Context context) { filePath = context.getDistributedDir() + File.separator + FILE_NAME; createFileIfNotExist(); } private void createFileIfNotExist() { File file = new File(filePath); if (file.exists() && file.isFile()) { return; } writeDataToFile("[]"); } private void writeDataToFile(String jsonData) { FileWriter fileWriter = null; try { File file = new File(filePath); if (file.exists() && file.isFile()) { file.delete(); } fileWriter = new FileWriter(filePath, true); fileWriter.write(jsonData); fileWriter.close(); } catch (IOException e) { e.printStackTrace(); } } } ``` 2、提供查询和修改更新文件的方法 查询文件,获取文件内容,如果该桌号没有订单数据,则返回菜单列表 ```java private List getInfoFromDistributedFile() { List menuInfoList = new ArrayList<>(); FileReader fileReader = null; try { fileReader = new FileReader(filePath); char[] buffer = new char[1024]; StringBuilder str = new StringBuilder(); int len; while ((len = fileReader.read(buffer)) != -1) { str.append(new String(buffer, 0, len)); } System.out.print(str.toString()); fileReader.close(); menuInfoList = new Gson().fromJson(str.toString(), new TypeToken>() { }.getType()); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return menuInfoList; } public List getOrderListByTableName(String tableName) { List menuInfoList = getInfoFromDistributedFile(); if (tableName != null && !tableName.trim().isEmpty()) { for (MenuInfo menuInfo : menuInfoList) { if (menuInfo.getTableNumber().equals(tableName)) { return menuInfo.getList(); } } } return MenuData.getMenuList(); } ``` 更新分布式文件: ```java private void writeDataToFile(String jsonData) { FileWriter fileWriter = null; try { File file = new File(filePath); if (file.exists() && file.isFile()) { file.delete(); } fileWriter = new FileWriter(filePath, true); fileWriter.write(jsonData); fileWriter.close(); } catch (IOException e) { e.printStackTrace(); } } public void updateData(String tableName, List details) { List menuInfoList = getInfoFromDistributedFile(); boolean isExistTable = false; if (tableName != null && !tableName.trim().isEmpty()) { for (MenuInfo menuInfo : menuInfoList) { if (menuInfo.getTableNumber().equals(tableName)) { isExistTable = true; menuInfo.setList(details); } } } if (!isExistTable) { MenuInfo menuInfo = new MenuInfo(); menuInfo.setTableNumber(tableName); menuInfo.setList(details); menuInfoList.add(menuInfo); } Gson gson = new Gson(); String json = gson.toJson(menuInfoList); writeDataToFile(json); } ``` 3、定时去查询最新数据 ``` Timer timer = new Timer(); timer.schedule(new TimerTask() { @Override public void run() { getUITaskDispatcher().asyncDispatch(new Runnable() { @Override public void run() { menuListProvider.setList(dataManager.getOrderListByTableName(tableNum)); } }); } },0,500); ``` 4、点击添加或删除按钮,更新分布式文件数据 ``` menuListProvider.setListener(new MenuListProvider.IChangeListener() { @Override public void onChange() { dataManager.updateData(tableNum,menuListProvider.getData()); } }); ``` 至此,核心功能基本完成。 ### 完整代码地址: ## **[OrderingSystemDemo](https://gitee.com/xiongwg/ordering-system-demo)**