Thread: code problems
View Single Post
Old 03-21-2005, 07:55 AM   #12 (permalink)
Valmont
[code][/code] enforcer
 
Valmont's Avatar
 
Join Date: Mar 2003
Location: Netherlands
Posts: 1,544
Valmont is on a distinguished road
Below is the updated code. Copy it exactly.
To use a menu for user input, you'll need to make it yourself. Currently the main() is only demonstrating the usage of the classes. It is still crappy, but because you didn't learn more features this will need to do.

For example, I don't free up memory and such.

Code:
#include <cstdlib>
#include <string>
#include <iostream>

using namespace std;

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

struct Item
{
  string _sName;
  string _sDescription;
  double _dCost;
};

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

class Shelf
{
//This needs to be called first by any user of this class.
public:
  //Since we didn't learn constructors/destructos yet, we will use this.
  void init()
  {
    _nItemQuantity = 0;
    //Let's make sure the shelf can hold at least one item.
    _theItem = new Item[1];    
  }  
public:
  void add_item(const string& name, const string& descr, const double cost)
  {
    _theItem[_nItemQuantity]._sName = name;
    _theItem[_nItemQuantity]._sDescription = descr;
    _theItem[_nItemQuantity]._dCost = cost;
    ++_nItemQuantity;
    
    //Let's increase the Shelf so it can hold a next item if needed.
    Item* temp = new Item[_nItemQuantity+1];
    for(unsigned i = 0; i < _nItemQuantity; ++i)
    {
      temp[i] = _theItem[i];
    }
    delete[] _theItem;
    _theItem = temp;
     
  }
  unsigned get_quantity() const
  {
    return _nItemQuantity;
  }
  
  Item get_item(unsigned shelf)
  {
    return _theItem[shelf-1];
  }  
private:
  string _sShelfName;
  unsigned _nItemQuantity;
  Item* _theItem;
};

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

class ShelfRow
{
//Accessors only.
public:
  Shelf* get_shelf()
  {
    return _theShelf;
  }
  unsigned max_index() const
  {
    //Max 2 items, so a max array index of 1. Arrays in C++ start with 0!
    return 1;
  }  
private:
  //Each ShelfRow contains 2 Shelfs.
  Shelf _theShelf[2];
};

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

class Warehouse
{
public:
  void init()
  {
    ShelfRow SR;
    const unsigned maxrows = SR.max_index();
    for(unsigned i = 0; i < 10; ++i)
    {
      for(unsigned j = 0; j < maxrows+1; ++j)
      {
        _theShelfRow[i].get_shelf()[j].init();
      }  
    }  
  }  
  
//Specific (controlling/managing) warehouse behaviour.
public:
  bool add_item(unsigned shelfrow, unsigned shelf, const string& itmname, 
  const string& itmdescr, const double cost)
  {
    
    if(bonds_ok(shelfrow, shelf) == true)
    {
      _theShelfRow[shelfrow-1].get_shelf()[shelf-1].add_item(itmname, itmdescr, cost);
      return true;
    }
    return false;
  }
  
  unsigned row_shelf_item_count( unsigned row, unsigned shelf) 
  {
    if(bonds_ok(row, shelf) == true)
    {
      return _theShelfRow[row-1].get_shelf()[shelf-1].get_quantity();
    }  
  }
  
  void print_row_shelf_items(unsigned row, unsigned shelf)
  {
    unsigned max = _theShelfRow[row-1].get_shelf()[shelf-1].get_quantity();
    for(unsigned i = 1; i < max+1; ++i)
    {
      cout<<_theShelfRow[row-1].get_shelf()[shelf-1].get_item(i)._sName<<endl;
      cout<<_theShelfRow[row-1].get_shelf()[shelf-1].get_item(i)._sDescription<<endl;
      cout<<_theShelfRow[row-1].get_shelf()[shelf-1].get_item(i)._dCost<<endl;
      cout<<"**************"<<endl;
    }  
  }  

//Accessor functions only.
public:
  void set_manager_name(const string& name)
  {
    _sManagerName = name;
  }
  void set_manager_address(const string& addr)
  {
    _sManagerAddress = addr;
  }  
  void set_warehouse_name(const string& name)
  {
    _sWarehouseName =  name;
  }
  string get_manager_name() const
  {
    return _sManagerName;
  }  
  string get_manager_address() const
  {
    return _sManagerAddress;
  }  
  string get_warehouse_name() const
  {
    return _sWarehouseName;
  }

private:
  //Each Warehouse contains 10 ShelfRows.
  ShelfRow _theShelfRow[10];
  string _sWarehouseName;
  string _sManagerName;
  string _sManagerAddress;
  
//Various helper method(s).
private:
  bool bonds_ok(unsigned row, unsigned shelf) const
  {
    if(row > 10)
    {
      return false;
    }
    if( shelf-1 > _theShelfRow[shelf-1].max_index() )
    {
      return false;
    }    
    //All O.K.
    return true;  
  }  
};

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

int main()
{
  Warehouse theWarehouse;
  theWarehouse.init();
  
  theWarehouse.set_manager_name("Valmont");
  theWarehouse.set_manager_address("Baker Street 5");
  theWarehouse.set_warehouse_name("Woodland Industrial Park 2");
  
  //Place an item in shelfrow 1, on shelf 2.
  //add_item() will check if that is possible.
  theWarehouse.add_item(1, 2, "Keyboard", "Logitech Media", 50.00);
  theWarehouse.add_item(1, 2, "Mouse", "Logitech MX510", 49.99);
  theWarehouse.add_item(1, 2, "Mouse Pad", "The Simpsons Model", 5.1);
  
   //Find the number of items stored at a specified row and shelf.
   cout<<"Total Items: "<<theWarehouse.row_shelf_item_count(1, 2)<<endl<<endl;
   
  theWarehouse.print_row_shelf_items(1,2);
   
  theWarehouse.add_item(5, 1, "banana", "Nice and yellow", 1);
  theWarehouse.add_item(5, 1, "tree", "A big one", 800.01);
  theWarehouse.add_item(5, 1, "house ", "Wrecked by the Simpsons", 200);
  
  cout<<endl<<"-----------On row 5, shelf 1:"<<endl<<endl;
  theWarehouse.print_row_shelf_items(5,1);
   
      
  system("PAUSE");
  return 0;
}
__________________

Last edited by Valmont; 03-21-2005 at 09:36 AM.
Valmont is offline   Reply With Quote