Advertisement
Guest User

Untitled

a guest
Nov 17th, 2017
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.15 KB | None | 0 0
  1. /* File: animalshelter.cpp
  2. * Course: CS216-005
  3. * Project: Lab 11
  4. * Purpose: AnimalShelter
  5. */
  6.  
  7. #include "animalshelter.h"
  8.  
  9. // add a new dog breed in the animal shelter
  10. // if the key (breed) exists, add the set of dognames to the values of the breed
  11. // otherwise add the key and the set of dognames to the map
  12. // breed is the new dog breed
  13. // dognames are the set of dog names of the breed
  14. void AnimalShelter::insert(string breed, set<string> dognames)
  15. {
  16. map<string, set<string> >::iterator i = BreedToNames.find(breed);
  17. if (i != BreedToNames.end()) {
  18. i->second.insert(dognames.begin(), dognames.end());
  19. }
  20. else
  21. BreedToNames.insert(make_pair(breed, dognames));
  22. }
  23.  
  24. // person: the name of the person who wants to adopt a dog of breed
  25. // breed: the breed of the dog a person whats to adopt
  26. // if such breed exists, display the message: person is adopting <a dog's name> (breed)
  27. // and then delete the dog from the map
  28. // otherwise, display the message: person CANNOT adopt a breed.
  29. void AnimalShelter::validAdoption(string person, string breed)
  30. {
  31. if (BreedToNames.find(breed) != BreedToNames.end()) {
  32. cout << person << " is currently adopting " << *BreedToNames[breed].begin() << "a " << breed << endl;
  33. BreedToNames[breed].erase(BreedToNames[breed].begin());
  34. if (BreedToNames[breed].empty())
  35. BreedToNames.erase(breed);
  36. }
  37. else
  38. cout << person << " CANNOT adopt a that breed of dog, it doesn't exist" << endl;
  39. }
  40.  
  41.  
  42. // check if the animal shelter is empty
  43. // return true if it is
  44. // otherwise return false
  45. bool AnimalShelter::empty() const
  46. {
  47. if (BreedToNames.empty())
  48. return true;
  49. else
  50. return false;
  51. }
  52.  
  53. // display all the animals currently in the shelter like this:
  54. // breed: [ dog_name1 dog_name2 ... ]
  55. void AnimalShelter::print() const
  56. {
  57. if (!(empty()))
  58. for (map<string, set<string> >::const_iterator i = BreedToNames.begin(); i != BreedToNames.end(); i++) {
  59. cout << i->first << " [";
  60. for (set<string>::const_iterator x = i->second.begin(); x != i->second.end(); x++) {
  61. cout << " " << *x;
  62. }
  63. cout << " ]" << endl;
  64. }
  65. else
  66. cout << "Sorry, there is currently no animal is in the shelter!" << endl;
  67. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement