订单管理系统分析及改进

一、来源
来自我的一个室友,他称自己为“C罗”,是他一年前学习c++时练手所写。
二、运行环境+运行结果截图

点击查看代码
#include "menu.h"

void Menu::readFile()
{
	ifstream ifs;
	ifs.open("Menu.txt", ios::out);
	int n = 0;
	ifs >> n;
	for (int i = 0; i < n; i++)
	{
		Menu m;
		ifs >> m.m_number >> m.m_name >> m.m_price >> m.m_cost;
		menuList.push_back(m);
	}
	ifs.close();
}

void Menu::init()
{
	this->readFile();
}

void Menu::writeFile()
{
	ofstream ofs;
	ofs.open("Menu.txt", ios::out);
	ofs << menuList.size() << endl; //先写入菜单数量
	for (int i = 0; i < menuList.size(); i++)
	{
		ofs << menuList[i].m_number << " " << menuList[i].m_name << " " << menuList[i].m_price << " " << menuList[i].m_cost << endl;
	}
	ofs.close();
}

void Menu::showAllDish()
{
	system("cls");
	cout << "\t\t-----------------------------------------------------------------------" << endl;
	cout << "\t\t";
	Menu::showHeader();
	cout << "\t\t-----------------------------------------------------------------------" << endl;
	for (int i = 0; i < menuList.size(); i++)
	{
		cout << "\t\t";
		menuList[i].showDishInfo();
	}
	cout << "\t\t-----------------------------------------------------------------------" << endl;
	cout << "\t\t";
	system("pause");
}

void Menu::showHeader()
{
	cout << left << setw(12) << "菜号";
	cout << left << setw(10) << "菜名";
	cout << left << setw(6) << "价格";
	cout << left << setw(8) << "成本" << endl;

}

void Menu::showDishInfo()
{
	cout << left << setw(12) << m_number;
	cout << left << setw(10) << m_name;
	cout << left << setw(6) << m_price;
	cout << left << setw(8) << m_cost << endl;
}

void Menu::showMenu()
{
	//主菜单
	string sel = "0";
	system("cls");
	cout << "\t\t\t**********欢迎来到菜单管理系统**********" << endl;
	cout << "\t\t\t你可以进行以下操作:" << endl;
	cout << "\t\t\t|------------------------------------------|" << endl;
	cout << "\t\t\t|             1   添加新的菜单             |" << endl;
	cout << "\t\t\t|------------------------------------------|" << endl;
	cout << "\t\t\t|             2   删除一个菜单             |" << endl;
	cout << "\t\t\t|------------------------------------------|" << endl;
	cout << "\t\t\t|             3   显示菜单信息             |" << endl;
	cout << "\t\t\t|------------------------------------------|" << endl;
	cout << "\t\t\t|             0   退出                     |" << endl;
	cout << "\t\t\t|------------------------------------------|" << endl;
	cout << "\t\t\t请选择【0-3】:";
	cin >> sel;
	while (sel != "0" && sel != "1" && sel != "2" && sel != "3"/* && sel != "4"*/)
	{
		cout << "\t\t\t输入不合法,请重新选择【0-3】:";
		cin >> sel;
	}
	if (sel == "1")
	{
		this->insertDish();
		this->showMenu();
	}
	else if (sel == "2")
	{
		this->deleteDish();
		this->showMenu();
	}
	else if (sel == "3")
	{
		this->showAllDish();
		this->showMenu();
	}
	else if (sel == "0")
	{
		exit(0);
	}
}

void Menu::insertDish()
{
	while (true)
	{
		system("cls");
		cout << "\t\t**********************欢迎来到添加菜单功能*************************" << endl;
		cout << "\t\t当前已有菜单:" << endl;
		showAllDish();
		cout << endl;
		cout << "\t\t------------------" << endl;
		cout << "\t\t1 添加菜单信息" << endl;
		cout << "\t\t2 返回主菜单" << endl;
		cout << "\t\t------------------" << endl;
		cout << "\t\t请选择【1-2】:";
		string select;
		cin >> select;
		while (select != "1" && select != "2")
		{
			cout << "\t\t输入不合法,请重新输入";
			cin >> select;
		}
		if (select == "1")
		{
			string flag = "1";
			while (flag == "1")
			{
				cout << "\t\t请输入菜单号" << endl;
				cout << "\t\t菜单号: ";
				Menu m;
				bool check = false;
				do
				{
					check = false;
					cin >> m.m_number;
					for (int i = 0; i < m.menuList.size(); i++)
					{
						if (m.m_number == menuList[i].m_number)
						{
							cout << "\t\t该单号已存在,请重新输入: ";
							check = true;
							break;
						}
					}
				} while (check);
				cout << "\t\t菜名: ";
				cin >> m.m_name;
				cout << "\t\t价格: ";
				cin >> m.m_price;
				cout << "\t\t成本:";
				cin >> m.m_cost;
				menuList.push_back(m);
				writeFile();
				cout << "\n\t\t该菜单信息添加成功!是否继续添加?(1 是 0 否)" << endl;
				cout << "\t\t请进行选择【0-1】:";
				cin >> flag;
				while (flag != "0" && flag != "1")
				{
					cout << "\t\t输入不合法,请重新选择【0-1】:";
					cin >> flag;
				}
			}
			cout << "\t\t";
			system("pause");
		}
		else
		{
			break;
		}
	}
}

void Menu::deleteDish()
{
	while (true)
	{
		system("cls");
		cout << "\t\t***********************欢迎来到删除菜单信息功能***********************" << endl;
		string sel = "0";
		cout << "\t\t-----------------" << endl;
		cout << "\t\t1 选择待删菜号" << endl;
		cout << "\t\t2 返回主菜单" << endl;
		cout << "\t\t-----------------" << endl;
		cout << "\t\t请进行选择【1-2】" << endl;
		cin >> sel;
		while (sel != "1" && sel != "2")
		{
			cout << "\t\t输入不合法,请重新选择【1-2】:";
			cin >> sel;
		}
		if (sel == "1")
		{
			int keyNum;
			bool flag = false;
			cout << "\t\t请输入待删除的菜号: ";
			cin >> keyNum;
			for (vector<Menu>::iterator it = menuList.begin(); it != menuList.end(); ++it)
			{
				if (it->m_number == keyNum)
				{
					flag = true;
					cout << "\t\t待删除的菜单信息如下: " << endl;
					cout << "\t\t-----------------------------------------------------------------------" << endl;
					cout << "\t\t";
					Menu::showHeader();
					cout << "\t\t-----------------------------------------------------------------------" << endl;
					cout << "\t\t";
					it->showDishInfo();
					cout << "\t\t-----------------------------------------------------------------------" << endl;
					cout << "\t\t确认删除?(1 是 0 否)" << endl;
					cout << "\t\t请进行选择【0-1】:";
					string ch = "0";
					cin >> ch;
					while (ch != "0" && ch != "1")
					{
						cout << "\t\t输入不合法,请重新选择【0-1】:";
						cin >> ch;
					}
					if (ch == "0") break;
					else
					{
						menuList.erase(it);
						writeFile();
						cout << "\t\t删除成功!" << endl;
						break;
					}
				}
			}
			if (!flag) cout << "\t\t查无此号,无法删除!\n" << endl;
			cout << "\t\t";
			system("pause");
		}
		else
		{
			break;
		}
	}

}



点击查看代码
#include "order.h"
#include <iostream>
using namespace std;
#include <vector>
#include <algorithm>
#include <iomanip>
#include "menu.h"
void order::init()
{
	readFile();
}

void order::showHeader()
{
	cout << left << setw(12) << "单号";
	cout << left << setw(10) << "时间";
	cout << left << setw(6) << "人数";
	cout << left << setw(8) << "总价";
	cout << left << setw(23) << "菜号";
	cout << left << setw(6) << "数量" << endl;
}

void order::showListInfo()
{
	cout << left << setw(12) << m_number;
	cout << left << setw(10) << m_time;
	cout << left << setw(6) << m_customers;
	cout << left << setw(8) << m_price;
	cout << left << setw(23) << menu_number;
	cout << left << setw(6) << menu_count << endl;
}

void order::showAllList()
{
	system("cls");
	cout << "\t\t-----------------------------------------------------------------------" << endl;
	cout << "\t\t";
	order::showHeader();
	cout << "\t\t-----------------------------------------------------------------------" << endl;
	for (int i = 0; i < ordList.size(); i++)
	{
		cout << "\t\t";
		ordList[i].showListInfo();
	}
	cout << "\t\t-----------------------------------------------------------------------" << endl;
	cout << "\t\t";
	system("pause");
}

void order::readFile()
{
	ifstream ifs;
	ifs.open("ordList.txt", ios::out);
	int n = 0;
	ifs >> n;
	for (int i = 0; i < n; i++)
	{
		order o;
		ifs >> o.m_number >> o.m_time >> o.m_customers >> o.m_price >> o.menu_number >> o.menu_count;
		ordList.push_back(o);
	}
	ifs.close();
}

void order::writeFile() 
{
	ofstream ofs;
	ofs.open("ordList.txt", ios::out);
	ofs << ordList.size() << endl; //先写入订单数量
	for (int i = 0; i < ordList.size(); i++)
	{
		ofs << ordList[i].m_number << " " << ordList[i].m_time << " " << ordList[i].m_customers
			<< " " << ordList[i].m_price << " " << ordList[i].menu_number << " " << ordList[i].menu_count << endl;
	}
	ofs.close();
}

void order::showMenu()
{
//主菜单
		string sel = "0";
		system("cls");
		cout << "\t\t\t**********欢迎来到订单管理系统**********" << endl;
		cout << "\t\t\t你可以进行以下操作:" << endl;
		cout << "\t\t\t             1   添加订单信息             |" << endl;
		cout << "\t\t\t             2   删除订单信息             |" << endl;
		cout << "\t\t\t             3   查询订单信息             |" << endl;
		cout << "\t\t\t             4   进入菜单管理             |" << endl;
		cout << "\t\t\t             5   统计营收数据             |" << endl;
		cout << "\t\t\t             6   清空系统数据             |" << endl;
		cout << "\t\t\t             0   退出                     |" << endl;
		cout << "\t\t\t请选择【0-6】:";
		cin >> sel;
		while (sel != "0" && sel != "1" && sel != "2" && sel != "3" && sel != "4" && sel != "5" && sel != "6" )
		{
			cout << "\t\t\t输入不合法,请重新选择【0-6】:";
			cin >> sel;
		}
		if (sel == "1")
		{
			this->insertList();
			this->showMenu();
		}
		else if (sel == "2")
		{
			this->deleteList();
			this->showMenu();
		}
		else if (sel == "3")
		{
			showAllList();
			this->showMenu();
		}
		else if (sel == "4")
		{
			Menu m;
			m.init();
			m.showMenu();
			this->showMenu();
		}
		else if (sel == "5")
		{
			this->sumProfits();
			this->showMenu();
		}
		else if (sel == "6")
		{
			this->clearList();
			this->showMenu();
		}
		else if (sel == "0")
		{
			exit(0);
		}
}

void order::insertList()
{
	while (true)
	{
		system("cls");
		cout << "\t\t**********************欢迎来到添加订单功能*************************" << endl;
		cout << "\t\t当前已有订单:" << endl;
		showAllList();
		cout << endl;
		cout << "\t\t------------------" << endl;
		cout << "\t\t1 添加订单信息" << endl;
		cout << "\t\t2 返回主菜单" << endl;
		cout << "\t\t------------------" << endl;
		cout << "\t\t请选择【1-2】:";
		string select;
		cin >> select;
		while (select != "1" && select != "2")
		{
			cout << "\t\t输入不合法,请重新输入";
			cin >> select;
		}
		if (select == "1")
		{
			string flag = "1";
			while (flag=="1")
			{
				cout << "\t\t请输入订单号" << endl;
				cout << "\t\t订单号: ";
				order o;
				bool check = false;
				do
				{
					check = false;
					cin >> o.m_number;
					for (int i = 0; i < o.ordList.size(); i++)
					{
						if (o.m_number == ordList[i].m_number)
						{
							cout << "\t\t该单号已存在,请重新输入: ";
							check = true;
							break;
						}
					}
				} while (check);
				cout << "\t\t下单时间: ";
				cin >> o.m_time;
				cout << "\t\t就餐人数: ";
				cin >> o.m_customers;
				cout << "\t\t订单总价: ";
				cin >> o.m_price;
				cout << "\t\t菜号: ";
				cin >> o.menu_number;
				cout << "\t\t数量:";
				cin >> o.menu_count;
				ordList.push_back(o);
				writeFile();
				cout << "\n\t\t该订单信息添加成功!是否继续添加?(1 是 0 否)" << endl;
				cout << "\t\t请进行选择【0-1】:";
				cin >> flag;
				while (flag != "0" && flag != "1")
				{
					cout << "\t\t输入不合法,请重新选择【0-1】:";
					cin >> flag;
				}
			}
			cout << "\t\t";
			system("pause");
		}
		else
		{
			break;
		}
	}
}

void order::deleteList()
{
	while (true)
	{
		system("cls");
		cout << "\t\t***********************欢迎来到删除订单信息功能***********************" << endl;
		string sel = "0";
		cout << "\t\t-----------------" << endl;
		cout << "\t\t1 按单号删除" << endl;
		cout << "\t\t2 按时间删除" << endl;
		cout << "\t\t3 返回主菜单" << endl;
		cout << "\t\t-----------------" << endl;
		cout << "\t\t请进行选择【1-3】" << endl;
		cin >> sel;
		while (sel != "1" && sel != "2" && sel != "3")
		{
			cout << "\t\t输入不合法,请重新选择【1-3】:";
			cin >> sel;
		}
		if (sel == "1")
		{
			int keyNum;
			bool flag = false;
			cout << "\t\t请输入待删除的单号: ";
			cin >> keyNum;
			for (vector<order>::iterator it = ordList.begin(); it != ordList.end(); ++it)
			{
				if (it->m_number == keyNum)
				{
					flag = true;
					cout << "\t\t待删除的订单信息如下: " << endl;
					cout << "\t\t-----------------------------------------------------------------------" << endl;
					cout << "\t\t";
					order::showHeader();
					cout << "\t\t-----------------------------------------------------------------------" << endl;
					cout << "\t\t";
					it->showListInfo();
					cout << "\t\t-----------------------------------------------------------------------" << endl;
					cout << "\t\t确认删除?(1 是 0 否)" << endl;
					cout << "\t\t请进行选择【0-1】:";
					string ch = "0";
					cin >> ch;
					while (ch != "0" && ch != "1")
					{
						cout << "\t\t输入不合法,请重新选择【0-1】:";
						cin >> ch;
					}
					if (ch == "0") break;
					else
					{
						ordList.erase(it);
						writeFile();
						cout << "\t\t删除成功!" << endl;
						break;
					}
				}
			}
			if (!flag) cout << "\t\t查无此单,无法删除!\n" << endl;
			cout << "\t\t";
			system("pause");
		}
		else if (sel == "2")
		{
			int keyTime;
			bool flag = false;
			cout << "\t\t请输入待删除订单的时间:";
			cin >> keyTime;
			for (vector<order>::iterator it = ordList.begin(); it != ordList.end(); ++it)
			{
				if (it->m_time == keyTime)
				{
					flag = true;
					cout << "\t\t待删除单号的信息如下:" << endl;
					cout << "\t\t-----------------------------------------------------------------------" << endl;
					cout << "\t\t\t";
					order::showHeader();
					cout << "\t\t-----------------------------------------------------------------------" << endl;
					cout << "\t\t";
					it->showListInfo();
					cout << "\t\t-----------------------------------------------------------------------" << endl;
					cout << "\t\t确认删除?(1 是 0 否)" << endl;
					cout << "\t\t请进行选择【0-1】:";
					string ch = "0";
					cin >> ch;
					while (ch != "0" && ch != "1")
					{
						cout << "\t\t输入不合法,请重新选择【0-1】:";
						cin >> ch;
					}
					if (ch == "0") break;
					else
					{
						ordList.erase(it);
						writeFile();
						cout << "\t\t删除成功!" << endl;
						break;
					}
				}
			}
			if (!flag) cout << "\t\t查无此单,无法删除!\n" << endl;
			cout << "\t\t";
			system("pause");
		}
		else
		{
			break;
		}
	}

}

