desislava_topuzakova

Firm

Apr 22nd, 2018
250
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 9.64 KB | None | 0 0
  1.  
  2.  
  3. #include "stdafx.h"
  4. #include <iostream>
  5. #include <iomanip>
  6. #include <string>
  7. #include <sstream>
  8. #include <queue>
  9. #include <vector>
  10. #include <ostream>
  11. #include <istream>
  12. #include <fstream>
  13.  
  14. const std::string filename = "Databases.dat";
  15. // -> Rule of zero
  16. class Firm {
  17. private:
  18.     std::string number;
  19.     std::string name;
  20.     std::string adress;
  21.     unsigned int workers;
  22.     double salary;
  23. public:
  24.     Firm(); //defalut constructor
  25.  
  26.     Firm(std::string number, std::string name, std::string adress, unsigned int mileage, double salary); //Constructor
  27.  
  28.     std::string getNumber(); // getter
  29.  
  30.     std::string getAdress(); // getter
  31.  
  32.     bool operator<(const Firm& other)const; // overloaded operator for comparison - used from containers -> priority_queue
  33.  
  34.     friend std::ostream& operator<<(std::ostream& ostream, const Firm& firm); // stream operators output
  35.  
  36.     friend std::istream& operator>>(std::istream& istream, Firm& firm); // stream operators input
  37.  
  38.     std::string toString(); /// -> print data in string
  39. };
  40. std::string Firm::toString() {
  41.     std::ostringstream info;
  42.  
  43.     int position;
  44.     for (int i = 2; i < this->name.size(); i++) {
  45.         if (isupper(this->name[i])) {                   //Only for driver with two names separated by capital letter
  46.             position = i;
  47.             break;
  48.         }
  49.     }
  50.  
  51.     info << "Number: " << this->number.substr(0, 2) << " " << this->number.substr(2, 4)
  52.         << " " << this->number.substr(6, 2) << ", Name: " << this->name << ", Adress: "
  53.         << this->adress.substr(0, position) << " "
  54.         << this->adress.substr(position, this->adress.size() - 1) << ", Workers: " << this->workers
  55.         << ", Salary: " << this->salary << std::endl;
  56.  
  57.     return info.str();
  58. }
  59. std::string Firm::getNumber() {
  60.     return this->number;
  61. }
  62. std::string Firm::getAdress() {
  63.     return this->adress;
  64. }
  65. bool Firm::operator<(const Firm& other)const {
  66.     return this->workers < other.workers;
  67. }
  68. std::ostream& operator<<(std::ostream& ostream, const Firm& firm) {
  69.     ostream << firm.number << " " << firm.name << " " << firm.adress << " " << firm.workers << " " << firm.salary << std::endl;
  70.     return ostream;
  71. }
  72. std::istream& operator>>(std::istream& istream, Firm& firm) {
  73.     istream >> firm.number >> firm.name >> firm.adress >> firm.workers>>firm.salary;
  74.     return istream;
  75. }
  76. Firm::Firm() {
  77. }
  78. Firm::Firm(std::string number, std::string name, std::string adress, unsigned int workers, double salary) :
  79.     number(number), //Initializer list -> for reg and model fields
  80.     workers(workers)
  81. { //-> constructor body
  82.     if (adress.size() > 20) {
  83.         this->adress = adress.substr(0, 20);
  84.     }
  85.     else {
  86.         this->adress = adress;
  87.     }
  88.     if (name.size() > 35) {
  89.         this->name = name.substr(0, 35);
  90.     }
  91.     else {
  92.         this->name = name;
  93.     }
  94. }
  95. std::vector<Firm> readFile() { // -> container to read from file
  96.     std::vector<Firm> firms;
  97.  
  98.     std::ifstream fileInput;
  99.     fileInput.open(filename, std::ios::in | std::ios::app | std::ios::binary);
  100.     if (!fileInput.is_open()) {
  101.         std::cerr << "Error opening file!" << std::endl;
  102.     }
  103.     else {
  104.         Firm temp;
  105.         while (fileInput >> temp) {
  106.             firms.push_back(temp);
  107.         }
  108.     }
  109.     fileInput.close();
  110.     return firms;
  111. }
  112. void replaceData(const std::vector<Firm>& firms) {
  113.     std::ifstream fileInput;
  114.     fileInput.open(filename, std::ios::out | std::ios::trunc | std::ios::binary);
  115.     fileInput.close();
  116.  
  117.     std::ofstream fileOutput;
  118.     fileOutput.open(filename, std::ios::in | std::ios::app | std::ios::binary);
  119.     for (int i = 0; i < firms.size(); i++) {
  120.         fileOutput << firms[i];
  121.     }
  122.     fileOutput.close();
  123. }
  124. std::string readEntry() {
  125.     std::string inputData;
  126.     getline(std::cin, inputData);
  127.     std::istringstream input(inputData);
  128.     std::string str;
  129.     std::string element;
  130.  
  131.     while (input >> str) {
  132.         element += str;
  133.     }
  134.     return element;
  135. }
  136. unsigned int readWorkers() {
  137.     unsigned int workers;
  138.  
  139.     std::string inputLine;
  140.     getline(std::cin, inputLine);
  141.     if (!isdigit(inputLine[0])) {
  142.         std::cout << std::endl;
  143.         std::cout << std::setw(20) << "Bad entry, please enter a number: ";
  144.         getline(std::cin, inputLine);
  145.     }
  146.     std::istringstream input(inputLine);
  147.  
  148.     input >> workers;
  149.  
  150.     return workers;
  151. }
  152. double readSalary() {
  153.     double salary;
  154.  
  155.     std::string inputLine;
  156.     getline(std::cin, inputLine);
  157.     if (!isdigit(inputLine[0])) {
  158.         std::cout << std::endl;
  159.         std::cout << std::setw(20) << "Bad entry, please enter a number: ";
  160.         getline(std::cin, inputLine);
  161.     }
  162.     std::istringstream input(inputLine);
  163.  
  164.     input >> salary;
  165.  
  166.     return salary;
  167. }
  168. void addEntry() {
  169.     std::string arr[4];
  170.     for (int i = 0; i <= 4; i++) {
  171.         if (i == 0) {
  172.             std::cout << std::setw(20) << std::setiosflags(std::ios::left) << "Enter number: ";
  173.         }
  174.         else if (i == 1) {
  175.             std::cout << std::setw(20) << std::setiosflags(std::ios::left) << "Enter name: ";
  176.         }
  177.         else if (i == 2) {
  178.             std::cout << std::setw(20) << std::setiosflags(std::ios::left) << "Enter adress: ";
  179.         }
  180.         else if (i == 3) {
  181.             std::cout << std::setw(20) << std::setiosflags(std::ios::left) << "Enter workers: ";
  182.             unsigned int workers;
  183.             workers = readWorkers();
  184.  
  185.             Firm firm(arr[0], arr[1], arr[2], workers,salary);
  186.  
  187.             std::ofstream fileOutput;
  188.             fileOutput.open(filename, std::ios::out | std::ios::app | std::ios::binary);
  189.             fileOutput << firm;
  190.             fileOutput.close();
  191.             std::cout << std::endl;
  192.             std::cout << "Firm successfully added!" << std::endl;
  193.             std::cout << std::endl;
  194.             return;
  195.         }
  196.         else if (i == 4) {
  197.             std::cout << std::setw(20) << std::setiosflags(std::ios::left) << "Enter salary: ";
  198.             double salary;
  199.             salary = readSalary();
  200.  
  201.             Firm firm(arr[0], arr[1], arr[2], workers ,salary);
  202.  
  203.             std::ofstream fileOutput;
  204.             fileOutput.open(filename, std::ios::out | std::ios::app | std::ios::binary);
  205.             fileOutput << firm;
  206.             fileOutput.close();
  207.             std::cout << std::endl;
  208.             std::cout << "Bus successfully added!" << std::endl;
  209.             std::cout << std::endl;
  210.             return;
  211.         }
  212.         std::string element = readEntry();
  213.         arr[i] = element;
  214.     }
  215. }
  216. void editEntry() {
  217.  
  218.     std::cout << std::setw(20) << std::setiosflags(std::ios::left) << "Enter number: ";
  219.     std::string number = readEntry();
  220.  
  221.     std::vector<Firm> firms = readFile();
  222.  
  223.     for (int i = 0; i < firms.size(); i++) {
  224.         if (firms[i].getNumber() == number) {
  225.             std::cout << std::setw(20) << std::setiosflags(std::ios::left) << "Enter new name: ";
  226.             std::string newName = readEntry();
  227.             std::cout << std::setw(20) << std::setiosflags(std::ios::left) << "Enter new adress: ";
  228.             std::string adress = readEntry();
  229.             std::cout << std::setw(20) << std::setiosflags(std::ios::left) << "Enter new workers: ";
  230.             unsigned int workers;
  231.             workers = readWorkers();
  232.             std::cout << std::setw(20) << std::setiosflags(std::ios::left) << "Enter new salary: ";
  233.             double salary;
  234.             salary = readSalary();
  235.             firms[i] = Firm(firms[i].getNumber, newName, firms[i].getAdress, workers, salary);
  236.  
  237.             replaceData(firms);
  238.  
  239.             std::cout << std::endl;
  240.             std::cout << std::setw(20) << "Firm successfully edited" << std::endl;
  241.             std::cout << std::endl;
  242.             return;
  243.         }
  244.     }
  245.     std::cout << std::endl;
  246.     std::cout << std::setw(20) << "Firm not found!" << std::endl;
  247.     std::cout << std::endl;
  248. }
  249. void deleteEntry() {
  250.     std::cout << std::setw(20) << "Enter number: ";
  251.     std::string number = readNumber();
  252.  
  253.     std::vector<Firm> firms = readFile();
  254.  
  255.     bool isDeleted = false;
  256.  
  257.     for (int i = 0; i < buses.size(); i++) {
  258.         if (buses[i].getRegistration() == registration) {
  259.             buses.erase(buses.begin() + i);
  260.             isDeleted = true;
  261.             break;
  262.         }
  263.     }
  264.     if (isDeleted) {
  265.  
  266.         replaceData(buses);
  267.  
  268.         std::cout << std::endl;
  269.         std::cout << std::setw(20) << "Record successfully deleted!" << std::endl;
  270.         std::cout << std::endl;
  271.         return;
  272.     }
  273.     else {
  274.         std::cout << std::endl;
  275.         std::cout << std::setw(20) << "Bus not found!" << std::endl;
  276.         std::cout << std::endl;
  277.     }
  278. }
  279. void showRecords() {
  280.     std::vector<Bus> buses = readFile();
  281.     std::cout << std::endl;
  282.     if (buses.size() == 0) {
  283.         std::cout << std::endl;
  284.         std::cout << std::setw(20) << "No records to show!" << std::endl;
  285.         std::cout << std::endl;
  286.         return;
  287.     }
  288.     for (int i = 0; i < buses.size(); i++) {
  289.         std::cout << buses[i].toString();
  290.     }
  291.     std::cout << std::endl;
  292. }
  293. void printHighestMileage() {
  294.     std::vector<Bus> buses = readFile();
  295.     if (buses.size() == 0) {
  296.         std::cout << std::endl;
  297.         std::cout << std::setw(20) << "No records to show!" << std::endl;
  298.         std::cout << std::endl;
  299.         return;
  300.     }
  301.     std::priority_queue<Bus, std::vector<Bus>, std::less<Bus>> busesContainer;
  302.     for (int i = 0; i < buses.size(); i++) {
  303.         busesContainer.push(buses[i]);
  304.     }
  305.     std::cout << std::endl;
  306.     std::cout << std::setw(20) << "The bus with highest mileage is:" << std::endl;
  307.     Bus temp = busesContainer.top();
  308.     std::cout << temp.toString();
  309.     std::cout << std::endl;
  310. }
  311. int main() {
  312.  
  313.     std::cin.sync_with_stdio(false); /// Faster cin, getline when not sync
  314.  
  315.     while (true) {
  316.  
  317.         std::cout << std::setw(20) << "BUSES DATABASE:" << std::endl;
  318.         std::cout << "Enter your choice:" << std::endl;
  319.         std::cout << "A - Add a bus record" << std::endl;
  320.         std::cout << "E - Edit a bus record" << std::endl;
  321.         std::cout << "D - Delete a bus record" << std::endl;
  322.         std::cout << "S - Show all bus records" << std::endl;
  323.         std::cout << "H - Show the bus with highest mileage" << std::endl;
  324.         std::cout << "Q - Quit Program" << std::endl;
  325.  
  326.         std::string inputLine;
  327.         getline(std::cin, inputLine);
  328.         char command;
  329.         command = inputLine[0];
  330.         inputLine = "";
  331.         command = toupper(command);
  332.  
  333.         if (command == 'Q') {
  334.             break;
  335.         }
  336.         else if (command == 'A') {
  337.             addEntry();
  338.         }
  339.         else if (command == 'E') {
  340.             editEntry();
  341.         }
  342.         else if (command == 'D') {
  343.             deleteEntry();
  344.         }
  345.         else if (command == 'S') {
  346.             showRecords();
  347.         }
  348.         else if (command == 'H') {
  349.             printHighestMileage();
  350.         }
  351.     }
  352.     return 0;
  353. }
Advertisement
Add Comment
Please, Sign In to add comment