Created
September 11, 2013 15:50
-
-
Save hagbarddenstore/6525585 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| namespace PriceCalculator | |
| { | |
| using System; | |
| public static class Program | |
| { | |
| public static void Main() | |
| { | |
| do | |
| { | |
| bool exited; | |
| var price = GetPrice(out exited); | |
| if (exited) | |
| { | |
| break; | |
| } | |
| var items = GetNumberOfItems(out exited); | |
| if (exited) | |
| { | |
| break; | |
| } | |
| var payment = GetPayment(out exited); | |
| if (exited) | |
| { | |
| break; | |
| } | |
| var change = payment - (price * items); | |
| Console.WriteLine("Change: {0}", change); | |
| } | |
| while (true); | |
| Console.WriteLine("Please come again!"); | |
| Console.ReadLine(); | |
| } | |
| private static T Get<T>(string name, Func<string, Tuple<bool, T>> parse, out bool exited) | |
| { | |
| string input; | |
| do | |
| { | |
| Console.Write("{0}: ", name); | |
| input = Console.ReadLine() ?? string.Empty; | |
| var result = parse(input); | |
| if (result.Item1) | |
| { | |
| exited = false; | |
| return result.Item2; | |
| } | |
| if (!"quit".Equals(input)) | |
| { | |
| break; | |
| } | |
| Console.WriteLine("Invalid number"); | |
| } | |
| while (!"quit".Equals(input)); | |
| exited = true; | |
| return default(T); | |
| } | |
| private static double GetPrice(out bool exited) | |
| { | |
| Func<string, Tuple<bool, double>> parse = s => | |
| { | |
| double number; | |
| var result = double.TryParse(s, out number); | |
| return new Tuple<bool, double>(result, number); | |
| }; | |
| return Get("Price", parse, out exited); | |
| } | |
| private static int GetNumberOfItems(out bool exited) | |
| { | |
| Func<string, Tuple<bool, int>> parse = s => | |
| { | |
| int number; | |
| var result = int.TryParse(s, out number); | |
| return new Tuple<bool, int>(result, number); | |
| }; | |
| return Get("Items", parse, out exited); | |
| } | |
| private static double GetPayment(out bool exited) | |
| { | |
| Func<string, Tuple<bool, double>> parse = s => | |
| { | |
| double number; | |
| var result = double.TryParse(s, out number); | |
| return new Tuple<bool, double>(result, number); | |
| }; | |
| return Get("Payment", parse, out exited); | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment