SHARE
TWEET

Coding Conundrum 3.1: Fish List

starbeamrainbowlabs Mar 8th, 2015 281 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  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.                 foreach(Fish currentFish in FishList)
  50.                 {
  51.                         if (currentFish.Weight > heaviestFish.Weight)
  52.                                 heaviestFish = currentFish;
  53.                         if (currentFish.Weight < lightestFish.Weight)
  54.                                 lightestFish = currentFish;
  55.  
  56.                         totalWeight += currentFish.Weight;
  57.                 }
  58.  
  59.                 // Print out the statistics calculated above
  60.                 Console.WriteLine("Total weight: {0}", totalWeight);
  61.                 Console.WriteLine("Heaviest fish: {0}", heaviestFish.ToString());
  62.                 Console.WriteLine("Lightest fish: {0}", lightestFish.ToString());
  63.         }
  64.  
  65.         /// <summary>
  66.         /// Reads a string in from the user.
  67.         /// </summary>
  68.         /// <param name="prompt">The prompt to display</param>
  69.         /// <param name="minlength">The minimum length of the input the user enters.</param>
  70.         /// <returns>The string the user entered.</returns>
  71.         public static string ReadString(string prompt, int minlength = 1)
  72.         {
  73.                 while(true)
  74.                 {
  75.                         Console.Write(prompt);
  76.                         string line = Console.ReadLine();
  77.                         if(line.Length < minlength)
  78.                         {
  79.                                 Console.WriteLine("Please enter something that is at least {0} characters.");
  80.                                 continue;
  81.                         }
  82.  
  83.                         return line;
  84.                 }
  85.         }
  86.  
  87.         /// <summary>
  88.         /// Reads a number from the user.
  89.         /// </summary>
  90.         /// <remarks>Depends on ReadString()</remarks>
  91.         /// <param name="prompt">The prompt to display to the user.</param>
  92.         /// <returns>The number that the user entered</returns>
  93.         public static int ReadNumber(string prompt)
  94.         {
  95.                 while(true)
  96.                 {
  97.                         string line = ReadString(prompt).Trim();
  98.                         try {
  99.                                 return int.Parse(line);
  100.                         }
  101.                         catch
  102.                         {
  103.                                 Console.WriteLine("Sorry, that was not a valid number.");
  104.                         }
  105.                 }
  106.         }
  107. }
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