D 的个人博客

但行好事莫问前程

  menu
417 文章
3446695 浏览
19 当前访客
ღゝ◡╹)ノ❤️

ProductManager

 

/* 
 * File:   Product.h
 * Author: 88250
 *
 * Created on May 2, 2008, 5:51 PM
 
*/

#ifndef _PRODUCT_H
#define    _PRODUCT_H

#include 
<string>

class Product {
public:

    Product(std::string productName) {
        name 
= productName;
    }

    std::string name;
};

#endif    
/* _PRODUCT_H */


/* 
 * File:   RecordRow.h
 * Author: 88250
 *
 * Created on May 2, 2008, 5:54 PM
 
*/


#ifndef _RECORDROW_H
#define    _RECORDROW_H

#include 
<string>

class RecordRow {
public:

    RecordRow(std::
string n, std::string dir, int am, float p, int m, int d) {
        name 
= n;
        direction 
= dir;
        amount 
= am;
        price 
= p;
        month 
= m;
        day 
= d;
    }

    std::
string name;
    std::
string direction;
    
int amount;
    
float price;
    
int month;
    
int day;
};


#endif    /* _RECORDROW_H */


/* 
 * File:   ProductManager.h
 * Author: 88250
 *
 * Created on May 2, 2008, 5:57 PM
 
*/


#ifndef _PRODUCTMANAGER_H
#define    _PRODUCTMANAGER_H

#include 
<vector>

#include 
"Product.h"
#include 
"RecordRow.h"

using namespace std;

class ProductManager {
public:
    
void init();
    
void saveProduct(std::vector<Product> products);
    
void readProduct(std::vector<Product> &products);
    
void saveRecord(std::vector<RecordRow> records);
    
void readRecord(std::vector<RecordRow> &records);
    
void newProduct(string name);
    
void newRecord(RecordRow rr);
    
void displayName(void);
    
void displayRecords(void);
    
void displayStat(int productNum, int startM, int endM, int startD, int endD);
    
void displayAllStat(int startM, int endM, int startD, int endD);
    vector
<Product> productVector;
    vector
<RecordRow> recordVector;
private:
    vector
<RecordRow> getRRByName(string name, vector<RecordRow> rrs);
    vector
<string> getNames(vector<RecordRow> rrs);
    
bool containsName(vector<string> names, string name);
    
void displayAProduct(vector<RecordRow> rr, float &mao, float &chun);
}
;

#endif    /* _PRODUCTMANAGER_H */


/*
 * File:   ProductManager.cpp
 * Author: 88250
 *
 * Created on May 2, 2008, 7:7 PM
 
*/

#include 
"ProductManager.h"

#include 
<vector>
#include 
<string>
#include 
<fstream>
#include 
<iostream>

using namespace std;

void ProductManager::displayAllStat(int startM, int endM, int starD, int endD) {
    vector
<RecordRow> rrs;
    
for (int i = 0; i < recordVector.size(); i++) {
        RecordRow rr 
= recordVector[i];
        
if (rr.month > startM && rr.month < endM) {
            rrs.push_back(rr);
            
continue;
        }
        
if (rr.month == startM || rr.month == endM) {
            
if (rr.day >= starD && rr.day <= endD) {
                rrs.push_back(rr);
            }
        }
    }

    vector
<string> names = getNames(rrs);
    vector
<vector<RecordRow> > rrss;
    
for (int i = 0; i < names.size(); i++) {
        rrss.push_back(getRRByName(names[i], rrs));
    }
    cout 
<< "品种 " << "进总数 " << "出总数 " << "进总价 "
            
<< "出总价 " << "阶段利润 " << "总金额流量" << endl;
    
float mao = 0.0// 毛利
    float chun = 0.0// 纯利

    
for (int i = 0; i < rrss.size(); i++) {
        displayAProduct(rrss[i], mao, chun);
    }

    cout 
<< "当前的总毛收入:" << mao << endl;
    cout 
<< "当前的总纯收入:" << chun << endl;
    cout 
<< endl;

}

void ProductManager::displayStat(int productNum,
        
int startM, int endM, int starD, int endD) {
    vector
<RecordRow> rrs;
    
for (int i = 0; i < recordVector.size(); i++) {
        RecordRow rr 
= recordVector[i];
        
if (rr.name == productVector[productNum].name) {
            
if (rr.month > startM && rr.month < endM) {
                rrs.push_back(rr);
                
continue;
            }
            
if (rr.month == startM || rr.month == endM) {
                
if (rr.day >= starD && rr.day <= endD) {
                    rrs.push_back(rr);
                }
            }
        }
    }

    cout 
<< "品种 " << "进总数 " << "出总数 " << "进总价 "
            
<< "出总价 " << "阶段利润 " << "总金额流量" << endl;

    
float mao = 0.0;
    
float chun = 0.0;
    displayAProduct(rrs, mao, chun);

    cout 
<< "当前的总毛收入:" << mao << endl;
    cout 
<< "当前的总纯收入:" << chun << endl;
    cout 
<< endl;
}

void ProductManager::displayAProduct(vector<RecordRow> rr,
        
float &mao, float &chun) {
    
int iam = 0;
    
int oam = 0;
    
float ip = 0.0;
    
float op = 0.0;
    
float pro = 0.0;
    
float ppro = 0.0;
    
float iavg = 0.0;
    
int inum = 0;
    
if (rr[0].name.length() >= 8){
       cout 
<< rr[0].name << " ";
    } 
else {
      cout 
<< rr[0].name << " ";
      }
   
    
for (int i = 0; i < rr.size(); i++) {
        
if (rr[i].direction == "") {
            iam 
+= rr[i].amount;
            iavg 
+= rr[i].price;
            ip 
+= rr[i].amount * rr[i].price;
            inum
++;
        } 
else {
            oam 
+= rr[i].amount;
            op 
+= rr[i].amount * rr[i].price;
        }
    }
    iavg 
= iavg / inum;
    pro 
= op - ip;
    ppro 
= op - oam * iavg;
    cout 
<< iam << " " << oam << " " << ip << " " << op << " "
            
<< ppro << " " << pro << " " << endl;
    
    chun 
+= ppro;
    mao 
+= op;
}

vector
<RecordRow> ProductManager::getRRByName(string name,
        vector
<RecordRow> rrs) {
    vector
<RecordRow> ret;
    
for (int i = 0; i < rrs.size(); i++) {
        
if (rrs[i].name == name) {
            ret.push_back(rrs[i]);
        }
    }
    
return ret;
}

vector
<string> ProductManager::getNames(vector<RecordRow> rrs) {
    vector
<string> ret;

    
for (int i = 0; i < rrs.size(); i++) {
        
if (!containsName(ret, rrs[i].name)) {
            ret.push_back(rrs[i].name);
        }
    }
    
return ret;
}

bool ProductManager::containsName(vector<string> names, string name) {
    
for (int i = 0; i < names.size(); i++) {
        
if (names[i] == name) {
            
return true;
        }
    }
    
return false;
}

void ProductManager::init() {
    std::vector
<Product> products;
    readProduct(products);
    productVector 
= products;

    std::vector
<RecordRow> rrs;
    readRecord(rrs);
    recordVector 
= rrs;
}

void ProductManager::newProduct(string name) {
    vector
<Product>::iterator i;
    
bool isExisted = false;
    
for (i = productVector.begin(); i != productVector.end(); i++) {
        
if ((*i).name == name) {
            isExisted 
= true;
        }
    }
    
if (!isExisted) {
        productVector.push_back(Product(name));
    }
    saveProduct(productVector);
}

void ProductManager::newRecord(RecordRow rr) {
    recordVector.push_back(rr);
    saveRecord(recordVector);
}

void ProductManager::displayName(void) {
    
for (int i = 0; i < productVector.size(); i++) {
        cout 
<< i << "" << productVector[i].name << endl;
    }
    cout 
<< endl;
}

void ProductManager::displayRecords(void) {
    
if (!recordVector.empty()) {
        cout 
<< "品种 " << "流向 " << "数量 " << "单价 " << "日期" << endl;
        
for (int i = 0; i < recordVector.size(); i++) {
            
if (recordVector[i].name.length() >= 8){
               cout 
<< recordVector[i].name << " " << recordVector[i].direction << " "
                    
<< recordVector[i].amount << " " << recordVector[i].price
                    
<< " " << recordVector[i].month << "-" << recordVector[i].day
                    
<< endl;
            } 
else {
              cout 
<< recordVector[i].name << " " << recordVector[i].direction << " "
                    
<< recordVector[i].amount << " " << recordVector[i].price
                    
<< " " << recordVector[i].month << "-" << recordVector[i].day
                    
<< endl;
            }
        }
        cout 
<< endl;
    }
}

void ProductManager::saveProduct(std::vector<Product> products) {
    ofstream fout(
"name");

    
if (!fout) {
        cout 
<< "Cannot open output file! ";
        
return;
    }

    
for (int i = 0; i < products.size(); i++) {
        fout 
<< products[i].name << endl;
    }

    fout.close();
}

void ProductManager::saveRecord(std::vector<RecordRow> records) {
    ofstream fout(
"data");

    
if (!fout) {
        cout 
<< "Cannot open output file! ";
        
return;
    }

    
for (int i = 0; i < records.size(); i++) {
        fout 
<< "Record " << endl
                
<< records[i].name << endl
                
<< records[i].direction << endl
                
<< records[i].amount << endl
                
<< records[i].price << endl
                
<< records[i].month << endl
                
<< records[i].day << endl;
    }

    fout.close();
}

void ProductManager::readProduct(std::vector<Product> &products) {
    ifstream fin(
"name");

    
if (!fin) {
        cout 
<< "Cannot open input file! ";
        
return;
    }

    
string aLine;
    
while (getline(fin, aLine)) {
        products.push_back(Product(aLine));
    }

    fin.close();
}

