Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- public class Program
- {
- public struct Fish
- {
- public string Name;
- public int Weight;
- public Fish(string inName, int inWeight)
- {
- Name = inName;
- Weight = inWeight;
- }
- public override string ToString()
- {
- return String.Format("{0} at {1}g", Name, Weight);
- }
- }
- public static List<Fish> FishList = new List<Fish>();
- public static int totalWeight;
- public static void Main(string[] args)
- {
- int i = 1;
- while(true)
- {
- string inFishName = ReadString(String.Format("Enter fish {0}'s name: ", i));
- if(inFishName.ToLower() == "finished")
- break;
- int inFishWeight = ReadNumber(String.Format("Enter fish {0}'s weight (in grams): ", i));
- FishList.Add(new Fish(inFishName, inFishWeight));
- Console.WriteLine("Fish added.");
- }
- /// calculate the statistics ///
- // variable declaration
- Fish heaviestFish = FishList[0];
- Fish lightestFish = FishList[0];
- foreach(Fish currentFish in FishList)
- {
- if (currentFish.Weight > heaviestFish.Weight)
- heaviestFish = currentFish;
- if (currentFish.Weight < lightestFish.Weight)
- lightestFish = currentFish;
- totalWeight += currentFish.Weight;
- }
- // Print out the statistics calculated above
- Console.WriteLine("Total weight: {0}", totalWeight);
- Console.WriteLine("Heaviest fish: {0}", heaviestFish.ToString());
- Console.WriteLine("Lightest fish: {0}", lightestFish.ToString());
- }
- /// <summary>
- /// Reads a string in from the user.
- /// </summary>
- /// <param name="prompt">The prompt to display</param>
- /// <param name="minlength">The minimum length of the input the user enters.</param>
- /// <returns>The string the user entered.</returns>
- public static string ReadString(string prompt, int minlength = 1)
- {
- while(true)
- {
- Console.Write(prompt);
- string line = Console.ReadLine();
- if(line.Length < minlength)
- {
- Console.WriteLine("Please enter something that is at least {0} characters.");
- continue;
- }
- return line;
- }
- }
- /// <summary>
- /// Reads a number from the user.
- /// </summary>
- /// <remarks>Depends on ReadString()</remarks>
- /// <param name="prompt">The prompt to display to the user.</param>
- /// <returns>The number that the user entered</returns>
- public static int ReadNumber(string prompt)
- {
- while(true)
- {
- string line = ReadString(prompt).Trim();
- try {
- return int.Parse(line);
- }
- catch
- {
- Console.WriteLine("Sorry, that was not a valid number.");
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement