Advertisement
Guest User

Untitled

a guest
Apr 20th, 2018
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.76 KB | None | 0 0
  1. #include<iostream>
  2. #include<string>
  3. #include<iomanip>
  4. #include<fstream>
  5. using namespace std;
  6.  
  7. // inventory class declaration
  8. class Inventory
  9. {
  10. private:
  11. string itemCode;
  12. string description;
  13. double price;
  14. public:
  15. Inventory()
  16. { itemCode = "XXX"; description = " "; price = 0.0;};
  17. Inventory(string c, string d, double p)
  18. {
  19. itemCode = c;
  20. description = d;
  21. price = p;
  22. }
  23. //Functions to retireve member varialbe values
  24. string getCode() const
  25. {
  26. string code = itemCode;
  27. return code;
  28. }
  29. string getDescription() const
  30. {
  31. string d = description;
  32. return d;
  33. }
  34. double getPrice() const
  35. {
  36. return price;
  37. }
  38. };//end of inventory Class declarations
  39.  
  40. //function prototypes
  41. void displayInventory(const Inventory[], int);
  42. void bubbleSort(Inventory[], int);
  43.  
  44. int search(const Inventory[], int, string);
  45. /*****************************************************/
  46.  
  47. int main()
  48. {
  49. string name;
  50. ifstream inputFile;
  51. //open the input file
  52. inputFile.open("getRich.txt");
  53. cout << "Here are the values stored in the get Rich .txt file.\n";
  54. for (int count = 1; count <= 8; count++)
  55. {
  56. inputFile >> name;
  57. cout << name<<" ";
  58. }
  59. inputFile.close(); //close the file
  60.  
  61.  
  62. const int SIZE = 6;
  63. //create and initialize the array of Inventory objects
  64. Inventory silverware[SIZE] =
  65. {
  66. Inventory("2S15", "soup spoon",2.35),
  67. Inventory("6S12", "teaspoon",2.19),
  68. Inventory("1F15", "dinner fork",3.19),
  69. Inventory("3F09", "salad fork" ,2.25),
  70. Inventory("4K33", "knife",2.35),
  71. Inventory("5K41", "steak knife",4.15)
  72. };
  73.  
  74. //display the inventory
  75. cout << "\n\nHere is the original data\n";
  76. displayInventory(silverware, SIZE);
  77. //sort the objects by their itemCode
  78. bubbleSort(silverware, SIZE);
  79.  
  80. // display the inventory again
  81. cout << "\n Here is the Sorted data\n";
  82. displayInventory(silverware, SIZE);
  83. /************************************************************/
  84.  
  85.  
  86. string desiredCode; //the itemCode to search for
  87. int pos; //Position of desired object in the array
  88. char doAgain; //Look up another price(Y/N)
  89.  
  90. do
  91. {
  92. //Get the itemCode to search for
  93. cout << "\nEnter an item code: ";
  94. cin >> desiredCode;
  95. //search for the object
  96. pos = search(silverware, SIZE, desiredCode);
  97. //If pos =-1 the code was not found
  98. if (pos == -1)
  99. cout << "That code does not exist in the array\n";
  100. else
  101. {//The object was found, so use pos to get the description and price}
  102. cout << "This " << silverware[pos].getDescription()
  103. << " costs $" << silverware[pos].getPrice() << endl;
  104. }
  105. //Does the user want to look up another price?
  106. cout << "\nLook up another price (Y/N)? ";
  107. cin >> doAgain;
  108. } while (doAgain == 'Y' || doAgain == 'y');
  109.  
  110. return 0;
  111. } //End of main
  112.  
  113. /**************************************/
  114. void displayInventory(const Inventory object[], int size)
  115. {
  116. for(int index=0; index<size; index++)
  117. {
  118. cout << setw(5) << left << object[index].getCode()
  119. << setw(13) << left << object[index].getDescription()
  120. << "$" << right << object[index].getPrice() << endl;
  121. }
  122. }
  123. void bubbleSort(Inventory array[], int size)
  124. {
  125. Inventory temp;
  126. bool swap;
  127. do
  128. {
  129. swap = false;
  130. for (int count = 0; count < (size - 1); count++)
  131. {
  132. if (array[count].getCode() > array[count + 1].getCode())
  133. {
  134. temp = array[count];
  135. array[count] = array[count + 1];
  136. array[count + 1] = temp;
  137. swap = true;
  138. }
  139. }
  140. } while (swap);
  141. } //end bublle sort
  142.  
  143. int search(const Inventory object[], int size, string value)
  144. {
  145. int index = 0;
  146. int position = -1;
  147. bool found = false;
  148. while (index < size && !found)
  149. {
  150. if (object[index].getCode() == value)// if the value is found
  151. {
  152. found = true;
  153. position = index;
  154. }
  155. index++;
  156. }
  157. return position;
  158. } //End search
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement