Advertisement
Guest User

Untitled

a guest
Oct 5th, 2016
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.02 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <vector>
  4. #include <array>
  5.  
  6. class DynamicArray
  7. {
  8. public:
  9.     DynamicArray(int size)
  10.     {
  11.        
  12.     }
  13.    
  14.     int *arr;
  15. };
  16. class Account
  17. {
  18.     public:
  19.         Account(const std::string& u,const std::string& p)
  20.         {
  21.             init(u,p);
  22.         }
  23.         Account(const Account& acc)
  24.         {
  25.             init(acc.getUsername(),acc.getPassword());
  26.         }
  27.         bool operator==(const Account& acc) const
  28.         {
  29.             return(username==acc.getUsername()&&password==acc.getPassword());
  30.         }
  31.         void init(const std::string& u,const std::string& p)
  32.         {
  33.             setUsername(u);
  34.             setPassword(p);
  35.         }
  36.         const std::string& getUsername() const
  37.         {
  38.             return username;
  39.         }
  40.         const std::string& getPassword() const
  41.         {
  42.             return password;
  43.         }
  44.         void setUsername(const std::string& u)
  45.         {
  46.             username = u;
  47.         }
  48.         void setPassword(const std::string& p)
  49.         {
  50.             password = p;
  51.         }
  52.  
  53.     private:
  54.         std::string username;
  55.         std::string password;
  56. };
  57.  
  58. class AccountManager
  59. {
  60.     public:
  61.         static bool registerAccount(const Account& newAccount)
  62.         {
  63.             if(getIndexOfExistingAccount(newAccount.getUsername())!=-1)
  64.                 return false;
  65.             accounts.push_back(Account(newAccount));
  66.             return true;
  67.         }
  68.         static std::vector<Account>& getAccounts()
  69.         {
  70.             return accounts;
  71.         }
  72.         static int getIndexOfExistingAccount(const std::string& username)
  73.         {
  74.             for ( int a = 0; a < accounts.size(); a++ )
  75.             {
  76.                     if(accounts[a].getUsername() == username)
  77.                     {
  78.                         return a;
  79.                     }
  80.             }
  81.             return -1;
  82.         }
  83.         static bool isLoginSucesful(const Account& acc)
  84.         {
  85.             int index = getIndexOfExistingAccount(acc.getUsername());
  86.             if(index > -1)
  87.             {
  88.                 return (accounts[index]==acc);
  89.             }
  90.             return false;
  91.         }
  92.     private:
  93.         static std::vector<Account> accounts;
  94.  
  95. };
  96.  
  97. class Item
  98. {
  99. public:
  100.     enum category { DRINKS , FOOD };
  101.     Item(const std::string& n, double p,category cat)
  102.     {
  103.         init(n,p,cat);
  104.     }
  105.     Item(const Item& item)
  106.     {
  107.         init(item.getName(),item.getPrice(),item.getCategory());
  108.     }
  109.     void init(const std::string& n, double p,category cat)
  110.     {
  111.         setName(n);
  112.         setPrice(p);
  113.         setCategory(cat);
  114.     }
  115.     const std::string& getName() const
  116.     {
  117.         return name;
  118.     }
  119.     double getPrice() const
  120.     {
  121.         return price;
  122.     }
  123.     category getCategory() const
  124.     {
  125.         return _category;
  126.     }
  127.     void setName(const std::string& n)
  128.     {
  129.         name = n;
  130.     }
  131.     void setPrice(double p)
  132.     {
  133.         price = p;
  134.     }
  135.     void setCategory(category cat)
  136.     {
  137.         _category = cat;
  138.     }
  139. private:
  140.     std::string name;
  141.     double price;
  142.     category _category;
  143. };
  144.  
  145. class Stock
  146. {
  147. public:
  148.     Stock(const Item& i,int s)
  149.         :item(i),_stock(s) {}
  150.     const Item& getItem() const
  151.     {
  152.         return item;
  153.     }
  154.     int getStock() const
  155.     {
  156.         return _stock;
  157.     }
  158. private:
  159.     Item item;
  160.     int _stock;
  161. };
  162. std::vector<Account> AccountManager::accounts = std::vector<Account>();
  163.  
  164.  
  165.  
  166. Account getInputtedAccount()
  167. {
  168.  
  169.     std::string username="";
  170.     std::string password="";
  171.  
  172.     std::cout << "Enter your username: ";
  173.     std::cin >> username;
  174.     std::cout << "Enter your password: ";
  175.     std::cin >> password;
  176.  
  177.     return Account(username,password);
  178. }
  179.  
  180. void reg()
  181. {
  182.     while(true)
  183.     {
  184.         Account inputtedAccount = getInputtedAccount();
  185.         if(!AccountManager::registerAccount(inputtedAccount))
  186.             std::cout << "Username: " << inputtedAccount.getUsername() << " already exist! try another name." << std::endl;
  187.         else
  188.         {
  189.             std::cout << "Sucesfully registered!" << std::endl;
  190.             break;
  191.         }
  192.     }
  193. }
  194.  
  195. void login()
  196. {
  197.     while(true)
  198.     {
  199.         Account inputtedAccount = getInputtedAccount();
  200.         if(!AccountManager::isLoginSucesful(inputtedAccount))
  201.             std::cout << "Wrong username or password try again!" << std::endl;
  202.         else
  203.         {
  204.             std::cout << "Succesfully login!" << std::endl;
  205.             break;
  206.         }
  207.     }
  208. }
  209.  
  210. int getInputtedOption(int min,int max)
  211. {
  212.     int option = 0;
  213.     while(true)
  214.     {
  215.         std::cin >> option;
  216.         if(option<min||option>max)
  217.         {
  218.             std::cout << "Pick with the showed number only!" << std::endl;
  219.         }
  220.         else
  221.             break;
  222.     }
  223. }
  224.  
  225.  
  226. int main(int argc, char** argv)
  227. {
  228.  
  229.     std::cout << "Welcome to my case study" << std::endl;
  230.  
  231.  
  232.     Stock stock[5] =
  233.     {
  234.         {{Item("Sphaggetti",100.00,Item::category::FOOD)},5},
  235.         {{Item("Carbonara",278.75,Item::category::FOOD)},5},
  236.         {{Item("Macaroni",35.00,Item::category::FOOD)},5},
  237.         {{Item("Bottled Water",35.00,Item::category::DRINKS)},5},
  238.         {{Item("Iced tea",49.69,Item::category::DRINKS)},5}
  239.     };
  240.  
  241.     std::vector<Item> orderedItem = std::vector<Item>();
  242.     while(true)
  243.     {
  244.  
  245.         std::cout << "[1]Login" << std::endl;
  246.         std::cout << "[2]Register" << std::endl;
  247.  
  248.  
  249.         int option = getInputtedOption(1,2);
  250.  
  251.         if(option==1)
  252.         {
  253.             login();
  254.             double totalPrice = 0;
  255.             while(true)
  256.             {
  257.                 std::cout << "Choose category here: " << std::endl;
  258.                 std::cout << "[1]Food" << std::endl;
  259.                 std::cout << "[2]Drinks" << std::endl;
  260.                 Item::category selectedCategory;
  261.                 if(getInputtedOption(1,2) == 1)
  262.                     selectedCategory = Item::FOOD;
  263.                 else
  264.                     selectedCategory = Item::DRINKS;
  265.  
  266.                 int index = 1;
  267.                 for ( int a = 0; a < 5; a++ )
  268.                 {
  269.                     if (stock[a].getItem().getCategory()==selectedCategory)
  270.                     {
  271.                         std::cout << "[" << index++ << "] " << stock[a].getItem().getName() << " - " << stock[a].getItem().getPrice() << std::endl;
  272.                     }
  273.                 }
  274.  
  275.                 option = getInputtedOption(1,5);
  276.  
  277.                 totalPrice += stock[option-1].getItem().getPrice();
  278.                 orderedItem.push_back(Item(stock[option-1].getItem()));
  279.                 std::cout << "Do you want to add? [1]Yes [2]No" << std::endl;
  280.            
  281.                 if(getInputtedOption(1,2) == 2)
  282.                 {
  283.                     std::cout << "You ordered: " << std::endl;
  284.                     for ( std::vector<Item>::const_iterator iter = orderedItem.begin(); iter != orderedItem.end(); iter++ )
  285.                     {
  286.                         std::cout << iter->getName() << " - " << iter->getPrice() << std::endl;
  287.                     }
  288.                     std::cout << "Total price: " << totalPrice << std::endl;
  289.                     orderedItem.clear();
  290.                     break;
  291.                 }
  292.             }
  293.  
  294.  
  295.         }
  296.  
  297.         else if(option==2)
  298.         {
  299.             reg();
  300.         }
  301.  
  302.     }
  303.  
  304.     return 0;
  305. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement