Advertisement
Guest User

Untitled

a guest
Dec 12th, 2017
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.92 KB | None | 0 0
  1. //Lab #8 - lab8.cpp
  2.  
  3. #include <iostream>
  4. #include <fstream>
  5. #include <iomanip>
  6. #include <string>
  7. #include <cstdlib>
  8. #include "HardwareData.h"
  9.  
  10. using namespace std;
  11.  
  12. //Enum class for menu choices
  13. enum class MenuChoice {OPEN_FILE = 1, LIST_RECORDS, ENTER_RECORD, EXIT};
  14.  
  15. //Function prototypes
  16. MenuChoice displayMenu();
  17. HardwareData entry; //Data will be read from file into entry
  18. void openFile(fstream&);
  19. void printList(fstream&);
  20. void enterRecord(fstream&);
  21.  
  22. string filename;
  23.  
  24.  
  25. int main() {
  26.  
  27. //Create file stream
  28. fstream inventoryFileInOut;
  29.  
  30. //Create object choice to store user's selection
  31. MenuChoice choice;
  32.  
  33. //Branch based on user's menu choice. Will loop until user chooses to exit
  34. while ((choice = displayMenu()) != MenuChoice::EXIT) {
  35. switch (choice) {
  36. //Open file
  37. case MenuChoice::OPEN_FILE:
  38. openFile(inventoryFileInOut);
  39. break;
  40. //List records
  41. case MenuChoice::LIST_RECORDS:
  42. printList(inventoryFileInOut);
  43. break;
  44. //Enter record
  45. case MenuChoice::ENTER_RECORD:
  46. enterRecord(inventoryFileInOut);
  47. break;
  48. //Exit. Breaks while loop
  49. case MenuChoice::EXIT:
  50. break;
  51. /*Notify of invalid input. Calls no functions
  52. and returns to beginning of while loop for new
  53. user input*/
  54. default:
  55. cout << "Invalid option!" << endl;
  56. break;
  57. }
  58. }
  59. }
  60.  
  61. MenuChoice displayMenu() {
  62.  
  63. //Display menu options
  64. cout << "***************************************" << endl;
  65. cout << "Enter 1 for opening data file" << endl;
  66. cout << "Enter 2 for listing all the records" << endl;
  67. cout << "Enter 3 for entering a new record" << endl;
  68. cout << "Enter 4 to exit" << endl;
  69. cout << "***************************************" << endl;
  70.  
  71. //Prompts for and stores choice
  72. cout << "Choice: ";
  73. int choice;
  74. cin >> choice;
  75.  
  76. //Casts choice as menuChoice and returns it
  77. return static_cast<MenuChoice>(choice);
  78. }
  79.  
  80. void openFile(fstream& inventoryFileInOut) {
  81.  
  82. //Prompt for and store filename
  83. cout << "Enter the file name: ";
  84. cin >> filename;
  85.  
  86. //Open file for input and output in binary mode
  87. inventoryFileInOut.open(filename, ios::in | ios::out | ios::binary);
  88.  
  89. //If file doesn't exist
  90. if (!inventoryFileInOut) {
  91. cout << "File does not exist. Creating the data file." << endl;
  92.  
  93. //Close current fstream and create new file with 100 blank records
  94. inventoryFileInOut.close();
  95. ofstream inventoryFileOut;
  96. inventoryFileOut.open(filename, ios::out | ios::binary);
  97.  
  98. HardwareData blankRecord; //Creates blank record of HardwareData
  99.  
  100. //Create 100 empty records in file
  101. for (int i{0}; i < 100; ++i) {
  102. inventoryFileOut.write(reinterpret_cast<const char*>(&blankRecord), sizeof(HardwareData));
  103. }
  104. inventoryFileOut.close();
  105. }
  106. else {
  107. inventoryFileInOut.close();
  108. }
  109. }
  110.  
  111. void printList(fstream& inventoryFileIn) {
  112. inventoryFileIn.open(filename, ios::in | ios::binary);
  113.  
  114. cout << left << setw(12) << "Record#" << setw(30) << "HardwareData name" << setw(15) << "Quantity" << setw(7) << "Cost" << endl;
  115.  
  116. inventoryFileIn.seekg(0);
  117.  
  118. inventoryFileIn.read(reinterpret_cast<char*>(&entry), sizeof(HardwareData));
  119.  
  120. while (!inventoryFileIn.eof()) {
  121. //if (entry.getRecordNumber() != 0) {
  122. cout << left << setw(12) << entry.getRecordNumber() << setw(30) << entry.getToolName() << setw(15) << entry.getQuantity() << setw(7) << entry.getCost() << endl;
  123.  
  124. //}
  125.  
  126. inventoryFileIn.read(reinterpret_cast<char*>(&entry), sizeof(HardwareData));
  127. }
  128. cout << endl;
  129. inventoryFileIn.close();
  130. }
  131.  
  132. void enterRecord(fstream& inventoryFileOut) {
  133.  
  134. //Open file for output
  135. inventoryFileOut.open(filename, ios::out | ios::binary);
  136.  
  137. //Variables to store input
  138. int recordNumber;
  139. string toolName;
  140. int quantity;
  141. double cost;
  142.  
  143. //Prompt user to enter part number. -1 will exit
  144. cout << "Enter the part number (0-99, -1 to end input): ";
  145. cin >> recordNumber;
  146. cout << recordNumber * sizeof(HardwareData);
  147.  
  148. //While user has not chosen to end input
  149. while (recordNumber != -1) {
  150.  
  151. //Prompt for and store remaining input
  152. cout << "Enter the tool name: ";
  153. cin >> toolName;
  154. cout << "Enter quantity and price: ";
  155. cin >> quantity;
  156. cin >> cost;
  157.  
  158. //Holds entered data, gets written to file
  159. //HardwareData newEntry;
  160.  
  161. //Set data in newEntry
  162. entry.setRecordNumber(recordNumber);
  163. entry.setToolName(toolName);
  164. entry.setQuantity(quantity);
  165. entry.setCost(cost);
  166.  
  167.  
  168. //Seek to record location
  169. inventoryFileOut.seekp(recordNumber * sizeof(HardwareData));
  170.  
  171. //Write newEntry to file
  172. inventoryFileOut.write(reinterpret_cast<const char*>(&entry), sizeof(HardwareData));
  173.  
  174. //Prompt for next set of input. -1 will exit
  175. cout << "Enter the part number (0-99, -1 to end input): ";
  176. cin >> recordNumber;
  177. }
  178. inventoryFileOut.close();
  179. }
  180.  
  181. //Lab #8 - HardwareData.cpp
  182.  
  183. #include <string>
  184. #include <iostream>
  185. #include "HardwareData.h"
  186.  
  187. using namespace std;
  188.  
  189. //Default constructor
  190. HardwareData::HardwareData()
  191. : recordNumber(0), quantity(0), cost(0), toolName("") {}
  192.  
  193. //Get and set functions
  194. //Record number
  195. int HardwareData::getRecordNumber() const {return recordNumber;}
  196. void HardwareData::setRecordNumber(int recordNumberValue) {recordNumber = recordNumberValue;}
  197.  
  198. //Tool name
  199. string HardwareData::getToolName() const {cout << toolName; return toolName;}
  200. void HardwareData::setToolName(string toolNameValue) {toolName = toolNameValue; cout << toolName;}
  201.  
  202. //Quantity
  203. int HardwareData::getQuantity() const {return quantity;}
  204. void HardwareData::setQuantity(int quantityValue) {quantity = quantityValue;}
  205.  
  206. //Cost
  207. double HardwareData::getCost() const {return cost;}
  208. void HardwareData::setCost(double costValue) {cost = costValue;}
  209.  
  210. //Lab #8 - HardwareData.h
  211.  
  212. #ifndef HARDWAREDATA_H
  213. #define HARDWAREDATA_H
  214.  
  215. #include <string>
  216.  
  217. class HardwareData {
  218. public:
  219. //Default constructor
  220. HardwareData();
  221.  
  222. //Get and set functions
  223. //Record number
  224. int getRecordNumber() const;
  225. void setRecordNumber(int);
  226. //Tool name
  227. std::string getToolName() const;
  228. void setToolName(std::string);
  229. //Quantity
  230. int getQuantity() const;
  231. void setQuantity(int);
  232. //Cost
  233. double getCost() const;
  234. void setCost(double);
  235.  
  236. private:
  237. int recordNumber;
  238. std::string toolName;
  239. int quantity;
  240. double cost;
  241. };
  242.  
  243. #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement