Advertisement
Guest User

class

a guest
Jul 12th, 2015
244
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.46 KB | None | 0 0
  1. #include <iostream>
  2. #include <algorithm>
  3. #include "Apothecary.h"
  4. #include "main.h"
  5. #include "potion.h"
  6.  
  7. using namespace std;
  8.  
  9. Apothecary::Apothecary(int oSize, int sSize) : orderSize(oSize), shelfSize(sSize), top(-1), rear(-1), front(-1)
  10. {
  11.     orderArray = new PotionType[orderSize];
  12.     shelfArray = new PotionType[shelfSize];
  13. }
  14.  
  15. Apothecary::~Apothecary()
  16. {
  17.     delete[] orderArray;
  18.     delete[] shelfArray;
  19. }
  20.  
  21. void Apothecary::SetOrderSize(int orderSize)
  22. {
  23.     this->orderSize = orderSize;
  24. }
  25.  
  26. void  Apothecary::SetShelfSize(int shelfSize)
  27. {
  28.     this->shelfSize = shelfSize;
  29. }
  30. void  Apothecary::SetTop(int top)
  31. {
  32.     this->top = top;
  33. }
  34. void  Apothecary::SetRear(int rear)
  35. {
  36.     this->rear = rear;
  37. }
  38. void  Apothecary::SetFront(int front)
  39. {
  40.     this->front = front;
  41. }
  42.  
  43. Apothecary::Apothecary(const Apothecary& Apo)
  44. {
  45.     SetOrderSize(Apo.orderSize);
  46.     SetShelfSize(Apo.shelfSize);
  47.     SetRear(Apo.rear);
  48.     SetFront(Apo.front);
  49.     SetTop(Apo.top);
  50.  
  51.     orderArray = new PotionType[orderSize];
  52.     shelfArray = new PotionType[shelfSize];
  53.  
  54.     for (int i = 0; i < orderSize; ++i)
  55.     {
  56.         orderArray[i] = Apo.orderArray[i];
  57.     }
  58.  
  59.     for (int i = 0; i < shelfSize; ++i)
  60.     {
  61.         shelfArray[i] = Apo.shelfArray[i];
  62.     }
  63. }
  64.  
  65. Apothecary& Apothecary::operator=(Apothecary& Apo)
  66. {
  67.     swap(*this, Apo);
  68.     return *this;
  69. }
  70.  
  71. int Apothecary::GetOrderSize()
  72. {
  73.     return orderSize;
  74. }
  75.  
  76. int Apothecary::GetShelfSize()
  77. {
  78.     return shelfSize;
  79. }
  80.  
  81. bool Apothecary::OrderPotion(PotionType type)
  82. {
  83.     if (IsOrderFull())
  84.         return false;
  85.  
  86.     else if (IsOrderEmpty())
  87.     {
  88.         front = 0;
  89.         rear = 0;
  90.     }
  91.     else
  92.         rear++;
  93.  
  94.     orderArray[rear] = type;
  95.     return true;
  96. }
  97.  
  98. bool Apothecary::IsOrderEmpty() const
  99. {
  100.     return ((rear == -1) && (front == -1));
  101. }
  102.  
  103. bool Apothecary::IsOrderFull() const
  104. {
  105.     return (rear == (orderSize - 1));
  106. }
  107.  
  108. bool Apothecary::IsShelfFull() const
  109. {
  110.     return ((top + 1) == shelfSize);
  111. }
  112.  
  113. bool Apothecary::IsShelfEmpty() const
  114. {
  115.     return (top == -1);
  116. }
  117.  
  118. bool Apothecary::PushShelf(PotionType aType)
  119. {
  120.     if (IsShelfFull())
  121.         return false;
  122.     else
  123.     {
  124.         top++;
  125.         shelfArray[top] = aType;
  126.     }
  127.     return true;
  128. }
  129.  
  130. PotionType Apothecary::PeekOrder() const
  131. {
  132.     if (!IsOrderEmpty())
  133.         return orderArray[front];
  134.     else
  135.         return UNKNOWN;
  136. }
  137.  
  138. void Apothecary::DequeueOrder()
  139. {
  140.     if (IsOrderEmpty())
  141.         return;
  142.     else
  143.     {
  144.         if (front == rear)
  145.             front = rear = -1;
  146.         else if (front == orderSize && rear == orderSize)
  147.             front = 0;
  148.         else
  149.             front++;
  150.         return;
  151.     }
  152. }
  153.  
  154. bool Apothecary::BuyPotion(Potion& potion)
  155. {
  156.     if (IsShelfEmpty())
  157.         return false;
  158.     else
  159.     {
  160.         potion.SetType(PeekShelf());
  161.         top--;
  162.     }
  163.     return true;
  164. }
  165.  
  166. PotionType Apothecary::PeekShelf() const
  167. {
  168.     if (!IsOrderEmpty())
  169.         return shelfArray[top];
  170.     else
  171.         return UNKNOWN;
  172. }
  173.  
  174. int Apothecary::MakePotions()
  175. {
  176.     int i = 0;
  177.     if (IsOrderEmpty())
  178.         return i;
  179.  
  180.     if (IsShelfFull())
  181.     {
  182.         cout << "The shelf of potions is full.  You can't make any more until somebody buys some.\n";
  183.         return i;
  184.     }
  185.     while ((!IsOrderEmpty()) && (!IsShelfFull()))
  186.     {
  187.         PushShelf(PeekOrder());
  188.         cout << "Made a " << PotionTypeString(PeekOrder()) << " potion.\n";
  189.         DequeueOrder();
  190.         i++;
  191.     }
  192.     return i;
  193. }
  194.  
  195. void swap(Apothecary& oldApo, Apothecary& newApo)
  196. {
  197.     swap(oldApo.front, newApo.front);
  198.     swap(oldApo.rear, newApo.rear);
  199.     swap(oldApo.top, newApo.top);
  200.     swap(oldApo.orderSize, newApo.orderSize);
  201.     swap(oldApo.shelfSize, newApo.shelfSize);
  202.     swap(oldApo.orderArray, newApo.orderArray);
  203.     swap(oldApo.shelfArray, newApo.shelfArray);
  204. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement