// program entry point
int main()
{
	//RBTreeTest();

	int choice = 0;
	string inputchoice;
	int asku;
	string inputasku;
	string adesc;
	double aprice;
	string inputaprice;
	int amount;
	string inputamt;
	string ctlg = "";

	StockSystem mystore;

	while (choice != 8)
	{
		PrintMenu();
		// get the menu choice from standard input
		getline(cin, inputchoice);
		choice = atoi(inputchoice.c_str());

		switch (choice)
		{
		case 1: // Print balance
			cout << "Funds: $" << mystore.GetBalance() << endl << endl;
			break;
		case 2: // Print catalogue
			ctlg = mystore.GetCatalogue();
			cout << ctlg << endl;
			break;
		case 3: // Add SKU
			cout << "Enter a numeric SKU (will be converted to 5 digits): ";
			getline(cin, inputasku);
			asku = atoi(inputasku.c_str());
			cout << "Enter item description: ";
			getline(cin, adesc);
			cout << "Enter a retail price: $";
			getline(cin, inputaprice);
			aprice = atoi(inputaprice.c_str());
			if (mystore.StockNewItem(StockItem(asku, adesc, aprice)))
				cout << "Successfully added item to catalogue." << endl;
			else
				cout << "Item not added to catalogue." << endl;
			break;
		case 4: // Edit item description
			cout << "Enter a numeric SKU to edit: ";
			getline(cin, inputasku);
			asku = atoi(inputasku.c_str());
			cout << "Enter item description: ";
			getline(cin, adesc);
			if (mystore.EditStockItemDescription(asku, adesc))
				cout << "Item successfully updated." << endl;
			else
				cout << "Item not updated." << endl;
			break;
		case 5: // Edit item price
			cout << "Enter a numeric SKU to edit: ";
			getline(cin, inputasku);
			asku = atoi(inputasku.c_str());
			cout << "Enter a retail price: $";
			getline(cin, inputaprice);
			aprice = atoi(inputaprice.c_str());
			if (mystore.EditStockItemPrice(asku, aprice))
				cout << "Item successfully updated." << endl;
			else
				cout << "Item not updated." << endl;
			break;
		case 6: // Restock an item
			cout << "Enter a numeric SKU to purchase: ";
			getline(cin, inputasku);
			asku = atoi(inputasku.c_str());
			cout << "Enter a quantity to purchase: ";
			getline(cin, inputamt);
			amount = atoi(inputamt.c_str());
			cout << "Enter the per-unit purchase price: $";
			getline(cin, inputaprice);
			aprice = atoi(inputaprice.c_str());
			if (mystore.Restock(asku, amount, aprice))
				cout << "Item successfully restocked." << endl;
			else
				cout << "Item not restocked." << endl;
			break;
		case 7: // Sell an item
			cout << "Enter the SKU of item to sell: ";
			getline(cin, inputasku);
			asku = atoi(inputasku.c_str());
			cout << "Enter a quantity to sell: ";
			getline(cin, inputamt);
			amount = atoi(inputamt.c_str());
			if (mystore.Sell(asku, amount))
				cout << "Transaction complete. Have a nice day." << endl;
			else
				cout << "Item is out of stock. Sorry!" << endl;
			break;
		case 8: // Quit
				// no need to do anything, will cause while loop to break
			break;
		default:
			cout << "Invalid choice." << endl;
			break;
		}
	}

	return 0;
}
// program entry point
int main()
{
  ExtraTests();

  int choice = 0;
  unsigned int asku;
  string adesc;
  double aprice;
  unsigned int amount;
  string ctlg = "";

  StockSystem mystore;

  while (choice != 8)
  {
    PrintMenu();
    cin >> choice;
    cin.ignore(numeric_limits<streamsize>::max(), '\n'); // flush the cin stream including newline

    switch (choice)
    {
      case 1: // Print balance
        cout << "Funds: $" << mystore.GetBalance() << endl << endl;
        break;
      case 2: // Print catalogue
        ctlg = mystore.GetCatalogue();
        cout << ctlg << endl;
        break;
      case 3: // Add SKU
        cout << "Enter a numeric SKU (will be converted to 5 digits): ";
        cin >> asku;
        cin.ignore(numeric_limits<streamsize>::max(), '\n');
        cout << "Enter item description: ";
        getline(cin, adesc);
        cout << "Enter a retail price: $";
        cin >> aprice;
        cin.ignore(numeric_limits<streamsize>::max(), '\n');
        if (mystore.StockNewItem(StockItem(asku, adesc, aprice)))
          cout << "Successfully added item to catalogue." << endl;
        else
          cout << "Item not added to catalogue." << endl;
        break;
      case 4: // Edit item description
        cout << "Enter a numeric SKU to edit: ";
        cin >> asku;
        cin.ignore(numeric_limits<streamsize>::max(), '\n');
        cout << "Enter item description: ";
        getline(cin, adesc);
        if (mystore.EditStockItemDescription(asku, adesc))
          cout << "Item successfully updated." << endl;
        else
          cout << "Item not updated." << endl;
        break;
      case 5: // Edit item price
        cout << "Enter a numeric SKU to edit: ";
        cin >> asku;
        cin.ignore(numeric_limits<streamsize>::max(), '\n');
        cout << "Enter a retail price: $";
        cin >> aprice;
        cin.ignore(numeric_limits<streamsize>::max(), '\n');
        if (mystore.EditStockItemPrice(asku, aprice))
          cout << "Item successfully updated." << endl;
        else
          cout << "Item not updated." << endl;
        break;
      case 6: // Restock an item
        cout << "Enter a numeric SKU to purchase: ";
        cin >> asku;
        cin.ignore(numeric_limits<streamsize>::max(), '\n');
        cout << "Enter a quantity to purchase: ";
        cin >> amount;
        cin.ignore(numeric_limits<streamsize>::max(), '\n');
        cout << "Enter the per-unit purchase price: $";
        cin >> aprice;
        cin.ignore(numeric_limits<streamsize>::max(), '\n');
        if (mystore.Restock(asku, amount, aprice))
          cout << "Item successfully restocked." << endl;
        else
          cout << "Item not restocked." << endl;
        break;
      case 7: // Sell an item
        cout << "Enter the SKU of item to sell: ";
        cin >> asku;
        cin.ignore(numeric_limits<streamsize>::max(), '\n');
        cout << "Enter a quantity to sell: ";
        cin >> amount;
        cin.ignore(numeric_limits<streamsize>::max(), '\n');
        if (mystore.Sell(asku, amount))
          cout << "Transaction complete. Have a nice day." << endl;
        else
          cout << "Item is out of stock. Sorry!" << endl;
        break;
      case 8: // Quit
        // no need to do anything, will cause while loop to break
        break;
      default:
        cout << "Invalid choice." << endl;
        break;
    }
  }

  return 0;
}
示例#3
0
void StockSystemTest()
{
	cout << "--------------------------StockSystemTest---------------------------" << endl;

	StockSystem Sys;

	cout << "Create Stock System..." << endl;
	cout << "Default Balance: $" << Sys.GetBalance() << endl << endl;

	if (Sys.StockNewItem(StockItem(11111, "Apple", 10)) == true) { cout << "Stock Added..." << endl; }
	else { cout << "Stock Not Added..." << endl; }
	if (Sys.StockNewItem(StockItem(77777, "Dog", 5)) == true) { cout << "Stock Added..." << endl; }
	else { cout << "Stock Not Added..." << endl; }
	if (Sys.StockNewItem(StockItem(11111, "Pear", 12)) == true) { cout << "Stock Added..." << endl; }
	else { cout << "Stock Not Added (Already Exists)..." << endl; }
	cout << endl << "Balance: $" << Sys.GetBalance() << endl;
	cout << Sys.GetCatalogue() << endl;

	if (Sys.EditStockItemDescription(77777, "Boat") == true) { cout << "Stock Description Changed..." << endl; }
	else { cout << "Stock Description Not Changed(Not Found)..." << endl; }
	if (Sys.EditStockItemDescription(77717, "Pie") == true) { cout << "Stock Description Changed..." << endl; }
	else { cout << "Stock Description Not Changed(Not Found)..." << endl; }
	cout << endl << "Balance: $" << Sys.GetBalance() << endl;
	cout << Sys.GetCatalogue() << endl;

	if (Sys.EditStockItemPrice(11111, 777) == true) { cout << "Stock Price Changed..." << endl; }
	else { cout << "Stock Price Not Changed..." << endl; }
	if (Sys.EditStockItemPrice(44444, 10) == true) { cout << "Stock Price Changed..." << endl; }
	else { cout << "Stock Price Not Changed(Not Found)..." << endl; }
	cout << endl << "Balance: $" << Sys.GetBalance() << endl;
	cout << Sys.GetCatalogue() << endl;

	if (Sys.Restock(11111, 5, 10) == true) { cout << "Stock Restock..." << endl; }
	else { cout << "Stock Not Restock..." << endl; }
	if (Sys.Restock(22222, 5, 10) == true) { cout << "Stock Restock..." << endl; }
	else { cout << "Stock Not Restock(Already Exists)..." << endl; }
	if (Sys.Restock(77777, 5, 200) == true) { cout << "Stock Restock..." << endl; }
	else { cout << "Stock Not Restock..." << endl; }
	if (Sys.Restock(77777, 3, 1000) == true) { cout << "Stock Restock..." << endl; }
	else { cout << "Stock Not Restock..." << endl; }
	if (Sys.Restock(77777, 10, 3000) == true) { cout << "Stock Restock..." << endl; }
	else { cout << "Stock Not Restock..." << endl; }
	if (Sys.Restock(77777, 555, 500000) == true) { cout << "Stock Restock..." << endl; }
	else { cout << "Stock Not Restock(Balance Too Low)..." << endl; }
	cout << endl << "Balance: $" << Sys.GetBalance() << endl;
	cout << Sys.GetCatalogue() << endl;

	if (Sys.Sell(11111, 7) == true) { cout << "Stock Sold..." << endl; }
	else { cout << "Stock Not Sold..." << endl; }
	if (Sys.Sell(77777, 11) == true) { cout << "Stock Sold..." << endl; }
	else { cout << "Stock Not Sold(Not Found)..." << endl; }
	if (Sys.Sell(99999, 100) == true) { cout << "Stock Sold..." << endl; }
	else { cout << "Stock Not Sold(Not Found)..." << endl; }
	if (Sys.Sell(11111, 11) == true) { cout << "Stock Sold..." << endl; }
	else { cout << "Stock Not Sold(0 Stock)..." << endl; }
	cout << endl << "Balance: $" << Sys.GetBalance() << endl;
	cout << Sys.GetCatalogue() << endl;


	cout << endl;
}