U E D R , A S I H C RSS

Super Market/인수

// 캬~X~X.. 졸~] ~K~K~U~X~K ~E| .~E|  ~U~Y~P ~Y~@~D~\ ~E~K거리~K~@ ~M ~U봤~]~L. ~L~L~D~\ ~L~S~\~J~T~M 80~T~D~\ ~]~C~A~]~X ~K~\~D~]~D..--;
// ~L~@~U~]~X ~X~H~Y~X리~O~D ~P~\거 ~Y| .. | | 거 ~^~H~]~L ~P~U주길
// DeleteMe ~W~I ~] 미~Y~D~]~X 컬~_~A ~D~@ 기~J~]~D .. ~V~V~L ~U~L| ..

// 문| ~\~]~X ~T~S~\ ~@~D
// ~U~D~^~X~\ ~]~V~@~J~T if/else-if~J~T ~T~S~\ ~Q복~]~]|  ~E~K~H~K. ~]걸 ~V~V~L | ~\거~U|  ~H~X ~^~H~]~D~L~Z~T? Command Pattern? Polymorphism? ~X~]~@ 그~C Table Lookup? --JuNe

                if(command == "help")
                {
                        Helper::showCommand();
                }
                else if(command == "mymoney")
                {
                        cout << sm.getRestMoney() << endl;
                }
                else if(command == "inventory")
                {
                        user.showBuyedGoods();
                }
                else if(command == "ask")
                {
                        string good(&str[token+1], &str[str.size()]);
                        sm.answerCost(good);
                }
                else if(command == "deposit")
                {
                        string m(&str[token+1], &str[str.size()]);
                        int money = StringConvertToInt(m);
                        user.depositMoney(sm, money);
                }
                else if(command == "exit")
                {
                        exit(0);
                }
                else if(command == "menu")
                {
                        sm.showMenu();
                }
                else if(command == "buy")
                {
                        int token2 = getToken(str,2);
                        string good(&str[token+1], &str[token2]);

                        string cnt(&str[token2+1], &str[str.size()]);
                        int count = StringConvertToInt(cnt);

                        user.buyGoods(sm, sm.findGoods(good), count);
                }
                else if(command == "cancel")
                {
                        int token2 = getToken(str,2);
                        string good(&str[token+1], &str[token2]);

                        string cnt(&str[token2+1], &str[str.size()]);
                        int count = StringConvertToInt(cnt);

                        user.cancleGoods(sm, sm.findGoods(good), count);
                }

// ~V~V~L ~U~X면 if/else if ~Q복~]~D ~W~F~U~H~X ~^~H~]~D~L | 민~U~X~K~@ ~O리모~T~X~]~D ~]~Z~U~X기~\ ~V~H~J~K~H~K.(근~M ~]거 ~O리모~T~X~] ~^~B~X?--;)
// map<string, Cmd*> ~]~_~K~]~\~\ string~W~P~J~T 커맨~S~\를,Cmd ~A~^~X~J~J~T HelpCmd~A~^~X~J, Deposit~A~^~X~J ~S~S~]~X ~@모 ~A~^~X~J, ~I ~E|  ~A~^~X~J~S~]~X
// 조~C~A ~A~^~X~J를 ~O~]~D~\ ~D~V줬~J~K~H~K. ~O~Y| ~A ~T~]~T~]~D ~U~X기 ~\~D~U~D~\..--; Parser~C~]~D~U| ~U~L map ~E~L~]~T~W~P~K ~E| ~S~]~D ~D~V주면~D~\ 그 ~E| ~W~P
// ~U~K~U~X~J~T ~A~^~X~J를 ~O~Y| ~A ~U| ~K~U주면~D~\ ~D~W~H~J~K~H~K. ~F~L멸~^~P~W~P~D~\~J~T ~U| ~\~U~X~J~T ~C~O~D ~^~J~@ ~U~J~U~X구~Z~T. ~B~X~D~L~@~\ ~C~A~K~^~H | 민~]~D ~V~H~@~L..--;
// ~]~C~V~W~T ~K~] ~U~H~B~X~X~D~Z~T.



// 그~^~X~D~\ ~B~X~D~L~@~\ | ~P본 ~C

#include <iostream>
#include <string>
#include <vector>
#include <cassert>
#include <map>
using namespace std;

class SuperMarket;
class User;
class Parser;
class Helper;
class Goods;
class Packages;
class Cmd;

class Goods
{
private :
        string _name;
        int _cost;
public :
        Goods(const string& name, int cost) : _name(name), _cost(cost) {}

        const string& getName() const
        {
                return _name;
        }
        int getCost() const
        {
                return _cost;
        }
};

class Packages
{
private :
        Goods _good;
        int _count;
public :
        Packages(const Goods& good, int count) : _good(good), _count(count) {}

        const Goods& getGoods() const
        {
                return _good;
        }

        int getCount() const
        {
                return _count;
        }

        void setCount(int n)
        {
                _count = n;
        }
};

class SuperMarket
{
private :
        int _receivedMoney;
        vector<Goods> _havingGoods;
public :
        SuperMarket()
        {
                Goods g1("candy",1000);
                _havingGoods.push_back(g1);
                Goods g2("diskette",1200);
                _havingGoods.push_back(g2);
                Goods g3("mouse",5000);
                _havingGoods.push_back(g3);
                _receivedMoney = 0;
        }
        void receiveMoney(int money)
        {
                _receivedMoney = money;
                cout << "OK" << endl;
        }
        void showMenu() const
        {
                cout << " ~T~I " << endl;
                for(int i = 0 ; i < _havingGoods.size() ; ++i)
                {
                        cout << _havingGoods[i].getName() << " " << _havingGoods[i].getCost() << endl;
                }
        }
        void sellGoods(const Goods& goods, int count)
        {
                _receivedMoney -= goods.getCost() * count;
        }
        int getRestMoney() const
        {
                return _receivedMoney;
        }
        const vector<Goods>& getGoods() const
        {
                return _havingGoods;
        }
        void answerCost(const string& goodsName) const
        {
                bool isFind = false;
                for(int i = 0 ; i < _havingGoods.size() ; ++i)
                {
                        if(_havingGoods[i].getName() == goodsName)
                        {
                                cout << _havingGoods[i].getCost() << endl;
                                isFind = true;
                                break;
                        }
                }
                if(!isFind)
                {
                        cout << "그~_ 물건 ~W~F~]~L!" << endl;
                }
        }

        const Goods& findGoods(const string& goodsName) const
        {
                for(int i = 0 ; i < _havingGoods.size() ; ++i)
                {
                        if(_havingGoods[i].getName() == goodsName)
                        {
                                return _havingGoods[i];
                        }
                }
        }
};

class User
{
private :
        vector<Packages> _buyedGoods;
public :
        void depositMoney(SuperMarket& sm, int money) const
        {
                sm.receiveMoney(money);
        }
        void showBuyedGoods()
        {
                if(_buyedGoods.size() == 0)
                        cout << "None" << endl;
                else
                {
                        for(int i = 0 ; i < _buyedGoods.size() ; ++i)
                        {
                                cout << _buyedGoods[i].getGoods().getName() << " " << _buyedGoods[i].getCount() << endl;
                        }
                }
        }
        void buyGoods(SuperMarket& sm, const Goods& goods, int count)
        {
                if(sm.getRestMoney() < goods.getCost() * count)
                        cout << "can't buy" << endl;
                else
                {
                        sm.sellGoods(goods, count);
                        Packages p(goods, count);
                        _buyedGoods.push_back(p);
                }
        }
        void cancleGoods(SuperMarket& sm, const Goods& goods, int count)
        {
                bool isFind = false;
                int nth = 0;
                for(int i = 0 ; i < _buyedGoods.size() ; ++i)
                {
                        if(goods.getName() == _buyedGoods[i].getGoods().getName())
                        {
                                isFind = true;
                                nth = i;
                        }
                }
                if(!isFind)
                        cout << "그 물건 ~U~H~C~@~V~Z~T." << endl;
                else
                {
                        if(_buyedGoods[nth].getCount() < count)
                        {
                                cout << "~B ~C보~K ~M~T ~N~] 취~F~L 못~U~K~H~K." << endl;
                        }
                        else if(_buyedGoods[nth].getCount() == count)
                        {
                                if(_buyedGoods[nth].getCount() == 0)
                                {
                                        _buyedGoods.erase(_buyedGoods.begin() + nth);
                                }
                        }
                        else
                        {
                                _buyedGoods[nth].setCount( _buyedGoods[nth].getCount() - count );
                        }
                }
        }
};

class Helper
{
public :
        static void showCommand()
        {
                cout << "* deposit <money> -- ~O~H~]~D money~L~A ~X~H~H~U~\~K. " << endl;
                cout << "* mymoney -- ~B~]~@ ~O~H~]~D 보~W~@~K." << endl;
                cout << "* buy <product> <count> -- product 물건~]~D count ~L~A ~B~K." << endl;
                cout << "* inventory -- ~B 물건~]~X 목~]~]~D 보~W~@~K ." << endl;
                cout << "* cancel <product> <count> -- ~B product 물건~]~D count~\~L~A 취~F~L~U~\~K ." << endl;
                cout << "* ask <product> -- procuct 물건~]~X ~@격~]~D 묻~J~T~K ." << endl;
                cout << "* menu -- 구매 ~@~J~U~\ 물건~]~X 목~]~]~D 보~W~@~K ." << endl;
                cout << "* exit -- ~@~L를 ~B~X~D~K ." << endl;
        }
};

class Cmd
{
public :
        virtual void executeCommand(SuperMarket& sm, User& user, const string& str) = 0;
        static int getToken(const string& str, int nth)
        {
                int ret = str.size();
                int count = 0;
                for(int i = 0 ; i < str.size() ; ++i)
                {
                        if(str[i] == ' ')
                        {
                                ret = i;
                                ++count;
                                if(count == nth)
                                        return ret;
                        }
                }
                return ret;
        }
        int StringConvertToInt(string& str)
        {
                int ret = 0;
                for(int i = 0 ; i < str.length() ; ++i)
                {
                        ret += CharToInt(str[i]) * Power(10,(str.length() - i));
                }
                return ret;
        }

        int CharToInt(char ch)
        {
                return ch - 48;
        }

        int Power(int c, int e)
        {
                int ret = 1;
                for(int i = 0 ; i < e - 1 ; ++i)
                {
                        ret *= c;
                }
                return ret;
        }
};

class HelpCmd : public Cmd
{
public :
        void executeCommand(SuperMarket& sm, User& user, const string& str)
        {
                Helper::showCommand();
        }
};

class MyMoneyCmd : public Cmd
{
public :
        void executeCommand(SuperMarket& sm, User& user, const string& str)
        {
                cout << sm.getRestMoney() << endl;
        }
};

class InventoryCmd : public Cmd
{
public :
        void executeCommand(SuperMarket& sm, User& user, const string& str)
        {
                user.showBuyedGoods();
        }
};

class AskCmd : public Cmd
{
public :
        void executeCommand(SuperMarket& sm, User& user, const string& str)
        {
                int token = getToken(str, 1);
                string good(&str[token+1], &str[str.size()]);
                sm.answerCost(good);
        }
};

class DepositCmd : public Cmd
{
public :
        void executeCommand(SuperMarket& sm, User& user, const string& str)
        {
                int token = getToken(str, 1);
                string m(&str[token+1], &str[str.size()]);
                int money = StringConvertToInt(m);
                user.depositMoney(sm, money);
        }
};

class ExitCmd : public Cmd
{
public :
        void executeCommand(SuperMarket& sm, User& user, const string& str)
        {
                exit(0);
        }
};

class MenuCmd : public Cmd
{
public :
        void executeCommand(SuperMarket& sm, User& user, const string& str)
        {
                sm.showMenu();
        }
};

class BuyCmd : public Cmd
{
public :
        void executeCommand(SuperMarket& sm, User& user, const string& str)
        {
                int token = getToken(str,1);
                int token2 = getToken(str,2);
                string good(&str[token+1], &str[token2]);

                string cnt(&str[token2+1], &str[str.size()]);
                int count = StringConvertToInt(cnt);

                user.buyGoods(sm, sm.findGoods(good), count);
        }
};

class CancleCmd : public Cmd
{
public :
        void executeCommand(SuperMarket& sm, User& user, const string& str)
        {
                int token = getToken(str,1);
                int token2 = getToken(str,2);
                string good(&str[token+1], &str[token2]);

                string cnt(&str[token2+1], &str[str.size()]);
                int count = StringConvertToInt(cnt);

                user.cancleGoods(sm, sm.findGoods(good), count);
        }
};

class Parser
{
private :
        map<string, Cmd*> _tableCmd;
public :
        Parser()
        {
                _tableCmd["help"] = new HelpCmd();
                _tableCmd["mymoney"] = new MyMoneyCmd();
                _tableCmd["inventory"] = new InventoryCmd();
                _tableCmd["ask"] = new AskCmd();
                _tableCmd["deposit"] = new DepositCmd();
                _tableCmd["exit"] = new ExitCmd();
                _tableCmd["menu"] = new MenuCmd();
                _tableCmd["buy"] = new BuyCmd();
                _tableCmd["cancle"] = new CancleCmd();
        }
        void translateCommand(SuperMarket& sm, User& user, const string& str)
        {
                int token = Cmd::getToken(str,1);
                string command(&str[0], &str[token]);

                _tableCmd[command]->executeCommand(sm, user, str);
        }

        virtual ~Parser()
        {
                map<string, Cmd*> :: iterator i;
                for(i = _tableCmd.begin() ; i != _tableCmd.end() ; ++i)
                {
                        delete &(i->second);
                }
        }
};

int main()
{
        User user;
        SuperMarket superMarket;
        Parser parser;

        char command[30];

        while(1)
        {
                cout << endl << ">>> " ;
                cin.getline(command, 30);

                parser.translateCommand(superMarket, user, command);
        }
        return 0;

}




Valid XHTML 1.0! Valid CSS! powered by MoniWiki
last modified 2021-02-07 05:28:09
Processing time 0.0175 sec