一、來源
來自我的一個室友,他稱自己為“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;
}
三、主要問題列表、針對問題所需的改善和重構
總體寫的非常細緻全面,只是存在一個小小的缺憾。這也是當時他想完成但沒能完成的內容。就是增加系統的“修改訂單選項”,這是當時的一個難點。
因此,此次我將個人的想法在原先基礎上做了補充,使得系統更加全面合理。此外,我還在部分排版,迴圈處理等方面做了小的修改,使得執行看上去更加清晰整潔。
四、新程式碼附上
點選檢視程式碼
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");
}
}
五、重構的軟體測試截圖
六、總結
當修改程式碼時,我會首先仔細閱讀原始程式碼,確保我充分理解其功能和邏輯。然後,我會考慮程式碼可能存在的問題或需要改進的地方。在修改程式碼的過程中,我會盡量保持程式碼風格的一致性。最後,我會進行測試,確保修改後的程式碼能夠正常執行並達到預期的效果。這次修改的主要難點是對原有內容的修改,這一部分我起初也並沒有掌握,然後透過網上的一些學習,最終實現了這一功能。
過程很是艱辛,看懂程式碼也需要耐心,雖然麻煩,但是真正實現還是很有成就感的。