Advertisement
Guest User

Untitled

a guest
Nov 28th, 2014
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.00 KB | None | 0 0
  1.  
  2. class App {
  3.  
  4. public:
  5. Magazyn m;
  6. Koszyk k;
  7.  
  8. bool akcja(string s)
  9. {
  10. if(s.find("koszyk") != string::npos)
  11. {
  12. this -> handle_koszyk(s);
  13. } else if(s.find("magazyn") != string::npos) {
  14. this -> handle_magazyn(s);
  15. } else if(s.find("produkt") != string::npos) {
  16. this -> handle_produkt(s);
  17. } else if(s.find("info") != string::npos) {
  18. this -> print_info();
  19. } else {
  20. cout << "Niepoprawna akcja: \"" << s << "\"" << endl;
  21. this -> print_info();
  22. }
  23. return true;
  24. }
  25.  
  26. void print_info()
  27. {
  28. cout << "Dostepne akcje: " << endl;
  29. cout << "koszyk dodaj nr_prod <- dodaje produkt do koszyka" << endl;
  30. cout << "koszyk usun nr_prod <- usuwa produkt z koszyka" << endl;
  31. cout << "magazyn <- pokazuje magazyn" << endl;
  32. cout << "info <- pokazuje te informacje" << endl;
  33. }
  34. void handle_magazyn(string s)
  35. {
  36. this->m.listuj();
  37. }
  38.  
  39. void handle_koszyk(string s)
  40. {
  41. if(s.find("dodaj") != string::npos)
  42. {
  43. int product_id = 0;
  44. int quantity = 0;
  45. int pos = (int)s.find("dodaj");
  46. if(s.length() > pos + 6)
  47. {
  48. product_id = strtoint(s.substr(pos+6));
  49. }
  50.  
  51. if(!product_id)
  52. {
  53. cout << "Podaj numer produktu (0 - wraca do głównego menu): ";
  54. cin >> product_id;
  55. }
  56.  
  57. Kontener *kontener;
  58.  
  59. if(product_id < 1) {
  60. return;
  61.  
  62. } else if(product_id > this->m.getKontenery()->size())
  63. {
  64. cout << "Nie ma produktu o takim numerze w magazynie." << endl;
  65. } else {
  66.  
  67. kontener = this->m.getContainer(product_id);
  68. cout << "---- Wybrany produkt: ---- " << endl;
  69. kontener->towar->podsumuj();
  70. cout << "---- Dostepna liczba sztuk: " << kontener->getIlosc() << " ---- " << endl;
  71.  
  72. }
  73.  
  74.  
  75. cout << "Podaj liczbe sztuk do zamowenia: ";
  76. cin >> quantity;
  77.  
  78. // this->k->dodajTowar(kontener->towar, quantity);
  79. // kontener->decreaseQuantity(quantity);
  80. this->k += kontener->towar;
  81.  
  82. cout << "Koszyk: pomyslnie dodano " << kontener->towar->getNazwa() << " w ilosci sztuk: " << quantity << endl;
  83. cout << "Pozostalo " << kontener->getIlosc() << " tego produktu. " << endl;
  84. cout << "Aby wyswietlic zawartosc koszyka, wpisz polecenie 'koszyk'. " << endl;
  85.  
  86. } else if(s.find("usun") != string::npos) {
  87.  
  88. int index;
  89.  
  90. int pos = (int)s.find("usun");
  91. if(s.length() > pos + 4)
  92. {
  93. index = strtoint(s.substr(pos+4));
  94. }
  95.  
  96. if(!index)
  97. {
  98. cout << "Ktory element chcesz usunac? Wprowdz: ";
  99. cin >> index;
  100. }
  101.  
  102. Towar *t = this->k.getTowar(index);
  103. if(t != NULL)
  104. {
  105. Kontener *kontener = this->m.getContainer(t);
  106. if(kontener != NULL)
  107. {
  108. kontener->increaseQuantity(1);
  109. this->k.usunTowar(index);
  110. } else {
  111. cout << "BLAD: nie udalo sie odnalezc tego produktu w magazynie!" << endl;
  112. exit(0);
  113. }
  114. } else {
  115. cout << "Nie ma towaru o takim indeksie w koszyku." << endl;
  116. }
  117.  
  118. cout << k;
  119.  
  120. } else {
  121. cout << k;
  122. }
  123. }
  124.  
  125. void handle_produkt(string s)
  126. {
  127. int product_id = 0;
  128. int pos = (int)s.find("produkt");
  129. if(s.length() > pos + 7)
  130. {
  131. product_id = strtoint(s.substr(pos+7));
  132. }
  133.  
  134. if(!product_id)
  135. {
  136. cout << "Podaj numer produktu do wyswietlenia szczegolow (1-" << this->m.getKontenery()->size() << "): ";
  137. cin >> product_id;
  138. }
  139.  
  140. if(this->m.containerExists(product_id))
  141. {
  142. this->m.getContainer(product_id)->towar->podsumuj();
  143. } else {
  144. cout << "Nie ma produktu z takim indeksem w magazynie." << endl;
  145. }
  146.  
  147. }
  148. };
  149.  
  150.  
  151. int main(int argc, const char * argv[]) {
  152.  
  153. App app;
  154. app.m = createMagazyn();
  155.  
  156. bool working = true;
  157.  
  158. while(working)
  159. {
  160. cout << "Wykonaj akcje: ";
  161. string action = "";
  162. getline(cin, action);
  163.  
  164. if(action.length())
  165. {
  166. working = app.akcja(action);
  167. }
  168.  
  169. }
  170.  
  171. cout << "Dziekujemy za zakupy." << endl;
  172.  
  173. return 0;
  174. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement