function fruitOptimizationSystem()
% 创建主图形窗口
mainFig = figure('Name', '水果店进货优化系统', ...
'NumberTitle', 'off', ...
'Position', [100, 100, 1000, 700], ...
'MenuBar', 'none', ...
'ToolBar', 'none', ...
'CloseRequestFcn', @safeCloseCallback, ...
'Color', [0.95 0.95 0.95], ...
'Tag', 'MainFigure');
% 初始化系统数据
initSystemData(mainFig);
% 创建登录界面
createLoginUI(mainFig);
% 等待窗口关闭
uiwait(mainFig);
end
%% 初始化系统数据
function initSystemData(figHandle)
% 水果数据
fruits = {'苹果', '香蕉', '橙子', '草莓', '葡萄'};
costs = [3.5, 2.8, 4.2, 6.5, 5.0]; % 成本价
prices = [5.0, 4.0, 6.0, 8.5, 7.0]; % 销售价
stock = [100, 80, 120, 60, 90]; % 当前库存
% 销售预测数据
salesPrediction = struct(...
'Monday', [50, 40, 60, 30, 45], ...
'Tuesday', [45, 35, 55, 25, 40], ...
'Wednesday', [55, 45, 65, 35, 50], ...
'Thursday', [60, 50, 70, 40, 55], ...
'Friday', [70, 60, 80, 50, 65], ...
'Saturday', [80, 70, 90, 60, 75], ...
'Sunday', [65, 55, 75, 45, 60]);
% 存储到图形对象
setappdata(figHandle, 'Fruits', fruits);
setappdata(figHandle, 'Costs', costs);
setappdata(figHandle, 'Prices', prices);
setappdata(figHandle, 'Stock', stock);
setappdata(figHandle, 'SalesPrediction', salesPrediction);
setappdata(figHandle, 'UserLoggedIn', false);
end
%% 创建登录界面
function createLoginUI(figHandle)
% 登录面板
loginPanel = uipanel(figHandle, ...
'Position', [0.25, 0.3, 0.5, 0.4], ...
'Title', '系统登录', ...
'FontSize', 14, ...
'BackgroundColor', [0.9 0.95 1], ...
'Tag', 'LoginPanel');
% 用户名标签和输入框
uicontrol(loginPanel, 'Style', 'text', ...
'String', '用户名:', ...
'Position', [150, 200, 80, 30], ...
'FontSize', 12, ...
'BackgroundColor', [0.9 0.95 1]);
usernameEdit = uicontrol(loginPanel, 'Style', 'edit', ...
'Position', [240, 200, 200, 30], ...
'FontSize', 12, ...
'Tag', 'UsernameEdit');
% 密码标签和输入框
uicontrol(loginPanel, 'Style', 'text', ...
'String', '密码:', ...
'Position', [150, 140, 80, 30], ...
'FontSize', 12, ...
'BackgroundColor', [0.9 0.95 1]);
passwordEdit = uicontrol(loginPanel, 'Style', 'edit', ...
'Position', [240, 140, 200, 30], ...
'FontSize', 12, ...
'Tag', 'PasswordEdit', ...
'KeyPressFcn', @(src,event)handleKeyPress(event, figHandle));
% 登录按钮
uicontrol(loginPanel, 'Style', 'pushbutton', ...
'String', '登录', ...
'Position', [280, 70, 120, 40], ...
'FontSize', 12, ...
'BackgroundColor', [0.3 0.7 0.5], ...
'Callback', @(src,event)loginCallback(figHandle, usernameEdit, passwordEdit));
end
%% 键盘事件处理
function handleKeyPress(event, figHandle)
if strcmp(event.Key, 'return') % 回车键
usernameEdit = findobj(figHandle, 'Tag', 'UsernameEdit');
passwordEdit = findobj(figHandle, 'Tag', 'PasswordEdit');
loginCallback(figHandle, usernameEdit, passwordEdit);
end
end
%% 登录回调函数
function loginCallback(figHandle, usernameEdit, passwordEdit)
username = get(usernameEdit, 'String');
password = get(passwordEdit, 'String');
% 简单验证(实际应用中应使用安全验证)
if strcmp(username, 'admin') && strcmp(password, '123456')
setappdata(figHandle, 'UserLoggedIn', true);
delete(findobj(figHandle, 'Tag', 'LoginPanel'));
createMainUI(figHandle);
else
errordlg('用户名或密码错误!', '登录失败');
end
end
%% 创建主界面
function createMainUI(figHandle)
% 获取数据
fruits = getappdata(figHandle, 'Fruits');
costs = getappdata(figHandle, 'Costs');
prices = getappdata(figHandle, 'Prices');
stock = getappdata(figHandle, 'Stock');
% 创建选项卡
tabGroup = uitabgroup(figHandle, 'Position', [0.05, 0.05, 0.9, 0.85]);
% 库存管理选项卡
inventoryTab = uitab(tabGroup, 'Title', '库存管理');
createInventoryUI(inventoryTab, fruits, stock);
% 采购优化选项卡
optimizationTab = uitab(tabGroup, 'Title', '采购优化');
createOptimizationUI(optimizationTab, fruits, costs, prices);
% 销售分析选项卡
analysisTab = uitab(tabGroup, 'Title', '销售分析');
createAnalysisUI(analysisTab, figHandle);
% 系统设置选项卡
settingsTab = uitab(tabGroup, 'Title', '系统设置');
createSettingsUI(settingsTab, figHandle);
end
%% 创建库存管理界面
function createInventoryUI(parent, fruits, stock)
% 创建表格显示库存数据
columnNames = {'水果名称', '当前库存 (kg)', '建议采购量 (kg)'};
data = [fruits; num2cell(stock); num2cell(zeros(1, numel(stock)))]';
uitable(parent, 'Data', data, ...
'ColumnName', columnNames, ...
'Position', [50, 300, 500, 250], ...
'ColumnEditable', [false, true, true], ...
'Tag', 'InventoryTable');
% 添加保存按钮
uicontrol(parent, 'Style', 'pushbutton', ...
'String', '保存库存数据', ...
'Position', [200, 200, 200, 40], ...
'FontSize', 12, ...
'Callback', @saveInventoryCallback);
end
%% 创建采购优化界面
function createOptimizationUI(parent, fruits, costs, prices)
% 计算利润
profits = prices - costs;
% 创建利润分析图
ax = axes(parent, 'Position', [0.1, 0.5, 0.8, 0.4]);
bar(ax, profits, 'FaceColor', [0.2 0.6 0.8]);
set(ax, 'XTickLabel', fruits, 'FontSize', 10);
title(ax, '水果单位利润分析', 'FontSize', 14);
ylabel(ax, '利润 (元/kg)', 'FontSize', 12);
grid(ax, 'on');
% 添加优化建议
[~, idx] = max(profits);
recommendation = sprintf('推荐优先采购: %s (利润: %.2f元/kg)', fruits{idx}, profits(idx));
uicontrol(parent, 'Style', 'text', ...
'String', recommendation, ...
'Position', [150, 100, 600, 30], ...
'FontSize', 12, ...
'FontWeight', 'bold', ...
'ForegroundColor', [0.8 0.2 0.2]);
end
%% 创建销售分析界面
function createAnalysisUI(parent, figHandle)
% 获取销售预测数据
salesPrediction = getappdata(figHandle, 'SalesPrediction');
days = fieldnames(salesPrediction);
fruits = getappdata(figHandle, 'Fruits');
% 创建下拉菜单选择水果
uicontrol(parent, 'Style', 'text', ...
'String', '选择水果:', ...
'Position', [100, 450, 80, 25], ...
'FontSize', 11);
fruitPopup = uicontrol(parent, 'Style', 'popupmenu', ...
'String', fruits, ...
'Position', [190, 450, 150, 25], ...
'Tag', 'FruitSelector', ...
'Callback', @updateSalesPlot);
% 创建分析图表区域
ax = axes(parent, 'Position', [0.15, 0.15, 0.75, 0.6], ...
'Tag', 'SalesAxes');
% 初始显示第一种水果的数据
updateSalesPlot(fruitPopup, []);
% 嵌套函数更新图表
function updateSalesPlot(~, ~)
selectedIdx = get(fruitPopup, 'Value');
salesData = zeros(1, numel(days));
for i = 1:numel(days)
daySales = salesPrediction.(days{i});
salesData(i) = daySales(selectedIdx);
end
% 绘制销售预测
cla(ax);
bar(ax, salesData, 'FaceColor', [0.9 0.5 0.1]);
set(ax, 'XTickLabel', days, 'FontSize', 10);
title(ax, ['每日销售预测: ' fruits{selectedIdx}], 'FontSize', 14);
ylabel(ax, '预测销量 (kg)', 'FontSize', 12);
grid(ax, 'on');
end
end
%% 创建系统设置界面
function createSettingsUI(parent, figHandle)
% 用户管理
uicontrol(parent, 'Style', 'text', ...
'String', '用户管理', ...
'Position', [50, 450, 100, 30], ...
'FontSize', 12, ...
'FontWeight', 'bold');
% 添加新用户
uicontrol(parent, 'Style', 'pushbutton', ...
'String', '添加新用户', ...
'Position', [50, 400, 120, 30], ...
'Callback', @addUserCallback);
% 数据备份
uicontrol(parent, 'Style', 'text', ...
'String', '数据备份', ...
'Position', [250, 450, 100, 30], ...
'FontSize', 12, ...
'FontWeight', 'bold');
% 备份按钮
uicontrol(parent, 'Style', 'pushbutton', ...
'String', '备份当前数据', ...
'Position', [250, 400, 120, 30], ...
'Callback', @backupDataCallback);
% 系统信息
uicontrol(parent, 'Style', 'text', ...
'String', '系统信息', ...
'Position', [450, 450, 100, 30], ...
'FontSize', 12, ...
'FontWeight', 'bold');
% 版本信息
uicontrol(parent, 'Style', 'text', ...
'String', '版本: 1.0.0', ...
'Position', [450, 400, 150, 30], ...
'FontSize', 11);
% 退出系统按钮
uicontrol(parent, 'Style', 'pushbutton', ...
'String', '退出系统', ...
'Position', [350, 100, 150, 40], ...
'FontSize', 12, ...
'BackgroundColor', [0.9 0.3 0.3], ...
'Callback', @(src,event)close(figHandle));
end
%% 安全关闭回调
function safeCloseCallback(src, ~)
% 检查用户是否登录
isLoggedIn = getappdata(src, 'UserLoggedIn');
if isLoggedIn
% 确认关闭对话框
selection = questdlg('确定要退出系统吗?', ...
'确认退出', ...
'是', '否', '是');
if ~strcmp(selection, '是')
return;
end
end
% 执行清理操作
cleanupSystem(src);
% 删除图形
delete(src);
end
%% 系统清理函数
function cleanupSystem(figHandle)
% 获取所有数据
fruits = getappdata(figHandle, 'Fruits');
stock = getappdata(figHandle, 'Stock');
% 模拟保存数据到文件
dataTable = table(fruits', stock', 'VariableNames', {'Fruit', 'Stock'});
try
writetable(dataTable, 'fruit_inventory.csv');
disp('库存数据已保存到 fruit_inventory.csv');
catch
warning('数据保存失败');
end
% 显示关闭消息
disp('系统资源已清理');
disp(['最后操作时间: ' datestr(now)]);
end
%% 其他回调函数(示例)
function saveInventoryCallback(src, ~)
% 在实际应用中实现保存逻辑
disp('库存数据已更新');
msgbox('库存数据保存成功!', '操作成功');
end
function addUserCallback(~, ~)
% 在实际应用中实现用户添加逻辑
disp('添加新用户操作');
end
function backupDataCallback(~, ~)
% 在实际应用中实现数据备份逻辑
disp('数据备份操作');
msgbox('数据备份完成!', '备份成功');
end
该代码能否输入数据
最新发布