界面要求:
如果选择了 1(2、3、4类似)界面如下:
如果选择了 0
输入:
用户输入功能选择的编号和购买商品时的数量
输出:
主菜单、各类商品的购买、以及结算退出的界面
主要处理过程:
根据用户的选择切换界面并将用户要购买的商品存储在结构体数组里,最后在用户退出时结算并退出。
#include
#include
#include
#include
using namespace std;
//存放顾客购买的商品信息
struct commodity{
int no;
string name;
double price;
int amount;
};
//标志循环是否结束的标志
bool flag = true;
//存放顾客购买商品的数组
vector<commodity> shoppingCar;
//结算退出界面,主要完成已购买商品的信息输出和总额的计算
bool settlementAndExit(){
system("cls");
double sum = 0.0;
cout << "ShoppingCar Commodity List:" << endl;
cout << "no\tname\tprice\tamount" << endl;
//输出已购买商品的信息
vector<commodity>::iterator it;
for(it = shoppingCar.begin();it != shoppingCar.end();it++){
commodity p = *it;
cout << p.no << "\t" << p.name <<"\t"<<p.price<<"\t"<<p.amount << endl;
sum += p.price * p.amount;
}
cout << endl << "Totally Need To Pay: " << sum << "元" << endl;
cout << "-------------------" << endl;
cout << "Good Bye!" << endl;
cout << "Press Any to Exit! ";
system("pause");
system("cls");
return false;
}
//Bread购买界面,其余商品类似,不做过多说明
void buyBread(){
system("cls");
int n;
cout << "Please Input The Bread Amount: ";
cin >> n;
bool again = true;
vector<commodity>::iterator it;
for(it = shoppingCar.begin();it != shoppingCar.end();++it){
if((*it).no == 1){
again = false;
(*it).amount += n;
break;
}
}
if(again){
commodity temp = {1,"Bread",1.00,n};
shoppingCar.push_back(temp);
}
system("cls");
cout << "I Hank You!" << endl;
cout << "You Have Selected [Bread], $1.00 * " << n << endl;
cout << "-------------------" <<endl << endl;
do{
cout <<"<1> Continue Buy <2> Settlement & Exit" << endl;
cout <<"Please Select A Number To Continue Buy Or Settle&Exit : ";
cin >> n;
switch(n){
case 1 : system("cls");break;
case 2 : flag = settlementAndExit();break;
default : n = 0;
cout << "Please Right Number, Press Any Key To Continue!";
cin.get();
system("cls");
break;
}
}while(!n);
}
void buyCocacola(){
system("cls");
int n;
cout << "Please Input The Coca Cola Amount: ";
cin >> n;
bool again = true;
vector<commodity>::iterator it;
for(it = shoppingCar.begin();it != shoppingCar.end();++it){
if((*it).no == 2){
again = false;
(*it).amount += n;
break;
}
}
if(again){
commodity temp = {2,"Coca",2.50,n};
shoppingCar.push_back(temp);
}
system("cls");
cout << "I Hank You!" << endl;
cout << "You Have Selected [Coca Cola], $2.50 * " << n << endl;
cout << "-------------------" <<endl << endl;
do{
cout <<"<1> Continue Buy <2> Settlement & Exit" << endl;
cout <<"Please Select A Number To Continue Buy Or Settle&Exit : ";
cin >> n;
switch(n){
case 1 : system("cls");break;
case 2 : flag = settlementAndExit();break;
default : n = 0;
cout << "Please Right Number, Press Any Key To Continue!";
cin.get();
system("cls");
break;
}
}while(!n);
}
void buyBeer(){
system("cls");
int n;
cout << "Please Input The Beer Amount: ";
cin >> n;
bool again = true;
vector<commodity>::iterator it;
for(it = shoppingCar.begin();it != shoppingCar.end();++it){
if((*it).no == 3){
again = false;
(*it).amount += n;
break;
}
}
if(again){
commodity temp = {3,"Beer",10.00,n};
shoppingCar.push_back(temp);
}
system("cls");
cout << "I Hank You!" << endl;
cout << "You Have Selected [Beer], $10.00 * " << n << endl;
cout << "-------------------" <<endl << endl;
do{
cout <<"<1> Continue Buy <2> Settlement & Exit" << endl;
cout <<"Please Select A Number To Continue Buy Or Settle&Exit : ";
cin >> n;
switch(n){
case 1 : system("cls");break;
case 2 : flag = settlementAndExit();break;
default : n = 0;
cout << "Please Right Number, Press Any Key To Continue!";
cin.get();
system("cls");
break;
}
}while(!n);
}
void buyChocolates(){
system("cls");
int n;
cout << "Please Input The Chocolates Amount: ";
cin >> n;
bool again = true;
vector<commodity>::iterator it;
for(it = shoppingCar.begin();it != shoppingCar.end();++it){
if((*it).no == 4){
again = false;
(*it).amount += n;
break;
}
}
if(again){
commodity temp = {4,"Choc",2.50,n};
shoppingCar.push_back(temp);
}
system("cls");
cout << "I Hank You!" << endl;
cout << "You Have Selected [Chocolates], $2.50 * " << n << endl;
cout << "-------------------" <<endl << endl;
do{
cout <<"<1> Continue Buy <2> Settlement & Exit" << endl;
cout <<"Please Select A Number To Continue Buy Or Settle&Exit : ";
cin >> n;
switch(n){
case 1 : system("cls");break;
case 2 : flag = settlementAndExit();break;
default : n = 0;
cout << "Please Right Number, Press Any Key To Continue!";
cin.get();
system("cls");
break;
}
}while(!n);
}
int main()
{
//更改控制台标题
system("title Xiao Fang Conveniences Store ");
//更改窗口大小
system("mode con cols=50 lines=15");
//更改背景和前景色
system("color F5");
//主界面
do{
int temp;
cout << "Store" << endl;
cout << "*************************************************" << endl;
cout << " Xiao Fang Conveniences Store"<<endl;
cout << "*************************************************" << endl;
cout << "<1> Bread 1.00" << endl;
cout << "<2> Coca cola 2.50" << endl;
cout << "<3> Beer 10.00" << endl;
cout << "<4> Chocolates 2.50" << endl << endl;
cout << "<0> Settlement & Exit" << endl;
cout << "-------------------------------------------------" << endl;
cout << "Please Select A Number: ";
cin >> temp;
switch(temp){
case 1 : buyBread();break;
case 2 : buyCocacola();break;
case 3 : buyBeer();break;
case 4 : buyChocolates();break;
case 0 : flag = settlementAndExit();break;
default : system("cls");
cout << "Please Right Number,press any key to choose again! ";
system("pause");
break;
}
system("cls");
}while(flag);
return 0;
}
测试用例:
Test2:bread购买界面测试
Test3:coca cola购买界面测试
Test4:beer购买界面测试
Test5:chocolates购买界面测试
Test6:结算退出界面测试
其余功能这里不做展示(所有功能经测试都是可行的)
更多相关内容请参见
我的博客