开发者

data structure to keep track of "consumptions"

开发者 https://www.devze.com 2023-02-04 01:01 出处:网络
hey everyone, i am writing a small application for my c++ class mini-project. it\'s about managing clients, lines and services at a telecom company.

hey everyone,

i am writing a small application for my c++ class mini-project.

it's about managing clients, lines and services at a telecom company.

i am supposed to use a Consumption class wich is supposed to represent a monthly consumption of a service/line.

class Consumption  
{  
private:  
     Ligne* m_line;//the line related to this consumption  
     Service* m_service;//service used by this line  
     int m_month;
  int m_year;  
  int m_units;//units of m_service used in the m_month of m_year  
public:  
     //some getters and setters  
     double price();//returns m_units * the service's price/unit
};

a line can use a maximum of 2 services/month.

consumptions wil开发者_开发知识库l be used to make a bill for a certain line.

my question is, what's the best way to keep track of the created Consumptions? i mean what data structure may be the best to use? should i make any changes to the class?

note that i am not using files to store anything.

any help is appreciated...


Best structure for storing something in array-like style with keeping the constant acces time to any element is vector. It;s just like you got dynamic sized array.

Second option to go is deque. If you plan having very much data go with this one as it have better storage managment and you can edit it from both front and back not just back as in vectors.

Last one could be list. If you plan big editting of your data, like erasing inserting new elements (not just accesing them), you should consider this one as it have linear complexity in inserting/erasing elements, while previous 2 have linear plus additional linear time in up to the number of elements between position and the end.

So conclusion:

  • vector - easiest
  • deque - good for big data storage
  • list - good for big editting of storage

Those are STL sequence containers and including of header should look like:

#include <vector>

here's reference for all STL containers

Edit: I see that you meant something else but I leave that here and add to it.

Do you really need consumption class? I mean it would be much more logical if you leave all data about line in Line class. That means I would either store services used each month in vector if needed, or I would immediately calculate price and remember just last month services. I would also make class called program that would store services used and let it handle number of services in use. It could either discard new service if there are 2 already, or rewrite the older one. You could also make then return price of program and multiply it by time used. Something like this:

#include <vector>

using namespace std;

class Service{
public:
    int getPrice(){return monthPrice;}
    void setPrice(const int &p) { monthPrice = p; }
private:
    int monthPrice;
};

class Program{
public:
    Program(){servicesUsed = 0; monthTime = 0;}
    Program(const int &t) : monthTime(t) {servicesUsed = 0;}
    void setTime(int t){monthTime = t;}
    void addService(Service &s);
    int price(); //calculate price of program
private:
    int monthTime; //time that program was used
    Service services[2]; //services used in program
    int servicesUsed;
};

void Program::addService(Service &s){
    if(servicesUsed < 2){ // Discarding solution
        services[servicesUsed] = s;
        servicesUsed++;
    }
}

int Program::price(){
    int pP = 0;
    for(int i = 0; i < servicesUsed; i++){
        pP += services[i].getPrice();
    }
    pP *= monthTime;
    return pP;
}

class Line{
public:
    Program *addMonth(const int &t); //will return handle for month
    int consuption(); //calculate full line consuption
private:
    vector<Program> monthList; //store data about services per month
};

Program *Line::addMonth(const int &t){
    monthList.push_back(Program(t));
    return &monthList.back();
}

int Line::consuption(){
    int p = 0;
    for(unsigned int i = 0; i < monthList.size(); i++){
        p += monthList[i].price();
    }
    return p;
}

int main(){
    //showcase
    Program *handle;
    Service s1,s2,s3;
    s1.setPrice(50);
    s2.setPrice(75);
    s3.setPrice(100); //probably read from file
    Line line;
    handle = line.addMonth(30); // monthTime probably also from file
    handle->addService(s1);
    handle->addService(s2);
    handle->addService(s3);
    handle = line.addMonth(60); 
    handle->addService(s3);
    handle->addService(s2);
    int p = line.consuption();
    return 0;
}

should be working fine altought you may need modify it more ;)


You are using a bad approach to solve your problem.

You say: what structure is the best to solve (your problem)

You first, must model that data want save and how must be organized the data, I'll suggest use a Entity Relationship Model that is commonly used in database design.

And WHEN you have the diagram return to the code editor to describe in C++ how to represent that model.

Then you'll able to choice which data structure are the best to you. I suggest look into STL ones, aren't beautiful, but are very efficient and you won't need reinvent the wheel :)

0

精彩评论

暂无评论...
验证码 换一张
取 消