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 char* filename = "Database.dat";
- class Bus {
- private:
- std::string registration;
- std::string model;
- std::string driverName;
- unsigned int mileage;
- public:
- Bus();
- Bus(std::string registration, std::string model, std::string driverName, unsigned int mileage);
- std::string getModel();
- std::string getRegistration();
- bool operator<(const Bus& other)const;
- friend std::ostream& operator<<(std::ostream& ostream, const Bus& bus);
- friend std::istream& operator>>(std::istream& istream, Bus& bus);
- };
- std::string Bus::getModel() {
- return this->model;
- }
- std::string Bus::getRegistration() {
- return this->registration;
- }
- bool Bus::operator<(const Bus& other)const{
- return this->mileage < other.mileage;
- }
- std::ostream& operator<<(std::ostream& ostream, const Bus& bus) {
- ostream << bus.registration << " " << bus.model << " " << bus.driverName << " " << bus.mileage << std::endl;
- return ostream;
- }
- std::istream& operator>>(std::istream& istream, Bus& bus) {
- istream >> bus.registration >> bus.model >> bus.driverName >> bus.mileage;
- return istream;
- }
- Bus::Bus() {
- }
- Bus::Bus(std::string registration, std::string model, std::string driverName, unsigned int mileage) :
- registration(registration),
- driverName(driverName),
- model(model),
- mileage(mileage) {
- }
- 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;
- }
- void readFromFile() {
- }
- void addEntry() {
- std::cin.ignore();
- std::string arr[3];
- for (int i = 0; i < 4; i++) {
- if (i == 0) {
- std::cout << std::setw(20) << "Enter registration: ";
- }else if (i == 1) {
- std::cout << std::setw(20) << "Enter model: ";
- }else if (i == 2) {
- std::cout << std::setw(20) << "Enter driver name: ";
- }else if (i == 3) {
- std::cout << std::setw(20) << "Enter mileage: ";
- unsigned int milage;
- std::cin >> milage;
- std::cin.ignore();
- Bus bus(arr[0], arr[1], arr[2], milage);
- std::ofstream fileOutput;
- fileOutput.open(filename, std::ios::out | std::ios::app | std::ios::binary);
- fileOutput << bus;
- 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) << "Enter registration: ";
- std::cin.ignore();
- std::string registration = readEntry();
- std::ifstream fileInput;
- fileInput.open(filename, std::ios::in | std::ios::app | std::ios::binary);
- std::vector<Bus> buses;
- Bus temp;
- while (fileInput >> temp) {
- buses.push_back(temp);
- }
- fileInput.close();
- for (int i = 0; i < buses.size(); i++) {
- if (buses[i].getRegistration() == registration) {
- std::cout << std::setw(20) << "Enter new registration: ";
- std::string registration = readEntry();
- std::cout << std::setw(20) << "Enter new driver name: ";
- std::string driverName = readEntry();
- std::cout << std::setw(20) << "Enter new mileage: ";
- int milage;
- std::cin >> milage;
- std::cin.ignore();
- buses[i] = Bus(registration, buses[i].getModel(), driverName, milage);
- 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 j = 0; j < buses.size(); j++){
- fileOutput << buses[j];
- }
- fileOutput.close();
- std::cout << std::endl;
- std::cout << std::setw(20) << "Bus successfully edited" << std::endl;
- std::cout << std::endl;
- return;
- }
- }
- std::cout << std::setw(20) << "Bus not found!" << std::endl;
- }
- void deleteEntry() {
- std::cout << std::setw(20) << "Enter registration: ";
- std::cin.ignore();
- std::string registration = readEntry();
- std::ifstream fileInput;
- fileInput.open(filename, std::ios::in | std::ios::app | std::ios::binary);
- std::vector<Bus> buses;
- Bus temp;
- bool isDeleted = false;
- while (fileInput >> temp) {
- if (temp.getRegistration() != registration) {
- buses.push_back(temp);
- }else {
- isDeleted = true;
- }
- }
- fileInput.close();
- 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 < buses.size(); i++) {
- fileOutput << buses[i];
- }
- if (isDeleted){
- std::cout << std::endl;
- std::cout << std::setw(20) << "Record successfully deleted!" << std::endl;
- std::cout << std::endl;
- }else{
- std::cout << std::endl;
- std::cout << std::setw(20) << "Bus not found!" << std::endl;
- std::cout << std::endl;
- }
- }
- void showRecords() {
- std::ifstream fileInput;
- fileInput.open(filename, std::ios::in | std::ios::app | std::ios::binary);
- std::vector<Bus> buses;
- Bus temp;
- while (fileInput >> temp) {
- buses.push_back(temp);
- }
- fileInput.close();
- std::cout << std::endl;
- for (int i = 0; i < buses.size(); i++) {
- std::cout << buses[i];
- }
- std::cout << std::endl;
- }
- void printHighestMilage() {
- std::ifstream fileInput;
- fileInput.open(filename, std::ios::in | std::ios::app | std::ios::binary);
- std::priority_queue<Bus, std::vector<Bus>, std::less<Bus>> busesContainer;
- Bus temp;
- while (fileInput >> temp) {
- busesContainer.push(temp);
- }
- fileInput.close();
- std::cout << std::setw(20) << "The bus with highest milage is:" << std::endl;
- std::cout << busesContainer.top() << std::endl;
- }
- int main() {
- std::cin.sync_with_stdio(false);
- while (true){
- std::cout << std::setw(20) << "BUS DATABASE:" << std::endl;
- std::cout << "Enter your choise:" << 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 milage" << std::endl;
- std::cout << "Q - Quit Program" << std::endl;
- std::string inputLine;
- std::cin >> inputLine;
- char command;
- command = inputLine[0];
- 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') {
- printHighestMilage();
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment