0
    bool Customer::checkout(Inventory* inv) {

  double total = 0;

  for( unsigned int i=0; i < _Cart.size(); i++ ) {
      total += (_Cart[i].price * _Cart[i].quant); //

  }

  if( balance < total ) {
      cout << "Checkout Failed. You do not have enough money to afford everything." 
          <<"Please go back and remove items as necessary.\n";
      return false;
  }

  else {

    unsigned int j = 0;                                          //Then, you need to add said food into the purchased vector
    for (j = 0; j < inv->_Purchases.size(); j++)    {                //When you add the food into the purchased vector, you need to look through
        if (inv->_Purchases[j].name == _Cart[j].name) {     //the entire purchased vector to see if the food is already there, 
             inv->_Purchases[j].quant += _Cart[j].quant;     //if so, increment quantity if not, just push the food into the vector
             break;

        }
    }

         if( j == inv->_Purchases.size()) {
             inv->_Purchases.push_back(_Cart[j]);
             cout << "Checkout is Complete.\n";
             return true;
        }

        _Cart.clear();

  }



    balance -= total;
    inv->interval += 1;
    inv->restock( "restock fruits.txt", 2 );
    inv->restock( "restock inventory.txt", 3);
    cout << "Checkout Complete.\n";
    return true;

}



    void Inventory::summary() {
    double total = 0;
    for( unsigned int j=0; j<_Purchases.size(); j++ ) {
      cout << "\nTotal purchases for the store are:";
      cout << "\nFood: " << _Purchases[j].name << " | Quantity: " << _Purchases[j].quant << " | Price: " << _Purchases[j].price << endl;
      total += (_Purchases[j].quant * _Purchases[j].price);
  }
  cout << "Total Purchase: " << total << endl;

    //cout the purchased vector's .name
    //cout the quant 
    //cout the price*quant
    //make a total, and cout it at the end
    }

这是我的主要内容:

    #include "foodservice.h"
    #include <iostream>
    using namespace std;

    int main() {
      Inventory Master;
      bool flag;
      Customer Bob("Bob", 12345, 100.00 );
      Customer Joe("Joe", 56789, 50.00 );
      Customer Arjun("Arjun", 98765, 35.89 );
      Customer Randy("Randy", 54689, 30.28);
      Customer Mark("Mark", 76598, 15.18);


  Master.firststock( "inventory.txt" );
  vector<Food> temp = Master._Inv;
  cout <<"Hi, What would you like to buy today?" << endl;
  for(unsigned int i=0; i<temp.size(); i++ ) {
    cout << temp[i].name << " " << temp[i].quant << " " << temp[i].price << endl;
  }

  cout <<"\n";
  Food Apple("Apples", .99, 10);
  Food Oranges("Oranges", .99, 2);
  Food Chips("Chips", 3.00, 2);

  cout <<"\nHi Bob" << endl;
  flag = Bob.addCart(Apple, 7, &Master);
  cout <<"Bob's total purchases are Currently: \n";
  Bob.report();
  flag = Bob.addCart(Oranges, 2, &Master);
  flag = Bob.addCart(Chips, 2, &Master);
  flag = Bob.removeCart(Apple, 3, &Master);
  Bob.report();
  cout <<"Bob, ";
  flag = Bob.checkout(&Master);

  cout <<"\nHi Arjun" << endl;
  flag = Arjun.addCart(Apple, 3, &Master);
  cout <<"Arjun, ";
  Arjun.report();
  flag = Arjun.checkout(&Master);

  Master.summary();

当我调用摘要()时;主要,由于某种原因,我似乎只让苹果显示输出,如下所示:

“商店的总购买量为:食物:苹果 | 数量:7 | 价格:0.99 总购买量:6.93”

但是正如你所看到的,我已经将苹果、橙子和薯条都添加到了 Bob 的购物车中,所以这三个都应该显示,但它们不是。我将不胜感激。这仍在进行中。我感觉好像有关系

if( j == inv->_Purchases.size()) {
             inv->_Purchases.push_back(_Cart[j]);
             cout << "Checkout is Complete.\n";
             return true;

But i am not sure.
4

3 回答 3

2

问题出在您添加已购买商品的循环中。逻辑是错误的。

你写了

for (j = 0; j < inv->_Purchases.size(); j++)
    if (inv->_Purchases[j].name == _Cart[j].name)
...

但这_Purchases[0]_Cart[0]与等_Purchases[1]进行比较_Cart[1]。您需要的是两个循环,因此您可以将每个_Cart项目与每个_Purchases项目进行比较。像这样的东西

for (i = 0; i < _Cart.size(); i++)
{
    for (j = 0; j < inv->_Purchases.size(); j++)
    {
        if (_Cart[i].name == inv->_Purchases[j].name)
...

剩下的就交给你了。这将是一个很好的锻炼。

于 2012-08-05T08:15:51.610 回答
0

您比较不同数组上的相同索引,inv->_Purchases[ j ].name == _Cart[ j ].name并在添加一个元素后返回,如果_Cart.size() < inv->_Purchases.size()(缓冲区溢出)或_Cart.size() == 0(取消引用空指针)有问题。

所以改变这个:

{
    unsigned int j = 0;
    for (j = 0; j < inv->_Purchases.size(); j++)
    {
        if (inv->_Purchases[j].name == _Cart[j].name)
        {
            inv->_Purchases[j].quant += _Cart[j].quant;
            break;
        }
    }
    if( j == inv->_Purchases.size() )
    {
        inv->_Purchases.push_back( _Cart[j] );
        cout << "Checkout is Complete.\n";
        return true;
    }
    _Cart.clear();
}

进入这个:

{
    unsigned i, PurchasesBefore = inv->_Purchases.size();
    while( !_Cart.empty() )
    {
        for( i = 0; i != inv->_Purchases.size(); ++i )
        {
            if( inv->_Purchases[i].name == _Cart.back().name )
            {
                inv->_Purchases[i].quant += _Cart.back().quant;
                break;
            }
        }
        if( i == inv->_Purchases.size() )
            inv->_Purchases.push_back( _Cart.back() );
        _Cart.pop_back();
    }
    if( PurchasesBefore != inv->_Purchases.size() )
    {
        cout << "Checkout is Complete.\n";
        return true;
    }
}

注意:我不确定此代码:

balance -= total;
inv->interval += 1;
inv->restock( "restock fruits.txt", 2 );
inv->restock( "restock inventory.txt", 3);
cout << "Checkout Complete.\n";
return true;
于 2012-08-05T08:25:42.140 回答
0

您的搜索循环似乎不太正确:

  • 您使用相同的索引来扫描 _Purchases 和 _Cart
  • 当第一个产品与库存匹配时,您就打破了循环

也许匹配的计数器,在循环完成时检查 _Cart.size(),可以更好地工作。

于 2012-08-05T08:15:49.703 回答