“网上买菜系统”的分析

以下是借用的同学写的网上买菜系统的代码:

一、分析代码

buy.h文件

#include<iostream>

#include<string>

using namespace std;

class buyer

{

protected:

        string name;

        int buyerID;

        int kilogram;

        double pay;

public:

        buyer();

        buyer(string n, int b, int k, double p);

        string getbuyname();

        double getkilogram();

        double getpay();

        int getid();

        virtual void display() = 0;

        virtual void setpay(double = 0) = 0;

};

class layfolk :public buyer

{

public:

        layfolk(string n, int b, int k, double p) :buyer(n, b, k, p)

        {

 

        }

        void display();

        void setpay(double p);

};

class member :public buyer

{

        int leaguer_grade;

public:

        member(string n, int b, int l, int k, double p) :buyer(n, b, k, p)

        {

                leaguer_grade = l;

        }

        void display();

        void setpay(double p);

};

buyer::buyer()

{

        name = "";

        buyerID = 0;

        kilogram = 0;

        pay = 0;

}

buyer::buyer(string n, int b, int k, double p)

{

        name = n;

        buyerID = b;

        kilogram = k;

        pay = p;

}

double buyer::getpay()

{

        return pay;

}

double buyer::getkilogram()

{

        return kilogram;

}

string buyer::getbuyname()

{

        return name;

}

int buyer::getid()

{

        return buyerID;

}

void layfolk::display()                //显示购菜人的信息和订单信息

{

        cout <<"购菜人姓名:" << name << "\t";

        cout <<"购菜人编号:" << buyerID << "\t";

        cout <<"购菜人为" << "\t\t\t";

        cout <<"购菜数量各为:" << kilogram <<"公斤\n";

}

void layfolk::setpay(double p)           //价格计算公式

{

        pay = pay + p*kilogram;

}

 

vegetables.h文件

#include<iostream>

#include<string>

using namespace std;

class vegetables

{

public:

        int vegetables_number;

        string vegetables_name;

        string vegetables_date;

        double price;

public:

        vegetables();

        vegetables(int v_num,string v_n, string v_d, double pr);

        void display();

        int getvegetables_number();

        string getvegetables_name();

        string getvegetables_date();

        double getprice();

};

vegetables::vegetables(int v_num,string v_n, string v_d, double pr)

{

        vegetables_number=v_num;

        vegetables_name=v_n;

        vegetables_date=v_d;

        price = pr;

}

vegetables::vegetables()

{

        vegetables_number=0;

        vegetables_name= "";

        vegetables_date= "";

        price = 0;

}

void vegetables::display()                   //显示蔬菜信息

{

        cout <<"蔬菜编号:"<<vegetables_number<<"\t\t";

        cout << "蔬菜名称:" << vegetables_name << "\t\t";

        cout << "蔬菜到货日期:" << vegetables_date << "\t\t";

        cout << "定价:" << price << "\n";

}

int vegetables::getvegetables_number()

{

        return vegetables_number;

}

string vegetables::getvegetables_name()

{

        return vegetables_name;

}

string vegetables::getvegetables_date()

{

        return vegetables_date;

}

double vegetables::getprice()

{

        return price;

}

 

Main函数

#include<iostream>

#include<cassert>

#include<fstream>

#include<string>

#include"buy.h"

#include"vegetables.h"

using namespace std;

int main()

{   ifstream fin("C:\\Users\\39230\\Desktop\\蔬菜\\菜单信息.txt",ios::in);              //打开文件

        if(fin)

                cout<<"\n"<<endl;

        else

                cout<<"文件打开失败"<<endl;

    int i;

        vegetables c[12];

    for(i=0;i<12;i++)

        {

            fin>>c[i].vegetables_number>>c[i].vegetables_name>>c[i].vegetables_date>>c[i].price;

                }

        fin.close();             //关闭文件

        int sum=0;

        for(int q=0;q<12;q++)             //统计价格

        {

                sum=sum+c[q].price;

        }

        char m;

        while(m!=5)

        {

    system("cls");                   //清屏

 

    cout<<"\n";

        cout<<"                            ****************************"<<endl;

        cout<<"                            *   欢迎来到网上购菜系统   *"<<endl;

        cout<<"                            *==========================*"<<endl;

        cout<<"                            *       1.蔬菜信息         *"<<endl;

        cout<<"                            *       2.订单人信息       *"<<endl;

        cout<<"                            *       3.金额统计         *"<<endl;

        cout<<"                            *       4.退出系统         *"<<endl;

        cout<<"                            *      请选择功能1-4       *"<<endl;

        cout<<"                            ****************************"<<endl;                //显示开始界面

    char k;

        cin>>k;

        fflush(stdin);

        if ((k<1)&&(k>4))

               

        {

                cout<<"输入编号错误!"<<endl;

                fflush(stdin);

                return 0;

        }

        system("cls");

        switch(k)                      //主界面跳转

        {

        case'1':                    //蔬菜信息

                {

                        system("cls");

                        cout<<"                                     ==========\n";

                        cout<<"                                     |蔬菜信息|\n";

                        cout<<"                                     ==========\n";

                for (i = 0; i < 12; i++)

                     c[i].display();

                fflush(stdin);          

                ;}

                break;

        case'2':                 //订单人信息

                {

                        system("cls");

                        layfolk b1("张三", 1, 10, 0);

                        member b2("李四",2,5,10,0);

                        buyer*b[2] = { &b1,&b2};

                        cout <<"                                  ============\n";

                        cout <<"                                  |购菜人信息|\n";

                        cout <<"                                  ============\n";

                        for (i = 0; i < 3; i++)

                                b[i]->display();

                                }break;

        case'3':                   //金额统计

                {

                        system("cls");

                        int buyerid, flag;

                        layfolk b1("张三", 1, 90, 0);

                        member b2("李四",2,5,10,0);

                        buyer*b[1] = { &b1 ,&b2};

                        cout << "\n请输入购菜人编号:";

                        cin >> buyerid;

                        flag = 0;

                        for(i=0;i<3;i++)

                                if (b[i]->getid() == buyerid)

                {

                        flag = 1;

                        break;

                }

        if (!flag)

        {

                cout << "编号不存在" << endl;

        }

        else

        {

                b[i]->setpay(sum);

                cout << endl << "购菜人"<<b[i]->getbuyname()<<"需要付费:" << b[i]->getpay() << "元"<<"\n";

        }

                }break;

               

        case'4':                //退出系统

        {

                system("cls");

                cout <<"                                 ===============\n";

                cout<<"                                 |谢谢使用!再见!|"<<endl;

                cout <<"                                 ===============\n";

                fflush(stdin);

                return 0;

        }

        break;}

        system("pause");

        }

        return 0;

}

运行结果:

二、增加功能

//顾客有会员和非会员之分,价格也会有所变化,添加了以下代码来实现不同顾客之间的购物价格的不同计算公式。

buy.h文件

#include<iostream>

#include<string>

using namespace std;

class buyer

{

protected:

        string name;

        int buyerID;

        int kilogram;

        double pay;

public:

        buyer();

        buyer(string n, int b, int k, double p);

        string getbuyname();

        double getkilogram();

        double getpay();

        int getid();

        virtual void display() = 0;

        virtual void setpay(double = 0) = 0;

};

class member :public buyer

{

        int leaguer_grade;

public:

        member(string n, int b, int l, int k, double p) :buyer(n, b, k, p)

        {

                leaguer_grade = l;

        }

        void display();

        void setpay(double p);

};

class honoured_guest :public buyer

{

        double discount_rate;

public:

        honoured_guest(string n, int b, double r, int k, double p) :buyer(n, b, k, p)

        {

                discount_rate = r;

        }

        void display();

        void setpay(double p);

};

class layfolk :public buyer

{

public:

        layfolk(string n, int b, int k, double p) :buyer(n, b, k, p)

        {

 

        }

        void display();

        void setpay(double p);

};

buyer::buyer()

{

        name = "";

        buyerID = 0;

        kilogram = 0;

        pay = 0;

}

buyer::buyer(string n, int b, int k, double p)

{

        name = n;

        buyerID = b;

        kilogram = k;

        pay = p;

}

double buyer::getpay()

{

        return pay;

}

double buyer::getkilogram()

{

        return kilogram;

}

string buyer::getbuyname()

{

        return name;

}

int buyer::getid()

{

        return buyerID;

}

void member::display()

{

        cout <<"购菜人姓名:" << name << "\t";

        cout <<"购菜人编号:" << buyerID << "\t";

        cout <<"购菜人为会员,级别:" << leaguer_grade << "\t\t";

        cout <<"购菜数量各为:" << kilogram << "公斤\n";

}

void member::setpay(double p)

{

        if (leaguer_grade == 1)

                pay = 0.95*p*kilogram + pay;

        else if (leaguer_grade == 2)

                pay = 0.90*p*kilogram + pay;

        else if (leaguer_grade == 3)

                pay = 0.85*p*kilogram + pay;

        else if (leaguer_grade == 4)

                pay = 0.80*p*kilogram + pay;

        else if (leaguer_grade == 5)

                pay = 0.7*p*kilogram + pay;

        else

                cout <<"级别错误!";

}

void honoured_guest::display()

{

        cout <<"购菜人姓名:" << name << "\t";

        cout <<"购菜人编号:" << buyerID << "\t";

        cout <<"购菜人为贵宾,折扣率为:" << discount_rate * 100 << "%\t";

        cout <<"购菜数量各为:" << kilogram << "公斤\n";

}

void honoured_guest::setpay(double p)

{

        pay = pay + (1 - discount_rate)*p*kilogram;

}

void layfolk::display()

{

        cout <<"购菜人姓名:" << name << "\t";

        cout <<"购菜人编号:" << buyerID << "\t";

        cout <<"购菜人为普通人" << "\t\t\t";

        cout <<"购菜数量各为:" << kilogram <<"公斤\n";

}

void layfolk::setpay(double p)

{

        pay = pay + p*kilogram;

}

vegetables.h文件

#include<iostream>

#include<string>

using namespace std;

class vegetables

{

public:

        int vegetables_number;

        string vegetables_name;

        string vegetables_date;

        double price;

public:

        vegetables();

        vegetables(int v_num,string v_n, string v_d, double pr);

        void display();

        int getvegetables_number();

        string getvegetables_name();

        string getvegetables_date();

        double getprice();

};

vegetables::vegetables(int v_num,string v_n, string v_d, double pr)

{

        vegetables_number=v_num;

        vegetables_name=v_n;

        vegetables_date=v_d;

        price = pr;

}

vegetables::vegetables()

{

        vegetables_number=0;

        vegetables_name= "";

        vegetables_date= "";

        price = 0;

}

void vegetables::display()

{

        cout <<"蔬菜编号:"<<vegetables_number<<"\t\t";

        cout << "蔬菜名称:" << vegetables_name << "\t\t";

        cout << "蔬菜到货日期:" << vegetables_date << "\t\t";

        cout << "定价:" << price << "\n";

}

int vegetables::getvegetables_number()

{

        return vegetables_number;

}

string vegetables::getvegetables_name()

{

        return vegetables_name;

}

string vegetables::getvegetables_date()

{

        return vegetables_date;

}

double vegetables::getprice()

{

        return price;

}

 

Main函数

#include<iostream>

#include<cassert>

#include<fstream>

#include<string>

#include"buy.h"

#include"vegetables.h"

using namespace std;

int main()

{   ifstream fin("C:\\Users\\39230\\Desktop\\蔬菜\\菜单信息.txt",ios::in);

        if(fin)

                cout<<"\n"<<endl;

        else

                cout<<"文件打开失败"<<endl;

    int i;

        vegetables c[12];

    for(i=0;i<12;i++)

        {

            fin>>c[i].vegetables_number>>c[i].vegetables_name>>c[i].vegetables_date>>c[i].price;

                }

        fin.close();

        int sum=0;

        for(int q=0;q<12;q++)

        {

                sum=sum+c[q].price;

        }

        char m;

        while(m!=5)

        {

    system("cls");

 

    cout<<"\n";

        cout<<"                            ****************************"<<endl;

        cout<<"                            *   欢迎来到网上购菜系统   *"<<endl;

        cout<<"                            *==========================*"<<endl;

        cout<<"                            *       1.蔬菜信息         *"<<endl;

    cout<<"                            *       2.订单人信息       *"<<endl;

        cout<<"                            *       3.金额统计         *"<<endl;

        cout<<"                            *       4.退出系统         *"<<endl;

    cout<<"                            *      请选择功能1-4       *"<<endl;

        cout<<"                            ****************************"<<endl;

    char k;

        cin>>k;

        fflush(stdin);

        if ((k<1)&&(k>4))

               

        {

                cout<<"输入编号错误!"<<endl;

                fflush(stdin);

                return 0;

        }

        system("cls");

        switch(k)

        {

        case'1':

                {

                        system("cls");

                        cout<<"                                     ==========\n";

                        cout<<"                                     |蔬菜信息|\n";

                        cout<<"                                     ==========\n";

                for (i = 0; i < 12; i++)

                     c[i].display();

                fflush(stdin);          

                ;}

                break;

        case'2':

                {

                        system("cls");

                        layfolk b1("张三", 1, 10, 0);

                        honoured_guest b2("李四", 2, 0.6, 20, 0);

                        member b3("王五",3, 5, 15, 0);

                        buyer*b[3] = { &b1,&b2,&b3 };

                        cout <<"                                  ============\n";

                        cout <<"                                  |购菜人信息|\n";

                        cout <<"                                  ============\n";

                        for (i = 0; i < 3; i++)

                                b[i]->display();

                                }break;

        case'3':

                {

                        system("cls");

                        int buyerid, flag;

                        layfolk b1("张三", 1, 90, 0);

                        honoured_guest b2("李四", 2, 0.6, 80, 0);

                        member b3("王五",3, 5, 100, 0);

                        buyer*b[3] = { &b1,&b2,&b3 };

                        cout << "\n请输入购菜人编号:";

                        cin >> buyerid;

                        flag = 0;

                        for(i=0;i<3;i++)

                                if (b[i]->getid() == buyerid)

                {

                        flag = 1;

                        break;

                }

        if (!flag)

        {

                cout << "编号不存在" << endl;

        }

        else

        {

                b[i]->setpay(sum);

                cout << endl << "购菜人"<<b[i]->getbuyname()<<"需要付费:" << b[i]->getpay() << "元"<<"\n";

        }

                }break;

               

        case'4':

        {

                system("cls");

                cout <<"                                 ===============\n";

                cout<<"                                 |谢谢使用!再见!|"<<endl;

                cout <<"                                 ===============\n";

                fflush(stdin);

                return 0;

        }

        break;}

        system("pause");

        }

        return 0;

}

三、测试用例

测试用例ID 场景  测试步骤  预期结果  备注
 TC1  初始页面显示  运行程序  页面元素完整,显示与详细设计一致  
 TC2  用户会员等级录入—验证  输入已存在的用户“李四”的会员等级  输入成功  
 TC3  用户会员等级—容错性验证  输入:7778899  信息录入失败  
 TC4  主界面链接  输入1,2,3,4测试是否成功跳转  成功跳转  
 TC5  显示蔬菜信息  键盘输入1,文件导入蔬菜信息  导入成功并显示  
 TC6  订单人信息 键盘输入2,输入多位订单人信息,并选择一位显示  正确显示  
 TC7 金额统计  键盘输入3,输入非常大的购物清单,测试是否能计算准确  准确计算  
 TC8  退出程序  键盘输入4  成功退出程序  

猜你喜欢

转载自www.cnblogs.com/areilxu/p/10469809.html