/// <summary> /// If user enters f or F, create an object of Tetrahedron, set dimensions for it based on input, then add it to the list. /// If any dimensions equal or less than 0, user should be given error message and has another chance to continue. /// However, the instance should not be added into the list. /// </summary> /// <param name="shapes">a list which contains elements that are Shape type</param> private static void Select_F(List <Shape> shapes) { Shape tetrahedron = new Tetrahedron("tetrahedron"); tetrahedron.SetData(); Tetrahedron t = (Tetrahedron)tetrahedron; if (t.Length <= 0) { InvalidDimensions(); } else { shapes.Add(tetrahedron); } }
/// <summary> /// Main Class, Runs at startup /// </summary> static void Main() { List <Shape> shapes = new List <Shape>(); while (true) { DisplayMenu(shapes.Count); string userInput = Console.ReadLine().ToUpper(); Shape tempShape = null; switch (userInput) { case "A": //Rectangle tempShape = new Rectangle(); break; case "B": //Square tempShape = new Square(); break; case "C": //Box tempShape = new Box(); break; case "D": //Cube tempShape = new Cube(); break; case "E": //Ellipse tempShape = new Ellipse(); break; case "F": //Circle tempShape = new Circle(); break; case "G": //Cylinder tempShape = new Cylinder(); break; case "H": //Sphere tempShape = new Sphere(); break; case "I": //Triangle tempShape = new Triangle(); break; case "J": //Tetrahedron tempShape = new Tetrahedron(); break; case "0": //List all shapes DisplayAllShapes(shapes.ToArray()); break; case "1": //Exit Environment.Exit(0); break; default: //None selected Console.WriteLine("\nSorry you must enter a letter between A - J or a number 0 - 1"); break; } if (tempShape != null) { Console.WriteLine(); tempShape.SetData(); Console.WriteLine("\n\nAdded new shape:"); Console.WriteLine($"{tempShape.GetType().Name} {tempShape}"); Console.WriteLine("\nPress any key to continue"); Console.ReadKey(); shapes.Add(tempShape); } } }
/// <summary> /// This is the method that runs the main block of /// program execution. /// </summary> /// <param name="args"></param> static void Main(string[] args) { /* * First we display the console information using the initializer method, then * we use the dataSetter method to promt the user to make a decision. Then we * create an empty Shape list, and create a boolean flag to run a loop that * validates the users input and then takes an action depending on the user input * to either create a new shape of choice, or display the Shape list info and exit. */ String input; initializer(); dataSetter(); bool flag = true; var shapeList = new List <Shape>(); while (flag) { if (input.ToUpper() == "A" || input.ToUpper() == "B" || input.ToUpper() == "C" || input.ToUpper() == "D" || input.ToUpper() == "E" || input.ToUpper() == "F" || input.ToUpper() == "G" || input.ToUpper() == "H" || input.ToUpper() == "I" || input.ToUpper() == "J" || input == "0") { switch (input.ToUpper()) { // Switch statement deciding which shape to make case "A": Rectangle a = new Rectangle(); a.SetData(); a.CalculateArea(); shapeList.Add(a); Console.Clear(); break; case "B": Square b = new Square(); b.SetData(); b.CalculateArea(); shapeList.Add(b); Console.Clear(); break; case "C": Box c = new Box(); c.SetData(); c.CalculateArea(); c.CalculateVolume(); shapeList.Add(c); Console.Clear(); break; case "D": Cube d = new Cube(); d.SetData(); d.CalculateArea(); d.CalculateVolume(); shapeList.Add(d); Console.Clear(); break; case "E": Ellipse e = new Ellipse(); e.SetData(); e.CalculateArea(); shapeList.Add(e); Console.Clear(); break; case "F": Circle f = new Circle(); f.SetData(); f.CalculateArea(); shapeList.Add(f); Console.Clear(); break; case "G": Cylinder g = new Cylinder(); g.SetData(); g.CalculateArea(); g.CalculateVolume(); shapeList.Add(g); Console.Clear(); break; case "H": Sphere h = new Sphere(); h.SetData(); h.CalculateArea(); h.CalculateVolume(); shapeList.Add(h); Console.Clear(); break; case "I": Triangle i = new Triangle(); i.SetData(); i.CalculateArea(); shapeList.Add(i); Console.Clear(); break; case "J": Tetrahedron j = new Tetrahedron(); j.SetData(); j.CalculateArea(); j.CalculateVolume(); shapeList.Add(j); Console.Clear(); break; case "0": Console.WriteLine(); outputTemplate(); foreach (Shape shapes in shapeList) { Console.WriteLine(shapes.ToString()); } flag = false; break; default: break; } if (flag) { initializer(); dataSetter(); } } else { Console.Clear(); initializer(); Console.WriteLine("Not valid input"); dataSetter(); } } /* * This line was used to prevent the console from closing instantly when user * chooses to display the shape list info. Allows user to hit any button to exit. */ Console.ReadLine(); /// <summary> /// This is the initializer method, used to easily refill the console with /// the main information when needed. /// </summary> void initializer() { Console.ForegroundColor = ConsoleColor.DarkGreen; Console.WriteLine("Bret's Geometry Class:"); Console.ResetColor(); Console.WriteLine("{0, -20} {1, -20}", "A - Rectangle", "F - Circle"); Console.WriteLine("{0, -20} {1, -20}", "B - Square", "G - Cylinder"); Console.WriteLine("{0, -20} {1, -20}", "C - Box", "H - Sphere"); Console.WriteLine("{0, -20} {1, -20}", "D - Cube", "I - Triangle"); Console.WriteLine("{0, -20} {1, -20}", "E - Ellipse", "J - Tetrahedron"); Console.WriteLine("\n0 - List all shapes | THEN HIT ENTER TO EXIT\n"); Console.WriteLine($"You have entered {Shape.GetCount()} shape(s) so far\n"); } /// <summary> /// This is the dataSetter method, used to easily prompt the user /// to input information when needed. /// </summary> void dataSetter() { Console.Write("Enter your choice: "); input = Console.ReadLine(); } /// <summary> /// This is the outputTemplate method, used to easily display the output Heading /// when the user prompt for the output of the Shape array. /// </summary> void outputTemplate() { Console.WriteLine("{0, -11} {1, -11} {2, -11} {3, -45}", "Shape", "Area", "Volume", "Details"); Console.WriteLine("{0, -11} {1, -11} {2, -11} {3, -45}", "===========", "===========", "===========", "============================================="); } }