Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Lab #8 - lab8.cpp
- #include <iostream>
- #include <fstream>
- #include <iomanip>
- #include <string>
- #include <cstdlib>
- #include "HardwareData.h"
- using namespace std;
- //Enum class for menu choices
- enum class MenuChoice {OPEN_FILE = 1, LIST_RECORDS, ENTER_RECORD, EXIT};
- //Function prototypes
- MenuChoice displayMenu();
- HardwareData entry; //Data will be read from file into entry
- void openFile(fstream&);
- void printList(fstream&);
- void enterRecord(fstream&);
- string filename;
- int main() {
- //Create file stream
- fstream inventoryFileInOut;
- //Create object choice to store user's selection
- MenuChoice choice;
- //Branch based on user's menu choice. Will loop until user chooses to exit
- while ((choice = displayMenu()) != MenuChoice::EXIT) {
- switch (choice) {
- //Open file
- case MenuChoice::OPEN_FILE:
- openFile(inventoryFileInOut);
- break;
- //List records
- case MenuChoice::LIST_RECORDS:
- printList(inventoryFileInOut);
- break;
- //Enter record
- case MenuChoice::ENTER_RECORD:
- enterRecord(inventoryFileInOut);
- break;
- //Exit. Breaks while loop
- case MenuChoice::EXIT:
- break;
- /*Notify of invalid input. Calls no functions
- and returns to beginning of while loop for new
- user input*/
- default:
- cout << "Invalid option!" << endl;
- break;
- }
- }
- }
- MenuChoice displayMenu() {
- //Display menu options
- cout << "***************************************" << endl;
- cout << "Enter 1 for opening data file" << endl;
- cout << "Enter 2 for listing all the records" << endl;
- cout << "Enter 3 for entering a new record" << endl;
- cout << "Enter 4 to exit" << endl;
- cout << "***************************************" << endl;
- //Prompts for and stores choice
- cout << "Choice: ";
- int choice;
- cin >> choice;
- //Casts choice as menuChoice and returns it
- return static_cast<MenuChoice>(choice);
- }
- void openFile(fstream& inventoryFileInOut) {
- //Prompt for and store filename
- cout << "Enter the file name: ";
- cin >> filename;
- //Open file for input and output in binary mode
- inventoryFileInOut.open(filename, ios::in | ios::out | ios::binary);
- //If file doesn't exist
- if (!inventoryFileInOut) {
- cout << "File does not exist. Creating the data file." << endl;
- //Close current fstream and create new file with 100 blank records
- inventoryFileInOut.close();
- ofstream inventoryFileOut;
- inventoryFileOut.open(filename, ios::out | ios::binary);
- HardwareData blankRecord; //Creates blank record of HardwareData
- //Create 100 empty records in file
- for (int i{0}; i < 100; ++i) {
- inventoryFileOut.write(reinterpret_cast<const char*>(&blankRecord), sizeof(HardwareData));
- }
- inventoryFileOut.close();
- }
- else {
- inventoryFileInOut.close();
- }
- }
- void printList(fstream& inventoryFileIn) {
- inventoryFileIn.open(filename, ios::in | ios::binary);
- cout << left << setw(12) << "Record#" << setw(30) << "HardwareData name" << setw(15) << "Quantity" << setw(7) << "Cost" << endl;
- inventoryFileIn.seekg(0);
- inventoryFileIn.read(reinterpret_cast<char*>(&entry), sizeof(HardwareData));
- while (!inventoryFileIn.eof()) {
- //if (entry.getRecordNumber() != 0) {
- cout << left << setw(12) << entry.getRecordNumber() << setw(30) << entry.getToolName() << setw(15) << entry.getQuantity() << setw(7) << entry.getCost() << endl;
- //}
- inventoryFileIn.read(reinterpret_cast<char*>(&entry), sizeof(HardwareData));
- }
- cout << endl;
- inventoryFileIn.close();
- }
- void enterRecord(fstream& inventoryFileOut) {
- //Open file for output
- inventoryFileOut.open(filename, ios::out | ios::binary);
- //Variables to store input
- int recordNumber;
- string toolName;
- int quantity;
- double cost;
- //Prompt user to enter part number. -1 will exit
- cout << "Enter the part number (0-99, -1 to end input): ";
- cin >> recordNumber;
- cout << recordNumber * sizeof(HardwareData);
- //While user has not chosen to end input
- while (recordNumber != -1) {
- //Prompt for and store remaining input
- cout << "Enter the tool name: ";
- cin >> toolName;
- cout << "Enter quantity and price: ";
- cin >> quantity;
- cin >> cost;
- //Holds entered data, gets written to file
- //HardwareData newEntry;
- //Set data in newEntry
- entry.setRecordNumber(recordNumber);
- entry.setToolName(toolName);
- entry.setQuantity(quantity);
- entry.setCost(cost);
- //Seek to record location
- inventoryFileOut.seekp(recordNumber * sizeof(HardwareData));
- //Write newEntry to file
- inventoryFileOut.write(reinterpret_cast<const char*>(&entry), sizeof(HardwareData));
- //Prompt for next set of input. -1 will exit
- cout << "Enter the part number (0-99, -1 to end input): ";
- cin >> recordNumber;
- }
- inventoryFileOut.close();
- }
- //Lab #8 - HardwareData.cpp
- #include <string>
- #include <iostream>
- #include "HardwareData.h"
- using namespace std;
- //Default constructor
- HardwareData::HardwareData()
- : recordNumber(0), quantity(0), cost(0), toolName("") {}
- //Get and set functions
- //Record number
- int HardwareData::getRecordNumber() const {return recordNumber;}
- void HardwareData::setRecordNumber(int recordNumberValue) {recordNumber = recordNumberValue;}
- //Tool name
- string HardwareData::getToolName() const {cout << toolName; return toolName;}
- void HardwareData::setToolName(string toolNameValue) {toolName = toolNameValue; cout << toolName;}
- //Quantity
- int HardwareData::getQuantity() const {return quantity;}
- void HardwareData::setQuantity(int quantityValue) {quantity = quantityValue;}
- //Cost
- double HardwareData::getCost() const {return cost;}
- void HardwareData::setCost(double costValue) {cost = costValue;}
- //Lab #8 - HardwareData.h
- #ifndef HARDWAREDATA_H
- #define HARDWAREDATA_H
- #include <string>
- class HardwareData {
- public:
- //Default constructor
- HardwareData();
- //Get and set functions
- //Record number
- int getRecordNumber() const;
- void setRecordNumber(int);
- //Tool name
- std::string getToolName() const;
- void setToolName(std::string);
- //Quantity
- int getQuantity() const;
- void setQuantity(int);
- //Cost
- double getCost() const;
- void setCost(double);
- private:
- int recordNumber;
- std::string toolName;
- int quantity;
- double cost;
- };
- #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement