Advertisement
Guest User

Untitled

a guest
Feb 24th, 2017
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 39.09 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <stdlib.h>
  4. #include <string.h>
  5. #include <fstream>
  6. #include <algorithm>
  7. #include <sstream>
  8. #include <stdio.h>
  9. #include <ctype.h>
  10. using namespace std;
  11. int betid=1;
  12. class category;
  13. class subcategory;
  14. class event;
  15. class market;
  16. class selection;
  17. void capitalize(string *str){
  18.     transform(str->begin(), str->end(), str->begin(), ::tolower);  
  19. }
  20. class root{
  21. protected:
  22.     string name;
  23.     vector <root*> nodes;
  24.     string ID;
  25.     int visibility;
  26. public:
  27.     root(string n="Location",string id=" "):visibility(1)
  28.     {   name.assign(n);
  29.         ID.assign(id);
  30.     }
  31.     root* visiblenode(int i)
  32.     {   int l=0;
  33.         while(l<i)
  34.         {   if(nodes[i]->get_visibility())
  35.             {   l++;
  36.             }
  37.         }
  38.         return nodes[l];
  39.     }
  40.     root* notvisiblenode(int i)
  41.     {   return nodes[i];
  42.     }
  43.     int get_visibility()
  44.     {   return visibility;
  45.     }
  46.     void change_name(string n)
  47.     {   name.replace(name.begin(),name.end(),n);
  48.     }
  49.     virtual void change_visibility()
  50.     {   cout << "You cannot change the visibility of root"<<endl;
  51.     }
  52.     string path()
  53.     {   return ID;
  54.     }
  55.     string get_name()
  56.     {   return name;
  57.     }
  58.     virtual string get_price()
  59.     {
  60.     }
  61.     void show()
  62.     {   for(int i=0;i<nodes.size();i++)
  63.             nodes[i]->printext();
  64.     }
  65.     virtual void printext()
  66.     {   cout<<ID<< " " << name <<endl;
  67.     }
  68.     virtual void push(string choice);
  69.     void readh(category* temp);
  70.     virtual void readh(subcategory* temp)
  71.     {
  72.     }
  73.     virtual void readh(event* temp)
  74.     {
  75.     }
  76.     virtual void readh(market* temp)
  77.     {
  78.     }
  79.     virtual void readh(selection* temp)
  80.     {
  81.     }
  82.     root* rnode(int i)
  83.     {   return nodes[i];
  84.     }
  85.     virtual void copynode(int i)
  86.     {   string n;
  87.         n.assign("Copied-");
  88.         n.append(nodes[i]->get_name());
  89.         push(n);
  90.         copyn(nodes[i],nodes[nodes.size()-1]);
  91.     }
  92.     virtual void copyn(root* copying,root* copied )
  93.     {   for(int i=0;i<copying->nodes.size();i++)
  94.         {   string t;
  95.             t.assign("Copied");
  96.             t.append(copying->nodes[i]->get_name());
  97.             copied->push(t);
  98.             copyn(copying->nodes[i],copied->nodes[i]);
  99.         }
  100.     }
  101.     virtual string voidd(int i)
  102.     {   root* node;
  103.         node=visiblenode(i);
  104.         return node->path();
  105.     }
  106. };
  107.  
  108. class category : public root{
  109. public:
  110.     string ID;
  111.     category(string n,string id):root(n,id)
  112.     {
  113.     }
  114.     void change_visibility()
  115.     {   visibility!=visibility;
  116.         for(int i=0;i<nodes.size();i++)
  117.         {   nodes[i]->change_visibility();
  118.         }
  119.     }
  120.     void push(string choice);
  121.     void readh(subcategory* temp);
  122. };
  123.  
  124. class subcategory : public root{
  125. public:
  126.     subcategory(string n,string id):root(n,id)
  127.     {
  128.     }
  129.     void change_visibility()
  130.     {   visibility!=visibility;
  131.         for(int i=0;i<nodes.size();i++)
  132.         {   nodes[i]->change_visibility();
  133.         }
  134.     }
  135.     void push(string choice);
  136.     void readh(event* temp);
  137.    
  138. };
  139.  
  140. class event : public root{
  141.     string date;
  142.     string time;
  143. public:
  144.     event(string n,string id , string d,string t):root(n,id)
  145.     {   date.assign(d);
  146.         time.assign(t);
  147.     }
  148.     void change_visibility()
  149.     {   visibility!=visibility;
  150.         for(int i=0;i<nodes.size();i++)
  151.         {   nodes[i]->change_visibility();
  152.         }
  153.     }
  154.     string get_date(){
  155.         return date;
  156.     }
  157.     string get_time(){
  158.         return time;
  159.     }
  160.     void printext()
  161.     {   cout<<ID<< " " << name <<" - "<<date<<" "<<time <<endl;
  162.     }
  163.     void push(string choice);
  164.     void readh(market* temp);
  165. };
  166.  
  167. class selection : public root{
  168.     string price;
  169. public:
  170.     selection(string n,string id,string p):root(n,id)
  171.     {   price.assign(p);
  172.     }
  173.     string get_price(){
  174.         return price;
  175.     }
  176.     void change_visibility()
  177.     {   visibility!=visibility;
  178.         for(int i=0;i<nodes.size();i++)
  179.         {   nodes[i]->change_visibility();
  180.         }
  181.     }
  182.     virtual void printext()
  183.     {   cout<<ID<< " " << name <<"#"<<price<<endl;
  184.     }
  185.  
  186. };
  187.  
  188. class market : public root{
  189. public:
  190.     market(string n,string id):root(n,id)
  191.     {
  192.     }
  193.     void change_visibility()
  194.     {   visibility!=visibility;
  195.         for(int i=0;i<nodes.size();i++)
  196.         {   nodes[i]->change_visibility();
  197.         }
  198.     }
  199.     void push(string choice);
  200.     void readh(selection* temp);
  201. };
  202. void root::readh(category* temp)
  203. {   nodes.push_back(temp);
  204. }
  205. void category::readh(subcategory* temp)
  206. {   nodes.push_back(temp);
  207. }
  208. void subcategory::readh(event* temp)
  209. {   nodes.push_back(temp);
  210. }
  211. void event::readh(market* temp)
  212. {   nodes.push_back(temp);
  213. }
  214. void market::readh(selection* temp)
  215. {   nodes.push_back(temp);
  216. }
  217.  
  218. void root::push(string choice)
  219. {   string NID;
  220.     NID.assign(ID);    
  221.     ostringstream convert;
  222.     convert << nodes.size();  
  223.     string temp3=convert.str();  
  224.     NID.insert(NID.size(),temp3);
  225.     NID.insert(NID.end(),'.');
  226.     root* temp2=new category(choice,NID);      
  227. }
  228. void category::push(string choice)
  229. {   string NID;
  230.     NID.assign(ID);
  231.     NID.insert(NID.end(),'.');    
  232.     ostringstream convert;
  233.     convert << nodes.size();  
  234.     string temp3=convert.str();  
  235.     NID.insert(NID.size(),temp3);
  236.     root* temp2=new subcategory(choice,NID);       
  237. }
  238. void subcategory::push(string choice)
  239. {   string NID;
  240.     NID.assign(ID);
  241.     NID.insert(NID.end(),'.');    
  242.     ostringstream convert;
  243.     convert << nodes.size();  
  244.     string temp3=convert.str();  
  245.     NID.insert(NID.size(),temp3);
  246.     string date;
  247.     string time;
  248.     cout<<"Dwse hmeromhnia"<<endl;
  249.     getline(cin,date);
  250.     cout<<"Dwse xrono"<<endl;
  251.     getline(cin,time);
  252.     root* temp2=new event(choice,NID,date,time);       
  253. }
  254. void event::push(string choice)
  255. {   string NID;
  256.     NID.assign(ID);
  257.     NID.insert(NID.end(),'.');    
  258.     ostringstream convert;
  259.     convert << nodes.size();  
  260.     string temp3=convert.str();  
  261.     NID.insert(NID.size(),temp3);
  262.     root* temp2=new market(choice,NID);
  263. }
  264. void market::push(string choice)
  265. {   string NID;
  266.     NID.assign(ID);
  267.     NID.insert(NID.end(),'.');    
  268.     ostringstream convert;
  269.     convert << nodes.size();  
  270.     string temp3=convert.str();  
  271.     NID.insert(NID.size(),temp3);
  272.     cout<<"Dwse price se klasma"<<endl;
  273.     string price;
  274.     getline(cin,price);
  275.     root* temp2=new selection(choice,NID,price);
  276. }
  277.  
  278.  
  279. struct History
  280. {   string username;
  281.     string operation;
  282.     string rename;
  283.     string target;
  284.     double   money;
  285. };
  286. struct Bethistory
  287. {   int bet_id;
  288.     int user_id;
  289.     string node_id;
  290.     double stake;
  291.     char result;
  292. };
  293. class elements
  294. {   public:
  295.         int user_id;
  296.         string username;
  297.         string fullname;
  298.         string password;
  299.         int type;
  300.         string status;
  301.         double balance;
  302.         vector<double> freebets;
  303.  
  304. };
  305.  
  306. void historyfun(vector<History*> &history,string operation,string target,string rename,string username,double x)
  307. {   history.push_back(new History);
  308.     int i=history.size()-1;
  309.     history[i]->operation.assign(operation);
  310.     history[i]->money=x;
  311.     history[i]->target.assign(target);
  312.     history[i]->rename.assign(rename);
  313.     history[i]->username.assign(username);
  314.      
  315. }
  316.  
  317. void bethistoryfun(vector<Bethistory*> &bethistory,int bet_id,int user_id,string node_id,double stake,char result)
  318. {   bethistory.push_back(new Bethistory);
  319.     int i=bethistory.size()-1;
  320.     bethistory[i]->bet_id=bet_id;
  321.     bethistory[i]->user_id=user_id;
  322.     bethistory[i]->node_id.assign(node_id);
  323.     bethistory[i]->stake=stake;
  324.     bethistory[i]->result=result;
  325. }
  326. class Users
  327. {   public:
  328.         class elements ID;
  329.         virtual void print()
  330.         {
  331.         }
  332.         virtual void exprint()
  333.         {   print();
  334.         }
  335.         void home()
  336.         {   //epistrofh arxikh selida
  337.         }
  338.         void toggle()
  339.         {   //enallafh timwn
  340.         }
  341.         virtual void bets(vector<Bethistory*> &bethistory,vector<History*> &history)
  342.         {   cout<< "Den exete prosbash se authn thn entolh"<<endl;
  343.         }
  344.         virtual void freebets(vector<Users*> &User,vector<History*> &history)
  345.         {   cout<< "Den exete prosbash se authn thn entolh"<<endl;
  346.         }
  347.         virtual void account(vector<History*> &history)
  348.         {   cout<< "Den exete prosbash se authn thn entolh"<<endl;
  349.         }
  350.         virtual void save()
  351.         {   cout<< "Den exete prosbash se authn thn entolh"<<endl;
  352.         }
  353.         virtual void logs(vector<History*> &history)
  354.         {   cout<< "Den exete prosbash se authn thn entolh"<<endl;
  355.         }
  356.         virtual void users()
  357.         {   cout<< "Den exete prosbash se authn thn entolh"<<endl;
  358.         }
  359.         virtual void rename(vector<Users*> &User,vector<History*> &history,root* parent,root* nodeid)
  360.         {   cout<< "Den exete prosbash se authn thn entolh"<<endl;
  361.         }
  362.         virtual void New(root* parent,root *nodeid)
  363.         {   cout<< "Den exete prosbash se authn thn entolh"<<endl;
  364.         }
  365.         virtual void copy()
  366.         {   cout<< "Den exete prosbash se authn thn entolh"<<endl;
  367.         }
  368.         virtual void Delete()
  369.         {   cout<< "Den exete prosbash se authn thn entolh"<<endl;
  370.         }
  371.         virtual void view(vector <Users*> &User)
  372.         {   cout<< "Den exete prosbash se authn thn entolh"<<endl;
  373.         }
  374.         virtual void search(vector <Users*> &User)
  375.         {   cout<< "Den exete prosbash se authn thn entolh"<<endl;
  376.         }
  377.         virtual void place(vector<Bethistory*> &bethistory,vector<History*> &history,string nodeid)
  378.         {   cout<< "Den exete prosbash se authn thn entolh"<<endl;
  379.         }
  380.         virtual void lock(vector<Users*> &User,vector<History*> &history)
  381.         {   cout<< "Den exete prosbash se authn thn entolh"<<endl;
  382.         }
  383.         virtual void password(vector<History*> &history)
  384.         {   cout<< "Den exete prosbash se authn thn entolh"<<endl;
  385.         }
  386.         virtual void deposit (vector<History*> &history)
  387.         {   cout<< "Den exete prosbash se authn thn entolh"<<endl;
  388.         }
  389.         virtual void Void(vector<Users*> &User,vector<Bethistory*> &bethistory)
  390.         {   cout<< "Den exete prosbash se authn thn entolh"<<endl;
  391.         }
  392.         virtual void Settle(vector<Users*> &User,vector<Bethistory*> &bethistory)
  393.         {   cout<< "Den exete prosbash se authn thn entolh"<<endl;
  394.         }
  395.         virtual void visibility(root* nodeid)
  396.         {   cout<< "Den exete prosbash se authn thn entolh"<<endl;
  397.         }
  398. };
  399.  
  400. class Guest:public Users
  401. {   public:
  402.         void print()
  403.         {   cout<<"Epile3te" << endl << "To ID tou kombou pou epi8umeite na episkefteite" <<endl<<"H(Home),gia epistrofh sthn arxikh o8onh"<<endl<<"T(Toggle) gia enallagh probolhs timwn" << endl<< "X(Exit) gia e3odo apo to susthma"<<endl;
  404.         }
  405. };
  406. class Punter:public Users
  407. {   public:
  408.         void print()
  409.         {   cout<<"Epile3te" << endl << "To ID tou kombou pou epi8umeite na episkefteite" <<endl<<"H(Home),gia epistrofh sthn arxikh o8onh"<<endl<<"T(Toggle) gia enallagh probolhs timwn" << endl<< "X(Exit) gia e3odo apo to susthma"<<endl<<"A(Account),gia diaxeirish twn plhroforiwn tou logariasmou xrhsth"<<endl;
  410.         }
  411.         void exprint()
  412.         {   print();
  413.             cout<<"P(Place),gia enapo8esh stoixhmatos"<<endl;
  414.         }
  415.         void rename(vector<Users*> &User,vector<History*> &history,root* parent,root* nodeid)
  416.         {   if(flag==1)
  417.             {   string str;
  418.                 while(1)
  419.                 {   int flag2=0;
  420.                      getline(cin,str);
  421.                     for(int i=0;i<User.size();i++)
  422.                     {   if (!User[i]->ID.username.compare(str))
  423.                         {   flag2=1;
  424.                             cout<<"To username xrhshmopoieitai hdh"<<endl;
  425.                             break;
  426.                         }
  427.                     }
  428.                     if(flag2==0)
  429.                         break;
  430.                 }
  431.                 ID.username.replace(ID.username.begin(),ID.username.end(),str);
  432.                 historyfun(history,"Rename"," "," ",ID.username,0.0);
  433.             }
  434.             else
  435.                 cout<< "Den exete prosbash se authn thn entolh"<<endl;
  436.  
  437.         }
  438.         void password(vector<History*> &history)
  439.         {   if(flag==1)
  440.             {   string str,str1;
  441.                 while(1)
  442.                 {   cout<< "Dwse ton neo kwdiko"<<endl;
  443.                     getline(cin,str);
  444.                     cout<< "Epanalabe"<<endl;
  445.                     getline(cin,str1);
  446.                     if (!str.compare(str1))
  447.                     {   ID.password.replace(ID.password.begin(),ID.password.end(),str);
  448.                         historyfun(history,"Password"," "," ",ID.username,0.0);
  449.                         break;
  450.                     }
  451.                 }
  452.  
  453.             }
  454.             else
  455.                 cout<< "Den exete prosbash se authn thn entolh"<<endl;
  456.  
  457.         }
  458.         void bets(vector<Bethistory*> &bethistory,vector<History*> &history)
  459.         {   if(flag==1)
  460.             {   for (int i=0;i<bethistory.size();i++)
  461.                 {   if (bethistory[i]->user_id==ID.user_id)
  462.                     {   cout<<"Bet to "<<bethistory[i]->node_id<< " "<< bethistory[i]->stake<< endl;
  463.  
  464.                     }
  465.                 }
  466.                 historyfun(history,"Bet"," "," ",ID.username,0.0);
  467.             }
  468.             else
  469.                 cout<< "Den exete prosbash se authn thn entolh"<<endl;
  470.  
  471.         }
  472.         void deposit(vector<History*> &history)
  473.         {   if(flag==1)
  474.             {   double x;
  475.                 cin >> x;
  476.                 ID.balance+=x;
  477.                 cout << "To neo sas poso sto logariasmo sas einai" << ID.balance<<endl;
  478.                 historyfun(history,"Deposit"," "," ",ID.username,x);
  479.             }
  480.             else
  481.                 cout<< "Den exete prosbash se authn thn entolh"<<endl;
  482.  
  483.         }
  484.         void account(vector<History*> &history)
  485.         {   cout<<"Paixths: " << ID.username<<endl<<"Dia8esimo upoloipo: "<< ID.balance<<endl<<"Dia8esima kouponia:";
  486.             for(int i=0;i<ID.freebets.size();i++)
  487.             {   if(i!=ID.freebets.size())
  488.                     cout<<ID.freebets[i]<<",";
  489.                 else
  490.                     cout<<ID.freebets[i]<<endl;
  491.             }
  492.             cout<<"Epiloges:"<<endl<<"R(Rename),gia allagh onomatos paikth"<<endl<<"P(Password),gia allagh kwdikou"<<endl<<"B(Bets),gia probolh istorikou stoixhmatwn"<<endl<<"D(Deposit),gia gemisma logariasmou upoloipou"<<endl;
  493.             flag=1;
  494.             historyfun(history,"Account"," "," ",ID.username,0.0);
  495.         }
  496.         void place(vector<Bethistory*> &bethistory,vector<History*> &history,string nodeid)
  497.         {   string choice;
  498.             string bet;
  499.             cout<<"Upoloipo portofoliou: "<<ID.balance<<endl;
  500.             cout<<"Dwse epilogh"<<endl;
  501.             getline(cin,choice) ;
  502.             if(choice.compare("cancel"));
  503.             {   char x='a';
  504.                 for(int i=0;i<ID.freebets.size();i++)
  505.                 {   cout<<x++<<"."<<ID.freebets[i]<<endl;
  506.                 }
  507.                 cout << "Dwse poso stoixhmatos"<<endl;
  508.                 while (1)
  509.                 {   getline(cin,bet);
  510.                     if(bet[0]>='a' && bet[0]<x)
  511.                     {   int l=bet[0]-'a';
  512.                         bethistoryfun(bethistory,betid++,ID.user_id," ",ID.freebets[l],'-');
  513.                         ID.freebets.erase(ID.freebets.begin()+l);
  514.                         break;
  515.                     }
  516.                     else if(isdigit(bet[0]))
  517.                     {   double bett=atof( bet.c_str());
  518.                         if(bett>ID.balance && ID.balance==0)
  519.                         {   cout<<"Mhdeniko upoloipo parakalw gemise to portofoli s apo to menou diaxeirhshs"<<endl;
  520.                             break;
  521.                         }
  522.                         else if(bett>ID.balance)
  523.                         {   cout<< "Dwse mikrotero poso"<<endl;
  524.                             continue;
  525.                         }
  526.                         else if(bett<=ID.balance)
  527.                         {   ID.balance-=bett;
  528.                             bethistoryfun(bethistory,betid++,ID.user_id," ",bett,'-');
  529.                             historyfun(history,"bet",nodeid," ",ID.username,bett);
  530.                             break;
  531.                         }
  532.                     }
  533.                     else
  534.                     {   cout<<"La8os epilogh"<<endl;
  535.                         break;
  536.                     }
  537.                 }
  538.  
  539.             }
  540.         }
  541.     private:
  542.         int flag;
  543. };
  544. class Trader:public Users
  545. {   public:
  546.         void bets(vector<Bethistory*> &bethistory,vector<History*> &history)
  547.         {   int l=0;
  548.             cout<< "bet_id | user_id | node_id | stake | result " << endl;
  549.             for (int i=0;i<bethistory.size();i++)
  550.             {   if (l++==20)
  551.                     break;
  552.                 cout << bethistory[i]->bet_id<< '\t' <<bethistory[i]->user_id<< '\t' <<bethistory[i]->node_id<< '\t' <<bethistory[i]->stake<< '\t' <<bethistory[i]->result<<endl;
  553.             }
  554.         }
  555.         void freebets(vector<Users*> &User,vector<History*> &history)
  556.         {   string name;
  557.             double x;
  558.             int flag=0,i;
  559.             while(1)
  560.             {   getline(cin,name);
  561.                 for(i=0;i<User.size();i++)
  562.                 {   if(!User[i]->ID.username.compare(name))
  563.                     {   flag=1;
  564.                         break;
  565.                     }
  566.                 }
  567.                 if(flag==1)
  568.                     break;
  569.  
  570.             }
  571.             if(flag==1)
  572.             {   cin >> x;
  573.                 User[i]->ID.freebets.push_back(x);
  574.             }
  575.  
  576.         }
  577.         void Void(vector<Users*> &User,vector<Bethistory*> &bethistory,root* nodeid)
  578.         {   cout<<"Dwse to id ths epiloghs stoixhmatismou p 8es na akurw8ei"<<endl;
  579.             string choice;
  580.             getline(cin,choice);
  581.             if(isdigit(choice[0]))
  582.             {   stringstream buffer(choice);
  583.                 int i;
  584.                 buffer >> i;
  585.                 i--;
  586.                 string id=nodeid->voidd(i);
  587.                 for(int l=0;l<bethistory.size();l++)
  588.                 {   if(!(id.compare(bethistory[l]->node_id)))
  589.                     {   if(bethistory[l]->result=='-')
  590.                         {   bethistory[l]->result='V';
  591.                             for(int j=0;j<User.size();j++)
  592.                             {   if(bethistory[l]->user_id==User[j]->ID.user_id)
  593.                                 {   User[j]->ID.balance+=bethistory[l]->stake;
  594.                                 }
  595.                             }
  596.                         }
  597.                         else
  598.                         {   cout<<"Already voided"<<endl;
  599.                             break;
  600.                         }
  601.                     }
  602.                 }
  603.                 //enhmerwsh arxeiwn xrhstwn kai bet
  604.                
  605.             }
  606.             else
  607.             {   cout<<"Operation aborted wrong input"<<endl;
  608.             }
  609.         }
  610.         void Settle(vector<Users*> &User,vector<Bethistory*> &bethistory,root* nodeid)
  611.         {   cout<<"Dwse to id ths epiloghs stoixhmatismou p 8es na 8ewrh8ei ws nikh"<<endl;
  612.             string choice;
  613.             getline(cin,choice);
  614.             if(isdigit(choice[0]))
  615.             {   stringstream buffer(choice);
  616.                 int i;
  617.                 buffer >> i;
  618.                 i--;
  619.                 string id=nodeid->voidd(i);
  620.                 root* temp;
  621.                 temp=nodeid->visiblenode(i);
  622.                 string sprice;
  623.                 string token;
  624.                 double price1,price2,price;
  625.                 sprice=temp->get_price();
  626.                 char *str = new char[sprice.length() + 1];
  627.                 char s[2]="/";
  628.                 strcpy(str, sprice.c_str());
  629.                 token = strtok(str,s);
  630.                 price1=atof(token.c_str());
  631.                 price2=atof(str);
  632.                 price=(price1/price2) + 1;
  633.                 for(int l=0;l<bethistory.size();l++)
  634.                 {   if(!(id.compare(bethistory[l]->node_id)))
  635.                     {   if(bethistory[l]->result=='-')
  636.                         {   bethistory[l]->result='W';
  637.                             for(int j=0;j<User.size();j++)
  638.                             {   if(bethistory[l]->user_id==User[j]->ID.user_id)
  639.                                 {   User[j]->ID.balance+=bethistory[l]->stake*price;
  640.                                 }
  641.                             }
  642.                         }
  643.                         else
  644.                         {   cout<<"Already changed"<<endl;
  645.                             break;
  646.                         }
  647.                     }
  648.                 }
  649.                 string lose_id;
  650.                 lose_id.assign(id);
  651.                 lose_id.erase(lose_id.size()-1);
  652.                 lose_id.erase(lose_id.size()-1);
  653.                 for(int l=0;l<bethistory.size();l++)
  654.                 {   if(bethistory[l]->node_id!=id && bethistory[l]->node_id.find(lose_id)!=bethistory[l]->node_id.npos && bethistory[l]->result=='-')
  655.                     {   bethistory[l]->result='L';
  656.                     }
  657.                 }
  658.                 //enhmerwsh arxeiwn xrhstwn kai bet
  659.             }
  660.             else
  661.             {   cout<<"Operation aborted wrong input"<<endl;
  662.             }
  663.         }
  664.         void print()
  665.         {   cout<<"Epile3te" << endl << "To ID tou kombou pou epi8umeite na episkefteite" <<endl<<"H(Home),gia epistrofh sthn arxikh o8onh"<<endl<<"T(Toggle) gia enallagh probolhs timwn" << endl<< "X(Exit) gia e3odo apo to susthma"<<endl<<"B(Bets),gia emfanish 20 teleutaiwn stoixhmatwn"<<endl<<"F(Freebets),gia apodosh kouponiou se xrhsth"<<endl;
  666.         }
  667.         void exprint()
  668.         {   print();
  669.             cout<<"V(Void),gia na akurw8ei mia epilogh"<<endl<<"S(Settle),gia dieu8ethsh ths agoras"<<endl;
  670.         }
  671.  
  672. };
  673. class Director:public Users
  674. {   public:
  675.         void bets(vector<Bethistory*> &bethistory,vector<History*> &history)
  676.         {   int l=0;
  677.             cout<< "bet_id | user_id | node_id | stake | result " << endl ;
  678.             for (int i=0;i<bethistory.size();i++)
  679.             {   if (l++==20)
  680.                     break;
  681.                 cout << bethistory[i]->bet_id<< '\t' <<bethistory[i]->user_id<< '\t' <<bethistory[i]->node_id<< '\t' <<bethistory[i]->stake<< '\t' <<bethistory[i]->result<<endl;
  682.             }
  683.             historyfun(history,"Bets"," "," ",ID.username,0);
  684.         }
  685.         void freebets(vector<Users*> &User,vector<History*> &history)
  686.         {   string name;
  687.             double x;
  688.             int flag=0,i;
  689.             while(1)
  690.             {   getline(cin,name);
  691.                 for(i=0;i<User.size();i++)
  692.                 {   if(!User[i]->ID.username.compare(name))
  693.                     {   flag=1;
  694.                         break;
  695.                     }
  696.                 }
  697.                 if(flag==1)
  698.                     break;
  699.  
  700.             }
  701.             if(flag==1)
  702.             {   cin >> x;
  703.                 User[i]->ID.freebets.push_back(x);
  704.             }
  705.  
  706.         }
  707.         void save()
  708.         {   //save
  709.         }
  710.         void logs(vector<History*> &history)
  711.         {   int l=0;
  712.             for (int i=0;i<history.size();i++)
  713.             {   if (l++==25)
  714.                     break;
  715.                 cout<<history[i]->username<<'\t'<<history[i]->operation<<'\t'<<history[i]->target<<'\t'<<history[i]->rename<<'\t'<<history[i]->money<<endl;
  716.             }
  717.         }
  718.         void users()
  719.         {   cout<<"Epiloges:"<<endl<<"V(View),gia emfanish xrhstwn pinaka xrhstwn"<<endl<<"S(Search),gia anazhthsh xrhsth"<<endl<<"L(Lock,gia enallagh katastashs apokleismou"<<endl;
  720.             flag=1;
  721.         }
  722.         void visibility(root* nodeid)
  723.         {   nodeid->change_visibility();
  724.         }
  725.         void rename(vector<Users*> &User,root* nodeid)
  726.         {   nodeid->show();
  727.             string choice;
  728.             cout<<"Dwse id h cancel/abort gia akurwsh"<<endl;
  729.             if(isdigit(choice[0]))
  730.             {   stringstream buffer(choice);
  731.                 int i;
  732.                 buffer >> i;
  733.                 i--;
  734.                 root* temp=nodeid->rnode(i);
  735.                 cout<<"Dwse neo onoma h abort/cancel gia akurwsh"<<endl;
  736.                 string rename;
  737.                 string temp2=rename;
  738.                 capitalize(&temp2);
  739.                 if(temp2!="cancel"&& temp2!="abort" && temp2!="a" && temp2!="c")
  740.                 {   temp->change_name(rename);
  741.                 }
  742.                 else
  743.                     cout<<"Procedure aborted"<<endl;
  744.             }
  745.             else
  746.             {   cout<<"Procedure aborted"<<endl;
  747.             }
  748.         }
  749.         void New(root *nodeid)
  750.         {   nodeid->show();
  751.             cout<<"Dwse neo onoma kombou h cancel/abort gia akurwsh"<<endl;
  752.             string choice;
  753.             getline(cin,choice);
  754.             string temp=choice;
  755.             capitalize(&temp);
  756.             if(temp!="cancel"&& temp!="abort" && temp!="a" && temp!="c")
  757.             {   nodeid->push(choice);      
  758.             }
  759.             else
  760.                 cout<<"Procedure aborted"<<endl;
  761.            
  762.         }
  763.         virtual void copynode(root* nodeid)
  764.         {   cout << "Dwse  ID gia na ginei copy h cancel/abort gia akurwsh";
  765.             string choice;
  766.             getline(cin,choice);
  767.             if(isdigit(choice[0]))
  768.             {   stringstream buffer(choice);
  769.                 int i;
  770.                 buffer >> i;
  771.                 i--;
  772.                 nodeid->copynode(i);
  773.             }
  774.             else
  775.             {   cout<<"Procedure aborted"<<endl;
  776.             }
  777.         }
  778.         void Delete()
  779.         {   //diagrafh kombou
  780.         }
  781.         void view(vector <Users*> &User)
  782.         {   if (flag==1)
  783.             {   cout << "user_id | username | fullname | password | type | status | balance | freebets" << endl ;
  784.                 for (int i=0;i<User.size();i++)
  785.                 {   cout<< User[i]->ID.user_id<<'\t'<<User[i]->ID.username<<'\t'<<User[i]->ID.fullname<<'\t'<<User[i]->ID.password<<'\t'<<User[i]->ID.type<<'\t'<<User[i]->ID.status<<'\t'<<User[i]->ID.balance<<'\t';
  786.                     for (int l=0;l<User[i]->ID.freebets.size();l++)
  787.                     {   cout<< User[i]->ID.freebets[l];
  788.                         if(l!=User[i]->ID.freebets.size()-1)
  789.                             cout<<",";
  790.                         else
  791.                             cout << endl;
  792.                     }
  793.                 }
  794.             }
  795.             else
  796.                 cout<< "Den exete prosbash se authn thn entolh"<<endl;
  797.         }
  798.         void search(vector <Users*> &User)
  799.         {   if (flag==1)
  800.             {   string name;
  801.                 getline(cin,name);
  802.                 for(int i=0;i<User.size();i++)
  803.                 {   if(User[i]->ID.username.find(name)!=User[i]->ID.username.npos)
  804.                     {   cout<< User[i]->ID.user_id<<'\t'<<User[i]->ID.username<<'\t'<<User[i]->ID.fullname<<'\t'<<User[i]->ID.password<<'\t'<<User[i]->ID.type<<'\t'<<User[i]->ID.status<<'\t'<<User[i]->ID.balance<<'\t';
  805.                         for (int l=0;l<User[i]->ID.freebets.size();l++)
  806.                         {   cout<< User[i]->ID.freebets[l];
  807.                             if(l!=User[i]->ID.freebets.size()-1)
  808.                                 cout<<",";
  809.                             else
  810.                                 cout << endl;
  811.                         }
  812.                     }
  813.                 }
  814.  
  815.             }
  816.             else
  817.                 cout<< "Den exete prosbash se authn thn entolh"<<endl;
  818.  
  819.         }
  820.         void lock(vector<Users*> &User)
  821.         {   if (flag==1)
  822.             {   string name;
  823.                 string name3;
  824.                 int flag2=0,i;
  825.                 getline(cin,name);
  826.                 for(i=0;i<User.size();i++)
  827.                 {  
  828.                     if(!User[i]->ID.username.compare(name) && User[i]->ID.type!=3)
  829.                     {   if(User[i]->ID.status[0]!='L')
  830.                         {   flag2=1;
  831.                             cout<<"Anefere logo"<<endl;
  832.                             getline(cin, name3);
  833.                             string name2="L,";
  834.                             User[i]->ID.status.replace(User[i]->ID.status.begin(),User[i]->ID.status.end(),name2);
  835.                             User[i]->ID.status.insert(2,name3);
  836.                             break;
  837.                         }
  838.                         else if(User[i]->ID.status[0]=='L')
  839.                         {   flag2=1;
  840.                             cout<<"Anefere logo"<<endl;
  841.                             getline(cin, name3);
  842.                             User[i]->ID.status.replace(User[i]->ID.status.begin(),User[i]->ID.status.end(),name3);
  843.                             break;
  844.                         }
  845.                     }
  846.                 }
  847.             }
  848.             else
  849.                 cout<< "Den exete prosbash se authn thn entolh"<<endl;
  850.  
  851.         }
  852.         void print()
  853.         {   cout<<"Epile3te" << endl << "To ID tou kombou pou epi8umeite na episkefteite" <<endl<<"H(Home),gia epistrofh sthn arxikh o8onh"<<endl<<"T(Toggle) gia enallagh probolhs timwn" << endl<< "X(Exit) gia e3odo apo to susthma"<<endl<<"B(Bets),gia emfanish 20 teleutaiwn stoixhmatwn"<<endl<<"F(Freebets),gia apodosh kouponiou se xrhsth"<<endl<<"S(Save),gia swsimo ths trexousas katastashs kai sugxronismo twn arxeiwn susthmatos"<<endl<<"Management:"<<endl<<"L(Logs),gia emfanish twn teleutaiwn 25 energeiwn sto susthma"<<endl<<"U(Users),gia diaxeirhsh xrhstwn"<<endl<<"V(Visibility),gia enallgah oratothtas kombou"<<endl<<"R(Rename),gia metonomasia kombou"<<endl<<"N(New),gia dhmiourgia kombou"<<endl<<"C(Copy),gia thn antigrafh enos kombou (ws mh oratou)"<<endl<<"D(Delete),gia th diagrafh kombou"<<endl;
  854.         }
  855.         void exprint()
  856.         {   print();
  857.             cout<<"Epiloges:"<<endl<<"V(View),gia emfanish pinaka xrhstwn"<<endl<<"S(Search),gia anazhthsh xrhsth"<<endl<<"L(Lock),gia enallagh katastashs apokleismou enos xrhsth"<<endl;
  858.         }
  859.     private:
  860.         int flag;
  861. };
  862.  
  863. void write_users(vector<Users*>& User){
  864.     remove("users.csv");
  865.     ofstream file;
  866.     file.open("users.csv",ios::out);
  867.     file << "user_id | username | fullname | password | type | status | balance | freebets" << endl ;
  868.     int i,j;
  869.     for(i=0;i<User.size();i++){
  870.         file << User[i]->ID.user_id << "|" << User[i]->ID.username << "|" << User[i]->ID.fullname << "|";
  871.         file << User[i]->ID.password << "|" << User[i]->ID.type << "|" << User[i]->ID.status << "|" << User[i]->ID.balance << "|";
  872.         for(j=0;j<User[i]->ID.freebets.size();j++){
  873.             file << User[i]->ID.freebets[j];
  874.             if (j<User[i]->ID.freebets.size()-1){
  875.                 file << ",";
  876.             }
  877.         }
  878.         file << endl;
  879.     }
  880.     file.close();
  881. }
  882.  
  883. void write_history(vector <History*> &history){
  884.     remove("audit.log");
  885.     ofstream file;
  886.     file.open("audit.log",ios::out);
  887.     file << "username | operation | money | target | rename";
  888.     for(int i=0;i<history.size();i++){
  889.         file << history[i]->username << "|" << history[i]->operation << "|" << history[i]->money << "|" << history[i]->target << "|" << history[i]->rename[i] << endl;
  890.     }
  891.     file.close();
  892. }
  893.  
  894. void write_bet(vector <Bethistory*> &bethistory){
  895.     remove("bets.csv");
  896.     ofstream file;
  897.     file.open("bets.csv",ios::out);
  898.     file << "bet_id | user_id | node_id | stake | result " << endl ;
  899.     for(int i=0;i<bethistory.size() ;i++){
  900.         file << bethistory[i]->bet_id<< "|" << bethistory[i]->user_id << "|" << bethistory[i]->node_id << "|" << bethistory[i]->stake <<"|" << bethistory[i]->result <<endl;
  901.     }
  902.     file.close();
  903. }
  904.  
  905. void read_hierarchy(root* Root){
  906.     vector <root*> hierarchy2;
  907.     vector <root*> hierarchy3;
  908.     vector <root*> hierarchy4;
  909.     vector <root*> hierarchy5;
  910.     ifstream file;
  911.     file.open("hierarchy.dat",ios::in);
  912.     if(file.fail()){
  913.         cerr << "Error openning file" << endl;
  914.         exit(1);
  915.     }
  916.     string line;
  917.     while(getline(file,line)){
  918.         const char s[2]=" ";
  919.         char* token;
  920.         //converting string to char*
  921.         char *str = new char[line.length() + 1];
  922.         strcpy(str, line.c_str());
  923.         //
  924.         token = strtok(str,s);  
  925.         string node(token);
  926.         size_t n = count(node.begin(), node.end(), '.'); // number of dots
  927.         token = strtok(NULL, s); //oti apomenei meta to node
  928.         if(n == 1)
  929.         {   category* temp=new category(token,node);
  930.             Root->readh(temp);
  931.             hierarchy2.push_back(temp);
  932.         }
  933.         if(n==2){
  934.             subcategory* temp=new subcategory(token,node);
  935.             hierarchy2[hierarchy2.size()-1]->readh(temp);
  936.             hierarchy3.push_back(temp);
  937.         }
  938.         if(n == 3){
  939.             strcpy(str,token); //str = token
  940.             const char h[2] = "-";
  941.             token = strtok(str,h);
  942.             char* n;
  943.             strcpy(n,token);
  944.             token = strtok(NULL,s); //date
  945.             char* d;
  946.             strcpy(d,token);
  947.             token = strtok(NULL,s);
  948.             event* temp=new event(n,node,d,token);
  949.             hierarchy3[hierarchy3.size()-1]->readh(temp);
  950.             hierarchy4.push_back(temp);
  951.         }
  952.         if(n == 4){
  953.             market* temp=new market(token,node);
  954.             hierarchy4[hierarchy4.size()-1]->readh(temp);
  955.             hierarchy5.push_back(temp);
  956.         }
  957.         if(n == 5){
  958.             strcpy(str,token); //str = token
  959.             const char h[2] = "#";
  960.             token = strtok(str,h);
  961.             string n=token; // name
  962.             token = strtok(NULL,h); //price
  963.             selection* temp=new selection(n,node,token);
  964.             hierarchy5[hierarchy5.size()-1]->readh(temp);
  965.         }
  966.     }
  967. }
  968.  
  969.  
  970. void read_users(vector<Users*> &User){
  971.     ifstream file;
  972.     file.open("users.csv",ios::in);
  973.     if(file.fail()){
  974.         cerr << "Error openning file" << endl;
  975.         exit(1);
  976.     }
  977.     string line;
  978.     getline(file,line);
  979.     //pairnw to prwto line to arxeiasto me ta userid username ktl kai de to xrhsimopoiw
  980.     while(getline(file,line)){
  981.         //exw twra to line pou me | exei ola ta stoixeia
  982.         const char s[2]="|";
  983.         char* token;
  984.         Users temp;
  985.         //converting string to char*
  986.         char *str = new char[line.length() + 1];
  987.         strcpy(str, line.c_str());
  988.         //
  989.         token = strtok(str,s);
  990.         temp.ID.user_id = atoi(token); //(kanw to char* int)
  991.         token = strtok(NULL, s);
  992.         temp.ID.username = token;
  993.         token = strtok(NULL, s);
  994.         temp.ID.fullname = token;
  995.         token = strtok(NULL, s);
  996.         temp.ID.password = token;
  997.         token = strtok(NULL, s);
  998.         temp.ID.type = atoi(token); //kanw to char* int
  999.         token = strtok(NULL, s);
  1000.         temp.ID.status =token;
  1001.         token = strtok(NULL, s);
  1002.         temp.ID.balance = atof(token); //kanw char* float
  1003.         token = strtok(NULL, s);
  1004.         //twra to token einai san ena char* kai prepei pali na to kopsw me delimeter to ,
  1005.         char* token2;
  1006.         const char d[2]=",";
  1007.         token2=strtok(token,d);
  1008.         while(token2 != NULL){
  1009.             temp.ID.freebets.push_back(atof(token2)); //char* to float
  1010.             token2 = strtok(NULL, d);
  1011.         }
  1012.         if(temp.ID.type==1){  
  1013.             User.push_back(new Punter());
  1014.         }
  1015.         else if(temp.ID.type==2){  
  1016.             User.push_back(new Trader());
  1017.         }
  1018.         else{  
  1019.             User.push_back(new Director());
  1020.         }
  1021.         int i=User.size()-1;
  1022.         User[i]->ID.user_id=temp.ID.user_id;
  1023.         User[i]->ID.username.assign(temp.ID.username);
  1024.         User[i]->ID.fullname.assign(temp.ID.fullname);
  1025.         User[i]->ID.password.assign(temp.ID.password);
  1026.         User[i]->ID.type=temp.ID.type;
  1027.         User[i]->ID.status.assign(temp.ID.status);
  1028.         User[i]->ID.balance=temp.ID.balance;
  1029.         for(int l=0;l<temp.ID.freebets.size();l++)
  1030.         {   User[i]->ID.freebets.push_back(temp.ID.freebets[l]);
  1031.         }
  1032.     }
  1033.     //etoimo to User
  1034.     file.close();
  1035. }
  1036.  
  1037. void read_history(vector <History*> &history){
  1038.     ifstream file;
  1039.     file.open("audit.log",ios::in);
  1040.     if(file.fail()){
  1041.         cerr << "Error openning file" << endl;
  1042.         exit(1);
  1043.     }
  1044.     string line;
  1045.     getline(file,line);
  1046.     while(getline(file,line)){
  1047.         const char s[2]="|";
  1048.         char* token;
  1049.         History temp;
  1050.         //converting string to char*
  1051.         char *str = new char[line.length() + 1];
  1052.         strcpy(str, line.c_str());
  1053.         //
  1054.         token = strtok(str,s);
  1055.         temp.username = token;
  1056.         token = strtok(NULL, s);
  1057.         temp.operation = token;
  1058.         token = strtok(NULL, s);
  1059.         temp.money = atof(token);
  1060.         token = strtok(NULL, s);
  1061.         temp.target =token;
  1062.         token = strtok(NULL, s);
  1063.         temp.rename =token;
  1064.         //
  1065.         history.push_back(new History());
  1066.         int i = history.size()-1;
  1067.         //
  1068.         history[i]->username.assign(temp.username);
  1069.         history[i]->operation.assign(temp.operation);
  1070.         history[i]->target.assign(temp.target);
  1071.         history[i]->rename.assign(temp.rename);
  1072.         history[i]->money = temp.money;
  1073.         //
  1074.     }
  1075.     file.close();
  1076. }
  1077.  
  1078. void read_bet(vector <Bethistory*> &bethistory){
  1079.     ifstream file;
  1080.     file.open("bets.csv",ios::in);
  1081.     if(file.fail()){
  1082.         cerr << "Error openning file" << endl;
  1083.         exit(1);
  1084.     }
  1085.     string line;
  1086.     getline(file,line);
  1087.     while(getline(file,line)){
  1088.         const char s[2]="|";
  1089.         char* token;
  1090.         Bethistory temp;
  1091.         //converting string to char*
  1092.         char *str = new char[line.length() + 1];
  1093.         strcpy(str, line.c_str());
  1094.         //
  1095.         token = strtok(str,s);
  1096.         temp.bet_id = atoi(token);
  1097.         token = strtok(NULL, s);
  1098.         temp.user_id = atoi(token);
  1099.         token = strtok(NULL, s);
  1100.         temp.node_id = token;
  1101.         token = strtok(NULL, s);
  1102.         temp.stake = atof(token);
  1103.         token = strtok(NULL, s);
  1104.         temp.result = token[0];
  1105.         token = strtok(NULL, s);
  1106.         //
  1107.         bethistory.push_back(new Bethistory());
  1108.         int i = bethistory.size()-1;
  1109.         //
  1110.         bethistory[i]->bet_id = temp.bet_id;
  1111.         bethistory[i]->user_id = temp.user_id;
  1112.         bethistory[i]->node_id.assign(temp.node_id);
  1113.         bethistory[i]->stake = temp.stake;
  1114.         bethistory[i]->result= temp.result;
  1115.         //
  1116.     }
  1117.     file.close();
  1118. }
  1119.  
  1120. void register_user(vector<Users*> &User){
  1121.     int redo;
  1122.     string name;
  1123.     do{
  1124.         redo=0;
  1125.         cout << "username: ";
  1126.         getline (cin, name);
  1127.         for(int i=0;i<User.size();i++){
  1128.             if(name.compare(User[i]->ID.username) == 0){
  1129.                 cout << "This name already exists please type a different one!" << endl;
  1130.                 redo = 1;
  1131.             }
  1132.         }
  1133.     }while(redo);
  1134. //
  1135.     User.push_back(new Punter);
  1136.     int i = User.size()-1;
  1137.     User[i]->ID.user_id = i+1;
  1138.     User[i]->ID.username.assign(name);
  1139.     User[i]->ID.fullname.assign(name); // this could be changed later in the account settings
  1140.     User[i]->ID.type = 1;
  1141.     User[i]->ID.status.assign("A");
  1142.     User[i]->ID.balance = 0.0;
  1143.     User[i]->ID.freebets.push_back(0.0); //
  1144.     do{
  1145.         redo=0;
  1146.         cout << "password: ";
  1147.         string pass1;
  1148.         getline (cin,pass1);
  1149.         cout << "Please retype your password:" ;
  1150.         string pass2;
  1151.         getline (cin,pass2);
  1152.         if (pass1.compare(pass2) != 0){
  1153.             cout << "You should insert the same password twice" << endl;
  1154.             redo=1;
  1155.         }
  1156.         else{
  1157.             User[i]->ID.password.assign(pass1);
  1158.         }
  1159.     }while(redo);
  1160.     write_users(User);  //arxeio susthmatos save
  1161.     //epishs 8eloume na exoume eisodo sto susthma
  1162. }
  1163.  
  1164. void login_user(){
  1165.  
  1166. }
  1167.  
  1168. int main(int argc, char** argv) {
  1169.     vector<Users*> User;
  1170.     vector<History*> history;
  1171.     vector<Bethistory*> bethistory;
  1172.     read_users(User);
  1173.     if (argc == 1){
  1174.         //xwris parameters
  1175.         login_user();
  1176.     }
  1177.     else if(argc == 2){
  1178.         //mia parametros
  1179.         string parameter(argv[1]);
  1180.         capitalize(&parameter);
  1181.         if(strcmp(parameter.c_str(),"-r") == 0){
  1182.             //register a user
  1183.             register_user(User);
  1184.         }
  1185.     }
  1186.     /*
  1187.     Users mark;
  1188.     mark.ID.user_id = 1;
  1189.     mark.ID.username = "Nikos";
  1190.     mark.ID.fullname = "Tsougkrana";
  1191.     mark.ID.password = "12345656";
  1192.     mark.ID.type = 3;
  1193.     mark.ID.status ="SMTH";
  1194.     mark.ID.balance = 1234.556;
  1195.     mark.ID.freebets.push_back(10.5 );
  1196.     mark.ID.freebets.push_back(70.5 );
  1197.     mark.ID.freebets.push_back(120.5 );
  1198.     User.push_back(&mark);
  1199.      
  1200.     Users nik;
  1201.     nik.ID.user_id = 2;
  1202.     nik.ID.username = "kombra";
  1203.     nik.ID.fullname = "anakloda";
  1204.     nik.ID.password = ";;45656";
  1205.     nik.ID.type = 1;
  1206.     nik.ID.status ="SMiTH";
  1207.     nik.ID.balance = 4.3;
  1208.     nik.ID.freebets.push_back(20.5 );
  1209.     nik.ID.freebets.push_back(7.5 );
  1210.     nik.ID.freebets.push_back(10.5 );
  1211.     User.push_back(&nik);
  1212.    
  1213.     write_users(User);
  1214.    
  1215.     bethistoryfun(bethistory,10,1,"1.1.2.3",10.5,'W');
  1216.     write_bet(bethistory);
  1217.     */
  1218.    
  1219.     write_users(User);    
  1220.     return 0;
  1221. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement