Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "stdafx.h"
- #include <iostream>
- #include <iomanip>
- #include <string>
- #include <sstream>
- #include <queue>
- #include <vector>
- #include <ostream>
- #include <istream>
- #include <fstream>
- const std::string filename = "Databases.dat";
- // -> Rule of zero
- class Firm {
- private:
- std::string number;
- std::string name;
- std::string adress;
- unsigned int workers;
- double salary;
- public:
- Firm(); //defalut constructor
- Firm(std::string number, std::string name, std::string adress, unsigned int mileage, double salary); //Constructor
- std::string getNumber(); // getter
- std::string getAdress(); // getter
- bool operator<(const Firm& other)const; // overloaded operator for comparison - used from containers -> priority_queue
- friend std::ostream& operator<<(std::ostream& ostream, const Firm& firm); // stream operators output
- friend std::istream& operator>>(std::istream& istream, Firm& firm); // stream operators input
- std::string toString(); /// -> print data in string
- };
- std::string Firm::toString() {
- std::ostringstream info;
- int position;
- for (int i = 2; i < this->name.size(); i++) {
- if (isupper(this->name[i])) { //Only for driver with two names separated by capital letter
- position = i;
- break;
- }
- }
- info << "Number: " << this->number.substr(0, 2) << " " << this->number.substr(2, 4)
- << " " << this->number.substr(6, 2) << ", Name: " << this->name << ", Adress: "
- << this->adress.substr(0, position) << " "
- << this->adress.substr(position, this->adress.size() - 1) << ", Workers: " << this->workers
- << ", Salary: " << this->salary << std::endl;
- return info.str();
- }
- std::string Firm::getNumber() {
- return this->number;
- }
- std::string Firm::getAdress() {
- return this->adress;
- }
- bool Firm::operator<(const Firm& other)const {
- return this->workers < other.workers;
- }
- std::ostream& operator<<(std::ostream& ostream, const Firm& firm) {
- ostream << firm.number << " " << firm.name << " " << firm.adress << " " << firm.workers << " " << firm.salary << std::endl;
- return ostream;
- }
- std::istream& operator>>(std::istream& istream, Firm& firm) {
- istream >> firm.number >> firm.name >> firm.adress >> firm.workers>>firm.salary;
- return istream;
- }
- Firm::Firm() {
- }
- Firm::Firm(std::string number, std::string name, std::string adress, unsigned int workers, double salary) :
- number(number), //Initializer list -> for reg and model fields
- workers(workers)
- { //-> constructor body
- if (adress.size() > 20) {
- this->adress = adress.substr(0, 20);
- }
- else {
- this->adress = adress;
- }
- if (name.size() > 35) {
- this->name = name.substr(0, 35);
- }
- else {
- this->name = name;
- }
- }
- std::vector<Firm> readFile() { // -> container to read from file
- std::vector<Firm> firms;
- std::ifstream fileInput;
- fileInput.open(filename, std::ios::in | std::ios::app | std::ios::binary);
- if (!fileInput.is_open()) {
- std::cerr << "Error opening file!" << std::endl;
- }
- else {
- Firm temp;
- while (fileInput >> temp) {
- firms.push_back(temp);
- }
- }
- fileInput.close();
- return firms;
- }
- void replaceData(const std::vector<Firm>& firms) {
- std::ifstream fileInput;
- fileInput.open(filename, std::ios::out | std::ios::trunc | std::ios::binary);
- fileInput.close();
- std::ofstream fileOutput;
- fileOutput.open(filename, std::ios::in | std::ios::app | std::ios::binary);
- for (int i = 0; i < firms.size(); i++) {
- fileOutput << firms[i];
- }
- fileOutput.close();
- }
- std::string readEntry() {
- std::string inputData;
- getline(std::cin, inputData);
- std::istringstream input(inputData);
- std::string str;
- std::string element;
- while (input >> str) {
- element += str;
- }
- return element;
- }
- unsigned int readWorkers() {
- unsigned int workers;
- std::string inputLine;
- getline(std::cin, inputLine);
- if (!isdigit(inputLine[0])) {
- std::cout << std::endl;
- std::cout << std::setw(20) << "Bad entry, please enter a number: ";
- getline(std::cin, inputLine);
- }
- std::istringstream input(inputLine);
- input >> workers;
- return workers;
- }
- double readSalary() {
- double salary;
- std::string inputLine;
- getline(std::cin, inputLine);
- if (!isdigit(inputLine[0])) {
- std::cout << std::endl;
- std::cout << std::setw(20) << "Bad entry, please enter a number: ";
- getline(std::cin, inputLine);
- }
- std::istringstream input(inputLine);
- input >> salary;
- return salary;
- }
- void addEntry() {
- std::string arr[4];
- for (int i = 0; i <= 4; i++) {
- if (i == 0) {
- std::cout << std::setw(20) << std::setiosflags(std::ios::left) << "Enter number: ";
- }
- else if (i == 1) {
- std::cout << std::setw(20) << std::setiosflags(std::ios::left) << "Enter name: ";
- }
- else if (i == 2) {
- std::cout << std::setw(20) << std::setiosflags(std::ios::left) << "Enter adress: ";
- }
- else if (i == 3) {
- std::cout << std::setw(20) << std::setiosflags(std::ios::left) << "Enter workers: ";
- unsigned int workers;
- workers = readWorkers();
- Firm firm(arr[0], arr[1], arr[2], workers,salary);
- std::ofstream fileOutput;
- fileOutput.open(filename, std::ios::out | std::ios::app | std::ios::binary);
- fileOutput << firm;
- fileOutput.close();
- std::cout << std::endl;
- std::cout << "Firm successfully added!" << std::endl;
- std::cout << std::endl;
- return;
- }
- else if (i == 4) {
- std::cout << std::setw(20) << std::setiosflags(std::ios::left) << "Enter salary: ";
- double salary;
- salary = readSalary();
- Firm firm(arr[0], arr[1], arr[2], workers ,salary);
- std::ofstream fileOutput;
- fileOutput.open(filename, std::ios::out | std::ios::app | std::ios::binary);
- fileOutput << firm;
- fileOutput.close();
- std::cout << std::endl;
- std::cout << "Bus successfully added!" << std::endl;
- std::cout << std::endl;
- return;
- }
- std::string element = readEntry();
- arr[i] = element;
- }
- }
- void editEntry() {
- std::cout << std::setw(20) << std::setiosflags(std::ios::left) << "Enter number: ";
- std::string number = readEntry();
- std::vector<Firm> firms = readFile();
- for (int i = 0; i < firms.size(); i++) {
- if (firms[i].getNumber() == number) {
- std::cout << std::setw(20) << std::setiosflags(std::ios::left) << "Enter new name: ";
- std::string newName = readEntry();
- std::cout << std::setw(20) << std::setiosflags(std::ios::left) << "Enter new adress: ";
- std::string adress = readEntry();
- std::cout << std::setw(20) << std::setiosflags(std::ios::left) << "Enter new workers: ";
- unsigned int workers;
- workers = readWorkers();
- std::cout << std::setw(20) << std::setiosflags(std::ios::left) << "Enter new salary: ";
- double salary;
- salary = readSalary();
- firms[i] = Firm(firms[i].getNumber, newName, firms[i].getAdress, workers, salary);
- replaceData(firms);
- std::cout << std::endl;
- std::cout << std::setw(20) << "Firm successfully edited" << std::endl;
- std::cout << std::endl;
- return;
- }
- }
- std::cout << std::endl;
- std::cout << std::setw(20) << "Firm not found!" << std::endl;
- std::cout << std::endl;
- }
- void deleteEntry() {
- std::cout << std::setw(20) << "Enter number: ";
- std::string number = readNumber();
- std::vector<Firm> firms = readFile();
- bool isDeleted = false;
- for (int i = 0; i < buses.size(); i++) {
- if (buses[i].getRegistration() == registration) {
- buses.erase(buses.begin() + i);
- isDeleted = true;
- break;
- }
- }
- if (isDeleted) {
- replaceData(buses);
- std::cout << std::endl;
- std::cout << std::setw(20) << "Record successfully deleted!" << std::endl;
- std::cout << std::endl;
- return;
- }
- else {
- std::cout << std::endl;
- std::cout << std::setw(20) << "Bus not found!" << std::endl;
- std::cout << std::endl;
- }
- }
- void showRecords() {
- std::vector<Bus> buses = readFile();
- std::cout << std::endl;
- if (buses.size() == 0) {
- std::cout << std::endl;
- std::cout << std::setw(20) << "No records to show!" << std::endl;
- std::cout << std::endl;
- return;
- }
- for (int i = 0; i < buses.size(); i++) {
- std::cout << buses[i].toString();
- }
- std::cout << std::endl;
- }
- void printHighestMileage() {
- std::vector<Bus> buses = readFile();
- if (buses.size() == 0) {
- std::cout << std::endl;
- std::cout << std::setw(20) << "No records to show!" << std::endl;
- std::cout << std::endl;
- return;
- }
- std::priority_queue<Bus, std::vector<Bus>, std::less<Bus>> busesContainer;
- for (int i = 0; i < buses.size(); i++) {
- busesContainer.push(buses[i]);
- }
- std::cout << std::endl;
- std::cout << std::setw(20) << "The bus with highest mileage is:" << std::endl;
- Bus temp = busesContainer.top();
- std::cout << temp.toString();
- std::cout << std::endl;
- }
- int main() {
- std::cin.sync_with_stdio(false); /// Faster cin, getline when not sync
- while (true) {
- std::cout << std::setw(20) << "BUSES DATABASE:" << std::endl;
- std::cout << "Enter your choice:" << std::endl;
- std::cout << "A - Add a bus record" << std::endl;
- std::cout << "E - Edit a bus record" << std::endl;
- std::cout << "D - Delete a bus record" << std::endl;
- std::cout << "S - Show all bus records" << std::endl;
- std::cout << "H - Show the bus with highest mileage" << std::endl;
- std::cout << "Q - Quit Program" << std::endl;
- std::string inputLine;
- getline(std::cin, inputLine);
- char command;
- command = inputLine[0];
- inputLine = "";
- command = toupper(command);
- if (command == 'Q') {
- break;
- }
- else if (command == 'A') {
- addEntry();
- }
- else if (command == 'E') {
- editEntry();
- }
- else if (command == 'D') {
- deleteEntry();
- }
- else if (command == 'S') {
- showRecords();
- }
- else if (command == 'H') {
- printHighestMileage();
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment