vendredi 27 mai 2022

Counter and ID attribute of class stored in vector

I have a dilemma regarding my revoming database function code. Whenever I remove the database in vector with unique, I cannot think of writing the code that could fill the gap with the removed number (like I removed database ID3 and I want that the IDs of further databases would increment to have a stable sequence, so the database with ID4 would become ID3). I also don't know how to decrement my static int counter. File:

**void Database::RemoveDB()
{
    int det;
    cout << "Enter the ID of the base that you want to remove:\n";
    std::cout << "The bases are:\n";
    printids();
    cin >> det;
    auto iter = std::find_if(std::begin(DbMain),  std::end(DbMain), [&](Natobase& nb)
    { return nb.get_ID() == det; });
    if (iter !=  DbMain.end())
    {
        DbMain.erase(iter);
    }
}**
std::istream &operator>>(std::istream &re, Natobase &product)
{
    std::cout << "Enter the name of the base: \n";
    re >> product.name;
    std::cout << "Enter the city where it is located: \n";
    re >> product.city;
    std::cout << "Enter the country where it is located: \n";
    re >> product.country;
    std::cout << "Enter the number of guns held: \n";
    re >> product.rifles;
    std::cout << "Enter the number of tanks stationing: \n";
    re >> product.tanks;
    std::cout << "Enter the number of planes stationing: \n";
    re >> product.planes;
    std::cout << "Enter the number of launchers stationing: \n";
    re >> product.launcher;
    product.get_counter();
    product.ID = product.counter;
    return re;
}
void Database::printids()
{
    for (auto it = std::begin(DbMain); it != std::end(DbMain); ++it)
    {
        std::cout << std::endl;
        printnames(std::cout, *it) << std::endl;
    }
}
std::ostream &printnames(std::ostream &pr, Natobase &pro)
{
    pr << "\nID:" << pro.ID << "\nName:" << pro.name;
    return pr;
}

Header file:

#ifndef OOPLABPROJECT_NATOBASE_H
#define OOPLABPROJECT_NATOBASE_H
#include <string>
#include <vector>
#include "Natobase.h"

class Natobase{
    friend std::ostream &tosave(std::ostream &, const Natobase &);
    friend std::istream &toim(std::istream &, Natobase &);
    friend std::ostream &printnames(std::ostream &, Natobase &);
    friend std::istream &operator>>(std::istream &, Natobase &);
    friend class Database;
public:
    Natobase() = default;
    Natobase(std::string n, std::string ci, std::string co, int ri, int tan, int pla, int lan); //read-only
    Natobase(std::string init); //initialisation

    const std::string &get_name() const { return this->name; }
    const std::string &get_city() const { return this->city; }
    const std::string &get_country() const { return this->country; }
    const int &get_rifle() const { return this->rifles; }
    const int &get_tanks() const { return this->tanks; }
    const int &get_planes() const { return this->planes; }
    const int &get_launch() const { return this->launcher; }
    const int &get_ID() const { return this->ID; }
private:

    std::string name;
    std::string city;
    std::string country;
    int rifles;
    int tanks;
    int planes;
    int launcher;
    static int counter;
    int ID;
    static int get_counter()
    {
        counter++;
        return counter;
    };
};
std::ostream &tosave(std::ostream &, const Natobase &); //save data into txt file
std::istream &toim(std::istream &, Natobase &);// to read the data from txt file
std::ostream &printnames(std::ostream &, Natobase &); //used for edit and remove function in Database class
std::ostream &operator<<(std::ostream &, const Natobase &); //input attributes
std::istream &operator>>(std::istream &, Natobase &); //output attributes
////////////////////////
class Database {
public:
    Database() = default;
    void print_data();
    void read_data();
    void saveDB();
    void openDB();
    void EditDB();
    void RemoveDB();
    void printids();
    void compare();
private:
    std::vector<Natobase> DbMain;
};


#endif //OOPLABPROJECT_NATOBASE_H

Aucun commentaire:

Enregistrer un commentaire