Class Record (Inventory System using Random File processing)
Posted by JanWan
Last Updated: November 29, 2012
  5983

Problem:>>

You are the owner of a hardware store and need to keep an inventory that can tell you what different tools you have, how many of each you have on hand, and the cost of each one.

Part 1

Implement the class Record that contains the following private and public members

private:

     int recordNum;

     char toolName [25];

     int quantity;

     double cost;

public:

     void setRecordNumber(int);

     int getRecordNumber() const;

     void setToolName(string);

     string getToolName() const;

     void setQuantity(int);

     int getQuantity() const;

     void setCost (double);

     double getCost() const;


Part 2

Write a program for the hardware store with the following functionalities (develop as a menu-driven application):

Function 1:  initializeFile

This function initializes the random-access file hardware.dat to 50 empty records.

Function 2: addDataToFile

This function allows the user to input data concerning each tool. See sample below

Record#

Tool name

Quantity

Cost

3

Electric sander

7

57.98

17

Hammer

76

11.99

24

Jig saw

21

11.00

Function 3: displayFileContent

This function will display the list of tools stored in the hardware.dat file.

Function 4: deleteRecordFromFile

This function will delete a record for a tool that you no longer have in stock.

Function 5: updateRecord

This function will be used to update the quantity on hand. If the hardware store sells an item, you should decrement the quantity, and vice versa, if new stocks are added. Make provisions for these transactions in this function.


J.W. PRODUCTION

   
  
 
 
   

 
 
This is an interesting program... It's a little challenging if you don't know what you are doing. Watch the tutorials videos, we have on this website and you will learn a lot more...
 
Interface for class Record
 
#include <cstdlib>
#include <iostream> //input output header library
#include <string>


using namespace std; //Allow to group entities like class

class Record
{
    private:
            int recordNum;
            char toolName[25];
            int quantity;
            double cost;  
      
    public:
           void setRecordNumber(int);
           int getRecordNumber() const;
           
           void setToolName(string);
           string getToolName() const;
           
           void setQuantity(int);
           int getQuantity() const;
           
           void setCost(double);
           double getCost() const;
          
};
 
This is the main screen of my program


When i select 1 of the option, a runtime error occur.


This is the error message that i encounter when i run the program


 
Implementation of class Record
 
#include <cstdlib>
#include <iostream> //input output header library
#include <string>
#include <cstring>
#include "Record.h"
//Header file for Record class

using namespace std; //Allow to group entities like class

//Record Number function/ Mutator
void Record::setRecordNumber(int RN)
{
     recordNum = RN;
}

//Record Number function/ Accessor
int Record::getRecordNumber() const
{
    return recordNum; //return value of record number
}

//Tool Name function/ Mutator
void Record::setToolName(string TN)
{
    
     toolName[25] = TN[25];
}

//Tool Name function/ Accessor
string Record::getToolName() const
{
       return toolName[25]; //return value of tool name
}

//Quantity function/ Mutator
void Record::setQuantity(int qty)
{
     quantity = qty;
}

//Quantity function/ Accessor
int Record::getQuantity() const
{
    return quantity; //return value of quantity
}

//Cost function/ Mutator
void Record::setCost(double C)
{
     cost = C;
}

//Cost function/ Accessor
double Record::getCost() const
{
       return cost;//return value of cost
}

//Constructor
Record::Record()
{
     setRecordNumber(0);
     setToolName(" ");
     setQuantity(0);
     setCost(0.0);
       
}
 
Main Program
 
#include <cstdlib>
#include <iostream>
#include <conio.h>
#include <string>
#include <iomanip>
#include <fstream>
#include "Record.h" //Header file for Record class


using namespace std;

//*******************************************************************************
int getRecNum(const char* const);
int menu();
void initializeFile(fstream&); //Initialize file function
void addDataToFile(fstream&); //Add Data to File Function
void displayFileContent(ostream&, const Record&); //Display File Content Function of record
void deleteRecordFromFile(fstream&); //Delete Record From File Function
void updateRecord(fstream&); //Update Record Function

//*******************************************************************************


int main(int argc, char *argv[])
{
    //open file for reading and writing
    fstream toolFile("Tool.dat", ios::in|ios::out|ios::binary);
    
    if(!toolFile)
    {
         //Display error message if file can not be open        
         cerr<<"File could not be open "<<endl;
         exit(1);        
    }
    else
   
    {
        cout<<"The file is open" <<endl;
        
    }
    
    int choice;
    menu();
    cout<<"Select"<<endl;
    cout<<"|";
    cin>>choice;
    Record item;
    //system("cls");
    
    while(choice != 6)
    {
        switch(choice)
        {
            case 1:
                    initializeFile(toolFile);//Initialize file function.
                      break;
            case 2:
                    addDataToFile(toolFile); //Add data to file function
                      break;
            case 3:
                    displayFileContent(toolFile, item); //Display file content function
                    break;
            case 4:
                    deleteRecordFromFile(toolFile); //Delete record from file function
                    break;
            case 5:
                    updateRecord(toolFile); //Update record function
                    break;
            default:
                    cerr<<"Incorrect option"<<endl;
                    break;
    
          
           toolFile.clear();
           }
            //menu();
    
    /*cout<<"Select"<<endl;
    cout<<"|";
    cin>>choice;*/
    toolFile.close();
    }
    
    system("color 75");
    system("PAUSE");
    return 0;
}


//*******************************************************************************
int menu()
{
   
    cout<<"******************************************"<<endl
        <<"********************************************"<<endl
         <<"***                                                    ***"<<endl
        <<"***  HARDWARD INVENTORY SYSTEM   ***"<<endl
        <<"***                                                     ***"<<endl
        <<"********************************************"<<endl
        <<"********************************************"<<endl;    
                  
    cout<<endl;
    cout<<"\tMENU"<<endl
    <<"(Selection an option)"<<endl
    <<"1 - Initialize File"<<endl
        <<"2 - Add Data to File"<<endl
        <<"3 - Display File"<<endl
        <<"4 - Delete Record"<<endl
        <<"5 - Update Record"<<endl
        <<"6 - Exit"<<endl<<endl;
       
        
}


//*******************************************************************************
void initializeFile(fstream &ToolFile)
{   
 
     //open file for reading and writing
     //ofstream toolFile("Tool.dat", ios::in|ios::out|ios::binary);
     
     if(!ToolFile)
     {
         cerr<<"File could not be open "<<endl;
         exit(1);        
     }
     
      Record start;
     
     //initialize variable to 0
     start.setRecordNumber(0);
     start.setToolName(" ");
     start.setQuantity(0);
     start.setCost(0.0);
     
     ToolFile.write("Record#\tTool\tName\tQuantity\tCost\r\n", sizeof(Record));
     for(int i = 0; i < 50; i++)
     {
          ToolFile.seekp((i+1)* sizeof(Record), ios::beg);
          ToolFile.write(reinterpret_cast <char*> (&start), sizeof(Record));
     }
     
}


//*******************************************************************************

//Add Data to File Function
void addDataToFile(fstream &addData)
{
     if(!addData)
     {
         cerr<<"File could not be open "<<endl;
         exit(1);        
     }
     
     int recordNum = 0;
     char toolName[25] = {" "};
     int quantity = 0;
     double cost = 0;
     
     cout<<"Enter Record #: ";
     cin>>recordNum;
    
      while(recordNum > 0 && recordNum < 50)
      {
           cout<<"Enter:   Tool Name      Quantity      Cost";       
           cin>>toolName >>quantity >>cost;
           addData << recordNum << toolName << quantity << cost <<endl;
           
            Record items;
           
           items.setRecordNumber(recordNum);
           items.setToolName(toolName);
           items.setQuantity(quantity);
           items.setCost(cost);
           cout<<"|";
           
           addData.seekp((recordNum - 1) * sizeof(Record));
           
           addData.write(reinterpret_cast <const char *> (&items), sizeof(Record));
      }
      
      addData.close();
        
}


//*******************************************************************************

//Display File Content Function of record
void displayFileContent(ostream &DisFile, const Record &Vrecord)
{
     DisFile <<left <<setw(10) <<Vrecord.getRecordNumber()
     <<setw(16) <<Vrecord.getToolName()
     <<setw(11) <<Vrecord.getQuantity()
     <<setw(10) <<setprecision(2) <<right <<fixed
     <<showpoint <<Vrecord.getCost() <<endl;

}//End Display  Function
     

//Obtain record number value from user
int getRecNum(const char * const prompt)
{
    int recordNum;
         
    //obtain record number value
    do
    {
         cout<< prompt <<"(1 - 50): ";
         cin>>recordNum;
             
    }while(recordNum < 1 || recordNum > 50);
         
    return recordNum;    
}


//*******************************************************************************

//Delete Record From File Function
void deleteRecordFromFile(fstream &deleteFile)
{
      
     //Enter number ofrecord to update
    int recordNum = getRecNum("Enter record to delete");
    
    deleteFile.seekg((recordNum - 1) * sizeof(Record));
    
    Record items;
    
    deleteFile.read(reinterpret_cast <char*>(&items), sizeof(Record));
    
    if(items.getRecordNumber() != 0)
    {
     Record newItem;
     
     deleteFile.seekp((recordNum - 1) * sizeof(Record));
    
     deleteFile.write(reinterpret_cast <const char*> (&newItem), sizeof(Record));
     
     cout<<"Record# " <<recordNum <<" is deleted" <<endl;
     }
     else
     
     cerr<<"Record# " <<recordNum <<" is empty" <<endl;
}
//End delete record

//*******************************************************************************

//Update Record Function
void updateRecord(fstream &updRecord)
{
     //Enter number ofrecord to update
    int recordNum = getRecNum("Enter record update");
    
    updRecord.seekg((recordNum - 1) * sizeof(Record));

    
    //Read first record
    Record items;
    updRecord.read(reinterpret_cast <char*> (&items), sizeof(Record));
    
    if(items.getRecordNumber() != 0)
    {
        displayFileContent(cout, items);

        
        //request amount of quantity
        int amount;
        cout<<"Enter amount: ";//quantity in hardware
        cin>>amount;

        
        //update record quantity
        int oldquantity = items.getQuantity();
        items.setQuantity(oldquantity + amount);
        displayFileContent(cout, items);

        
        //Move file pointer to correct record in file
        updRecord.seekp((recordNum - 1) * sizeof(Record));
        

        //write updated record over old record
        updRecord.write(reinterpret_cast <const char*> (&items), sizeof(Record));
        
    }
    else
    
    //Display error message
    cerr<<"Record # "<<recordNum <<" has no information" <<endl;
    
}

//*******************************************************************************


 
I have the updated source code for this program
 
whoa this is a neat program yo
 
Such a great way to do OOP...
 
How complex can c++ coding get
 
How complex can c++ coding get