No older revisions available
No older revisions available
// 캬ì~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;
}