Advertisement
EstEsca

Untitled

Oct 4th, 2015
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 13.35 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <fstream>
  4. #include <stdlib.h>
  5. #include <map>
  6. #include <unistd.h>
  7. #define SPACE 4
  8.  
  9. using namespace std;
  10.  
  11. //REFERENCES
  12. //it = myMap.find(3);
  13. //map1.find( 10 );
  14. //bool empty() const; true if the container size is 0, false otherwise.
  15.  
  16. void Space(void){
  17. for(int i=0;i<SPACE;i++)
  18. cout<<" ";
  19. }
  20.  
  21. void space(void){
  22. for(int i=0;i<SPACE-1;i++)
  23. cout<<" ";
  24. }
  25.  
  26. class Commodity {
  27. private :
  28. int id_;
  29. std::string name_;
  30. int price_;
  31. public :
  32. Commodity() {
  33. id_ = 0;
  34. name_ = std::string("");
  35. price_ = 0;
  36. }
  37. Commodity(int id, std::string name, int price) {
  38. id_ = id;
  39. name_ = name ;
  40. price_ = price;
  41. }
  42. int printId(void){
  43. return id_;
  44. }
  45. std::string printName(void){
  46. return name_;
  47. }
  48. int printPrice(void){
  49. return price_;
  50. }
  51. // ~Commodity(){}
  52.  
  53. };
  54.  
  55. class Pos {
  56. private:
  57. std::map<int, int> sList; //save code and amount
  58. std::map<int, Commodity> List;
  59. int tSum, tAmt;
  60.  
  61. public:
  62. Pos() {
  63. std::map<int, int> sList;
  64. std::map<int, Commodity> List;
  65. int tSum, tAmt;
  66. tSum=tAmt=0;
  67. }
  68. void addsList(int ID, int amount){
  69. map< int, int >::iterator itersList;
  70. itersList=sList.find(ID);
  71. if(itersList!=sList.end()){
  72. itersList->second=itersList->second+amount;
  73. }
  74. else{
  75. sList[ID]=amount;
  76. }
  77. }
  78.  
  79. void addList(Commodity co){
  80. int Id=co.printId();
  81. List[Id] = co;
  82. }
  83.  
  84. void printsList(void){
  85. map< int, int >::iterator itersList;
  86. map< int, Commodity >::iterator iterList;
  87.  
  88. int itemSum;
  89.  
  90. cout.fill(' ');
  91. cout<<endl<<"===================================================================="<<endl;
  92. cout<<"|| Product ID\t";cout.width(25);cout<<left<<"Name"; cout.width(10);cout<<left<<"Price";
  93. cout.width(10);cout<<left<<"Amount";cout.width(5);cout<<left<<"Sum";cout<<"||";
  94. cout<<endl<<"|| -------------------------------------------------------------- ||"<<endl;
  95. tSum=tAmt=0;
  96. for(itersList = sList.begin(); itersList != sList.end(); ++itersList){
  97. itemSum=0;
  98. //initialize for each loop(each item)
  99.  
  100. iterList=List.find(itersList->first);
  101. Commodity co = iterList->second;
  102. itemSum = co.printPrice() * itersList->second;
  103. //look ID from sellingList for CommodityList, and get its price.
  104.  
  105. tAmt=tAmt+itersList->second;
  106. tSum=tSum+itemSum;
  107.  
  108. cout<<"|| "<<co.printId()<<"\t";
  109. cout.width(25); cout<<left<<co.printName();
  110. cout.width(10); cout<<left<<co.printPrice();
  111. cout.width(10); cout<<left<<itersList->second;
  112. cout.width(5); cout<<left<<itemSum<<"||"<<endl;
  113. }
  114. cout<<"--------------------------------------------------------------------"<<endl;
  115. cout.width(51);cout<<"|| Total";
  116. cout.width(10);cout<<left<<tAmt;
  117. cout.width(5);cout<<left<<tSum;
  118. cout<<"||"<<endl;
  119. cout<<"===================================================================="<<endl;
  120.  
  121. return ;
  122. }
  123.  
  124. void saveFile(std::string filename) {
  125. std::ofstream output( filename.c_str(), std::ios::app);
  126. // output<<id_<<" "<<name_<<" "<<price_<<std::endl;
  127. map< int, int >::iterator itersList;
  128. map< int, Commodity >::iterator iterList;
  129.  
  130. int itemSum;
  131. tSum=tAmt=0;
  132. output<<"ID"<<" "<<"Name"<<" "<<"Price"<<" "<<"Amount"<<" "<<"Total"<<std::endl;
  133. for(itersList = sList.begin(); itersList != sList.end(); ++itersList){
  134. itemSum=0;
  135. //initialize for each loop(each item)
  136.  
  137. iterList=List.find(itersList->first);
  138. Commodity co = iterList->second;
  139. itemSum = co.printPrice() * itersList->second;
  140. //look ID from sellingList for CommodityList, and get its price.
  141.  
  142. tAmt=tAmt+itersList->second;
  143. tSum=tSum+itemSum;
  144.  
  145. output<<co.printId()<<" "<<co.printName()<<" "<<co.printPrice()<<" "<<itersList->second<<" "<<itemSum<<std::endl;
  146. }
  147. output<<tAmt<<" "<<tSum<<std::endl;
  148.  
  149. output.close();
  150. return ;
  151. }
  152.  
  153. void printList(void){
  154. map< int, Commodity >::iterator iterList;
  155.  
  156. cout.fill(' ');
  157. cout<<endl<<"================================================="<<endl;
  158. cout<<"|| Product ID\t"; cout.width(25);cout<<left<<"Name"; cout.width(5);cout<<left<<"Price"<< " ||"<<endl;
  159. cout<<endl<<"|| ------------------------------------------- ||"<<endl;
  160. for(iterList = List.begin(); iterList != List.end(); ++iterList){
  161. Commodity co = iterList->second;
  162. cout<<"|| "<<co.printId()<<"\t";
  163. cout.width(25); cout<<left<<co.printName();
  164. cout.width(5); cout<<co.printPrice()<<" ||"<<endl;
  165. }
  166. cout<<"================================================="<<endl;
  167. }
  168.  
  169. void Init(void){
  170. sList.clear();
  171. tSum=0;
  172. tAmt=0;
  173. }
  174.  
  175. int chkEmpty(void){
  176. return sList.empty();
  177. }
  178.  
  179. int chkID(int id){
  180. map< int, Commodity >::iterator iterList;
  181. iterList=List.find(id);
  182. if(iterList!=List.end()){
  183. return 1;}
  184. else
  185. {return 0;}
  186. }
  187.  
  188. int chksID(int id){
  189. map< int, int >::iterator itersList;
  190. itersList=sList.find(id);
  191. if(itersList!=sList.end()){
  192. return 1;}
  193. else
  194. {return 0;}
  195. }
  196.  
  197. string id2Product(int id){
  198. map< int, Commodity >::iterator iterList;
  199. iterList=List.find(id);
  200. Commodity co = iterList->second;
  201.  
  202. return co.printName();
  203. }
  204.  
  205. int chkAmt(int id){
  206. map< int, int >::iterator itersList;
  207. itersList=sList.find(id);
  208. return itersList->second;
  209. }
  210.  
  211. void eraseID(int id){
  212. sList.erase(id);
  213. }
  214.  
  215. int printTSum(void){
  216. return tSum;
  217. }
  218.  
  219. };
  220.  
  221. int main()
  222. {
  223. int co_id, co_price;
  224. char yn;
  225. std::string co_name;
  226. int input, id, amt, chk; amt = chk = id = 1;
  227. Pos pos = Pos();
  228. //Declaration
  229.  
  230. std::ifstream commodity("commodity.txt");
  231. while( commodity>>co_id>>co_name>>co_price) {
  232. Commodity co( co_id, co_name, co_price);
  233. pos.addList(co);
  234. }
  235. //Initialization
  236.  
  237. system("clear");
  238. pos.printList();
  239. while(1){
  240. chk=0;
  241. {
  242. cout<<"Please input 1 to choose, ";
  243. if(!pos.chkEmpty())cout<<"2 to buy, 3 to cancel, 4 to cancel partly, ";
  244. cout<<"and 5 to terminate POS"<<endl;
  245. }
  246. cin.clear(); cin>>input;
  247. if(input>5 || input<1){
  248. system("clear");
  249. pos.printList();
  250. if(!pos.chkEmpty())pos.printsList();
  251. cout<<"Please input integer in 1~5!"<<endl;
  252. }
  253. else{
  254. if((input==2 || input==3 || input==4)&&pos.chkEmpty()){
  255. system("clear");
  256. pos.printList();
  257. }
  258. else if(input==2){
  259. system("clear");
  260. pos.printList();
  261. pos.printsList();
  262. cout<<"(to go back, input 0)Input the amount of cash: ";
  263. while(chk==0){
  264. cin.clear(); cin>>amt;
  265. if(amt==0){
  266. break;
  267. }
  268. else if(amt<0){
  269. system("clear");
  270. pos.printsList();
  271. cout<<"(to go back, input 0)Please input valid amount!";}
  272.  
  273. else if(amt>0 && amt<pos.printTSum()){
  274. system("clear");
  275. pos.printsList();
  276. cout<<"(to go back, input 0)You have lack of money, you need at least "<<pos.printTSum()-amt<<" of more money";
  277. }
  278. else{
  279. system("clear");
  280. pos.printsList();
  281. cout.width(60); cout<<right<<"Your charge will be: "<<amt-pos.printTSum()<<endl;
  282. cout<<"Are you going to purchase those above?";
  283. cin.clear(); cin>>yn;
  284. while(1){
  285. if(toupper(yn)!='Y'&&toupper(yn)!='N'){}
  286. else if(toupper(yn)=='Y'){
  287. system("clear");
  288. pos.printsList();
  289. cout<<"Recieved "<<amt<<" of money, and Here is your charge of: "<<amt-pos.printTSum()<<endl<<endl;
  290. pos.saveFile("History.txt");
  291.  
  292. cout<<"Purchase Successfully Recorded"<<endl;
  293. cout<<"Input any number to proceed...";
  294. cin.clear(); cin>>input;
  295. pos.Init();
  296. system("clear");
  297. pos.printList();
  298.  
  299. chk=1;
  300. break;
  301. }
  302. else if(toupper(yn)=='N'){
  303. chk=1;
  304. break;
  305. }
  306. }
  307. }
  308. }
  309. }
  310. else if(input==5){
  311. system("clear");
  312. cout<<"Terminating POS..."<<endl<<endl;
  313. return 0;
  314. }
  315. else if(input==4){
  316. system("clear");
  317. pos.printList();
  318. pos.printsList();
  319. cout<<"The one going to be canceled: ";
  320. while(chk==0){
  321. cin.clear(); cin>>id;
  322. if(!pos.chksID(id) && id!=0){
  323. system("clear");
  324. pos.printList();
  325. pos.printsList();
  326. cout<<"(to go back, input 0)Please input valid proudct ID!";
  327. }
  328. else if(id==0){
  329. system("clear");
  330. pos.printList();
  331. pos.printsList();
  332. break;
  333. }
  334. else if(pos.chksID(id)){
  335. cout<<"(to go back, input 0)How many (much) would be canceled? ";
  336. while(1){
  337. cin.clear(); cin>>amt;
  338. if(amt==0){
  339. system("clear");
  340. pos.printList();
  341. pos.printsList();
  342. cout<<"(to go back, input 0)The one going to be canceled: ";
  343. break;
  344. }
  345. if(amt<0){
  346. system("clear");
  347. pos.printList();
  348. pos.printsList();
  349. cout<<"(to go back, input 0)Please input valid amount!";
  350. }
  351. if(pos.chkAmt(id)<amt){
  352. amt=pos.chkAmt(id); //remove all of them if it exceeds max amount
  353. }
  354. while(1){
  355. system("clear");
  356. cout<<"<Order of Now>";
  357. pos.printsList();
  358. if(amt==pos.chkAmt(id)){
  359. pos.eraseID(id);
  360. }
  361. else{
  362. pos.addsList(id,-amt);
  363. }
  364. cout<<endl<<endl<<"<After Changed>";
  365. pos.printsList();
  366. cout<<"Are you sure to cancel "<<amt<<" of "<<pos.id2Product(id)<<"? (y/n)";
  367. cin.clear(); cin>>yn;
  368. if(toupper(yn)!='Y' && toupper(yn)!='N'){}
  369. else if(toupper(yn)=='Y'){
  370. system("clear");
  371. pos.printList();
  372. if(!pos.chkEmpty())pos.printsList();
  373. if(!pos.chksID(id)){
  374. cout<<"All of "<<pos.id2Product(id)<<" is (are) removed"<<endl;
  375. }
  376. else{
  377. cout<<amt<<" of "<<pos.id2Product(id)<<" is (are) removed"<<endl;
  378. }
  379. chk=1;
  380. break;
  381. }
  382. else if(toupper(yn)=='N'){
  383. system("clear");
  384. pos.addsList(id,amt);
  385. pos.printList();
  386. pos.printsList();
  387. chk=1;
  388. break;
  389. }
  390. }
  391. if(amt>0){
  392. break;
  393. }
  394. }
  395. }
  396. }
  397. }
  398. else if(input==3){
  399. while(chk == 0){
  400. system("clear");
  401. pos.printsList();
  402. cout<<"Are you sure to cancel this order? (y/n)";
  403. cin.clear(); cin>>yn;
  404. if(yn!='Y'&&yn!='y'&&yn!='N'&&yn!='n'){}
  405. else if(toupper(yn) == 'Y'){
  406. pos.Init();
  407. system("clear");
  408. pos.printList();
  409. cout<<"Cancel the order"<<endl;
  410. chk=1;
  411. }
  412. else if(toupper(yn) == 'N'){
  413. system("clear");
  414. pos.printList();
  415. pos.printsList();
  416. chk=1;
  417. }
  418. }
  419. }
  420. else if(input==1){
  421. system("clear");
  422. pos.printList();
  423. if(!pos.chkEmpty())pos.printsList();
  424. cout<<"(to go back, input 0)Input Product ID: ";
  425. while(1){
  426. cin.clear(); cin>>id;
  427. if(id==0){
  428. system("clear");
  429. pos.printList();
  430. if(!pos.chkEmpty())pos.printsList();
  431. break;}
  432. if(pos.chkID(id)==0){
  433. cout<<"(to go back, input 0)Please input valid prouduct ID!"<<endl;
  434. }
  435. else{
  436. cout<<"(to go back, input 0)How many(much) are you going to buy "<<pos.id2Product(id)<<"?";
  437. while(1){
  438. cin.clear(); cin>>amt;
  439. if(amt<0){
  440. cout<<"(to go back, input 0)Please input valid amount!"<<endl;}
  441. else if(amt>0){
  442. pos.addsList(id,amt);
  443. system("clear");
  444. pos.printList();
  445. pos.printsList();
  446. cout<<amt<<" of "<<pos.id2Product(id)<<" is registered"<<endl;
  447. // usleep(1000);
  448. break;
  449. }
  450. else if(amt==0){
  451. system("clear");
  452. pos.printList();
  453. if(!pos.chkEmpty())pos.printsList();
  454. cout<<"(to go back, input 0)Input Product ID: ";
  455. break;
  456. }
  457. }
  458. if(amt>0) break;
  459. }
  460. }
  461. }
  462. }
  463. }
  464. return 0;
  465. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement