MartinPaunov

Buses

Mar 28th, 2018
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.77 KB | None | 0 0
  1. #include "stdafx.h"
  2. #include <iostream>
  3. #include <iomanip>
  4. #include <string>
  5. #include <sstream>
  6. #include <queue>
  7. #include <vector>
  8. #include <ostream>
  9. #include <istream>
  10. #include <fstream>
  11.  
  12. const char* filename = "Database.dat";
  13.  
  14. class Bus {
  15. private:
  16.     std::string registration;
  17.     std::string model;
  18.     std::string driverName;
  19.     unsigned int mileage;
  20. public:
  21.     Bus();
  22.     Bus(std::string registration, std::string model, std::string driverName, unsigned int mileage);
  23.  
  24.     std::string getModel();
  25.  
  26.     std::string getRegistration();
  27.  
  28.     bool operator<(const Bus& other)const;
  29.  
  30.     friend std::ostream& operator<<(std::ostream& ostream, const Bus& bus);
  31.  
  32.     friend std::istream& operator>>(std::istream& istream, Bus& bus);
  33. };
  34. std::string Bus::getModel() {
  35.     return this->model;
  36. }
  37.  
  38. std::string Bus::getRegistration() {
  39.     return this->registration;
  40. }
  41.  
  42. bool Bus::operator<(const Bus& other)const{
  43.     return this->mileage < other.mileage;
  44. }
  45.  
  46. std::ostream& operator<<(std::ostream& ostream, const Bus& bus) {
  47.     ostream << bus.registration << " " << bus.model << " " << bus.driverName << " " << bus.mileage << std::endl;
  48.     return ostream;
  49. }
  50.  
  51. std::istream& operator>>(std::istream& istream, Bus& bus) {
  52.     istream >> bus.registration >> bus.model >> bus.driverName >> bus.mileage;
  53.     return istream;
  54. }
  55. Bus::Bus() {
  56. }
  57.  
  58. Bus::Bus(std::string registration, std::string model, std::string driverName, unsigned int mileage) :
  59.     registration(registration),
  60.     driverName(driverName),
  61.     model(model),
  62.     mileage(mileage) {
  63. }
  64. std::string readEntry() {
  65.     std::string inputData;
  66.     getline(std::cin, inputData);
  67.     std::istringstream input(inputData);
  68.     std::string str;
  69.     std::string element;
  70.  
  71.     while (input >> str) {
  72.         element += str;
  73.     }
  74.     return element;
  75. }
  76. void readFromFile() {
  77.  
  78. }
  79.  
  80. void addEntry() {
  81.     std::cin.ignore();
  82.     std::string arr[3];
  83.     for (int i = 0; i < 4; i++) {
  84.         if (i == 0) {
  85.             std::cout << std::setw(20) << "Enter registration: ";
  86.         }else if (i == 1) {
  87.             std::cout << std::setw(20) << "Enter model: ";
  88.         }else if (i == 2) {
  89.             std::cout << std::setw(20) << "Enter driver name: ";
  90.         }else if (i == 3) {
  91.             std::cout << std::setw(20) << "Enter mileage: ";
  92.             unsigned int milage;
  93.             std::cin >> milage;
  94.             std::cin.ignore();
  95.  
  96.             Bus bus(arr[0], arr[1], arr[2], milage);
  97.  
  98.             std::ofstream fileOutput;
  99.             fileOutput.open(filename, std::ios::out | std::ios::app | std::ios::binary);
  100.             fileOutput << bus;
  101.             fileOutput.close();
  102.             std::cout << std::endl;
  103.             std::cout << "Bus successfully added!" << std::endl;
  104.             std::cout << std::endl;
  105.             return;
  106.         }
  107.         std::string element = readEntry();
  108.         arr[i] = element;
  109.     }
  110. }
  111. void editEntry() {
  112.    
  113.     std::cout << std::setw(20) << "Enter registration: ";
  114.     std::cin.ignore();
  115.     std::string registration = readEntry();
  116.  
  117.     std::ifstream fileInput;
  118.     fileInput.open(filename, std::ios::in | std::ios::app | std::ios::binary);
  119.  
  120.     std::vector<Bus> buses;
  121.     Bus temp;
  122.     while (fileInput >> temp) {
  123.         buses.push_back(temp);
  124.     }
  125.     fileInput.close();
  126.     for (int i = 0; i < buses.size(); i++) {
  127.         if (buses[i].getRegistration() == registration) {
  128.             std::cout << std::setw(20) << "Enter new registration: ";
  129.             std::string registration = readEntry();
  130.             std::cout << std::setw(20) << "Enter new driver name: ";
  131.             std::string driverName = readEntry();
  132.             std::cout << std::setw(20) << "Enter new mileage: ";
  133.             int milage;
  134.             std::cin >> milage;
  135.             std::cin.ignore();
  136.             buses[i] = Bus(registration, buses[i].getModel(), driverName, milage);
  137.  
  138.             fileInput.open(filename, std::ios::out | std::ios::trunc |std::ios::binary);
  139.             fileInput.close();
  140.  
  141.             std::ofstream fileOutput;
  142.             fileOutput.open(filename, std::ios::in | std::ios::app | std::ios::binary);
  143.             for (int j = 0; j < buses.size(); j++){
  144.                 fileOutput << buses[j];
  145.             }
  146.             fileOutput.close();
  147.             std::cout << std::endl;
  148.             std::cout << std::setw(20) << "Bus successfully edited" << std::endl;
  149.             std::cout << std::endl;
  150.             return;
  151.         }
  152.     }
  153.     std::cout << std::setw(20) << "Bus not found!" << std::endl;
  154. }
  155.  
  156. void deleteEntry() {
  157.     std::cout << std::setw(20) << "Enter registration: ";
  158.     std::cin.ignore();
  159.     std::string registration = readEntry();
  160.  
  161.     std::ifstream fileInput;
  162.     fileInput.open(filename, std::ios::in | std::ios::app | std::ios::binary);
  163.  
  164.     std::vector<Bus> buses;
  165.     Bus temp;
  166.  
  167.     bool isDeleted = false;
  168.  
  169.     while (fileInput >> temp) {
  170.         if (temp.getRegistration() != registration) {
  171.             buses.push_back(temp);
  172.         }else {
  173.             isDeleted = true;
  174.         }
  175.     }
  176.     fileInput.close();
  177.  
  178.     fileInput.open(filename, std::ios::out | std::ios::trunc | std::ios::binary);
  179.     fileInput.close();
  180.  
  181.     std::ofstream fileOutput;
  182.     fileOutput.open(filename, std::ios::in | std::ios::app | std::ios::binary);
  183.  
  184.     for (int i = 0; i < buses.size(); i++) {
  185.             fileOutput << buses[i];
  186.     }
  187.     if (isDeleted){
  188.         std::cout << std::endl;
  189.         std::cout << std::setw(20) << "Record successfully deleted!" << std::endl;
  190.         std::cout << std::endl;
  191.     }else{
  192.         std::cout << std::endl;
  193.         std::cout << std::setw(20) << "Bus not found!" << std::endl;
  194.         std::cout << std::endl;
  195.     }
  196. }
  197. void showRecords() {
  198.     std::ifstream fileInput;
  199.     fileInput.open(filename, std::ios::in | std::ios::app | std::ios::binary);
  200.     std::vector<Bus> buses;
  201.     Bus temp;
  202.     while (fileInput >> temp) {
  203.             buses.push_back(temp);
  204.     }
  205.     fileInput.close();
  206.     std::cout << std::endl;
  207.     for (int i = 0; i < buses.size(); i++) {
  208.         std::cout << buses[i];
  209.     }
  210.     std::cout << std::endl;
  211. }
  212.  
  213. void printHighestMilage() {
  214.     std::ifstream fileInput;
  215.     fileInput.open(filename, std::ios::in | std::ios::app | std::ios::binary);
  216.  
  217.     std::priority_queue<Bus, std::vector<Bus>, std::less<Bus>> busesContainer;
  218.     Bus temp;
  219.  
  220.     while (fileInput >> temp) {
  221.         busesContainer.push(temp);
  222.     }
  223.     fileInput.close();
  224.  
  225.     std::cout << std::setw(20) << "The bus with highest milage is:" << std::endl;
  226.     std::cout << busesContainer.top() << std::endl;
  227. }
  228.  
  229. int main() {
  230.  
  231.     std::cin.sync_with_stdio(false);
  232.  
  233.     while (true){
  234.  
  235.         std::cout << std::setw(20) << "BUS DATABASE:" << std::endl;
  236.         std::cout << "Enter your choise:" << std::endl;
  237.         std::cout << "A - Add a bus record" << std::endl;
  238.         std::cout << "E - Edit a bus record" << std::endl;
  239.         std::cout << "D - Delete a bus record" << std::endl;
  240.         std::cout << "S - Show all bus records" << std::endl;
  241.         std::cout << "H - Show the bus with highest milage" << std::endl;
  242.         std::cout << "Q - Quit Program" << std::endl;
  243.  
  244.         std::string inputLine;
  245.         std::cin >> inputLine;
  246.         char command;
  247.         command = inputLine[0];
  248.         command = toupper(command);
  249.  
  250.         if (command == 'Q'){
  251.             break;
  252.         }else if (command == 'A') {
  253.             addEntry();
  254.         }else if (command == 'E') {
  255.             editEntry();
  256.         }else if (command == 'D') {
  257.             deleteEntry();
  258.         }else if (command == 'S') {
  259.             showRecords();
  260.         }else if (command == 'H') {
  261.             printHighestMilage();
  262.         }
  263.     }
  264.     return 0;
  265. }
Advertisement
Add Comment
Please, Sign In to add comment