Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<iostream>
- #include<string>
- #include<iomanip>
- #include<fstream>
- using namespace std;
- // inventory class declaration
- class Inventory
- {
- private:
- string itemCode;
- string description;
- double price;
- public:
- Inventory()
- { itemCode = "XXX"; description = " "; price = 0.0;};
- Inventory(string c, string d, double p)
- {
- itemCode = c;
- description = d;
- price = p;
- }
- //Functions to retireve member varialbe values
- string getCode() const
- {
- string code = itemCode;
- return code;
- }
- string getDescription() const
- {
- string d = description;
- return d;
- }
- double getPrice() const
- {
- return price;
- }
- };//end of inventory Class declarations
- //function prototypes
- void displayInventory(const Inventory[], int);
- void bubbleSort(Inventory[], int);
- int search(const Inventory[], int, string);
- /*****************************************************/
- int main()
- {
- string name;
- ifstream inputFile;
- //open the input file
- inputFile.open("getRich.txt");
- cout << "Here are the values stored in the get Rich .txt file.\n";
- for (int count = 1; count <= 8; count++)
- {
- inputFile >> name;
- cout << name<<" ";
- }
- inputFile.close(); //close the file
- const int SIZE = 6;
- //create and initialize the array of Inventory objects
- Inventory silverware[SIZE] =
- {
- Inventory("2S15", "soup spoon",2.35),
- Inventory("6S12", "teaspoon",2.19),
- Inventory("1F15", "dinner fork",3.19),
- Inventory("3F09", "salad fork" ,2.25),
- Inventory("4K33", "knife",2.35),
- Inventory("5K41", "steak knife",4.15)
- };
- //display the inventory
- cout << "\n\nHere is the original data\n";
- displayInventory(silverware, SIZE);
- //sort the objects by their itemCode
- bubbleSort(silverware, SIZE);
- // display the inventory again
- cout << "\n Here is the Sorted data\n";
- displayInventory(silverware, SIZE);
- /************************************************************/
- string desiredCode; //the itemCode to search for
- int pos; //Position of desired object in the array
- char doAgain; //Look up another price(Y/N)
- do
- {
- //Get the itemCode to search for
- cout << "\nEnter an item code: ";
- cin >> desiredCode;
- //search for the object
- pos = search(silverware, SIZE, desiredCode);
- //If pos =-1 the code was not found
- if (pos == -1)
- cout << "That code does not exist in the array\n";
- else
- {//The object was found, so use pos to get the description and price}
- cout << "This " << silverware[pos].getDescription()
- << " costs $" << silverware[pos].getPrice() << endl;
- }
- //Does the user want to look up another price?
- cout << "\nLook up another price (Y/N)? ";
- cin >> doAgain;
- } while (doAgain == 'Y' || doAgain == 'y');
- return 0;
- } //End of main
- /**************************************/
- void displayInventory(const Inventory object[], int size)
- {
- for(int index=0; index<size; index++)
- {
- cout << setw(5) << left << object[index].getCode()
- << setw(13) << left << object[index].getDescription()
- << "$" << right << object[index].getPrice() << endl;
- }
- }
- void bubbleSort(Inventory array[], int size)
- {
- Inventory temp;
- bool swap;
- do
- {
- swap = false;
- for (int count = 0; count < (size - 1); count++)
- {
- if (array[count].getCode() > array[count + 1].getCode())
- {
- temp = array[count];
- array[count] = array[count + 1];
- array[count + 1] = temp;
- swap = true;
- }
- }
- } while (swap);
- } //end bublle sort
- int search(const Inventory object[], int size, string value)
- {
- int index = 0;
- int position = -1;
- bool found = false;
- while (index < size && !found)
- {
- if (object[index].getCode() == value)// if the value is found
- {
- found = true;
- position = index;
- }
- index++;
- }
- return position;
- } //End search
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement