Advertisement
Guest User

Untitled

a guest
Mar 7th, 2016
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 40.75 KB | None | 0 0
  1. /*
  2.  * To change this license header, choose License Headers in Project Properties.
  3.  * To change this template file, choose Tools | Templates
  4.  * and open the template in the editor.
  5.  */
  6.  
  7. /*
  8.  * File:   main.cpp
  9.  * Author: BT
  10.  *
  11.  * Created on January 12, 2016, 7:57 PM
  12.  */
  13. #include <iostream>
  14. #include <fstream>
  15. #include <string>
  16. #include <sstream>
  17. #include <vector>
  18.  
  19. using namespace std;
  20.  
  21.  
  22.  
  23.  
  24. class Product{
  25. protected:
  26.     int prodId;
  27.     string prodModel;
  28.     string prodManuf;
  29.     string prodDesc;
  30.     int prodPrice;
  31.     string prodType;
  32.    
  33.    
  34. public:
  35.     Product(int id, string type, string model, string manuf, string desc, int price){
  36.        
  37.         this->prodId = id;
  38.         this->prodModel = model;
  39.         this->prodManuf = manuf;
  40.         this->prodDesc = desc;
  41.         this->prodPrice = price;
  42.         this->prodType = type;
  43.  
  44.        
  45.     }
  46.    
  47.     Product(){
  48.        
  49.     };
  50.    
  51.     int getProdId(){
  52.         return prodId;
  53.     }
  54.    
  55.     void setProdId(int newId){
  56.         this->prodId = newId;
  57.     }
  58.    
  59.     string getProdModel(){
  60.         return prodModel;
  61.     }
  62.    
  63.     void setProdModel(string newModel){
  64.         this->prodModel = newModel;
  65.     }
  66.    
  67.     string getProdManuf(){
  68.         return prodManuf;
  69.     }
  70.    
  71.     void setProdManuf(string newManuf){
  72.         this->prodManuf = newManuf;
  73.     }
  74.    
  75.     void setProdDesc(string newDesc){
  76.         this->prodDesc = newDesc;
  77.     }
  78.    
  79.     string getProdDesc(){
  80.         return prodDesc;
  81.     }
  82.      
  83.     int getProdPrice(){
  84.         return prodPrice;
  85.     }
  86.    
  87.     void setProdPrice(int prodPrice){
  88.         this->prodPrice = prodPrice;
  89.     }
  90.    
  91.      void setProdType(string prodType){
  92.         this->prodType = prodType;
  93.     }
  94.    
  95.     string getProdType(){
  96.         return prodType;
  97.     }
  98.    
  99. };
  100.  
  101. class Pc : public Product{
  102. private:
  103.     int cpuSpeed;
  104.     int ramCap;
  105.     string discType;
  106.     string graphCard;
  107.    
  108.  
  109. public:
  110.     Pc(int prodId, string prodType, string prodModel, string prodManuf, string prodDesc, int prodPrice, int cpu, int ram, string disc, string graph) : Product (prodId, prodType, prodModel, prodManuf, prodDesc, prodPrice){
  111.        
  112.         this->cpuSpeed = cpu;
  113.         this->ramCap = ram;
  114.         this->discType= disc;
  115.         this->graphCard = graph;
  116.     }
  117.    
  118.     Pc(string totalDesc){
  119.         Product();
  120.             istringstream  desc(totalDesc);
  121.             vector<string> token;
  122.             string word;
  123.            
  124.                 while(getline(desc, word, ':')) {
  125.                     token.push_back(word);
  126.                 }
  127.                 stringstream(token[0]) >> this->prodId;
  128.                 this->prodType = token[1];
  129.                 this->prodModel = token[2];
  130.                 this->prodManuf = token[3];
  131.                 this->prodDesc = token[4];
  132.                 stringstream(token[5]) >> this->prodPrice;
  133.                 stringstream(token[6]) >> this->cpuSpeed;
  134.                 stringstream(token[7]) >> this->ramCap;
  135.                 this->discType = token[8];
  136.                 this->graphCard = token[9];
  137.     }
  138.                
  139.      
  140.    
  141.     int getCpuSpeed(){
  142.         return cpuSpeed;
  143.     }
  144.    
  145.     void setCpuSpeed(int newSpeed){
  146.         this->cpuSpeed = newSpeed;
  147.     }
  148.    
  149.     int getRamCap(){
  150.         return ramCap;
  151.     }
  152.    
  153.     void setRamCap(int newCap){
  154.         this->ramCap = newCap;
  155.     }
  156.  
  157.     string getDiscType(){
  158.         return discType;
  159.     }
  160.    
  161.     void setDiscType(string newDisc){
  162.         this->discType = newDisc;
  163.     }
  164.    
  165.     string getGraphCard(){
  166.         return graphCard;
  167.     }
  168.    
  169.     void setGraphCard(string newCard){
  170.         this->graphCard = newCard;
  171.     }
  172.    
  173.     string toString() {
  174.         stringstream sstm;
  175.         sstm << prodId << ":" << prodType << ":" << prodModel << ":" << prodManuf << ":" << prodDesc << ":" << prodPrice << ":" << ramCap << ":" << cpuSpeed << ":" << discType << ":" << graphCard;
  176.         return sstm.str();
  177.     }
  178.    
  179.     void appendToFile() {
  180.         ofstream infile;
  181.     infile.open("products.dat", std::ios_base::app | std::ios_base::out);
  182.     infile << this->toString() << endl;
  183.         infile.close();
  184.     }
  185. };
  186.  
  187.  
  188. //void updatePcs(){
  189. //        ifstream infile;
  190. //        infile.open("products.dat");
  191. //        string tok;
  192. //        while(getline(infile, tok)){
  193. //        pcs.emplace_back(tok);
  194. //        }
  195. //}
  196.  
  197. ////
  198. class SmartPhone : public Product{
  199. private:
  200.     int screenInch;
  201.     int batCap;
  202.     int _4d;
  203.  
  204. public:
  205.     SmartPhone(int prodId, string prodType, string prodModel, string prodManuf, string prodDesc, int prodPrice, int cpu, int screenInch, int batCap, int _4d) : Product (prodId, prodType, prodModel, prodManuf, prodDesc, prodPrice){
  206.         this->screenInch = screenInch;
  207.         this->batCap = batCap;
  208.         this->_4d= _4d;
  209.     }
  210.    
  211.     SmartPhone(string totalDesc){
  212.         Product();
  213.             istringstream  desc(totalDesc);
  214.             vector<string> token;
  215.             string word;
  216.            
  217.                 while(getline(desc, word, ':')) {
  218.                     token.push_back(word);
  219.                 }
  220.            
  221.                 stringstream(token[0]) >> this->prodId;
  222.                 this->prodType = token[1];
  223.                 this->prodModel = token[2];
  224.                 this->prodManuf = token[3];
  225.                 this->prodDesc = token[4];
  226.                 stringstream(token[5]) >> this->prodPrice;
  227.                 stringstream(token[6]) >> this->screenInch;
  228.                 stringstream(token[7]) >> this->batCap;
  229.                 stringstream(token[8]) >> this->_4d;
  230.     }
  231.                
  232.  
  233.    
  234.     int getScreenInch(){
  235.         return screenInch;
  236.     }
  237.    
  238.     void setScreenInch(int screenInch){
  239.         this->screenInch = screenInch;
  240.     }
  241.    
  242.     int getBatCap(){
  243.         return batCap;
  244.     }
  245.    
  246.     void setBatCap(int batCap){
  247.         this->batCap = batCap;
  248.     }
  249.  
  250.     int get_4d(){
  251.         return _4d;
  252.     }
  253.    
  254.     set_4d(int _4d){
  255.         this->_4d = _4d;
  256.     }
  257.    
  258.    
  259.     string toString() {
  260.         stringstream sstm;
  261.         sstm << prodId << ":" << prodType << ":" << prodModel << ":" << prodManuf << ":" << prodDesc << ":" << prodPrice << ":" << screenInch << ":" << batCap << ":" << _4d ;
  262.         return sstm.str();
  263.     }
  264.    
  265.     void appendToFile() {
  266.         ofstream infile;
  267.     infile.open("products.dat", std::ios_base::app | std::ios_base::out);
  268.     infile << this->toString() << endl;
  269.         infile.close();
  270.     }
  271. };
  272.  
  273.  
  274. class Tv : public Product{
  275. private:
  276.     int screenInch;
  277.     int _3d;
  278.  
  279. public:
  280.     Tv(int prodId, string prodType, string prodModel, string prodManuf, string prodDesc, int prodPrice, int screenInch, int _3d) : Product (prodId, prodType, prodModel, prodManuf, prodDesc, prodPrice){
  281.         this->screenInch = screenInch;
  282.         this->_3d= _3d;
  283.     }
  284.    
  285.     Tv(string totalDesc){
  286.         Product();
  287.             istringstream  desc(totalDesc);
  288.             vector<string> token;
  289.             string word;
  290.            
  291.                 while(getline(desc, word, ':')) {
  292.                     token.push_back(word);
  293.                 }
  294.            
  295.                 stringstream(token[0]) >> this->prodId;
  296.                 this->prodType = token[1];
  297.                 this->prodModel = token[2];
  298.                 this->prodManuf = token[3];
  299.                 this->prodDesc = token[4];
  300.                 stringstream(token[5]) >> this->prodPrice;
  301.                 stringstream(token[6]) >> this->screenInch;
  302.                 stringstream(token[7]) >> this->_3d;
  303.     }
  304.                
  305.  
  306.    
  307.     int getScreenInch(){
  308.         return screenInch;
  309.     }
  310.    
  311.     void setScreenInch(int screenInch){
  312.         this->screenInch = screenInch;
  313.     }
  314.  
  315.     int get_3d(){
  316.         return _3d;
  317.     }
  318.    
  319.     void set_3d(int _3d){
  320.         this->_3d = _3d;
  321.     }
  322.    
  323.    
  324.     string toString() {
  325.         stringstream sstm;
  326.         sstm << prodId << ":" << prodType << ":" << prodModel << ":" << prodManuf << ":" << prodDesc << ":" << prodPrice << ":" << screenInch << ":" << _3d ;
  327.         return sstm.str();
  328.     }
  329.    
  330.     void appendToFile() {
  331.         ofstream infile;
  332.     infile.open("products.dat", std::ios_base::app | std::ios_base::out);
  333.     infile << this->toString() << endl;
  334.         infile.close();
  335.     }
  336. };
  337.  
  338.  
  339. class User{
  340.    
  341. protected:
  342.     string username;
  343.     string password;
  344.     string accType;
  345.  
  346. public:
  347.     User(){
  348.     }
  349.    
  350.     User(string username, string password, string acc){
  351.         this->username = username;
  352.         this->password = password;
  353.         this->accType = acc;
  354.     }
  355.    
  356.     User(string desc){
  357.         Product();
  358.             vector<string> token;
  359.             string word;
  360.             istringstream descr(desc);
  361.                 while(getline(descr, word, ':')) {
  362.                     token.push_back(word);
  363.                 }
  364.            
  365.                
  366.                 this->username = token[0];
  367.                 this->password = token[1];
  368.                 this->accType = "ADMIN";
  369.     }
  370.        
  371.    
  372.    
  373.     void setPassword(string password){
  374.         this->password = password;
  375.     }
  376.    
  377.     void setUsername(string username){
  378.         this->username = username;
  379.     }
  380.    
  381.     void setAccType(string accType){
  382.         this->accType = accType;
  383.     }
  384.    
  385.     string getPassword(){
  386.         return password;
  387.     }
  388.    
  389.     string getUsername(){
  390.         return username;
  391.     }
  392.    
  393.     string getAccType(){
  394.         return accType;
  395.     }
  396.    
  397.     void appendToFile() {
  398.         ofstream infile;
  399.     infile.open("users.dat", std::ios_base::app | std::ios_base::out);
  400.     infile << this->toString() << endl;
  401.         infile.close();
  402.     }
  403.    
  404.     string toString() {
  405.         stringstream sstm;
  406.         sstm << username << ":" << password << ":" << accType;
  407.         return sstm.str();
  408.     }
  409.    
  410. };
  411.  
  412.  
  413. class Customer: public User{
  414. protected:
  415.     int afm;
  416.     int phone;
  417.     string adress;
  418. public:
  419.     Customer(string username, string password, string type, int afm, int phone, string adress) : User(username, password, type){
  420.         this->afm = afm;
  421.         this->phone = phone;
  422.         this->adress = adress;
  423.     }
  424.    
  425.     Customer(){
  426.        
  427.     }
  428.    
  429.     void setAfm(int afm){
  430.         this->afm = afm;
  431.     }
  432.    
  433.     void setPhone(int phone){
  434.         this->phone = phone;
  435.     }
  436.    
  437.     void setAdress(string adress){
  438.         this->adress = adress;
  439.     }
  440.    
  441.     int getAfm(){
  442.         return afm;
  443.     }
  444.    
  445.     int getPhone(){
  446.         return phone;
  447.     }
  448.    
  449.     string getAdress(){
  450.         return adress;
  451.     }
  452.    
  453. };
  454.  
  455. class Person : public Customer{
  456. protected:
  457.     string name;
  458.     string surname;
  459.     int idNum;
  460. public:
  461.     Person(string username, string password, string type, int afm, int phone, string adress, string name, string surname, int idNum) : Customer(username, password, type,  afm,  phone, adress){
  462.         this->accType = "CUSTOMER";
  463.         this->name = name;
  464.         this->surname = surname;
  465.         this->idNum = idNum;
  466.     }
  467.    
  468.     Person(string totalDesc){
  469.         Customer();
  470.             istringstream  desc(totalDesc);
  471.             vector<string> token;
  472.             string word;
  473.            
  474.                 while(getline(desc, word, ':')) {
  475.                     token.push_back(word);
  476.                 }
  477.            
  478.                 this->username = token[0];
  479.                 this->password = token[1];
  480.                 this->accType = token[2];
  481.                 stringstream(token[3]) >> this->afm;
  482.                 stringstream(token[4]) >> this->phone;
  483.                 stringstream(token[5]) >> this->adress;
  484.                 this->name = token[6];
  485.                 this->surname = token[7];
  486.                 stringstream(token[8]) >> this->idNum;
  487.                
  488.     }
  489.    
  490.     void setName(string name){
  491.         this->name = name;
  492.     }
  493.    
  494.     void setSurname(string surname){
  495.         this->surname = surname;
  496.     }
  497.    
  498.     void setIdNum(int idNum){
  499.         this->idNum = idNum;
  500.     }
  501.    
  502.     string getName(){
  503.         return name;
  504.     }
  505.    
  506.     string getSurname(){
  507.         return surname;
  508.     }
  509.    
  510.     int getIdNum(){
  511.         return idNum;
  512.     }
  513.    
  514.      string toString() {
  515.         stringstream sstm;
  516.         sstm << username << ":" << password << ":" << accType << ":" << afm << ":" << phone << ":" << adress << ":" << name << ":" << surname << ":" << idNum;
  517.         return sstm.str();
  518.     }
  519.      
  520.      void appendToFile() {
  521.         ofstream infile;
  522.     infile.open("users.dat", std::ios_base::app | std::ios_base::out);
  523.     infile << this->toString() << endl;
  524.         infile.close();
  525.     }
  526.    
  527. };
  528.    
  529.    
  530. class Company : public Customer{
  531. private:
  532.     string name;
  533.     string supName;
  534.     string supSurname;
  535.     int disc;
  536.     int fax;
  537.    
  538. public:
  539.     Company(string username, string password, string type, int afm, int phone, string adress, string name, string supName, string supSuername, int disc, int fax) : Customer(username, password, type, afm, phone, adress){
  540.         this->accType = "CUSTOMER";
  541.         this->name = name;
  542.         this->supName = supName;
  543.         this->supSurname = supSurname;
  544.         this->disc = disc;
  545.         this->fax = fax;
  546.     }
  547.    
  548.       Company(string totalDesc){
  549.         Customer();
  550.             istringstream  desc(totalDesc);
  551.             vector<string> token;
  552.             string word;
  553.            
  554.                 while(getline(desc, word, ':')) {
  555.                     token.push_back(word);
  556.                 }
  557.            
  558.                 this->username = token[0];
  559.                 this->password = token[1];
  560.                 this->accType = token[2];
  561.                 stringstream(token[3]) >> this->afm;
  562.                 stringstream(token[4]) >> this->phone;
  563.                 stringstream(token[5]) >> this->adress;
  564.                 this->name = token[6];
  565.                 this->supName = token[7];
  566.                 this->supSurname = token[8];
  567.                 stringstream(token[9]) >> this->disc;
  568.                 stringstream(token[10]) >> this->fax;
  569.                
  570.     }
  571.    
  572.    
  573.     void setName(string name){
  574.         this->name = name;
  575.     }
  576.    
  577.     void setSupName(string supName){
  578.         this->supName; supName;
  579.     }
  580.  
  581.    
  582.     void setSupSurname(string supSurname){
  583.         this->supSurname = supSurname;
  584.     }
  585.    
  586.     void setDisc(int disc){
  587.         this->disc = disc;
  588.     }
  589.    
  590.     void setFax(int fax){
  591.         this->fax = fax;
  592.     }
  593.    
  594.     string getName(){
  595.         return name;
  596.     }
  597.    
  598.     string getSupName(){
  599.         return supName;
  600.     }
  601.    
  602.     string getSupSurname(){
  603.         return supSurname;
  604.     }
  605.    
  606.     int getFax(){
  607.         return fax;
  608.     }
  609.    
  610.     int getDisc(){
  611.         return disc;
  612.     }
  613.    
  614.      string toString() {
  615.         stringstream sstm;
  616.         sstm << username << ":" << password << ":" << accType << ":" << afm << ":" << phone << ":" << adress << ":" << name << ":" << supName << ":" << supSurname << ":" << disc << ":" <<fax;
  617.         return sstm.str();
  618.     }
  619.      
  620.      void appendToFile() {
  621.         ofstream infile;
  622.     infile.open("users.dat", std::ios_base::app | std::ios_base::out);
  623.     infile << this->toString() << endl;
  624.         infile.close();
  625.     }
  626.      
  627. };
  628.  
  629.  
  630.  
  631.  
  632. class CartItem{
  633. private:
  634.     string ownerUsername;
  635.     string cartProduct;
  636.     string productAmount;
  637. public:
  638.    CartItem(string name, string cartProduct, string productAmount){
  639.        this->ownerUsername = name;
  640.        this->cartProduct = cartProduct;
  641.        this->productAmount = productAmount;
  642.    }
  643.    
  644.    CartItem(string desc){
  645.        vector<string> token;
  646.             string word;
  647.             istringstream descr(desc), sstm;
  648.                 while(getline(descr, word, '-')) {
  649.                 token.push_back(word);
  650.         }
  651.             this->ownerUsername = token[0];
  652.             this->cartProduct = token[1];
  653.             this->productAmount = token[2];
  654.    }
  655.    
  656.    
  657.    void setOwnerUsername(string username){
  658.        this->ownerUsername = username;
  659.    }
  660.    
  661.    void setCartProduct(string product){
  662.        this->cartProduct = product;
  663.    }
  664.    
  665.    void setProductAmount(string amount){
  666.        this->productAmount = amount;
  667.    }
  668.    
  669.    string getCartProduct(){
  670.        return cartProduct;
  671.    }
  672.  
  673.    string getProductAmount(){
  674.        return productAmount;
  675.    }
  676.    string getOwnerUsername(){
  677.        return ownerUsername;
  678.    }
  679.    
  680.    
  681.    string toString(){
  682.        stringstream sstm;
  683.            sstm << ownerUsername << "-" << cartProduct << "-" << productAmount;
  684.        return sstm.str();
  685.    }
  686.    
  687.    void appendToFile() {
  688.         ofstream infile;
  689.     infile.open("carts.dat", std::ios_base::app | std::ios_base::out);
  690.     infile  << this->toString() << endl ;
  691.         infile.close();
  692.     }
  693. };
  694.  
  695.  
  696. class Order{
  697. private:
  698.     string ownerUsername;
  699.     string cartProduct;
  700.     string productAmount;
  701.     string orderComplete;
  702.    
  703. public:
  704.    Order(string name, string cartProduct, string productAmount, string complete){
  705.        this->ownerUsername = name;
  706.        this->cartProduct = cartProduct;
  707.        this->productAmount = productAmount;
  708.        this->orderComplete = complete;
  709.    }
  710.    
  711.    Order(CartItem item){
  712.        this->ownerUsername = item.getOwnerUsername();
  713.        this->productAmount = item.getProductAmount();
  714.        this->cartProduct = item.getCartProduct();
  715.        this->orderComplete = "PENDING";
  716.    }
  717.    
  718.    
  719.    Order(string desc){
  720.        vector<string> token;
  721.             string word;
  722.             istringstream descr(desc), sstm;
  723.                 while(getline(descr, word, '-')) {
  724.                 token.push_back(word);
  725.         }
  726.             this->ownerUsername = token[0];
  727.             this->cartProduct = token[1];
  728.             this->productAmount = token[2];
  729.             this->orderComplete = token[3];
  730.    }
  731.    
  732.    
  733.    void setOwnerUsername(string username){
  734.        this->ownerUsername = username;
  735.    }
  736.    
  737.    void setCartProduct(string product){
  738.        this->cartProduct = product;
  739.    }
  740.    
  741.    void setProductAmount(string amount){
  742.        this->productAmount = amount;
  743.    }
  744.    
  745.    void setOrderComplete(string complete){
  746.        this->orderComplete = complete;
  747.    }
  748.    
  749.    string getCartProduct(){
  750.        return cartProduct;
  751.    }
  752.  
  753.    string getProductAmount(){
  754.        return productAmount;
  755.    }
  756.    string getOwnerUsername(){
  757.        return ownerUsername;
  758.    }
  759.    
  760.    string getOrderComplete(){
  761.        return orderComplete;
  762.    }
  763.    
  764.    
  765.    string toString(){
  766.        stringstream sstm;
  767.            sstm << ownerUsername << "-" << cartProduct << "-" << productAmount << "-" << orderComplete;
  768.        return sstm.str();
  769.    }
  770.    
  771.    void appendToFile() {
  772.         ofstream infile;
  773.     infile.open("orders.dat", std::ios_base::app | std::ios_base::out);
  774.     infile  << this->toString() << endl ;
  775.         infile.close();
  776.     }
  777. };
  778.  
  779.  
  780. vector<Pc> pcs;
  781. vector<SmartPhone> smartPhones;
  782. vector<Tv> tvs;
  783. vector<Person> persons;
  784. vector<Company> companies;
  785. vector<User> admins;
  786. vector<CartItem> cartItems;
  787. vector<Order> orders;
  788.  
  789.  
  790.  
  791.  
  792. string getType(string desc,int choice){
  793.     string word;
  794.     vector<string> resVec;
  795.     istringstream descr(desc);
  796.         while (getline(descr, word, ':')) {
  797.             resVec.push_back(word);
  798.             }
  799.     word = resVec[choice];  
  800.     return word;
  801. }
  802.  
  803. vector<string> splitLine(string desc, char ch){
  804.     stringstream locos(desc);
  805.     string word;
  806.     vector<string> words;
  807.     while(getline(locos,word, ch)){
  808.         words.push_back(word);
  809.     }
  810.     return words;
  811. }
  812.  
  813. void storeData(){
  814.     remove("carts.dat");
  815.     for(CartItem c : cartItems){
  816.         c.appendToFile();
  817.     }
  818.  
  819.     remove("products.dat");
  820.     for(Pc p : pcs){
  821.       p.appendToFile();      
  822.     }
  823.     for(SmartPhone t : smartPhones){
  824.         t.appendToFile();
  825.     }
  826.     for(Tv t : tvs){
  827.         t.appendToFile();
  828.     }
  829.    
  830.     remove("users.dat");
  831.     for(Company c : companies){
  832.         c.appendToFile();
  833.     }
  834.     for(Person p : persons){
  835.         p.appendToFile();
  836.     }
  837.     for(User a : admins){
  838.         a.appendToFile();
  839.     }
  840.    
  841.     remove("orders.dat");
  842.     for(Order o : orders){
  843.         o.appendToFile();
  844.     }
  845.    
  846.    
  847. }
  848.  
  849.  
  850. void updateData(){
  851.     ifstream file;              //
  852.     file.open("products.dat");
  853.     string line;
  854.     string type;
  855.     while(getline(file, line)){
  856.         type = getType(line,1);
  857.         if(type == "PC"){
  858.             pcs.push_back(Pc(line));
  859.         }
  860.         if(type == "SMARTPHONE"){
  861.             smartPhones.push_back(SmartPhone(line));
  862.         }
  863.         if(type == "TV"){
  864.             tvs.push_back(Tv(line));
  865.         }
  866.     }
  867.    
  868.     file.close();
  869.     ifstream file2;
  870.     file2.open("users.dat");
  871.     while(getline(file2, line)){
  872.         type=getType(line,2);
  873.         if(type=="PERSON"){
  874.             persons.push_back(Person(line));
  875.         }
  876.         if(type=="COMPANY"){
  877.             companies.push_back(Company(line));
  878.         }
  879.         if(type=="ADMIN"){
  880.             admins.push_back(User(line));
  881.         }
  882.     }
  883.    
  884.     file2.close();
  885.     ifstream file3;
  886.     file3.open("carts.dat");
  887.     while(getline(file3, line)){
  888.         vector<string> items;
  889.         items = splitLine(line,'-');
  890.         cartItems.push_back(CartItem(items[0],items[1],items[2]));
  891.     }
  892.    
  893.     file3.close();
  894.     ifstream file4;
  895.     file4.open("orders.dat");
  896.     while(getline(file4, line)){
  897.         vector<string> items;
  898.         items = splitLine(line,'-');
  899.         orders.push_back(Order(items[0],items[1],items[2],items[3]));
  900.     }
  901.     file4.close();
  902.     cout << endl << "Data updated." << endl<< "Total objects loaded into vectors:" << endl << "Pcs: " << pcs.size() << endl << "Tvs: " << tvs.size() << endl << "SmartPhones: " << smartPhones.size() << endl << "Persons: " << persons.size() << endl << "Companies: " << companies.size() << endl << "Admins: " << admins.size() << endl << "Cart items: " << cartItems.size() << endl << "Orders: " << orders.size() << endl;
  903. }
  904.  
  905.  
  906.  
  907.  
  908. string userLogin(){
  909.     string username,password;
  910.     while(1){
  911.     cout << endl << "Enter Username" << endl;
  912.     cin >> username;
  913.     cout << "Enter password" << endl;
  914.     cin >> password;
  915.    
  916.     for(Person p : persons){
  917.         if(p.getUsername() == username && p.getPassword() == password){
  918.             cout << "Log in sucessful PERSON." << endl;
  919.             return p.toString();
  920.         }
  921.     for(Company c : companies){
  922.         if(c.getUsername() == username && c.getPassword() == password){
  923.             cout << "Log in sucessful COMPANY." << endl;
  924.             return c.toString();  
  925.         }
  926.     }      
  927.     for(User a : admins){
  928.         if(a.getUsername() == username && a.getPassword() == password){
  929.             cout << "Log in sucessful ADMIN." << endl;
  930.             return a.toString();  
  931.         }
  932.         }    
  933.      }
  934.     cout << endl << "Username and/or password does not correspond to any existing user." << endl << "If you have trouble connecting please contact the administrator." << endl;
  935.   }
  936. }
  937.  
  938. void printProducts(int sort){
  939.     if(sort == 1){
  940.         cout << "----------Catergory Printing----------" << endl;
  941.     cout << "---Available PCs---" << endl << endl;
  942.     for(Pc p : pcs){
  943.         cout << "Product Id: " << p.getProdId() << endl << "Price: " << p.getProdPrice() << endl << "Product Model: " << p.getProdModel() << endl << "Product Manufacturer" << p.getProdManuf() << endl <<  "Product description: " << p.getProdDesc() << endl << "Cpu speed " << p.getCpuSpeed() << "Ghz" << endl << "Ram capacity: " << p.getRamCap() << endl << "Disc Type: " << p.getDiscType()<< endl << "Graphic Card: " << p.getGraphCard() << endl << endl ;
  944.     }
  945.     cout << "---Available SmartPhones---" << endl << endl;
  946.     for(SmartPhone p : smartPhones){
  947.         cout << "Product Id: " << p.getProdId() << endl << "Price: " << p.getProdPrice() << endl << "Product Model: " << p.getProdModel() << endl << "Product Manufacturer" << p.getProdManuf() << endl <<  "Product description: " << p.getProdDesc() << endl << "Screen Inches: " << p.getScreenInch() << endl << "Battery Mha" << p.getBatCap() << endl << "4d capable" << p.get_4d() << endl << endl;
  948.     }
  949.     cout << "---Available Tvs---" << endl << endl;
  950.     for(Tv p : tvs){
  951.         cout << "Product Id: " << p.getProdId() << endl << "Price: " << p.getProdPrice() << endl << "Product Model: " << p.getProdModel() << endl << "Product Manufacturer" << p.getProdManuf() << endl <<  "Product description: " << p.getProdDesc() << endl << "Screen Inches: " << p.getScreenInch() << endl << "3d capable" << p.get_3d() << endl << endl;
  952.         }
  953.     }
  954. }
  955.  
  956. string prodString(string sid){
  957.    
  958.     int id;
  959.     stringstream str(sid);
  960.     str >> id;
  961.    
  962.     for(Pc p : pcs){
  963.         if(p.getProdId() == id){
  964.             return p.toString();
  965.         }
  966.     }
  967.     for(SmartPhone p : smartPhones){
  968.         if(p.getProdId() == id){
  969.             return p.toString();
  970.         }
  971.     }
  972.     for(Tv p : tvs){
  973.         if(p.getProdId() == id){
  974.             return p.toString();
  975.         }
  976.     }
  977.     return "E";
  978. }
  979.  
  980.  
  981.  
  982.  
  983.  
  984. void customerUi(Customer currUser){
  985.     while(1){
  986.     string id, amount, prodS, cho;
  987.         cout << endl << "---Menu Options---" << endl << "Product browse (B)" << endl << "View cart (C)"<< endl << "Delete Cart product from cart(D)"<< endl << "Make order(O)" << endl << "View products ordered (V)" << endl << "Logout (L)" << endl << endl;
  988.         cin >> cho;
  989.         if(cho == "B" | cho == "b"){    // Sorting options - VIEW PRODS
  990.             while(1){
  991.             printProducts(1);      
  992.             cout << "If you would like to add a product to your cart, specify ID." << endl << "In order to go back menu, insert a dot" << endl;
  993.             cin >> id;
  994.             if(id!="."){
  995.                 prodS = prodString(id);
  996.                 if(prodS != "E"){
  997.                 cout << "Product selected: " << prodS << endl <<  "Please specify amount" << endl;
  998.                 cin >> amount;
  999.                 CartItem tor(currUser.getUsername(), prodS, amount);
  1000.                 tor.appendToFile();
  1001.                 updateData();
  1002.                 cout << endl << "Product successfully added to cart, make a menu choice." << endl;
  1003.                 break;
  1004.                 }else{
  1005.                     cout << endl << "Id provided does not correspond to existing product, make a menu choice.";
  1006.                     break;
  1007.                 }
  1008.             }else{
  1009.                 cout << endl << "Exiting to main menu, make a menu choice" << endl;
  1010.                 break;
  1011.             }
  1012.         }
  1013.            
  1014.            
  1015.         }else if(cho == "C" | cho == "c"){  //CART CHOICE
  1016.             vector<int> prodMatch;
  1017.         int i = 0;
  1018.         for(CartItem c : cartItems){
  1019.             if(c.getOwnerUsername() == currUser.getUsername()){
  1020.                 cout << "-- Uniquea cart ID: " << i <<endl << " Info: " << c.getCartProduct() << endl << " Amount: " << c.getProductAmount()<< endl;
  1021.                 prodMatch.push_back(i);
  1022.             }
  1023.             i++;
  1024.         }
  1025.         }else if(cho == "D" | cho == "d"){  // DELETE
  1026.             while(1){
  1027.             cout << endl <<  "Your cart cosists of the following products" << endl;
  1028.            cout << endl <<  "If you would like to delete a cart Item, please specify its unique cart ID" << endl << "Else hit . to go to main menu" << endl;
  1029.            
  1030.            int i = 0;
  1031.            vector<int> prodMatch;
  1032.            for(CartItem c : cartItems){
  1033.                if(c.getOwnerUsername() == currUser.getUsername()){
  1034.                    cout << "-- Uniquea cart ID: " << i <<endl << " Info: " << c.getCartProduct() << endl << " Amount: " << c.getProductAmount()<< endl;
  1035.                    prodMatch.push_back(i);
  1036.                }
  1037.                i++;
  1038.            }
  1039.            string cChoice;
  1040.            cin >> cChoice;
  1041.            if(cChoice != "."){
  1042.                stringstream tor(cChoice);
  1043.                int unId;
  1044.                tor >> unId;
  1045.                cartItems.erase(cartItems.begin() + unId);
  1046.                storeData();
  1047.                break;
  1048.                }
  1049.             }
  1050.         }else if(cho == "O" | cho == "o"){
  1051.             int i = 0;
  1052.             string cChoice;
  1053.             for(CartItem o : cartItems){
  1054.                 if(o.getOwnerUsername() == currUser.getUsername()){
  1055.                     cout << "--Unique order Id: " << i << endl << "Product info: " << o.getCartProduct() << endl << "Amount of order: " << o.getProductAmount() << endl;
  1056.                 }
  1057.                 i++;
  1058.             }
  1059.             cout << endl << "If you would like to order a cart product please specify its unique order Id" << endl;
  1060.             int orId;
  1061.             cin >> cChoice;
  1062.             stringstream tor(cChoice);
  1063.             tor >> orId;
  1064.             orders.push_back(cartItems[orId]);
  1065.             cartItems.erase(cartItems.begin() + orId);
  1066.             storeData();
  1067.             cout << "Order complete" << endl;
  1068.         }else if(cho == "v" || cho == "V"){
  1069.             for(Order o : orders){
  1070.                 if(currUser.getUsername() == o.getOwnerUsername()){
  1071.                     cout << "Product info: " << o.getCartProduct() << endl << "Product amount: " << o.getProductAmount() << endl << "Status: " << o.getOrderComplete() << endl;
  1072.                 }
  1073.             }
  1074.         }else if (cho == "L" | cho == "l"){
  1075.             break;
  1076.         }
  1077.     }
  1078. }
  1079.  
  1080. void adminUi(){
  1081.     while(1){
  1082.         cout << endl << "---Menu options---" << endl << "Create new user (U)" << endl << "Print products (P)" << endl << "Add products to store (A)" << endl << "Delete products from store (D)" << endl << "Modify product (MP)" << endl << "Update order status (UO)" << endl << "Print clients (PC)" << endl << "Print all orders (PO)" << endl << "Calculate total income (CI)" << endl << "Log out (L)" << endl;
  1083.         string cho;
  1084.         cin >> cho;
  1085.         if(cho == "U" | cho == "u"){
  1086.             while(1){
  1087.             string choi, username, password, fax, phone, afm, adress, supName, supSurname, idNum, name, discAmount, surName, compName, type = "ADMIN";
  1088.             cout << endl << "Create new administrator (A)" << endl << "Create new Person (P)" << endl << "Create a new company (C)" << endl;
  1089.             cin >> choi;
  1090.            
  1091.             cout << "Enter username: " << endl;
  1092.             cin >> username;
  1093.             cout << "Enter password: " << endl;
  1094.             cin >> password;
  1095.             if(choi == "A" | choi == "a"){
  1096.                 admins.push_back(User(username, password, type));
  1097.                 storeData();
  1098.                 break;
  1099.             }
  1100.            
  1101.             cout << "Enter phone number: " << endl;
  1102.             cin >> phone;
  1103.             cout << "Enter adress: " << endl;
  1104.             cin >> adress;
  1105.             cout << "Enter TAX ID: " << endl;
  1106.             cin >> afm;
  1107.            
  1108.             if(choi == "P" | choi == "p"){
  1109.                 cout << "Enter person's name: " << endl;
  1110.                 cin >> name;
  1111.                 cout << "Enter surname: " << endl;
  1112.                 cin >> surName;
  1113.                 cout << "Enter id number: " << endl;
  1114.                 cin >> idNum;
  1115.                 type = "PERSON";
  1116.                 stringstream make;
  1117.                 make << username << ":" << password << ":PERSON:" << afm << ":" << phone << ":" << adress << ":" << name << ":" << surName << ":" << idNum;
  1118.                 string tor = make.str();
  1119.                 cout << tor << endl << endl;
  1120.                 persons.push_back(Person(tor));
  1121.                 storeData();
  1122.                 break;
  1123.             }
  1124.            
  1125.             if(choi == "C" | choi == "c"){
  1126.                 cout << "Enter company name: " << endl;
  1127.                 cin >> compName;
  1128.                 cout << "Enters company's supervisor name: " << endl;
  1129.                 cin >> supName;
  1130.                 cout << "Enter company's supervisor surname: " << endl;
  1131.                 cin >> supSurname;
  1132.                 cout << "Enter Fax number" << endl;
  1133.                 cin >> fax;
  1134.                 cout << "Enter amount of discount (%)" << endl;
  1135.                 cin >> discAmount;
  1136.                 stringstream make;
  1137.                 make << username << ":" << password << ":COMPANY:" << afm << ":" << phone << ":" << adress << ":" << compName << ":" << supName << ":" << supSurname << ":" << discAmount << ":" << fax;
  1138.                 string tor = make.str();
  1139.                 companies.push_back(Company(tor));
  1140.                 storeData();
  1141.                 break;
  1142.             }
  1143.            
  1144.            
  1145.            
  1146.            
  1147.            
  1148.         }
  1149.     }else if(cho == "P" | cho == "p"){
  1150.         printProducts(1);
  1151.        
  1152.     }else if (cho == "A" | cho == "a"){
  1153.         while(1){
  1154.         string choi, id, price, manuf, desc, disc, card, model, screen, cpu, ram, bat, _4d, _3d;
  1155.         cout << "Add PC: (P)" << endl << "Add Smartphone (S)" << endl << "Add Tv (T)" << endl;
  1156.         cin >> choi;
  1157.        
  1158.         cout << "Enter product Id: " << endl;
  1159.         cin >> id;
  1160.         cout << "Enter product model: " << endl;
  1161.         cin >> model;
  1162.         cout << "Enter product manufacturer: " << endl;
  1163.         cin >> manuf;
  1164.         cout << "Enter product description: " << endl;
  1165.         cin >> desc;
  1166.         cout << "Enter product price: " << endl;
  1167.         cin >> price;
  1168.        
  1169.         if(choi == "s" | choi == "S"){
  1170.             cout << "Enter screen inches: " << endl;
  1171.             cin >> screen;
  1172.             cout << "Enter battery capability: " << endl;
  1173.             cin >> bat;
  1174.             cout << "Enter 4d capability: (0-1 only) : " << endl;
  1175.             cin >> _4d;
  1176.             stringstream make;
  1177.             make << id << ":SMARTPHONE:" << model << ":" << manuf << ":" << desc << ":" << price << ":" << screen << ":" << bat << ":" << _4d;
  1178.             string tor = make.str();
  1179.             smartPhones.push_back(SmartPhone(tor));
  1180.             storeData();
  1181.             cout << endl << "Smartphone added" << endl;
  1182.             break;
  1183.         }
  1184.         if(choi == "p" | choi == "P"){
  1185.             cout << "Enter cpu gHz: " << endl;
  1186.             cin >> cpu;
  1187.             cout << "Enter ram capability: " << endl;
  1188.             cin >> ram;
  1189.             cout << "Enter graphics card name: " << endl;
  1190.             cin >> card;
  1191.             cout << "Enter disc type: " << endl;
  1192.             cin >> disc;
  1193.             stringstream make;
  1194.             make << id << ":PC:" << model << ":" << manuf << ":" << desc << ":" << price << ":" << cpu << ":" << ram << ":" << disc << ":" << card;
  1195.             string tor = make.str();
  1196.             pcs.push_back(Pc(tor));
  1197.             storeData();
  1198.             cout << endl << "Pc added" << endl;
  1199.             break;
  1200.         }
  1201.         if(choi == "t" | choi == "T"){
  1202.             cout << "Enter screen inches: " << endl;
  1203.             cin >> screen;
  1204.             cout << "Enter 3d capability: " << endl;
  1205.             cin >> _3d;
  1206.             stringstream make;
  1207.             make << id << ":TV:" << model << ":" << manuf << ":" << desc << ":" << price << ":" << screen << ":" << _3d;
  1208.             string tor = make.str();
  1209.             tvs.push_back(Tv(tor));
  1210.             storeData();
  1211.             cout << endl << "Tv added"  << endl;
  1212.             break;
  1213.         }
  1214.         }
  1215.     }
  1216.         if(cho == "D" | cho == "d"){
  1217.             while(1){
  1218.             printProducts(1);
  1219.             cout << "In order to delete an item specify its Id, else hit . to go back." << endl;
  1220.             string cChoi;
  1221.             cin >> cChoi;
  1222.             int unId;
  1223.             stringstream tor(cChoi);
  1224.             tor >> unId;
  1225.             int i = 0;
  1226.             for(Pc p : pcs){
  1227.                 if(p.getProdId() == unId){
  1228.                     pcs.erase(pcs.begin() + i);
  1229.                     storeData();
  1230.                     cout << "Item deleted sucessfully" << endl;
  1231.                     break;
  1232.                 }
  1233.                 i++;
  1234.             }
  1235.             i = 0;
  1236.             for(Tv p : tvs){
  1237.                 if(p.getProdId() == unId){
  1238.                     tvs.erase(tvs.begin() + i);
  1239.                     storeData();
  1240.                     cout << "Item deleted sucessfully" << endl;
  1241.                     break;
  1242.                 }
  1243.                 i++;
  1244.             }
  1245.             i = 0;
  1246.             for(SmartPhone p : smartPhones){
  1247.                 if(p.getProdId() == unId){
  1248.                     smartPhones.erase(smartPhones.begin() + i);
  1249.                     storeData();
  1250.                     cout << "Item deleted sucessfully" << endl;
  1251.                     break;
  1252.                 }
  1253.                 i++;
  1254.             }
  1255.             if(cChoi == "."){
  1256.                 cout << "Returning to main menu" << endl;
  1257.                 break;
  1258.             }
  1259.             cout << "Id does not correspond to existing item, please re-try." << endl;
  1260.             }
  1261.            
  1262.         }
  1263.         else if(cho == "MP" | cho == "mp"){
  1264.             printProducts(1);
  1265.             cout << "Enter the Id of the product you would wish to modify: " << endl;
  1266.             string choi;
  1267.             cin >> choi;
  1268.             int id;
  1269.             stringstream tor(choi);
  1270.             tor >> id;
  1271.             for(Pc p : pcs){
  1272.                 if(p.getProdId() == id){
  1273.                     cout << "Modify id (I)" << endl << "Modify Model (M)" << endl << "Modify description (D)" << endl << "Modify price" << endl << "Modify manufacturer (MM)" << endl << "Modify Cpu ghz (C)" << endl << "Modify ram capability (R)" << endl << "Modify disc type (DI)" << endl << "Modify graphic hard (G)" << endl; ;
  1274.                     string choi2,mv;
  1275.                     cin >> choi2;
  1276.                 }
  1277.             }
  1278.             }else if(cho == "uo" | cho == "UO"){
  1279.                 int i = 0;
  1280.                 for(Order o : orders){
  1281.                     cout << "Order no: " << i << endl << "Owner :" << o.getOwnerUsername() << endl << "Product information :" << o.getCartProduct() << endl << "Product amount: " << o.getProductAmount() << endl <<  "Order status: " << o.getOrderComplete() << endl << "-- " << endl;
  1282.                     i++;
  1283.                 }
  1284.                 cout << "Enter order no id to modify status (COMPLETE/PENDING/SENT)" << endl;
  1285.                 string choi,sta;
  1286.                
  1287.                 cin >> choi;
  1288.                 stringstream tor(choi);
  1289.                 tor >> i;
  1290.                 if(i < orders.size() && i >= 0){
  1291.                 cout << endl << "Status: " << endl;
  1292.                 cin >> sta;
  1293.                
  1294.                 orders[i].setOrderComplete(sta);
  1295.                 storeData();
  1296.                 }else{
  1297.                     cout << "No such existing order number, back to main menu." << endl;
  1298.                 }
  1299.             }else if(cho == "pc" | cho == "PC"){
  1300.                 cout << "---Priting persons---" << endl;
  1301.                 for(Person p : persons){
  1302.                     cout << endl  << "Username: " << p.getUsername() << endl <<  "Password: " << p.getPassword() << endl << "Name: " << p.getName() << endl << "Surname: " << p.getSurname() << endl;
  1303.                 }
  1304.                 cout << "---Priting companies---" << endl;
  1305.                 for(Company p : companies){
  1306.                     cout << endl << "Username: " << p.getUsername() << endl <<  "Password: " << p.getPassword() << endl << "Company name: " << p.getName() << endl << "Supervisor name: " << p.getSupName() << endl << "Discount amount" << p.getDisc() << endl;
  1307.                 }
  1308.             }else if(cho == "po" | cho == "PO"){
  1309.                 for(Order o : orders){
  1310.                 cout << endl << "Order owner: " << o.getOwnerUsername() << endl << "Order product: " << o.getCartProduct() << endl << "Order status: " << o.getOrderComplete() << endl;
  1311.                 }
  1312.             }else if(cho == "ci" | cho == "CI"){
  1313.                 float income = 0;
  1314.                 for(Order o : orders){
  1315.                     vector<string> amounts, items, items2, prices;
  1316.                    
  1317.                     items = splitLine(o.toString(), '-');
  1318.                     amounts.push_back(items[2]);
  1319.                     cout << items[2] << endl;
  1320.                     items2 = splitLine(o.toString(), ':');
  1321.                     prices.push_back(items2[4]);
  1322.                     cout << items2[5] << endl;
  1323.                     stringstream am(items[2]), pr(items2[5]);
  1324.                     float pri,ams;
  1325.                     am >> ams;
  1326.                     pr >> pri;
  1327.                     income += pri*ams;
  1328.                    
  1329.                 }
  1330.                 cout << endl << "Total income: " << income << endl;
  1331.                
  1332.                 }else if(cho == "L" | cho == "l"){
  1333.                     cout << "Loging out" << endl;
  1334.                     break;
  1335.                 }
  1336.             }
  1337.        
  1338.         }
  1339.    
  1340.  
  1341.  
  1342. int main() {    
  1343.     while(1){
  1344.         updateData();
  1345.         string userIn = userLogin();
  1346.         cout << "Loged in as: "  << userIn << endl;
  1347.         if(getType(userIn,2) == "PERSON"){
  1348.             Person currUser(userIn);
  1349.                 customerUi(currUser);
  1350.         }else if(getType(userIn,2) == "COMPANY"){
  1351.             Company currUser(userIn);
  1352.         }else{
  1353.             User currUser(userIn);
  1354.             adminUi();
  1355.         }
  1356.     }
  1357. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement