Advertisement
starbeamrainbowlabs

Coding Conundrums 1.3: Shoe Popularity Contest

Feb 9th, 2015
526
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.72 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3.  
  4. class Program
  5. {
  6.     public static void PrintHelp()
  7.     {
  8.         Console.WriteLine("Coding Conundrums Problem 1.2 Solution: Number Shuffle");
  9.         Console.WriteLine("------------------------------------------------------");
  10.         Console.WriteLine("Problem Description:");
  11.         Console.WriteLine("    I want a program that reads in 20 integers and tells me how much of each number");
  12.         Console.WriteLine("    was entered. I actually have a use for this, in that each day I sell 20 pairs of");
  13.         Console.WriteLine("    shoes and I want to find out the most popular shoe sizes. It's amazing that I");
  14.         Console.WriteLine("    manage to hold down my lecturing job, what with all the shoe sales I'm involved");
  15.         Console.WriteLine("    with.");
  16.         Console.WriteLine();
  17.         Console.WriteLine("Use it like this: ");
  18.         Console.WriteLine("    shoesales.exe");
  19.     }
  20.  
  21.     ///<summary>
  22.     ///Gets an integer from the user between min and max inclusive.
  23.     ///</summary>
  24.     ///<param name="prompt">The prompt to display to the user.</param>
  25.     ///<param name="min">The minimum value the user should enter.</param>
  26.     ///<param name="max">The maximum value the user should enter.</param>
  27.     static int ReadNumber(string prompt, int min, int max)
  28.     {
  29.         int number;
  30.         while (true)
  31.         {
  32.             Console.Write(prompt);
  33.             try
  34.             {
  35.                 number = int.Parse(Console.ReadLine().Trim());
  36.             }
  37.             catch
  38.             {
  39.                 Console.WriteLine("That was not a number. Numbers may contain numeric characters only.");
  40.                 continue;
  41.             }
  42.  
  43.             if (number < min)
  44.             {
  45.                 Console.WriteLine("That number was too low. Please enter a number between " + min + " and " + max + ".");
  46.                 continue;
  47.             }
  48.             if (number > max)
  49.             {
  50.                 Console.WriteLine("That number was too high. Please enter a number between " + min + " and " + max + ".");
  51.                 continue;
  52.             }
  53.  
  54.             break;
  55.         }
  56.  
  57.         return number;
  58.     }
  59.  
  60.     public static void PrintShoeSizes(Dictionary<int, int> shoes)
  61.     {
  62.         Console.WriteLine("Shoe Sizes:");
  63.         Console.WriteLine("-----------");
  64.         Console.WriteLine("Size     Count");
  65.         foreach(KeyValuePair<int, int> shoeSize in shoes)
  66.         {
  67.             Console.WriteLine("{0,-8} {1}", shoeSize.Key, shoeSize.Value);
  68.         }
  69.     }
  70.  
  71.     public static void Main(string[] args)
  72.     {
  73.         if(args.Length == 1 && args[0].ToLower().Trim('-') == "help")
  74.         {
  75.             PrintHelp();
  76.             return;
  77.         }
  78.  
  79.         int shoeTotal = 20;
  80.  
  81.         Dictionary<int, int> shoes = new Dictionary<int, int>();
  82.         for(int i = 0; i < shoeTotal; i++)
  83.         {
  84.             PrintShoeSizes(shoes);
  85.             int currentShoeSize = ReadNumber(String.Format("Enter the shoe size #{0}: ", i + 1), 1, 12);
  86.  
  87.             if(!shoes.ContainsKey(currentShoeSize))
  88.             {
  89.                 shoes.Add(currentShoeSize, 1);
  90.             }
  91.             else
  92.             {
  93.                 shoes[currentShoeSize] += 1;
  94.             }
  95.         }
  96.     }
  97. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement