Thread: code problems
View Single Post
Old 03-17-2005, 10:09 AM   #9 (permalink)
Valmont
[code][/code] enforcer
 
Valmont's Avatar
 
Join Date: Mar 2003
Location: Netherlands
Posts: 1,544
Valmont is on a distinguished road
See if this comes close to what you want. Then tell me what changes need to be made:
Code:
#include <cstdlib>
#include <string>
#include <iostream>

using namespace std;

//-------------------------------------------

struct Item
{
  Item(const string& name = "", const string& descr = "", const double cost=0) :
    sName_(name), sDescription_(descr), dCost_(cost)
  {}
  //--
  string sName_;
  string sDescription_;
  double dCost_;
};

//-------------------------------------------

class Shelf
{
public:
  Shelf(const string& name = "") : 
    nItemQuantity_(0), theItem_(0), sShelfName_(name)
  {}
  ~Shelf()
  {
    if(theItem_ !=0 )
    {
      delete[] theItem_;
    }  
  }
public:
  void add_item(const string& name, const string& descr, const double cost)
  {
    increase_item_size();
    theItem_[nItemQuantity_-1].sName_ = name;
    theItem_[nItemQuantity_-1].sDescription_ = descr;
    theItem_[nItemQuantity_-1].dCost_ = cost;
  }
  unsigned get_quantity() const
  {
    return nItemQuantity_;
  }
  void set_shelf_name(const string& name)
  {
    sShelfName_ = name;
  }
  string get_shelf_name() const
  {
    return sShelfName_;
  }  
  Item* get_item() const
  {
    return theItem_;
  }  
private:
  string sShelfName_;
  unsigned nItemQuantity_;
  Item* theItem_;
private:
  void increase_item_size()
  {
    Item* itm = new Item[nItemQuantity_+1];
    for(unsigned i = 0; i < nItemQuantity_ ; ++i)
    {
      itm[i] = theItem_[i];
    }
    delete[] theItem_;
    theItem_ = itm;
    ++nItemQuantity_;
  }  
};

//-------------------------------------------

class Warehouse
{
public: 
  Warehouse() : theShelf_(0), nSize_(0)
  {}
  ~Warehouse()
  {
    if(theShelf_ != 0)
    {
      delete[] theShelf_;
    }  
  }
public:
  void add_shelf(const string& shname,  const string& itmname,  const string& itmdescr,
    const double itcost)
  {
    increase_shelf_size();
    theShelf_[nSize_-1].set_shelf_name(shname);
    theShelf_[nSize_-1].add_item(itmname, itmdescr, itcost);
  }
  Shelf* get_shelf() const
  {
    return theShelf_;
  }
  unsigned get_size() const
  {
    return nSize_;
  }  
private:
  unsigned nSize_;
  Shelf* theShelf_;
private:
  void increase_shelf_size()
  {
    Shelf* sh = new Shelf[nSize_+1];
    for(unsigned i = 0; i < nSize_ ; ++i)
    {
      sh[i] = theShelf_[i];
    }
    delete[] theShelf_;
    theShelf_ = sh;
    ++nSize_;
  }  
};

//-------------------------------------------

int main()
{
  Warehouse theWarehouse;
  theWarehouse.add_shelf("Computers", "Mouse", "Logitech", 50.00);
  
  cout<<"*** "<<theWarehouse.get_shelf()[0].get_shelf_name()<<" ***"<<endl;
  cout<<theWarehouse.get_shelf()[0].get_item()[0].sName_<<endl;
  cout<<theWarehouse.get_shelf()[0].get_item()[0].sDescription_<<endl;
  cout<<theWarehouse.get_shelf()[0].get_item()[0].dCost_<<endl;
    
  system("PAUSE");
  return 0;
}
__________________
Valmont is offline   Reply With Quote