Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- public class Program
- {
- public enum Mode
- {
- DataEntry,
- Search
- }
- public static Mode CurrentMode = Mode.DataEntry;
- static Dictionary<string, int> Stock = new Dictionary<string, int>();
- public static void Main()
- {
- Console.WriteLine("Kitechen Tracker v0.2");
- Console.WriteLine("---------------------");
- Console.WriteLine("Coding Conundrum 3.3, challenge set by Rob Miles, conquered by Starbeamrainbowlabs");
- Console.WriteLine("Enter the special name 'search' to switch to search mode");
- Console.WriteLine("Enter the special name 'enter' to switch back to data entry mode");
- Console.WriteLine("Enter the special name 'list' at any time to view the current stock");
- Console.WriteLine();
- while(true)
- {
- switch (CurrentMode)
- {
- case Mode.DataEntry:
- string inName = ReadString("Enter the name of the next ingredient: ").Trim().ToLower();
- switch(inName)
- {
- case "search":
- Console.WriteLine("Switching to search mode.");
- CurrentMode = Mode.Search;
- continue;
- case "list":
- ListStock();
- continue;
- default:
- int inAmount = ReadNumber("Enter the amount: ");
- int currentAmount;
- if(Stock.TryGetValue(inName, out currentAmount))
- {
- 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)))
- {
- continue;
- }
- }
- Console.WriteLine("{0} has been added to the list.", inName);
- Stock[inName] = inAmount;
- break;
- }
- break;
- case Mode.Search:
- string searchString = ReadString("Enter ingredient name: ").Trim().ToLower();
- switch(searchString)
- {
- case "enter":
- Console.WriteLine("Switching to data entry mode");
- CurrentMode = Mode.DataEntry;
- continue;
- case "list":
- ListStock();
- continue;
- default:
- int amountInStock;
- if(Stock.TryGetValue(searchString, out amountInStock))
- {
- Console.WriteLine("Amount of {0}: {1}", searchString, amountInStock);
- }
- else
- {
- Console.WriteLine("There isn't anything called {0} in stock at the moment.", searchString);
- }
- break;
- }
- break;
- }
- }
- }
- public static void ListStock()
- {
- Console.WriteLine("Current Stock:");
- foreach(KeyValuePair<string, int> Ingredient in Stock)
- {
- Console.WriteLine("{0,-12}: {1}", Ingredient.Key, Ingredient.Value);
- }
- }
- /// <summary>
- /// Asks the user a simple yes / no question.
- /// </summary>
- /// <param name="prompt">The prompt to display</param>
- /// <returns>The user's choice as a boolean</returns>
- public static bool Confirm(string prompt)
- {
- Console.WriteLine(prompt);
- while(true)
- {
- ConsoleKeyInfo nextChar = Console.ReadKey(true);
- switch(nextChar.Key.ToString().ToLower())
- {
- case "y":
- return true;
- case "n":
- return false;
- }
- }
- }
- /// <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