C++實(shí)現(xiàn)團(tuán)購訂單管理系統(tǒng)
項(xiàng)目需求
功能說明
要求編寫一個團(tuán)購訂單信息管理系統(tǒng)。
系統(tǒng)包含對訂單的添加、查詢、修改、刪除和瀏覽等功能。
系統(tǒng)設(shè)有口令,只有正確輸入口令才能使用該信息管理系統(tǒng)
功能具體說明
菜單的設(shè)計(jì):共設(shè)置6個選項(xiàng),包括訂單的添加、查詢、修改、刪除、瀏覽和退出。退出系統(tǒng)前菜單是重復(fù)循環(huán)的。
訂單信息的設(shè)計(jì):本案例采用簡化形式,只定義了訂單編號、商品編號、商品單價、商品數(shù)量、收件人姓名等。
添加訂單:添加時訂單的詳細(xì)信息從鍵盤輸入相應(yīng)內(nèi)容。
瀏覽訂單:顯示當(dāng)前訂單的所有信息,要求有格式控制。
查詢訂單:可輸入訂單編號或收件人姓名查詢相應(yīng)訂單。
修改訂單:對特定訂單信息進(jìn)行修改并保存。
刪除訂單:可根據(jù)訂單編號對該訂單進(jìn)行刪除操作。
口令設(shè)置:被設(shè)為一個字符串常量。程序開始運(yùn)行時,要求通過鍵盤輸入口令。3次輸入不正確,直接結(jié)束程序。
代碼實(shí)現(xiàn)
對于訂單設(shè)計(jì)的類
class Order
{
public:
//構(gòu)造函數(shù)
Order(string oid = "", string sid = "", double sp = 0, int sc = 0, string n
= "")
{
order_num = oid;
goods_num = sid;
goods_price = sp;
goods_count = sc;
name = n;
}
//打印基本信息
void print()
{
cout << "訂單編號:" << setw(N) << order_num << " 商品編號:" <<
setw(N) << goods_num << " 商品價格:"
<< setw(N) << goods_price << " 商品數(shù)量:" << setw(N) <<
goods_count << " 收件人姓名:" << setw(N) << name << endl;
}
//獲得訂單編號
string getOid()
{
return order_num;
}
//獲得姓名
string getName()
{
return name;
}
private:
string order_num; //訂單編號
string goods_num; //商品編號
double goods_price; //商品價格
int goods_count; //商品數(shù)量
string name; //收件人姓名
};
系統(tǒng)中各個功能的實(shí)現(xiàn)
對于系統(tǒng)中的添加,瀏覽,查詢,修改,刪除五項(xiàng)功能分別定義了函數(shù)來實(shí)現(xiàn)。
其中的orders是用來保存訂單信息的數(shù)組,index是記錄當(dāng)前數(shù)組位置
void add(Order orders[], int& index)
{
if (index == MAX)
{
cout << "訂單已滿" << endl;
return;
}
string oid;
string sid;
double sp;
int sc;
string n;
cout << "請輸入訂單編號:";
cin >> oid;
cout << "請輸入商品編號:";
cin >> sid;
cout << "請輸入商品價格:";
cin >> sp;
cout << "請輸入商品數(shù)量:";
cin >> sc;
cout << "請輸入收件人姓名:";
cin >> n;
Order o(oid, sid, sp, sc, n);
orders[index++] = o;
cout << "添加成功" << endl;
}
void visit(Order orders[], int& index)
{
cout << "目前共有" << index << "個訂單" << endl;
for (int i = 0; i < index; i++)
{
orders[i].print();
}
}
void find(Order orders[], int& index)
{
if (index == 0)
{
cout << "訂單為空" << endl;
}
int chiose;
cout << "請選擇查詢方式 1、訂單編號 2、收件人姓名" << endl;
cin >> chiose;
if (chiose == 1)
{
string oid;
cout << "請輸入要查詢的訂單編號" << endl;
cin >> oid;
for (int i = 0; i < index; i++)
{
if (orders[i].getOid() == oid)
{
orders[i].print();
}
}
}
else if (chiose == 2)
{
string name;
cout << "請輸入要查詢的收件人姓名" << endl;
cin >> name;
for (int i = 0; i < index; i++)
{
if (orders[i].getName() == name)
{
orders[i].print();
}
}
}
else
{
cout << "錯誤的選擇" << endl;
}
}
void fixed(Order orders[], int& index)
{
if (index == 0)
{
cout << "訂單為空" << endl;
}
string oid;
cout << "請輸入要修改的訂單編號" << endl;
cin >> oid;
bool have = false;
for (int i = 0; i < index; i++)
{
if (orders[i].getOid() == oid)
{
have = true;
string oid;
string sid;
double sp;
int sc;
string n;
cout << "請輸入訂單編號:";
cin >> oid;
cout << "請輸入商品編號:";
cin >> sid;
cout << "請輸入商品價格:";
cin >> sp;
cout << "請輸入商品數(shù)量:";
cin >> sc;
cout << "請輸入收件人姓名:";
cin >> n;
Order o(oid, sid, sp, sc, n);
orders[i] = o;
}
}
if (have == false)
{
cout << "沒有該訂單號的訂單" << endl;
return;
}
cout << "修改成功" << endl;
}
void deleteOrder(Order orders[], int& index)
{
if (index == 0)
{
cout << "訂單為空" << endl;
}
string oid;
cout << "請輸入要刪除的訂單編號" << endl;
cin >> oid;
bool have = false;
int i;
for (i = 0; i < index; i++)
{
if (orders[i].getOid() == oid)
{
have = true;
break;
}
}
if (have == false)
{
cout << "沒有該訂單號的訂單" << endl;
return;
}
else
{
for (int j = i; j < index - 1; j++)
{
orders[j] = orders[j + 1];
}
index--;
}
cout << "刪除成功" << endl;
}完整代碼
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
#define N 5
#define MAX 3 //最大訂單數(shù)量
class Order
{
public:
//構(gòu)造函數(shù)
Order(string oid = "", string sid = "", double sp = 0, int sc = 0, string n
= "")
{
order_num = oid;
goods_num = sid;
goods_price = sp;
goods_count = sc;
name = n;
}
//打印基本信息
void print()
{
cout << "訂單編號:" << setw(N) << order_num << " 商品編號:" <<
setw(N) << goods_num << " 商品價格:"
<< setw(N) << goods_price << " 商品數(shù)量:" << setw(N) <<
goods_count << " 收件人姓名:" << setw(N) << name << endl;
}
//獲得訂單編號
string getOid()
{
return order_num;
}
//獲得姓名
string getName()
{
return name;
}
private:
string order_num; //訂單編號
string goods_num; //商品編號
double goods_price; //商品價格
int goods_count; //商品數(shù)量
string name; //收件人姓名
};
Order orders[MAX];
int index = 0;
void add(Order orders[], int& index)
{
if (index == MAX)
{
cout << "訂單已滿" << endl;
return;
}
string oid;
string sid;
double sp;
int sc;
string n;
cout << "請輸入訂單編號:";
cin >> oid;
cout << "請輸入商品編號:";
cin >> sid;
cout << "請輸入商品價格:";
cin >> sp;
cout << "請輸入商品數(shù)量:";
cin >> sc;
cout << "請輸入收件人姓名:";
cin >> n;
Order o(oid, sid, sp, sc, n);
orders[index++] = o;
cout << "添加成功" << endl;
}
void visit(Order orders[], int& index)
{
cout << "目前共有" << index << "個訂單" << endl;
for (int i = 0; i < index; i++)
{
orders[i].print();
}
}
void find(Order orders[], int& index)
{
if (index == 0)
{
cout << "訂單為空" << endl;
}
int chiose;
cout << "請選擇查詢方式 1、訂單編號 2、收件人姓名" << endl;
cin >> chiose;
if (chiose == 1)
{
string oid;
cout << "請輸入要查詢的訂單編號" << endl;
cin >> oid;
for (int i = 0; i < index; i++)
{
if (orders[i].getOid() == oid)
{
orders[i].print();
}
}
}
else if (chiose == 2)
{
string name;
cout << "請輸入要查詢的收件人姓名" << endl;
cin >> name;
for (int i = 0; i < index; i++)
{
if (orders[i].getName() == name)
{
orders[i].print();
}
}
}
else
{
cout << "錯誤的選擇" << endl;
}
}
void fixed(Order orders[], int& index)
{
if (index == 0)
{
cout << "訂單為空" << endl;
}
string oid;
cout << "請輸入要修改的訂單編號" << endl;
cin >> oid;
bool have = false;
for (int i = 0; i < index; i++)
{
if (orders[i].getOid() == oid)
{
have = true;
string oid;
string sid;
double sp;
int sc;
string n;
cout << "請輸入訂單編號:";
cin >> oid;
cout << "請輸入商品編號:";
cin >> sid;
cout << "請輸入商品價格:";
cin >> sp;
cout << "請輸入商品數(shù)量:";
cin >> sc;
cout << "請輸入收件人姓名:";
cin >> n;
Order o(oid, sid, sp, sc, n);
orders[i] = o;
}
}
if (have == false)
{
cout << "沒有該訂單號的訂單" << endl;
return;
}
cout << "修改成功" << endl;
}
void deleteOrder(Order orders[], int& index)
{
if (index == 0)
{
cout << "訂單為空" << endl;
}
string oid;
cout << "請輸入要刪除的訂單編號" << endl;
cin >> oid;
bool have = false;
int i;
for (i = 0; i < index; i++)
{
if (orders[i].getOid() == oid)
{
have = true;
break;
}
}
if (have == false)
{
cout << "沒有該訂單號的訂單" << endl;
return;
}
else
{
for (int j = i; j < index - 1; j++)
{
orders[j] = orders[j + 1];
}
index--;
}
cout << "刪除成功" << endl;
}
int main(void)
{
int n = 0; //記錄口令輸入次數(shù)
string password; //輸入的密碼
cout << "請輸入登錄口令(默認(rèn)abcd)" << endl;
while (1)
{
cin >> password;
if (password == "abcd")
{
cout << "輸入口令正確!" << endl;
break;
}
else
{
cout << "輸入口令正確,請重新輸入!" << endl;
n++;
if (n == 3)
{
cout << "已輸入三次,您無權(quán)進(jìn)行操作!" << endl;
return 0;
}
}
}
cout << endl;
while (1)
{
system("cls");
cout << "*******************************************" << endl;
cout << "* 根據(jù)所做操作選擇一下數(shù)字序號 *" << endl;
cout << "* 1.添加訂單 2.瀏覽訂單 *" << endl;
cout << "* 3.查詢訂單 4.修改訂單 *" << endl;
cout << "* 5.刪除訂單 6.退出系統(tǒng) *" << endl;
cout << "*******************************************" << endl;
int n;
cin >> n;
switch (n)
{
case 1:
add(orders, index);
system("pause");
break;
case 2:
visit(orders, index);
system("pause");
break;
case 3:
find(orders, index);
system("pause");
break;
case 4:
fixed(orders, index);
system("pause");
break;
case 5:
deleteOrder(orders, index);
system("pause");
break;
case 6:
return 0;
break;
default:
break;
}
}
return 0;
}結(jié)語
該系統(tǒng)比較簡單,對數(shù)據(jù)的存儲使用了數(shù)組,也可以使用順序表,鏈表等數(shù)據(jù)結(jié)構(gòu),代碼中也有很多值得優(yōu)化的地方。
以上就是C++實(shí)現(xiàn)團(tuán)購訂單管理系統(tǒng)的詳細(xì)內(nèi)容,更多關(guān)于C++訂單管理系統(tǒng)的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
使用pybind11封裝C++結(jié)構(gòu)體作為參數(shù)的函數(shù)實(shí)現(xiàn)步驟
這篇文章主要介紹了用pybind11封裝C++結(jié)構(gòu)體作為參數(shù)的函數(shù)實(shí)現(xiàn)步驟,本文分步驟通過實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價值,需要的朋友可以參考下2020-02-02

