Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- #include <vector>
- #include <array>
- class DynamicArray
- {
- public:
- DynamicArray(int size)
- {
- }
- int *arr;
- };
- class Account
- {
- public:
- Account(const std::string& u,const std::string& p)
- {
- init(u,p);
- }
- Account(const Account& acc)
- {
- init(acc.getUsername(),acc.getPassword());
- }
- bool operator==(const Account& acc) const
- {
- return(username==acc.getUsername()&&password==acc.getPassword());
- }
- void init(const std::string& u,const std::string& p)
- {
- setUsername(u);
- setPassword(p);
- }
- const std::string& getUsername() const
- {
- return username;
- }
- const std::string& getPassword() const
- {
- return password;
- }
- void setUsername(const std::string& u)
- {
- username = u;
- }
- void setPassword(const std::string& p)
- {
- password = p;
- }
- private:
- std::string username;
- std::string password;
- };
- class AccountManager
- {
- public:
- static bool registerAccount(const Account& newAccount)
- {
- if(getIndexOfExistingAccount(newAccount.getUsername())!=-1)
- return false;
- accounts.push_back(Account(newAccount));
- return true;
- }
- static std::vector<Account>& getAccounts()
- {
- return accounts;
- }
- static int getIndexOfExistingAccount(const std::string& username)
- {
- for ( int a = 0; a < accounts.size(); a++ )
- {
- if(accounts[a].getUsername() == username)
- {
- return a;
- }
- }
- return -1;
- }
- static bool isLoginSucesful(const Account& acc)
- {
- int index = getIndexOfExistingAccount(acc.getUsername());
- if(index > -1)
- {
- return (accounts[index]==acc);
- }
- return false;
- }
- private:
- static std::vector<Account> accounts;
- };
- class Item
- {
- public:
- enum category { DRINKS , FOOD };
- Item(const std::string& n, double p,category cat)
- {
- init(n,p,cat);
- }
- Item(const Item& item)
- {
- init(item.getName(),item.getPrice(),item.getCategory());
- }
- void init(const std::string& n, double p,category cat)
- {
- setName(n);
- setPrice(p);
- setCategory(cat);
- }
- const std::string& getName() const
- {
- return name;
- }
- double getPrice() const
- {
- return price;
- }
- category getCategory() const
- {
- return _category;
- }
- void setName(const std::string& n)
- {
- name = n;
- }
- void setPrice(double p)
- {
- price = p;
- }
- void setCategory(category cat)
- {
- _category = cat;
- }
- private:
- std::string name;
- double price;
- category _category;
- };
- class Stock
- {
- public:
- Stock(const Item& i,int s)
- :item(i),_stock(s) {}
- const Item& getItem() const
- {
- return item;
- }
- int getStock() const
- {
- return _stock;
- }
- private:
- Item item;
- int _stock;
- };
- std::vector<Account> AccountManager::accounts = std::vector<Account>();
- Account getInputtedAccount()
- {
- std::string username="";
- std::string password="";
- std::cout << "Enter your username: ";
- std::cin >> username;
- std::cout << "Enter your password: ";
- std::cin >> password;
- return Account(username,password);
- }
- void reg()
- {
- while(true)
- {
- Account inputtedAccount = getInputtedAccount();
- if(!AccountManager::registerAccount(inputtedAccount))
- std::cout << "Username: " << inputtedAccount.getUsername() << " already exist! try another name." << std::endl;
- else
- {
- std::cout << "Sucesfully registered!" << std::endl;
- break;
- }
- }
- }
- void login()
- {
- while(true)
- {
- Account inputtedAccount = getInputtedAccount();
- if(!AccountManager::isLoginSucesful(inputtedAccount))
- std::cout << "Wrong username or password try again!" << std::endl;
- else
- {
- std::cout << "Succesfully login!" << std::endl;
- break;
- }
- }
- }
- int getInputtedOption(int min,int max)
- {
- int option = 0;
- while(true)
- {
- std::cin >> option;
- if(option<min||option>max)
- {
- std::cout << "Pick with the showed number only!" << std::endl;
- }
- else
- break;
- }
- }
- int main(int argc, char** argv)
- {
- std::cout << "Welcome to my case study" << std::endl;
- Stock stock[5] =
- {
- {{Item("Sphaggetti",100.00,Item::category::FOOD)},5},
- {{Item("Carbonara",278.75,Item::category::FOOD)},5},
- {{Item("Macaroni",35.00,Item::category::FOOD)},5},
- {{Item("Bottled Water",35.00,Item::category::DRINKS)},5},
- {{Item("Iced tea",49.69,Item::category::DRINKS)},5}
- };
- std::vector<Item> orderedItem = std::vector<Item>();
- while(true)
- {
- std::cout << "[1]Login" << std::endl;
- std::cout << "[2]Register" << std::endl;
- int option = getInputtedOption(1,2);
- if(option==1)
- {
- login();
- double totalPrice = 0;
- while(true)
- {
- std::cout << "Choose category here: " << std::endl;
- std::cout << "[1]Food" << std::endl;
- std::cout << "[2]Drinks" << std::endl;
- Item::category selectedCategory;
- if(getInputtedOption(1,2) == 1)
- selectedCategory = Item::FOOD;
- else
- selectedCategory = Item::DRINKS;
- int index = 1;
- for ( int a = 0; a < 5; a++ )
- {
- if (stock[a].getItem().getCategory()==selectedCategory)
- {
- std::cout << "[" << index++ << "] " << stock[a].getItem().getName() << " - " << stock[a].getItem().getPrice() << std::endl;
- }
- }
- option = getInputtedOption(1,5);
- totalPrice += stock[option-1].getItem().getPrice();
- orderedItem.push_back(Item(stock[option-1].getItem()));
- std::cout << "Do you want to add? [1]Yes [2]No" << std::endl;
- if(getInputtedOption(1,2) == 2)
- {
- std::cout << "You ordered: " << std::endl;
- for ( std::vector<Item>::const_iterator iter = orderedItem.begin(); iter != orderedItem.end(); iter++ )
- {
- std::cout << iter->getName() << " - " << iter->getPrice() << std::endl;
- }
- std::cout << "Total price: " << totalPrice << std::endl;
- orderedItem.clear();
- break;
- }
- }
- }
- else if(option==2)
- {
- reg();
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement