/*************************************************/
// R. A. Hillyard
// inventory.h
// November 2001
//
// The Inventory class used as an array element
//
/*************************************************/
//inventory.h
/*************************************************/
#include<iostream>
using namespace std;
class Inventory
{
public:
//friend functions
friend istream& operator >>(istream& ins, Inventory& p1);
friend ostream& operator <<(ostream& outs, const Inventory& p1);
friend bool operator > (const Inventory& p1, const Inventory& p2);
//constructors
Inventory();
Inventory (int num);
Inventory (int num, int stock, double price);
//accessor functions
int getPartNum() const;
int getInStock() const;
double getCost() const;
//mutator functions
void setPartNum(int num);
void setInStock(int num) ;
void setCost(double num);
private:
int partNum;
int inStock;
double cost;
};
/*************************************************/
/*************************************************/
// R. A. Hillyard
// inventory.cpp
// November 2001
//
// The Inventory class used as an array element
//
/*************************************************/
#include <iostream>
#include <iomanip>
#include "inventory.h"
/****************************************************************/
istream& operator >>(istream& ins, Inventory& p1)
{
ins >> p1.partNum >> p1.inStock >> p1.cost;
return ins;
}
/****************************************************************/
ostream& operator <<(ostream& outs, const Inventory& p1)
{
outs << setw(5) << p1.partNum << setw(6) << p1.inStock << setw(10) << p1.cost << endl;
return outs;
}
/****************************************************************/
bool operator > (const Inventory& p1, const Inventory& p2)
{ return(p1.inStock > p2.inStock); }
/****************************************************************/
Inventory hasMost(const Inventory& p1, const Inventory& p2, const Inventory& p3)
{
if(p1 > p2 && p1 > p3) {return p1;}
if(p2 > p1 && p2 > p3) {return p2;}
return p3;
}
/****************************************************************/
Inventory::Inventory()
{
partNum = -1;
inStock = 0;
cost = 0.0;
}
/****************************************************************/
Inventory::Inventory (int num)
{
partNum = num;
inStock = 0;
cost = 0.0;
}
/****************************************************************/
Inventory::Inventory (int num, int stock, double price)
{
partNum = num;
inStock = stock;
cost = price;
}
/****************************************************************/
int Inventory::getPartNum() const
{ return partNum; }
/****************************************************************/
int Inventory::getInStock() const
{ return inStock; }
/****************************************************************/
double Inventory::getCost() const
{ return cost; }
/****************************************************************/
void Inventory::setPartNum(int num)
{ partNum = num; }
/****************************************************************/
void Inventory::setInStock(int num)
{ inStock = num; }
/****************************************************************/
void Inventory::setCost(double num)
{ cost = num; }
/****************************************************************/
/*************************************************/
// R. A. Hillyard
// inventoryListMain.cpp
//
// Test program to demonstrate the use
// of classes as array elements
/*************************************************/
#include <iostream>
#include <fstream>
#include <iomanip>
#include "inventory.h"
using namespace std;
//function prototypes
void display(Inventory list[], int numVals);
int getInput(Inventory list[], int size);
void sortList(Inventory list[], int numVals);
const int ListSize = 10;
int main(void)
{
Inventory inventoryList[ListSize]; //declate an array of type Inventory
int numVals = 0; //variable to hold number of array elements
numVals = getInput(inventoryList, ListSize); //get Input from file
sortList(inventoryList, numVals); //sort the list
display(inventoryList, numVals); //display the list
}
/*************************************************/
/*************************************************/
//getInput
// open a file for reading and read file data into
// an array of Inventory objects up to "size" values.
// prints message if file is empty or greater than size
// returns the actual number of entries read
/*************************************************/
int getInput(Inventory list[], int size)
{
int num = 0; //counter for number of entries read
char inFile[] = "inv.dat"; //file name for testing - allow user to enter later
ifstream ins(inFile); //open file and check for success
if(ins.fail())
{
cout << "failed to open " << inFile << endl;
exit(1);
}
//read data from file - stop when all data is read
//or maximum size is reached
//note use of extraction
while((num < size) && (ins >> list[num]))
{
num++;
}
//check for error conditions
if(num == 0)
{cout << "Data file was empty\n";}
if(num == size && !ins.eof())
{cout << "Data file has more than " << size << " entries\n";}
return num; //return actual number of entries read
}
/*************************************************/
// print each element of the array
// note use of insertion operator
/*************************************************/
void display(Inventory list[], int numVals)
{
for (int i = 0; i< numVals; i++)
cout << list[i];
}
/*************************************************/
//sort list based on part number
/*************************************************/
void sortList(Inventory list[], int numVals)
{
Inventory temp;
int i, j;
int smallest;
int smallestIndex = 0;
for(i = 0; i< numVals; i++)
{
smallest = list[i].getPartNum();
smallestIndex = i;
for(j = i+1; j < numVals; j++)
{
if(list[j].getPartNum() < smallest)
{
smallest = list[j].getPartNum();
smallestIndex = j;
}
}
//swap
temp = list[i];
list[i] = list[smallestIndex];
list[smallestIndex] = temp;
}
}
/*************************************************/
/***********************Data File*****************/
3563 126 13.88
8734 238 7.33
2345 765 12.88
1456 345 100.51
8723 7654 0.88
/*************************Output******************/
1456 345 100.51
2345 765 12.88
3563 126 13.88
8723 7654 0.88
8734 238 7.33