void ProductManager::readRecord(std::vector<RecordRow> &records) {
    ifstream fin(
"data");

    
if (!fin) {
        cout 
<< "Cannot open input file! ";
        
return;
    }

    
string aLine;
    
while (getline(fin, aLine)) {
        
string n, dir;
        
int am, m, d;
        
float pr;

        
if (aLine.at(0== 'R') {
            getline(fin, aLine);
            n 
= aLine;
            getline(fin, aLine);
            dir 
= aLine;
            getline(fin, aLine);
            am 
= atoi(aLine.c_str());
            getline(fin, aLine);
            pr 
= atof(aLine.c_str());
            getline(fin, aLine);
            m 
= atoi(aLine.c_str());
            getline(fin, aLine);
            d 
= atoi(aLine.c_str());
            records.push_back(RecordRow(n, dir, am, pr, m, d));
        }
    }

    fin.close();
}

/*
 * File:   Main.cpp
 * Author: 88250
 *
 * Created on May 2, 2008, 9:36 PM
 
*/

#include 
"ProductManager.h"

#include 
<iostream>

using namespace std;

int main(int argc, char** argv) {
    ProductManager pm;
    pm.init();

    
int input;
    
int num = 0;
    
string name;
    
int amount = 0;
    
float price = 0.0;
    
int month = 1;
    
int day = 1;
    
int dir = 1;
    
int sm = 1;
    
int sd = 1;
    
int em = 2;
    
int ed = 1;
    
int pn = 0;
    
string d;
    
char confirm = 'n';
    
while (true) {
        cout 
<< "1. 查看现有品种" << endl;
        cout 
<< "2. 增加新品种" << endl;
        cout 
<< "3. 添加销售记录" << endl;
        cout 
<< "4. 查看所有销售记录" << endl;
        cout 
<< "5. 统计帐目" << endl;

        cout 
<< "请选择:" << "  ";
        cin 
>> input;

        
switch (input) {
            
case 1:
                pm.displayName();
                
break;
            
case 2:
                cout 
<< "请输入品种名称(111返回):" << "  ";
                cin 
>> name;
                
if (name == "111") {
                    
break;
                }
                pm.newProduct(name);
                cout 
<< "新建品种成功!" << endl;
                
break;
            
case 3:
                pm.displayName();

                cout 
<< "请输入产品编号(111返回):  ";
                cin 
>> num;
                
if (num == 111) {
                    
break;
                }
                
if (num >= 0 && num <= pm.productVector.size()) {
                    name 
= pm.productVector[num].name;
                }
else {
                    cout 
<< "输入有误!";
                    
break;
                }
                cout 
<< "请选择流向(1. 进;2. 出):  ";
                cin 
>> dir;
                
if (dir == 1) {
                    d 
= "";
                    cout 
<< "请输入进货数量:  ";
                    cin 
>> amount;
                    cout 
<< "请输入进货单价:  ";
                    cin 
>> price;

                    cout 
<< "请输入进货月:  ";
                    cin 
>> month;

                    cout 
<< "请输入进货日:  ";
                    cin 
>> day;
                }
else if (dir == 2) {
                    d 
= "";
                    cout 
<< "请输入卖出数量:  ";
                    cin 
>> amount;

                    cout 
<< "请输入卖出单价:  ";
                    cin 
>> price;

                    cout 
<< "请输入销售月:  ";
                    cin 
>> month;

                    cout 
<< "请输入销售日:  ";
                    cin 
>> day;
                }
else {
                    cout 
<< "输入有误!";
                    
break;
                }

                cout 
<< "确定?(y. 确定 n.放弃)" << endl;
                cin 
>> confirm;
                
if (confirm == 'y') {
                    pm.newRecord(RecordRow(name, d, amount, price, month, day));
                }
                
break;
            
case 4:
                pm.displayRecords();
                
break;
            
case 5:
                cout 
<< "开始月(111返回):  ";
                cin 
>> sm;
                
if (sm == 111) {
                    
break;
                }

                cout 
<< "开始日:  ";
                cin 
>> sd;

                cout 
<< "结束月:  ";
                cin 
>> em;

                cout 
<< "结束日:  ";
                cin 
>> ed;

                cout 
<< "请选择统计方式(1. 单项品种 2. 所有品种):  ";
                cin 
>> num;
                
if (num == 1) {
                    pm.displayName();
                    cout 
<< "请输入产品编号:  ";
                    cin 
>> pn;
                    pm.displayStat(pn, sm, em, sd, ed);
                } 
else if (num == 2) {
                    pm.displayAllStat(sm, em, sd, ed);
                } 
else {
                    cout 
<< "选择有误,返回!" << endl;
                }
                
break;
            
default:
                cout 
<< "选择有误!" << endl;
        }


    }

    
return (EXIT_SUCCESS);
}