A+ Work

profileThehonest
productinventory.zip

ProductInventory.cpp

ProductInventory.cpp


// File Name: ProductInventory.cpp
//
// Author: Ayisha 
// Date: 04/04/2016
// Assignment Number: 6
// CS 2308.252 Spring 2016
// Instructor: Jill Seaman
//
// File to implement the functions from the corresponding header file

#include   < iostream >
#include   < iomanip >
#include   "ProductInventory.h"
using   namespace  std ;

//***********************************************************
// Inventory : Constructor that takes an argument and
//   dynamically allocates an array of product and specifies
//   the size.
// size : number of products.
//***********************************************************

ProductInventory :: ProductInventory ()
{
     productList  =  NULL ;
}

//***********************************************************
// Inventory : Destructor that deletes the pointer of array
//   created by the contructor.
//***********************************************************

ProductInventory ::~ ProductInventory ()
{
      ProductNode   * p ;
      ProductNode   * n ;
     
     p  =  productList ;
     
      while   ( p )   {
          n  =  p -> next ;
           delete  p ;
          p  =  n ;
      }
}

//***********************************************************
// addProduct : takes a product and adds it to the inventory
//   it calls the function resize.
//   checks if the product with same name and locator already
//        exists in the inventory
// newProduct : accepts an argument from class Product
//***********************************************************

bool   ProductInventory :: addProduct ( Product  product )
{
     ProductNode   * temp ;
    temp  =  productList ;
     while ( temp != NULL )
     {
         if ( product . isEqual ( temp -> data ))
             return   false ;
        temp  =  temp -> next ;
     }
     if   ( product . getPrice ()   <   0   ||  product . getQuantity ()   < 0 )   {
           return   false ;
      }
     
      ProductNode   * newNode ;
     newNode  =   new   ProductNode ;
     newNode -> data  =  product ;
     newNode -> next  =  NULL ;
 
      if   ( productList  ==  NULL )
          productList  =  newNode ;
      else   {
         newNode -> next  =  productList ;
         productList  =  newNode ;
      }
      return   true ;
}

//***********************************************************
// removeProduct : takes a product name and locator and removes
//   any matching product from the inventory.
// n : name of the product
// l : name of the location
//***********************************************************

bool   ProductInventory :: removeProduct ( string rName ,  string rLocator )
{
      Product  remProduct ( rLocator , 0 , 0 , rName );   // use for comparison
      ProductNode   * current  =  productList ;

     /*ProductNode * prev;
    while(current!=NULL)
    {
        if(!current->data.isEqual(remProduct))
        {
            prev=current;
            current = current->next;
        }
        else
        {
            prev->next = current->next;
            delete current;
            return true;
        }
    }
     return false;*/

      ProductNode   * pPre  =  NULL ,   * pDel  =  NULL ;
    
     /* Check whether it is the head node?
     if it is, delete and update the head node */
     if   ( productList -> data . isEqual ( remProduct ))   {
         /* point to the node to be deleted */
        pDel  =  productList ;
         /* update */
        productList  =  pDel -> next ;
         delete  pDel ;
         return   true ;
     }
    
    pPre  =  productList ;
    pDel  =  productList -> next ;
    
     while   ( pDel  !=  NULL )   {
         if   ( pDel -> data . isEqual ( remProduct ))   {
            pPre -> next  =  pDel -> next ;
             delete  pDel ;
             return   true ;
         }
        pPre  =  pDel ;
        pDel  =  pDel -> next ;
     }
     return   false ;
}

//***********************************************************
// showInventory : displays the list from the inventory
//***********************************************************

void   ProductInventory :: showInventory ()
{
     cout  <<  fixed  <<  setprecision ( 2 );
      ProductNode   * p ;
     p  =  productList ;
      while   ( p )   {
          cout  <<  p -> data  <<   " " ;
          p  =  p -> next ;
      }
     cout  <<  endl ;
}

//***********************************************************
// sortInventory : reorders the products in the inventory
//***********************************************************

void   ProductInventory :: sortInventory ()
{
     ProductNode   * temp  =  productList ;
     ProductInventory   * newlist ;
    newlist  =   new   ProductInventory ();
     while   ( productList )
     {
         /*if(temp->data.getLocator()=="" || temp->data.getProductName()=="")
            break;*/
         Product  min  =  findMinimum ();
        newlist -> appendNode ( min );
        removeProduct ( min . getProductName (),  min . getLocator ());
     }
    productList  =  newlist -> productList ;
      //for (ProductNode *p = productList; p->next != NULL; p = p->next)
      //{
      //     // compare to the list ahead
      //     for (ProductNode *n = p->next; n != NULL; n = n->next)
      //     {
      //          // compare and swap
      //          if (p->data.greaterThan(n->data))
      //          {
                  //  Product temp;
      //               temp = p->data;
                     //p->data = n->data;
                     //n->data = temp;
      //          }
      //     }
      //}
}

//***********************************************************
// getTotalQuantity : returns the total quantity of all
//   products in the inventory.
//***********************************************************

int   ProductInventory :: getTotalQuantity ()
{
      int  total = 0 ;
      ProductNode   * p ;
     p  =  productList ;
      while ( p != NULL ){
           Product  product  =  p -> data ;
          total  +=  product . getQuantity ();
          p  =  p -> next ;
      }
      return  total ;
}



//***********************************************************
// findMinimum : double the size of inventory array
//***********************************************************

/*returns the minimum product in the list, using the greaterThan()
function over the products*/
Product   ProductInventory :: findMinimum ()
{
      Product  min ;
      Product  inventory ;
      ProductNode   * =  productList ;
      if   ( ==  NULL )
          return  min ;
     min  =  productList -> data ;
      for   ( =  productList ;  p  !=  NULL ;  p  =  p -> next )   {
           if   ( min . greaterThan ( p -> data ))
               min  =  p -> data ;
      }
      return  min ;
}


void   ProductInventory :: appendNode ( Product  inventory )   {
      ProductNode   * newNode ;   // To point to the new node
      // Create a new node and store the data in it
     newNode  =   new   ProductNode ;
     newNode -> data  =  inventory ;
     newNode -> next  =  NULL ;
      // If empty, make head point to new node
      if   ( productList == NULL )
          productList  =  newNode ;
      else   {
           ProductNode   * p ;    // To move through the list
          p  =  productList ;       // initialize to start of list
           // traverse list to find last node
           while   ( p -> next )           //it’s not last
               p  =  p -> next ;           //make it pt to next
           // now p pts to last node
           // make last node point to newNode
          p -> next  =  newNode ;
      }
}


ProductInventory.h

// File Name: ProductInventory.h // // Author: Ayisha // Date: 04/04/2016 // Assignment Number: 6 // CS 2308.252 Spring 2016 // Instructor: Jill Seaman // // Manages the store inventory. #include "Product.h" using namespace std; class ProductInventory { private: struct ProductNode // the Nodes of the linked list { Product data; // data is a product ProductNode *next; // points to next node in list }; ProductNode *productList; // the head pointer public: ProductInventory(); // constructor ~ProductInventory(); // destructor bool addProduct(Product); bool removeProduct(string, string); void showInventory(); int getTotalQuantity(); Product findMinimum(); void sortInventory(); void appendNode(Product); };

Capture.JPG

Product.cpp

Product.cpp


// File Name: Product.cpp
//
// Author: Ayisha 
// Date: 04/04/2016
// Assignment Number: 6
// CS 2308.252 Spring 2016
// Instructor: Jill Seaman
//
// File to implement the functions from the corresponding header file

#include   "Product.h"

//***********************************************************
// Product : Constructor that takes no argument but stores:
//        the name of product
//        the location of the product
//        the quantity of the product
//        the price of the product
//***********************************************************

Product :: Product ()   {
     productName  = "" ;
     locator = "" ;
     quatity = 0 ;
     price = 0.0 ;
}

//***********************************************************
// Product : Constructor that takes four argument and
//             assigns a value for each of the four members.
// l: to assign the locator
// q: to assign the quantity
// p: to assign the price
// n: to assign the name
//***********************************************************

Product :: Product ( string newLocator , int  newQuantity , double  newPrice ,
                 string newName )   {
     locator  =  newLocator ;
     quatity  =  newQuantity ;
     price  =  newPrice ;
     productName  =  newName ;
}

//***********************************************************
// setProductName : accepts a string and stores it in the
//                  member variable
// n : product name
//***********************************************************

void   Product :: setProductName ( string newName )   {
     productName  =  newName ;
}

//***********************************************************
// getProductName : returns the name stored in the member
//                  variable
//***********************************************************

string  Product :: getProductName ()   {
      return  productName ;
}

//***********************************************************
// setLocator : accepts a string and stores it in the
//                  member variable
// l : locator
//***********************************************************

void   Product :: setLocator ( string newLocator )   {
     locator  =  newLocator ;
}

//***********************************************************
// getLocator : returns the locator stored in the member
//                  variable
//***********************************************************

string  Product :: getLocator ()   {
      return  locator ;
}

//***********************************************************
// setQuantity : accepts a string and stores it in the
//                  member variable
// q : quantity
//***********************************************************

void   Product :: setQuantity ( int  newQuantity )   {
     quatity  =  newQuantity ;
}

//***********************************************************
// getQuantity : returns the quantity stored in the member
//                  variable
//***********************************************************

int   Product :: getQuantity ()   {
      return  quatity ;
}

//***********************************************************
// setPrice : accepts a string and stores it in the
//                  member variable
// p : price
//***********************************************************

void   Product :: setPrice ( double  newPrice )   {
     price  =  newPrice ;
}

//***********************************************************
// getPrice : returns the price stored in the member
//                  variable
//***********************************************************

double   Product :: getPrice ()   {
      return  price ;
}

//***********************************************************
// isEqual : accepts an argument from a class Product and
//   checks if the product is equal to another if they have
//   the same product name and locator values.
// p : name of the class.
//***********************************************************

bool   Product :: isEqual ( Product  other )   {
      if   ( locator  !=  other . locator )
           return   false ;
      if   ( productName  !=  other . productName )
           return   false ;
      return   true ;
}

//***********************************************************
// greaterThan : accepts an argument from a class Product and
//   checks if the product is greaterThan if its product name
//   is greater than other, or if they have the same product
//   names, if this product's locator is greater than the
//   other's locator. 
// p : name of the class.
//***********************************************************

bool   Product :: greaterThan ( Product  other )   {
      if   ( productName  ==  other . productName )
           return   ( locator  >  other . locator );
      return  productName  >  other . productName ;
}

ostream &   operator   <<   ( ostream  & out ,   Product  p )
{
    out  <<  p . getLocator ()   <<   "   "   <<   p . getQuantity ()   <<   "    $"   <<  setprecision ( 2 )   <<  fixed  <<  p . getPrice () <<   "   "   <<  p . getProductName ()   <<  endl ;
     return  out ;
}

Product.h

// File Name: Product.h // // Author: Ayisha // Date: 04/04/2016 // Assignment Number: 6 // CS 2308.252 Spring 2016 // Instructor: Jill Seaman // // Stores the content to the inventory and // allows to set its information and get its information #include <iostream> #include <iomanip> #include <string> using namespace std; class Product { private: // variables to store the content to the inventory string productName; string locator; int quatity; double price; public: //constructors Product(); Product(string,int,double,string); string getProductName(); string getLocator(); int getQuantity(); double getPrice(); void setProductName(string); void setLocator(string); void setQuantity(int); void setPrice(double); bool isEqual(Product); bool greaterThan(Product); friend ostream& operator << (ostream &out, Product p); };

ProductDriver.cpp

ProductDriver.cpp


// File Name: ProductDriver.cpp
//
// Author: Ayisha 
// Date: 04/04/2016
// Assignment Number: 6
// CS 2308.252 Spring 2016
// Instructor: Jill Seaman
//
// The program manages the inventory of a small store
//  This file contains the main function and tests the functionality of
//   the Product and ProductInventory classes.

#include   < iostream >
#include   < iomanip >
using   namespace  std ;

#include   "ProductInventory.h"

int  main ()
{
      // testing Product
      Product  product0 ;
     product0 . setLocator ( "box1" );
     product0 . setQuantity ( 2 );
     product0 . setPrice ( 111.99 );
     product0 . setProductName ( "Apple iPod Touch 4th Generation 8GB" );
     
     cout  <<  setw ( 6 )   <<  product0 . getLocator ()
           <<   " "   <<  product0 . getQuantity ()
           <<   " $"   <<  product0 . getPrice ()
      <<   " "   <<  product0 . getProductName ()   <<   " "   <<  endl ;
     
      // testing ProductInventory
     ProductInventory  pi ;
     
      Product  product1 ( "box1" ,   2 ,   105.75 ,   "Apple iPhone 3GS" );
      Product  product2 ( "box1" ,   3 ,   172.50 ,   "Samsung Captivate SGH-1897" );
      Product  product3 ( "box3" ,   1 ,   215.35 ,   "Blackberry RIM Bold 9930" );
      Product  product4 ( "box2" ,   1 ,   215.35 ,   "Blackberry RIM Bold 9930" );
      Product  product5 ( "box2" ,   2 ,   322.50 ,   "Apple iPad 2 16GB WiFi" );
     
      bool  result  =  pi . addProduct ( product1 );
     cout  <<   "add result = "   <<  result  <<  endl ;
     
     pi . showInventory ();
     
     result  =  pi . addProduct ( product2 );
     cout  <<   "add result = "   <<  result  <<  endl ;
     
     pi . showInventory ();
     
     result  =  pi . addProduct ( product3 );
     cout  <<   "add result = "   <<  result  <<  endl ;
     
     result  =  pi . addProduct ( product4 );
     cout  <<   "add result = "   <<  result  <<  endl ;
     
     result  =  pi . addProduct ( product5 );
     cout  <<   "add result = "   <<  result  <<  endl ;
    
     pi . sortInventory ();
     pi . showInventory ();
      bool  delResult ;
     delResult  =  pi . removeProduct ( product2 . getProductName (),
                                  product2 . getLocator ());
     cout  <<   "delete  result = "   <<  delResult  <<  endl ;
     
     delResult  =  pi . removeProduct ( product3 . getProductName (),
                                  product3 . getLocator ());
     cout  <<   "delete  result = "   <<  delResult  <<  endl ;
     
     pi . showInventory ();
     cout  <<   "total quantity (should be 5) = "   <<  pi . getTotalQuantity ()   <<
     endl ;
     
     result  =  pi . addProduct ( product5 );
     cout  <<   "add result = "   <<  result  <<  endl ;
     
     pi . showInventory ();

     system  ( "pause" );
      return   0 ;
}