Sanath12345

Untitled

Jun 3rd, 2021
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.57 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <string>
  4. #include <cstring>
  5. using namespace std;
  6. #include "DoggyDaycare.h"
  7.  
  8. //cin.ignore();
  9. //getline(cin, breed, ';');
  10.  
  11.  
  12.  
  13. // Constructors
  14. DoggyDaycare::DoggyDaycare()
  15. {
  16.  
  17. }
  18.  
  19. DoggyDaycare::DoggyDaycare(const string n, const float energy, const string b, const int a, const float s)
  20. {
  21. name = n; energyLevel = energy; breed = b; size = s; age = a;
  22.  
  23. }
  24.  
  25. // Destructor
  26. DoggyDaycare::~DoggyDaycare()
  27. {
  28.  
  29. }
  30.  
  31. // setters
  32. void DoggyDaycare::setName(const string n)
  33. {
  34. name = n;
  35. }
  36.  
  37. void DoggyDaycare::setDogBreed(const string b)
  38. {
  39. breed = b;
  40. }
  41. void DoggyDaycare::setEnergyLevel(const float energy)
  42. {
  43. energyLevel = energy;
  44. }
  45. void DoggyDaycare::setAge(const int a)
  46. {
  47. age = a;
  48. }
  49. void DoggyDaycare::setSize(const float s)
  50. {
  51. size = s;
  52. }
  53. void DoggyDaycare::loadDogs() const
  54. {
  55. ifstream file("dogs.txt");
  56. DoggyDaycare *dog[MAX_DOGS];
  57. std::string str;
  58. int num=0;
  59. int age, pos1, pos2;
  60. float EnergyLevel, size;
  61. string name, DogBreed;
  62. while (std::getline(file, str)) {
  63. pos1 = str.find(",");
  64. name = str.substr(0, pos1);
  65. pos2 = str.find(",", pos1 + 1);
  66. EnergyLevel = stof(str.substr(pos1 + 1, pos2-pos1-1));
  67. pos1 = str.find(",", pos2 + 1);
  68. DogBreed = str.substr(pos2 + 1, pos1 - pos2-1);
  69. pos2 = str.find(",", pos1 + 1);
  70. size = stof( str.substr(pos1 + 1, pos2 - pos1-1) );
  71. age = stoi( str.substr(pos2 + 1) );
  72. dog[num] = new DoggyDaycare(name, EnergyLevel , DogBreed , size, age);
  73. num++;
  74. }
  75.  
  76. file.close();
  77. cout << "Loaded " << num << " dogs from our directory!" << endl;
  78.  
  79. for (int i = 0; i < num; i++) {
  80. cout << "Name: " << dog[i]->getName() << ", Energy Level: " << dog[i]->getEnergyLevel() << ", Breed: " << dog[i]->getDogBreed() << ", Age: " << dog[i]->getSize() << ", Size: " << dog[i]->getAge() << " lbs" << endl;
  81. }
  82. cout << endl;
  83.  
  84. cout << endl;
  85. }
  86.  
  87. void DoggyDaycare::enrollNew() const
  88. {
  89.  
  90. ifstream file("dogs.txt");
  91. DoggyDaycare *dog[MAX_DOGS];
  92. std::string str;
  93. int num=0;
  94. int age, pos1, pos2;
  95. float energyLevel, size;
  96. string name, breed;
  97. while (std::getline(file, str)) {
  98. pos1 = str.find(",");
  99. name = str.substr(0, pos1);
  100. pos2 = str.find(",", pos1 + 1);
  101. energyLevel = stof(str.substr(pos1 + 1, pos2-pos1-1));
  102. pos1 = str.find(",", pos2 + 1);
  103. breed = str.substr(pos2 + 1, pos1 - pos2-1);
  104. pos2 = str.find(",", pos1 + 1);
  105. size = stof( str.substr(pos1 + 1, pos2 - pos1-1) );
  106. age = stoi( str.substr(pos2 + 1) );
  107. dog[num] = new DoggyDaycare(name, energyLevel , breed , size, age);
  108. num++;
  109. }
  110.  
  111. file.close();
  112. cin.ignore();
  113. cout << "Name: ";
  114. getline(cin, name);
  115. cout << "Breed: ";
  116. getline(cin, breed);
  117. cout << "Size in pounds: ";
  118. cin >> size;
  119. cout << "Age: ";
  120. cin >> age;
  121. cout << "You just enrolled: " << name << ", Breed: " << breed << ", Size: " << size << "lbs, Age: " << age << endl;
  122. cout << endl;
  123.  
  124. cout<< "Enter the energy level for your dog (1-10) to find other dogs to socialize with your pup: " << endl;
  125. cout << "Energy level: ";
  126. cin >> energyLevel;
  127.  
  128. int matched_num = 0;
  129. for (int i = 0; i < num; i++) {
  130. if ((dog[i]->getEnergyLevel() >= (float)(energyLevel - 1)) && (dog[i]->getEnergyLevel() <= (float)(energyLevel + 1))) {
  131. cout << "Your dog's energy level matches with: " << dog[i]->getName() << ", " << dog[i]->getEnergyLevel() << ", " << dog[i]->getDogBreed() << ", " << dog[i]->getSize() << ", " << dog[i]->getAge() << endl;
  132. matched_num++;
  133. }
  134. }
  135. if (!matched_num)
  136. cout << "There are no dogs enrolled yet with similar energy levels." << endl;
  137. dog[num] = new DoggyDaycare(name, energyLevel , breed , size, age);
  138. num++;
  139. ofstream outdata;
  140. outdata.open("dogs.txt");
  141. for (int i = 0; i < num; i++) {
  142. outdata << dog[i]->getName() << "," << dog[i]->getEnergyLevel() << "," << dog[i]->getDogBreed() << "," << dog[i]->getSize() << "," << dog[i]->getAge() << endl;
  143. }
  144. outdata.close();
  145. cout << "All dog saved into file. " << endl;
  146. cout << endl;
  147. }
  148.  
  149. void DoggyDaycare::allDogs() const
  150. {
  151.  
  152. }
  153.  
  154.  
  155. // getters
  156. string DoggyDaycare::getName() const
  157. {
  158. return name;
  159. }
  160. string DoggyDaycare::getDogBreed() const
  161. {
  162. return breed;
  163. }
  164. float DoggyDaycare::getEnergyLevel() const
  165. {
  166. return energyLevel;
  167. }
  168.  
  169.  
  170. int DoggyDaycare::getAge() const
  171. {
  172. return age;
  173. }
  174.  
  175. float DoggyDaycare::getSize() const
  176. {
  177. return size;
  178. }
  179.  
  180. // Print method
  181. void DoggyDaycare::print() const
  182. {
  183. ifstream file("dogs.txt");
  184. DoggyDaycare *dog[MAX_DOGS];
  185. std::string str;
  186. int num=0;
  187. int age, pos1, pos2;
  188. float energyLevel, size;
  189. string name, breed;
  190. while (std::getline(file, str)) {
  191. pos1 = str.find(",");
  192. name = str.substr(0, pos1);
  193. pos2 = str.find(",", pos1 + 1);
  194. energyLevel = stof(str.substr(pos1 + 1, pos2-pos1-1));
  195. pos1 = str.find(",", pos2 + 1);
  196. breed = str.substr(pos2 + 1, pos1 - pos2-1);
  197. pos2 = str.find(",", pos1 + 1);
  198. size = stof( str.substr(pos1 + 1, pos2 - pos1-1) );
  199. age = stoi( str.substr(pos2 + 1) );
  200. dog[num] = new DoggyDaycare(name, energyLevel , breed , size, age);
  201. num++;
  202. }
  203.  
  204. file.close();
  205.  
  206.  
  207.  
  208. }
  209.  
  210.  
Advertisement
Add Comment
Please, Sign In to add comment