private void ChocolateTest1() { ConsoleHelper ch = new ConsoleHelper(); Console.WriteLine("The chocolate contains 24 pieces"); int people = ch.AskForInteger("How many want to share?"); decimal piecePerPerson = 24M / people; // Has to have M to avoid int division // Alternative: // decimal piecePerPerson = decimal.Divide(24, people); // Doesn't work, gives Infinity as answer (not exception): //double piecePerPerson = 24.0 / people; // Has to have .0 to avoid int division Console.WriteLine($"Everyone get {piecePerPerson:.##} pieces"); }
static void Main(string[] args) { var service = new ShippingService(); Letter letter; while (true) { try { bool rek = false; bool bulky = false; int weight = ch.AskForInteger("Enter weight:"); string input = ch.AskForString("Recommended (y/n)"); if (input == "y") { rek = true; } input = ch.AskForString("Bulky (y/n)"); if (input == "y") { bulky = true; } letter = new Letter(weight, rek, bulky); } catch (ArgumentException ex) { Console.WriteLine(ex.Message); continue; } break; } Console.WriteLine(); Console.WriteLine(letter.Weight); Console.WriteLine(letter.RecommendedLetter); Console.WriteLine(letter.Bulky); Console.WriteLine(); decimal shipping = service.CalculateShipping(letter, "2019"); Console.WriteLine(shipping); }
private void ChocolateTest2() { ConsoleHelper ch = new ConsoleHelper(); Console.WriteLine("The chocolate contains 10 pieces"); int people = ch.AskForInteger("How many want to share?"); try { decimal piecePerPerson = 10M / people; Console.WriteLine($"Everyone get {piecePerPerson:.##} pieces"); } catch (DivideByZeroException ex) { ch.WriteLineRed("Zero people can't divide a chocolate"); } catch (Exception ex) { ch.WriteLineRed("Something strange happened!"); } }