-
November 5th, 2011, 03:08 AM
#1
Vectors iterators
Code:
this is my header file
#ifndef HW1_H
#define HW1_H
#include <iostream>
#include <string>
#include <vector>
using namespace std;
enum SystemError
{
ERR_NOT_ENOUGH_PRODUCT = 0,
ERR_PRODUCT_DOES_NOT_EXIST = 1,
ERR_ORDER_ALREADY_EXISTS = 2,
ERR_NOT_ENOUGH_BUDGET = 3,
ERR_CUSTOMER_DOES_NOT_EXIST= 4,
ERR_STORE_DOES_NOT_EXIST = 5,
ERR_CUSTOMER_ALREADY_EXISTS= 6,
ERR_STORE_ALREADY_EXISTS = 7,
ERR_ORDER_DOES_NOT_EXIST = 8,
};
/* Product */
class Product
{
private:
string productName;
string productType;
int productPrice;
int productAmount;
public:
Product(string productName, string productType, int productPrice,int productAmount);
Product();
~Product();
string getProductName();
string getProductType();
int getProductPrice();
int getProductAmount();
void setAmount(int newAmount);
void setPrice(int newPrice);
friend ostream& operator<<(ostream& out, const Product &product);
};
ostream& operator<<(ostream &out, const Product &product);
/* Order */
class Order
{
private:
string orderID;
int orderDate;
string customerID;
string storeName;
vector<Product> shoppingList;
public:
Order();
Order(string orderID, int orderDate, string customerID, string
storeName);
~Order();
string getOrderID();
int getOrderDate();
string getCustomerID();
string getStoreName();
void setStore(string newStore);
void addProduct(Product p);
void removeProduct (string pname, int amount);
int orderTotal();
void listAllProducts();
void listProductsOfType (string t);
Product getProduct(int index);
int getProductSize();
friend ostream& operator<<(ostream& out, const Order &order);
};
ostream& operator<<(ostream &out, const Order &order);
/* Store */
class Store
{
private:
string storeName;
string storeAddress;
vector<Product> availableProducts;
public:
Store();
Store(string storeName, string storeAddress);
~Store();
string getStoreName();
string getStoreAddress();
void setAddress(string newAddress);
void addProduct(Product p);
void removeProduct(string pname, int amount);
Product getProduct(int index);
int getProductSize();
void setProductAmount(int productIndex, int newAmount);
friend ostream& operator<<(ostream& out, const Store &store);
};
ostream& operator<<(ostream &out, const Store &store);
/* Customer */
class Customer
{
private:
string customerID;
string customerName;
int phone;
int creditCardNo;
string passwd;
int budget;
public:
Customer();
Customer(string customerID, string customerName, int phone, int
creditCardNo, string passwd, int budget);
~Customer();
string getCustomerID();
string getCustomerName();
int getCustomerPhone();
int getCustomerCreditCardNo();
string getCustomerPasswd();
int getBudget();
void setPhone(int newPhone);
void changePasswd(string newPasswd);
void setBudget(int newBudget);
friend ostream& operator<<(ostream& out, const Customer &customer);
};
ostream& operator<<(ostream &out, const Customer &customer);
/* Shopping Zone */
class ShoppingZone
{
private:
vector<Order> orders;
vector<Customer> customers;
vector<Store> stores;
public:
ShoppingZone();
~ShoppingZone();
void addOrder(Order o);
void addCustomer(Customer c);
void addStore(Store s);
Order getOrder( string orderID);
Customer getCustomer( string customerID);
Store getStore( string storeName);
void listAllStores();
void listProductsOfCustomer (string customerID);
void listProductInfo (string storeName, string type);
};
#endif
and this is my listProductInfo func.
Code:
void ShoppingZone::listProductInfo (string storeName, string type)
{
int i,j;
vector<Store>::iterator it;
vector<Product>::iterator it2;
for ( it=(this->stores.begin()) ; it < (this->stores.end()); it++ )
{
if( it->getStoreName() == storeName )
{
cout << it->availableProducts[0].getProductType();
/*for ( it2=(it->availableProducts.begin()) ; it2 < (this->availableProducts.end()); it2++ )
{cout <<"evet";}*/
}
}
}
how can reach any Product which exist in availableProducts vector from this function. THis cause problem because availableProducts is fixed. MOreover header file is fixed it cannot be changed. Thanks in advance
-
November 5th, 2011, 03:31 AM
#2
Re: Vectors iterators
Originally Posted by loves_oi
how can reach any Product which exist in availableProducts vector from this function.
You should make use of the Store interface, i.e., call the getProduct or setProductAmount member functions of the Store object found.
Originally Posted by loves_oi
MOreover header file is fixed it cannot be changed.
I would change certain parts of your class definitions in the header file, because:
- You should not have a using directive (using namespace std; ) at file scope in a header file.
- Many cases of pass by value, especially of std::string objects, should have been changed to pass by (const) reference.
- Many of your member functions are not const correct, e.g., getProductName should not change the Product, yet it is not declared const.
-
November 5th, 2011, 05:25 PM
#3
Re: Vectors iterators
Originally Posted by loves_oi
[code]
Code:
void ShoppingZone::listProductInfo (string storeName, string type)
{
int i,j;
vector<Store>::iterator it;
vector<Product>::iterator it2;
for ( it=(this->stores.begin()) ; it < (this->stores.end()); it++ )
{
if( it->getStoreName() == storeName )
{
cout << it->availableProducts[0].getProductType();
/*for ( it2=(it->availableProducts.begin()) ; it2 < (this->availableProducts.end()); it2++ )
{cout <<"evet";}*/
}
}
}
Look at the code below.
Code:
#include <algorithm>
#include <string>
//...
using namespace std;
//...
struct FindByName
{
std::string sName_;
FindByName(const std::string& sName) : sName_(sName) {}
bool operator()(Store& theStore) const
{ return sName_ == theStore.getStoreName(); }
};
void DisplayName(Product& theProduct)
{
cout << theProduct.getProductName() << "\n";
}
void ShoppingZone::listProductInfo (const string& storeName,
const string& type)
{
// Search for the store matching the store name
vector<Store>::iterator it = find_if(stores.begin(), stores.end(), FindByName(storeName));
if ( it != stores.end())
{
// store exists, now display the name of each product in the store that was found
for_each(it->availableProducts.begin(), it->availableProducts.end(), DisplayName);
}
}
I leave it to you as an exercise as to why the code above works without having to code a single for loop.
Regards,
Paul McKenzie
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|