SHARE
TWEET

Untitled

a guest Jul 21st, 2019 56 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. class GuessStatisticsMessage
  2. {
  3. public:
  4.     GuessStatisticsMessage() = default;
  5.     ~GuessStatisticsMessage() = default;
  6.  
  7.     std::string make(char candidate, int count);
  8.  
  9. private:
  10.     void createPluralDependentMessageParts(int count);
  11.     void createThereAreManyLetters(int count);
  12.     void createThereIsOneLetter();
  13.     void createThereAreNoLetters();
  14.  
  15.     std::string number;
  16.     std::string verb;
  17.     std::string pluralModifier;
  18. };
  19.      
  20. std::string GuessStatisticsMessage::make(char candidate, int count)
  21. {
  22.     createPluralDependentMessageParts(count);
  23.     return fmt::format("There {} {} {}{}", verb, number, candidate, pluralModifier);
  24. }
  25.  
  26. void GuessStatisticsMessage::createPluralDependentMessageParts(int count)
  27. {
  28.     if (count == 0) {
  29.         createThereAreNoLetters();
  30.     } else if (count == 1) {
  31.         createThereIsOneLetter();
  32.     } else {
  33.         createThereAreManyLetters(count);
  34.     }
  35. }
  36.  
  37. void GuessStatisticsMessage::createThereAreManyLetters(int count)
  38. {
  39.     verb = "are";
  40.     number = std::to_string(count);
  41.     pluralModifier = "s";
  42. }
  43.  
  44. void GuessStatisticsMessage::createThereIsOneLetter()
  45. {
  46.     verb = "is";
  47.     number = "one";
  48.     pluralModifier = "";
  49. }
  50.  
  51. void GuessStatisticsMessage::createThereAreNoLetters()
  52. {
  53.     verb = "are";
  54.     number = "no";
  55.     pluralModifier = "s";
  56. }
  57.      
  58. class GuessStatisticsMessageAlt
  59. {
  60. public:
  61.     GuessStatisticsMessageAlt() = default;
  62.     ~GuessStatisticsMessageAlt() = default;
  63.  
  64.     std::string make(char candidate, int count);
  65.  
  66. private:
  67.     struct MessageComponents
  68.     {
  69.         std::string verb;
  70.         std::string number;
  71.         std::string pluralModifier;
  72.     };
  73.  
  74.     MessageComponents createPluralDependentMessageParts(int count);
  75.     MessageComponents createThereAreManyLetters(int count);
  76.     MessageComponents createThereIsOneLetter();
  77.     MessageComponents createThereAreNoLetters();
  78. };
  79.      
  80. std::string GuessStatisticsMessageAlt::make(char candidate, int count)
  81. {
  82.     auto messageComponents = createPluralDependentMessageParts(count);
  83.     return fmt::format("There {} {} {}{}", messageComponents.verb, messageComponents.number,
  84.         candidate, messageComponents.pluralModifier);
  85. }
  86.  
  87. GuessStatisticsMessageAlt::MessageComponents GuessStatisticsMessageAlt::createPluralDependentMessageParts(int count)
  88. {
  89.     if (count == 0) {
  90.         return createThereAreNoLetters();
  91.     } else if (count == 1) {
  92.         return createThereIsOneLetter();
  93.     } else {
  94.         return createThereAreManyLetters(count);
  95.     }
  96. }
  97.  
  98. GuessStatisticsMessageAlt::MessageComponents GuessStatisticsMessageAlt::createThereAreManyLetters(int count)
  99. {
  100.     return { "are", std::to_string(count), "s" };
  101. }
  102.  
  103. GuessStatisticsMessageAlt::MessageComponents GuessStatisticsMessageAlt::createThereIsOneLetter()
  104. {
  105.     return { "is", "one", "" };
  106. }
  107.  
  108. GuessStatisticsMessageAlt::MessageComponents GuessStatisticsMessageAlt::createThereAreNoLetters()
  109. {
  110.     return { "are", "no", "s" };
  111. }
  112.      
  113. std::string makeGuessStatisticsMessage(char candidate, int count);
  114.      
  115. namespace {
  116.  
  117. struct MessageComponents
  118. {
  119.     std::string verb;
  120.     std::string number;
  121.     std::string pluralModifier;
  122. };
  123.  
  124. MessageComponents createThereAreManyLetters(int count)
  125. {
  126.     return { "are", std::to_string(count), "s" };
  127. }
  128.  
  129. MessageComponents createThereIsOneLetter()
  130. {
  131.     return { "is", "one", "" };
  132. }
  133.  
  134. MessageComponents createThereAreNoLetters()
  135. {
  136.     return { "are", "no", "s" };
  137. }
  138.  
  139. MessageComponents createPluralDependentMessageParts(int count)
  140. {
  141.     if (count == 0) {
  142.         return createThereAreNoLetters();
  143.     }
  144.     else if (count == 1) {
  145.         return createThereIsOneLetter();
  146.     }
  147.     else {
  148.         return createThereAreManyLetters(count);
  149.     }
  150. }
  151.  
  152. }
  153.  
  154. std::string makeGuessStatisticsMessage(char candidate, int count)
  155. {
  156.     auto messageComponents = createPluralDependentMessageParts(count);
  157.     return fmt::format("There {} {} {}{}", messageComponents.verb, messageComponents.number,
  158.         candidate, messageComponents.pluralModifier);
  159. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top