
【急】一道C++题,求高手帮我解决!!! 10
Writeamenudrivenprogramforasoftdrinkdistributioncompany.Thereare4brands:Coca-Cola(ID:...
Write a menu driven program for a soft drink distribution company. There are 4 brands:
Coca-Cola (ID: 1), Pesi (ID: 2), Canada Dry (ID: 3), Seven-up (ID-4).
The menu items are:
(E)nter Inventory
(P)urchase Soda
(S)ell Soda
(D)isplay inventory
(Q)uit program
For menu items E, P, S, first the program will prompt the user to enter the ID of the
brand. Second will be
the amount of inventory/amount purchase/amount sold.
If data is entered so that the inventory will become negative, you must reject the operation,
inform the user that there is error in the data entered.
Test Data:
Enter the operation that you want to perform
(E)nter Inventory
(P)urchase Soda
(S)ell Soda
(D)isplay inventory
(Q)uit program
E
Enter brand : 1
Enter inventory : -10
inventory must be > 0. Try again !!
Enter inventory : 35
Enter the operation that you want to perform
(E)nter Inventory
(P)urchase Soda
(S)ell Soda
(D)isplay inventory
(Q)uit program 展开
Coca-Cola (ID: 1), Pesi (ID: 2), Canada Dry (ID: 3), Seven-up (ID-4).
The menu items are:
(E)nter Inventory
(P)urchase Soda
(S)ell Soda
(D)isplay inventory
(Q)uit program
For menu items E, P, S, first the program will prompt the user to enter the ID of the
brand. Second will be
the amount of inventory/amount purchase/amount sold.
If data is entered so that the inventory will become negative, you must reject the operation,
inform the user that there is error in the data entered.
Test Data:
Enter the operation that you want to perform
(E)nter Inventory
(P)urchase Soda
(S)ell Soda
(D)isplay inventory
(Q)uit program
E
Enter brand : 1
Enter inventory : -10
inventory must be > 0. Try again !!
Enter inventory : 35
Enter the operation that you want to perform
(E)nter Inventory
(P)urchase Soda
(S)ell Soda
(D)isplay inventory
(Q)uit program 展开
展开全部
写好了。总共三个文件,main.cpp ,company.h, company.cpp。
其中main.cpp为主程序文件,company.h和company.cpp定义了一个company的类。
********* company.h *******
#ifndef COMPANY_H
#define COMPANY_H
class company
{
public:
company():coca_count(0),pesi_count(0),canada_count(0),seven_count(0){}
void inventory();
void purchase();
void sell();
void display();
private:
int coca_count;
int pesi_count;
int canada_count;
int seven_count;
static enum{Coca_Cola,Pesi,Canada_Dry,Seven_up};
};
#endif
************ company.h **************
*********** company.cpp ************
#include "company.h"
#include <iostream>
using std::cout;
using std::cin;
using std::endl;
void company::inventory()
{
cout << "Enter brand : ";
int brand_num ;
cin >> brand_num;
while(brand_num <= 0 || brand_num > 4)
{
cout << "brand must be 1 or 2 or 3 or 4. Try again !!" << endl;
cout << "Enter brand : ";
cin >> brand_num;
}
cout << "Enter inventory : ";
int inventory_num;
cin >> inventory_num;
while(inventory_num < 0)
{
cout << "inventory must be > 0. Try again !!" << endl;
cout << "Enter inventory : ";
cin >> inventory_num;
}
switch(brand_num)
{
case 1:
coca_count = inventory_num;
break;
case 2:
pesi_count = inventory_num;
break;
case 3:
canada_count = inventory_num;
break;
case 4:
seven_count = inventory_num;
break;
default:
break;
}
}
void company::purchase()
{
cout << "Enter brand : ";
int brand_num ;
cin >> brand_num;
while(brand_num <= 0 || brand_num > 4)
{
cout << "brand must be 1 or 2 or 3 or 4. Try again !!" << endl;
cout << "Enter brand : ";
cin >> brand_num;
}
cout << "Enter amount : ";
int inventory_num;
cin >> inventory_num;
while(inventory_num <= 0)
{
cout << "amount must be > 0. Try again !!" << endl;
cout << "Enter inventory : ";
cin >> inventory_num;
}
switch(brand_num)
{
case 1:
coca_count += inventory_num;
break;
case 2:
pesi_count += inventory_num;
break;
case 3:
canada_count += inventory_num;
break;
case 4:
seven_count += inventory_num;
break;
default:
break;
}
}
void company::sell()
{
cout << "Enter brand : ";
int brand_num ;
cin >> brand_num;
while(brand_num <= 0 || brand_num > 4)
{
cout << "brand must be 1 or 2 or 3 or 4. Try again !!" << endl;
cout << "Enter brand : ";
cin >> brand_num;
}
cout << "Enter bottles : ";
int inventory_num;
cin >> inventory_num;
while(inventory_num < 0)
{
cout << "bottles must be > 0. Try again !!" << endl;
cout << "Enter inventory : ";
cin >> inventory_num;
}
switch(brand_num)
{
case 1:
if(coca_count <inventory_num)
cout << "Sorry,Coca Cola are not enough!" << endl;
else
{
coca_count -= inventory_num;
}
break;
case 2:
if(pesi_count <inventory_num)
cout << "Sorry,Pesi are not enough!" << endl;
else
{
pesi_count -= inventory_num;
}
break;
case 3:
if(canada_count <inventory_num)
cout << "Sorry,Canada Dry are not enough!" << endl;
else
{
canada_count -= inventory_num;
}
break;
case 4:
if(seven_count <inventory_num)
cout << "Sorry,Seven-Up are not enough!" << endl;
else
{
seven_count -= inventory_num;
}
break;
default:
break;
}
}
void company::display()
{
cout << "Coca Cola inventory : " << coca_count << endl;
cout << "Pesi inventory : " << pesi_count << endl;
cout << "Canada Dry inventory : " << canada_count << endl;
cout << "Seven-Up inventory : " << seven_count << endl;
}
*************** company.cpp *************
**************** main.cpp ***************
#include "company.h"
#include <iostream>
using std::cout;
using std::cin;
using std::endl;
int main()
{
company zhang;
char choice = 'h';
while(choice != 'q' && choice != 'Q')
{
cout << "Enter the operation that you want to perform\n"
<<"(E)nter Inventory\n"
<<"(P)urchase Soda\n"
<<"(S)ell Soda\n"
<<"(D)isplay inventory\n"
<<"(Q)uit program"<<endl;
cin >> choice;
switch(choice)
{
case 'e':
case 'E':
zhang.inventory();
break;
case 'p':
case 'P':
zhang.purchase();
break;
case 's':
case 'S':
zhang.sell();
break;
case 'd':
case 'D':
zhang.display();
break;
case 'q':
case 'Q':
return 0 ;
break;
}//switch
}//while
return 0;
}
***************** main.cpp *************
其中main.cpp为主程序文件,company.h和company.cpp定义了一个company的类。
********* company.h *******
#ifndef COMPANY_H
#define COMPANY_H
class company
{
public:
company():coca_count(0),pesi_count(0),canada_count(0),seven_count(0){}
void inventory();
void purchase();
void sell();
void display();
private:
int coca_count;
int pesi_count;
int canada_count;
int seven_count;
static enum{Coca_Cola,Pesi,Canada_Dry,Seven_up};
};
#endif
************ company.h **************
*********** company.cpp ************
#include "company.h"
#include <iostream>
using std::cout;
using std::cin;
using std::endl;
void company::inventory()
{
cout << "Enter brand : ";
int brand_num ;
cin >> brand_num;
while(brand_num <= 0 || brand_num > 4)
{
cout << "brand must be 1 or 2 or 3 or 4. Try again !!" << endl;
cout << "Enter brand : ";
cin >> brand_num;
}
cout << "Enter inventory : ";
int inventory_num;
cin >> inventory_num;
while(inventory_num < 0)
{
cout << "inventory must be > 0. Try again !!" << endl;
cout << "Enter inventory : ";
cin >> inventory_num;
}
switch(brand_num)
{
case 1:
coca_count = inventory_num;
break;
case 2:
pesi_count = inventory_num;
break;
case 3:
canada_count = inventory_num;
break;
case 4:
seven_count = inventory_num;
break;
default:
break;
}
}
void company::purchase()
{
cout << "Enter brand : ";
int brand_num ;
cin >> brand_num;
while(brand_num <= 0 || brand_num > 4)
{
cout << "brand must be 1 or 2 or 3 or 4. Try again !!" << endl;
cout << "Enter brand : ";
cin >> brand_num;
}
cout << "Enter amount : ";
int inventory_num;
cin >> inventory_num;
while(inventory_num <= 0)
{
cout << "amount must be > 0. Try again !!" << endl;
cout << "Enter inventory : ";
cin >> inventory_num;
}
switch(brand_num)
{
case 1:
coca_count += inventory_num;
break;
case 2:
pesi_count += inventory_num;
break;
case 3:
canada_count += inventory_num;
break;
case 4:
seven_count += inventory_num;
break;
default:
break;
}
}
void company::sell()
{
cout << "Enter brand : ";
int brand_num ;
cin >> brand_num;
while(brand_num <= 0 || brand_num > 4)
{
cout << "brand must be 1 or 2 or 3 or 4. Try again !!" << endl;
cout << "Enter brand : ";
cin >> brand_num;
}
cout << "Enter bottles : ";
int inventory_num;
cin >> inventory_num;
while(inventory_num < 0)
{
cout << "bottles must be > 0. Try again !!" << endl;
cout << "Enter inventory : ";
cin >> inventory_num;
}
switch(brand_num)
{
case 1:
if(coca_count <inventory_num)
cout << "Sorry,Coca Cola are not enough!" << endl;
else
{
coca_count -= inventory_num;
}
break;
case 2:
if(pesi_count <inventory_num)
cout << "Sorry,Pesi are not enough!" << endl;
else
{
pesi_count -= inventory_num;
}
break;
case 3:
if(canada_count <inventory_num)
cout << "Sorry,Canada Dry are not enough!" << endl;
else
{
canada_count -= inventory_num;
}
break;
case 4:
if(seven_count <inventory_num)
cout << "Sorry,Seven-Up are not enough!" << endl;
else
{
seven_count -= inventory_num;
}
break;
default:
break;
}
}
void company::display()
{
cout << "Coca Cola inventory : " << coca_count << endl;
cout << "Pesi inventory : " << pesi_count << endl;
cout << "Canada Dry inventory : " << canada_count << endl;
cout << "Seven-Up inventory : " << seven_count << endl;
}
*************** company.cpp *************
**************** main.cpp ***************
#include "company.h"
#include <iostream>
using std::cout;
using std::cin;
using std::endl;
int main()
{
company zhang;
char choice = 'h';
while(choice != 'q' && choice != 'Q')
{
cout << "Enter the operation that you want to perform\n"
<<"(E)nter Inventory\n"
<<"(P)urchase Soda\n"
<<"(S)ell Soda\n"
<<"(D)isplay inventory\n"
<<"(Q)uit program"<<endl;
cin >> choice;
switch(choice)
{
case 'e':
case 'E':
zhang.inventory();
break;
case 'p':
case 'P':
zhang.purchase();
break;
case 's':
case 'S':
zhang.sell();
break;
case 'd':
case 'D':
zhang.display();
break;
case 'q':
case 'Q':
return 0 ;
break;
}//switch
}//while
return 0;
}
***************** main.cpp *************
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询