Advertisement
starbeamrainbowlabs

Coding Conundrum 3.3: Kitchen Tracker

Mar 8th, 2015
605
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.14 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3.  
  4. public class Program
  5. {
  6.     public enum Mode
  7.     {
  8.         DataEntry,
  9.         Search
  10.     }
  11.  
  12.     public static Mode CurrentMode = Mode.DataEntry;
  13.  
  14.     static Dictionary<string, int> Stock = new Dictionary<string, int>();
  15.  
  16.     public static void Main()
  17.     {
  18.         Console.WriteLine("Kitechen Tracker v0.2");
  19.         Console.WriteLine("---------------------");
  20.         Console.WriteLine("Coding Conundrum 3.3, challenge set by Rob Miles, conquered by Starbeamrainbowlabs");
  21.         Console.WriteLine("Enter the special name 'search' to switch to search mode");
  22.         Console.WriteLine("Enter the special name 'enter' to switch back to data entry mode");
  23.         Console.WriteLine("Enter the special name 'list' at any time to view the current stock");
  24.         Console.WriteLine();
  25.         while(true)
  26.         {
  27.             switch (CurrentMode)
  28.             {
  29.                 case Mode.DataEntry:
  30.                     string inName = ReadString("Enter the name of the next ingredient: ").Trim().ToLower();
  31.                     switch(inName)
  32.                     {
  33.                         case "search":
  34.                             Console.WriteLine("Switching to search mode.");
  35.                             CurrentMode = Mode.Search;
  36.                             continue;
  37.  
  38.                         case "list":
  39.                             ListStock();
  40.                             continue;
  41.  
  42.                         default:
  43.                             int inAmount = ReadNumber("Enter the amount: ");
  44.                             int currentAmount;
  45.                             if(Stock.TryGetValue(inName, out currentAmount))
  46.                             {
  47.                                 if(!Confirm(String.Format("There is already an entry for {0} (with an amount of {1}). Do you want to overwrite it? (y / n)", inName, currentAmount)))
  48.                                 {
  49.                                     continue;
  50.                                 }
  51.                             }
  52.                             Console.WriteLine("{0} has been added to the list.", inName);
  53.                             Stock[inName] = inAmount;
  54.                             break;
  55.                     }
  56.                     break;
  57.  
  58.                 case Mode.Search:
  59.                     string searchString = ReadString("Enter ingredient name: ").Trim().ToLower();
  60.                     switch(searchString)
  61.                     {
  62.                         case "enter":
  63.                             Console.WriteLine("Switching to data entry mode");
  64.                             CurrentMode = Mode.DataEntry;
  65.                             continue;
  66.  
  67.                         case "list":
  68.                             ListStock();
  69.                             continue;
  70.  
  71.                         default:
  72.                             int amountInStock;
  73.                             if(Stock.TryGetValue(searchString, out amountInStock))
  74.                             {
  75.                                 Console.WriteLine("Amount of {0}: {1}", searchString, amountInStock);
  76.                             }
  77.                             else
  78.                             {
  79.                                 Console.WriteLine("There isn't anything called {0} in stock at the moment.", searchString);
  80.                             }
  81.                             break;
  82.                     }
  83.                     break;
  84.             }
  85.         }
  86.     }
  87.  
  88.     public static void ListStock()
  89.     {
  90.         Console.WriteLine("Current Stock:");
  91.         foreach(KeyValuePair<string, int> Ingredient in Stock)
  92.         {
  93.             Console.WriteLine("{0,-12}: {1}", Ingredient.Key, Ingredient.Value);
  94.         }
  95.     }
  96.  
  97.     /// <summary>
  98.     /// Asks the user a simple yes / no question.
  99.     /// </summary>
  100.     /// <param name="prompt">The prompt to display</param>
  101.     /// <returns>The user's choice as a boolean</returns>
  102.     public static bool Confirm(string prompt)
  103.     {
  104.         Console.WriteLine(prompt);
  105.         while(true)
  106.         {
  107.             ConsoleKeyInfo nextChar = Console.ReadKey(true);
  108.             switch(nextChar.Key.ToString().ToLower())
  109.             {
  110.                 case "y":
  111.                     return true;
  112.                 case "n":
  113.                     return false;
  114.             }
  115.         }
  116.     }
  117.  
  118.     /// <summary>
  119.     /// Reads a string in from the user.
  120.     /// </summary>
  121.     /// <param name="prompt">The prompt to display</param>
  122.     /// <param name="minlength">The minimum length of the input the user enters.</param>
  123.     /// <returns>The string the user entered.</returns>
  124.     public static string ReadString(string prompt, int minlength = 1)
  125.     {
  126.         while (true)
  127.         {
  128.             Console.Write(prompt);
  129.             string line = Console.ReadLine();
  130.             if (line.Length < minlength)
  131.             {
  132.                 Console.WriteLine("Please enter something that is at least {0} characters.");
  133.                 continue;
  134.             }
  135.  
  136.             return line;
  137.         }
  138.     }
  139.  
  140.     /// <summary>
  141.     /// Reads a number from the user.
  142.     /// </summary>
  143.     /// <remarks>Depends on ReadString()</remarks>
  144.     /// <param name="prompt">The prompt to display to the user.</param>
  145.     /// <returns>The number that the user entered</returns>
  146.     public static int ReadNumber(string prompt)
  147.     {
  148.         while (true)
  149.         {
  150.             string line = ReadString(prompt).Trim();
  151.             try
  152.             {
  153.                 return int.Parse(line);
  154.             }
  155.             catch
  156.             {
  157.                 Console.WriteLine("Sorry, that was not a valid number.");
  158.             }
  159.         }
  160.     }
  161. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement