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();
summary(); を呼び出すと、メインでは、何らかの理由で、次のようにリンゴのみを出力に表示するようです:
"ストアの購入合計: 食品: りんご | 数量: 7 | 価格: 0.99 購入合計: 6.93"
しかし、ご覧のとおり、Bob のカートにリンゴ、オレンジ、チップスをすべて追加したので、3 つすべてが表示されるはずですが、表示されません。助けていただければ幸いです。これはまだ進行中の作業です。なんか関係ある気がする
if( j == inv->_Purchases.size()) {
inv->_Purchases.push_back(_Cart[j]);
cout << "Checkout is Complete.\n";
return true;
But i am not sure.