static void Main(string[] args) { Rectangle rec = new Rectangle(); int l; int w; bool r = true; while (r) { try { Console.WriteLine("\n Welcome"); Console.WriteLine("Select one option from list"); Console.WriteLine("1. Get Rectangle Length"); Console.WriteLine("2. Change Rectangle Length"); Console.WriteLine("3. Get Rectangle Width"); Console.WriteLine("4. Change Rectangle Width"); Console.WriteLine("5. Get Rectangle Perimeter"); Console.WriteLine("6. Get Rectangle Area"); Console.WriteLine("7. Exit \n"); int input = int.Parse(Console.ReadLine()); switch (input) { case 1: Console.WriteLine(rec.GetLength()); break; case 2: try { Console.WriteLine("1. Enter the Length of Rectangle"); l = int.Parse(Console.ReadLine()); if (l <= 0) { Console.WriteLine("Wrong input"); } rec.SetLength(l); } catch (Exception e) { Console.WriteLine("Wrong Input"); } break; case 3: Console.WriteLine(rec.GetWidth()); break; case 4: try { Console.WriteLine("2. Enter Rectangle's Width"); w = int.Parse(Console.ReadLine()); if (w <= 0) { Console.WriteLine("Enter Valid Value"); } rec.SetWidth(w); } catch (Exception e) { Console.WriteLine("Wrong Input"); } break; case 5: Console.WriteLine("Perimeter " + rec.GetPerimeter()); break; case 6: Console.WriteLine("Area " + rec.GetArea()); break; case 7: Console.WriteLine("Please Visit Again "); Environment.Exit(0); r = false; break; } } catch (Exception e) { Console.WriteLine("Wrong Input"); }; } }
static void Main(string[] args) { Rectangle c = new Rectangle(); bool validCalcSelect = false; string calcSelection; int selection; while (validCalcSelect == false) { Console.WriteLine("1 = Use default values of length=1 and width=1\n"); Console.WriteLine("2 = Provide your own length and width\n"); Console.WriteLine("Choose a menu item to begin:"); calcSelection = Console.ReadLine(); Console.WriteLine(); if (calcSelection != "1" && calcSelection != "2") { Console.WriteLine("That's not a valid selection, please try again.\n"); } else if (int.Parse(calcSelection) == 1) { validCalcSelect = true; Random random = new Random(); int length = 1; int width = 1; Console.WriteLine($"Your default length is {length} and width is {width}.\n"); Rectangle customCalc = new Rectangle(length, width); c = customCalc; } else if (int.Parse(calcSelection) == 2) { validCalcSelect = true; int length; int width; length = ValidateUserInput("length"); width = ValidateUserInput("width"); Console.WriteLine($"Your custom length is {length} and width is {width}.\n"); Rectangle customCalc = new Rectangle(length, width); c = customCalc; } } selection = ValidationMenuSelection(); while (selection != 7) { int result; switch (selection) { case 1: Console.WriteLine($"Length is: {c.GetLength()}\n"); break; case 2: result = ValidateUserInput("length"); c.SetLength(result); break; case 3: Console.WriteLine($"Width is: {c.GetWidth()}\n"); break; case 4: result = ValidateUserInput("width"); c.SetWidth(result); break; case 5: Console.WriteLine($"The Perimeter of {c.GetLength()} and {c.GetWidth()} is: {c.GetPerimeter()}\n"); break; case 6: Console.WriteLine($"The Area of {c.GetLength()} and {c.GetWidth()} is:{c.GetArea()}\n"); break; default: break; } selection = ValidationMenuSelection(); } }
static void Main(string[] args) { int inputLength; int inputWidth; Rectangle rectangle; Console.WriteLine("***********Welcome to Rectangle Program************"); string lengthInput = null; string widthInput = null; int updatedLength; int updatedWidth; int userMenuSelection; try { do { Console.Clear(); Console.WriteLine("Please enter the Length"); lengthInput = Console.ReadLine(); Console.WriteLine("Please enter the Width"); widthInput = Console.ReadLine(); } while (!(int.TryParse(lengthInput, out inputLength) && int.TryParse(widthInput, out inputWidth) && inputWidth > 1 && inputLength > 0)); rectangle = new Rectangle(inputLength, inputWidth); do { Console.WriteLine("\n\nPlease select the number choice given for the below menu options\n\n1. GetLength\n2. SetLength\n3. GetWidth\n4. SetWidth\n5. GetPerimeter\n6. GetArea\n7. Exit"); bool userSelection = int.TryParse(Console.ReadLine(), out userMenuSelection); if (userSelection) { Console.Clear(); switch (userMenuSelection) { case 1: Console.WriteLine($"Rectangle's length is {rectangle.GetLength()}"); break; case 2: Console.WriteLine("Please enter the Length of the rectangle"); int.TryParse(Console.ReadLine(), out updatedLength); if (updatedLength > 0) { Console.WriteLine($"You have updated the length and rectangle's new length is {rectangle.SetLength(updatedLength)}"); break; } else { Console.WriteLine("Please enter positive length"); break; } case 3: Console.WriteLine($"Rectangle's width is {rectangle.GetWidth()}"); break; case 4: Console.WriteLine("Please enter the Width of the rectangle"); int.TryParse(Console.ReadLine(), out updatedWidth); if (updatedWidth > 0) { Console.WriteLine($"You have updated the width and rectangle's new width is {rectangle.SetWidth(updatedWidth)}"); break; } else { Console.WriteLine("Please enter positive width"); break; } case 5: Console.WriteLine($"Rectangle's perimeter is {rectangle.GetPerimeter()}"); break; case 6: Console.WriteLine($"Area of the rectangle is {rectangle.GetArea()}"); break; case 7: System.Environment.Exit(0); break; default: Console.WriteLine("Please enter the right input from the menu"); break; } } else { Console.Clear(); Console.WriteLine("Please enter only numerics"); } } while (!(userMenuSelection == 7)); } catch (Exception) { Console.WriteLine("Some error occured while entering and processing the rectangle details. Please close the application and try again."); } Console.ReadKey(); }