void order::clearList()
{
	while (true)
	{
		string sel = "0";
		system("cls");
		cout << "\t\t**************欢迎来到清空系统数据功能***************" << endl;
		cout << "\t\t------------------" << endl;
		cout << "\t\t1 确认清空系统数据" << endl;
		cout << "\t\t2 返回主菜单" << endl;
		cout << "\t\t------------------" << endl;
		cout << "\t\t请慎重选择【1-2】:";
		cin >> sel;
		while (sel != "1" && sel != "2")
		{
			cout << "\t\t输入不合法,请重新输入【1-2】:";
			cin >> sel;
		}
		if (sel == "1")
		{
			ordList.clear();
			cout << "\t\t清空成功!" << endl;
			cout << "\t\t";
			system("pause");
			writeFile();
		}
		else
		{
			break;
		}
	}

}

void order::sumProfits()
{
	system("cls");
	double sum = 0;
	for (vector<order>::iterator it = ordList.begin(); it < ordList.end(); ++it)
	{
		sum += it->m_price;
	}
	cout << "\t\t\t总计卖出" << sum << "元" << endl;
	system("pause");
}

点击查看代码
#include <iostream>
#include <fstream>
#include <vector>
#include <algorithm>
#include <string>
#include "order.h"
#include "menu.h"
using namespace std;
int main()
{
	order od;
	od.init();
	od.showMenu();
	return 0;
}
![](https://img2024.cnblogs.com/blog/3398664/202403/3398664-20240307002336658-1549795652.png) ![](https://img2024.cnblogs.com/blog/3398664/202403/3398664-20240306234704805-122730950.png) ![](https://img2024.cnblogs.com/blog/3398664/202403/3398664-20240306234736906-589735721.png) ![](https://img2024.cnblogs.com/blog/3398664/202403/3398664-20240306234812739-1700416321.png) ![](https://img2024.cnblogs.com/blog/3398664/202403/3398664-20240306234850031-1614593422.png) ![](https://img2024.cnblogs.com/blog/3398664/202403/3398664-20240306234900919-1008845268.png)

三、主要问题列表、针对问题所需的改善和重构
总体写的非常细致全面,只是存在一个小小的缺憾。这也是当时他想完成但没能完成的内容。就是增加系统的“修改订单选项”,这是当时的一个难点。
因此,此次我将个人的想法在原先基础上做了补充,使得系统更加全面合理。此外,我还在部分排版,循环处理等方面做了小的修改,使得运行看上去更加清晰整洁。

四、新代码附上

点击查看代码
void Menu::updateDish()
{
	while (true)
	{
		system("cls");
		cout << "\t\t***********************欢迎来到修改菜单信息功能***********************" << endl;
		string sel = "0";
		cout << "\t\t-----------------" << endl;
		cout << "\t\t1 修改菜单基本信息" << endl;
		cout << "\t\t2 返回主菜单" << endl;
		cout << "\t\t-----------------" << endl;
		cout << "\t\t请进行选择【1-2】:";
		cin >> sel;
		while (sel != "1" && sel != "2")
		{
			cout << "\t\t输入不合法,请重新选择【1-3】:";
			cin >> sel;
		}

		if (sel == "1")
		{
			bool flag = false;
			int keyNum;
			cout << "\t\t请输入待修改菜单的单号:";
			cin >> keyNum;
			for (int i = 0; i < menuList.size(); i++)
			{
				if (menuList[i].m_number == keyNum)
				{
					flag = true;
					cout << "\t\t待修改菜单基本信息如下:" << endl;
					cout << "\t\t-----------------------------------------------------------------------" << endl;
					cout << "\t\t";
					Menu::showHeader();
					cout << "\t\t-----------------------------------------------------------------------" << endl;
					cout << "\t\t";
					menuList[i].showDishInfo();
					cout << "\t\t-----------------------------------------------------------------------" << endl;

					Menu s = menuList[i];
					cout << "\t\t请输入修改后的单号:";
					bool check = false;
					do
					{
						check = false;
						cin >> s.m_number;
						for (int j = 0; j < menuList.size(); ++j)
						{
							if (s.m_number == menuList[j].m_number && i != j)
							{
								cout << "\t\t该单号已被录入,请重新输入单号:";
								check = true;
								break;
							}
						}
					} while (check);
					cout << "\t\t请输入修改后的菜号:";
					cin >> s.m_number;
					cout << "\t\t请输入修改后的菜名:";
					cin >> s.m_name;
					cout << "\t\t请输入修改后的价格:";
					cin >> s.m_price;
					cout << "\t\t请输入修改后的成本:";
					cin >> s.m_cost;
					cout << "\t\t是否确认修改?(1 是 0 否)" << endl;
					cout << "\t\t请进行选择【0-1】:";
					string ch = "0";
					cin >> ch;
					while (ch != "0" && ch != "1")
					{
						cout << "\t\t输入不合法,请重新选择【0-1】:";
						cin >> ch;
					}

					if (ch == "0") break;
					else
					{
						menuList[i] = s;
						cout << "\t\t修改成功!" << endl;
						writeFile();
						break;
					}
				}
			}
			if (!flag) cout << "\t\t查无此号,无法修改!\n" << endl;
		}
		else
		{
			break;
		}
		cout << "\t\t";
		system("pause");
	}
}
点击查看代码
void order::updateList()
{
	while (true)
	{
		system("cls");
		cout << "\t\t***********************欢迎来到修改订单信息功能***********************" << endl;
		string sel = "0";
		cout << "\t\t-----------------" << endl;
		cout << "\t\t1 修改订单基本信息" << endl;
		cout << "\t\t2 返回主菜单" << endl;
		cout << "\t\t-----------------" << endl;
		cout << "\t\t请进行选择【1-2】:";
		cin >> sel;
		while (sel != "1" && sel != "2")
		{
			cout << "\t\t输入不合法,请重新选择【1-3】:";
			cin >> sel;
		}

		if (sel == "1")
		{
			bool flag = false;
			int keyNum;
			cout << "\t\t请输入待修改订单的单号:";
			cin >> keyNum;
			for (int i = 0; i < ordList.size(); i++)
			{
				if (ordList[i].m_number == keyNum)
				{
					flag = true;
					cout << "\t\t待修改订单基本信息如下:" << endl;
					cout << "\t\t-----------------------------------------------------------------------" << endl;
					cout << "\t\t";
					order::showHeader();
					cout << "\t\t-----------------------------------------------------------------------" << endl;
					cout << "\t\t";
					ordList[i].showListInfo();
					cout << "\t\t-----------------------------------------------------------------------" << endl;

					order s = ordList[i];
					cout << "\t\t请输入修改后的单号:";
					bool check = false;
					do
					{
						check = false;
						cin >> s.m_number;
						for (int j = 0; j < ordList.size(); ++j)
						{
							if (s.m_number == ordList[j].m_number && i != j)
							{
								cout << "\t\t该单号已被录入,请重新输入单号:";
								check = true;
								break;
							}
						}
					} while (check);
					cout << "\t\t请输入修改后的时间:";
					cin >> s.m_time;
					cout << "\t\t请输入修改后的就餐人数:";
					cin >> s.m_customers;
					cout << "\t\t请输入修改后的总价:";
					cin >> s.m_price;
					cout << "\t\t请输入修改后的菜号:";
					cin >> s.menu_number;
					cout << "\t\t请输入修改后的数量:";
					cin >> s.menu_count;
					cout << "\t\t是否确认修改?(1 是 0 否)" << endl;
					cout << "\t\t请进行选择【0-1】:";
					string ch = "0";
					cin >> ch;
					while (ch != "0" && ch != "1")
					{
						cout << "\t\t输入不合法,请重新选择【0-1】:";
						cin >> ch;
					}

					if (ch == "0") break;
					else
					{
						ordList[i] = s;
						cout << "\t\t修改成功!" << endl;
						writeFile();
						break;
					}
				}
			}
			if (!flag) cout << "\t\t查无此人,无法修改!\n" << endl;
		}
		else
		{
			break;
		}
		cout << "\t\t";
		system("pause");
	}
}

五、重构的软件测试截图


六、总结
当修改代码时,我会首先仔细阅读原始代码,确保我充分理解其功能和逻辑。然后,我会考虑代码可能存在的问题或需要改进的地方。在修改代码的过程中,我会尽量保持代码风格的一致性。最后,我会进行测试,确保修改后的代码能够正常运行并达到预期的效果。这次修改的主要难点是对原有内容的修改,这一部分我起初也并没有掌握,然后通过网上的一些学习,最终实现了这一功能。
过程很是艰辛,看懂代码也需要耐心,虽然麻烦,但是真正实现还是很有成就感的。

posted @ 2024-03-07 00:32  Lucaba  阅读(21)  评论(0编辑  收藏  举报