本文整理汇总了C++中Customer::subtractBudget方法的典型用法代码示例。如果您正苦于以下问题:C++ Customer::subtractBudget方法的具体用法?C++ Customer::subtractBudget怎么用?C++ Customer::subtractBudget使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Customer
的用法示例。
在下文中一共展示了Customer::subtractBudget方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: purchaseItem
void purchaseItem(vector<items> & vectorOfItems, Customer & user) {
int count = 0;
int index;
string input;
string temp;
cout << "Enter the name of the item: " << endl;
getline(cin, temp);
getline(cin, input);
for (unsigned int i = 0; i < input.length(); i++)
{
input[i] = tolower(input[i]);
}
bool isInStock = false;
for (size_t i = 0; i < vectorOfItems.size(); i++)
{
if (vectorOfItems[i].getItemName() == input) {
isInStock = true;
index = i;
count++;
}
}
if (!isInStock) {
cout << "Sorry, that item is not in stock" << endl;
}
else {
cout << "We have " << count << " " << input << "(s) in stock for " << vectorOfItems[index].getItemPrice() << " each. Continue with purchase?" << endl;
cout << "1. Yes" << endl;
cout << "2. No" << endl;
string optionS;
int option = 0;
getline(cin, optionS);
option = atoi(optionS.c_str());
while (!isNumber(optionS) || (option != 1 && option != 2))
{
cout << "Not a valid choice. Please enter 1 or 2." << endl;
getline(cin, optionS);
if (isNumber(optionS))
{
option = atoi(optionS.c_str());
}
}
if (option == 1)
{
int amt = -1;
cout << "How many would you like to purchase?" << endl;
getline(cin, optionS);
amt = atoi(optionS.c_str());
int num = 0;
while ((!isNumber(optionS)) || (amt < 1) || (amt > count))
{
cout << "Not a valid choice. Please enter a number between 1 and " << count << "." << endl;
getline(cin, optionS);
if (isNumber(optionS))
{
amt = atoi(optionS.c_str());
}
}
for (size_t i = 0; i < vectorOfItems.size(); i++)
{
if (input == vectorOfItems[i].getItemName())
{
if (user.noMoreMoney(vectorOfItems[i].getItemPrice()))
{
cout << "You have no more money left!" << endl;
break;
}
else
{
user.subtractBudget(vectorOfItems[i].getItemPrice());
num += 1;
vectorOfItems.erase(vectorOfItems.begin() + i);
i--;
amt--;
if (amt == 0)
i = vectorOfItems.size();
}
}
}
cout << "Congratulations, you purchased " << num << " " << input << "(s)!" << endl;
}
else if (option == 2)
{
}
else
{
cout << "Invalid choice. Purchase?" << endl;
cout << "1. Yes" << endl;
cout << "2. No" << endl;
}
}
}