Advertisement
starbeamrainbowlabs

Coding Conundrum 3.2: Fish List Plus

Mar 8th, 2015
574
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.23 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3.  
  4. public class Program
  5. {
  6.     public struct Fish
  7.     {
  8.         public string Name;
  9.         public int Weight;
  10.  
  11.         public Fish(string inName, int inWeight)
  12.         {
  13.             Name = inName;
  14.             Weight = inWeight;
  15.         }
  16.  
  17.         public override string ToString()
  18.         {
  19.             return String.Format("{0} at {1}g", Name, Weight);
  20.         }
  21.     }
  22.  
  23.     public static List<Fish> FishList = new List<Fish>();
  24.  
  25.     public static int totalWeight;
  26.  
  27.     public static void Main(string[] args)
  28.     {
  29.         int i = 1;
  30.         while(true)
  31.         {
  32.             string inFishName = ReadString(String.Format("Enter fish {0}'s name: ", i));
  33.  
  34.             if(inFishName.ToLower() == "finished")
  35.                 break;
  36.  
  37.             int inFishWeight = ReadNumber(String.Format("Enter fish {0}'s weight (in grams): ", i));
  38.  
  39.             FishList.Add(new Fish(inFishName, inFishWeight));
  40.  
  41.             Console.WriteLine("Fish added.");
  42.         }
  43.  
  44.         /// calculate the statistics ///
  45.         // variable declaration
  46.         Fish heaviestFish = FishList[0];
  47.         Fish lightestFish = FishList[0];
  48.  
  49.         Dictionary<string, int> PopularFish = new Dictionary<string, int>();
  50.  
  51.         foreach(Fish currentFish in FishList)
  52.         {
  53.             if (currentFish.Weight > heaviestFish.Weight)
  54.                 heaviestFish = currentFish;
  55.             if (currentFish.Weight < lightestFish.Weight)
  56.                 lightestFish = currentFish;
  57.  
  58.             totalWeight += currentFish.Weight;
  59.  
  60.             int CurrentFishCount;
  61.             if(PopularFish.TryGetValue(currentFish.Name.ToLower(), out CurrentFishCount))
  62.             {
  63.                 PopularFish[currentFish.Name.ToLower()]++;
  64.             }
  65.             else
  66.             {
  67.                 PopularFish[currentFish.Name.ToLower()] = 1;
  68.             }
  69.         }
  70.  
  71.         // Print out the statistics calculated above
  72.         Console.WriteLine(" ----------------------------- ");
  73.         Console.WriteLine("| {0,-12} | {1,12} |", "Name", "Count");
  74.         Console.WriteLine("|--------------|--------------|");
  75.         foreach(KeyValuePair<string, int> FishCount in PopularFish)
  76.         {
  77.             Console.WriteLine("| {0,-12} | {1,12} |", FishCount.Key, FishCount.Value);
  78.         }
  79.         Console.WriteLine(" ----------------------------- ");
  80.  
  81.         Console.WriteLine("Total weight: {0}", totalWeight);
  82.         Console.WriteLine("Heaviest fish: {0}", heaviestFish.ToString());
  83.         Console.WriteLine("Lightest fish: {0}", lightestFish.ToString());
  84.     }
  85.  
  86.     /// <summary>
  87.     /// Reads a string in from the user.
  88.     /// </summary>
  89.     /// <param name="prompt">The prompt to display</param>
  90.     /// <param name="minlength">The minimum length of the input the user enters.</param>
  91.     /// <returns>The string the user entered.</returns>
  92.     public static string ReadString(string prompt, int minlength = 1)
  93.     {
  94.         while(true)
  95.         {
  96.             Console.Write(prompt);
  97.             string line = Console.ReadLine();
  98.             if(line.Length < minlength)
  99.             {
  100.                 Console.WriteLine("Please enter something that is at least {0} characters.");
  101.                 continue;
  102.             }
  103.  
  104.             return line;
  105.         }
  106.     }
  107.  
  108.     /// <summary>
  109.     /// Reads a number from the user.
  110.     /// </summary>
  111.     /// <remarks>Depends on ReadString()</remarks>
  112.     /// <param name="prompt">The prompt to display to the user.</param>
  113.     /// <returns>The number that the user entered</returns>
  114.     public static int ReadNumber(string prompt)
  115.     {
  116.         while(true)
  117.         {
  118.             string line = ReadString(prompt).Trim();
  119.             try {
  120.                 return int.Parse(line);
  121.             }
  122.             catch
  123.             {
  124.                 Console.WriteLine("Sorry, that was not a valid number.");
  125.             }
  126.         }
  127.     }
  128. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement