diff --git a/Order.Management/Circle.cs b/Order.Management/Circle.cs index 9824ecc..1d5f2e3 100644 --- a/Order.Management/Circle.cs +++ b/Order.Management/Circle.cs @@ -1,37 +1,14 @@ -using System; -using System.Collections.Generic; -using System.Text; +using System.Collections.Generic; namespace Order.Management { - class Circle : Shape + internal class Circle : Shape { - public int circlePrice = 3; - public Circle(int red, int blue, int yellow) + private const int CirclePrice = 3; + public Circle(List shapeVariants) : base(shapeVariants) { Name = "Circle"; - base.Price = circlePrice; - AdditionalCharge = 1; - base.NumberOfRedShape = red; - base.NumberOfBlueShape = blue; - base.NumberOfYellowShape = yellow; - } - public override int Total() - { - return RedCirclesTotal() + BlueCirclesTotal() + YellowCirclesTotal(); - } - - public int RedCirclesTotal() - { - return (base.NumberOfRedShape * Price); - } - public int BlueCirclesTotal() - { - return (base.NumberOfBlueShape * Price); - } - public int YellowCirclesTotal() - { - return (base.NumberOfYellowShape * Price); + Price = CirclePrice; } } -} +} \ No newline at end of file diff --git a/Order.Management/Constants.cs b/Order.Management/Constants.cs new file mode 100644 index 0000000..898c348 --- /dev/null +++ b/Order.Management/Constants.cs @@ -0,0 +1,24 @@ +namespace Order.Management +{ + internal static class Constants + { + public const int AdditionalCharge = 1; + + public const int SmallTableWidth = 20; + public const int LargeTableWidth = 73; + } + + public enum ShapeTypes + { + Square = 0, + Triangle = 1, + Circle = 2, + } + + public enum ShapeColors + { + Red = 1, + Blue = 2, + Yellow = 3, + } +} \ No newline at end of file diff --git a/Order.Management/CuttingListReport.cs b/Order.Management/CuttingListReport.cs index 125d45f..edbf0a0 100644 --- a/Order.Management/CuttingListReport.cs +++ b/Order.Management/CuttingListReport.cs @@ -1,68 +1,26 @@ using System; using System.Collections.Generic; -using System.Text; namespace Order.Management { - class CuttingListReport : Order + internal class CuttingListReport : Order { - public int tableWidth = 20; public CuttingListReport(string customerName, string customerAddress, string dueDate, List shapes) + : base(customerName, customerAddress, dueDate, shapes) { - base.CustomerName = customerName; - base.Address = customerAddress; - base.DueDate = dueDate; - base.OrderedBlocks = shapes; } public override void GenerateReport() { Console.WriteLine("\nYour cutting list has been generated: "); Console.WriteLine(base.ToString()); - generateTable(); - } - public void generateTable() - { - PrintLine(); - PrintRow(" ", " Qty "); - PrintLine(); - PrintRow("Square",base.OrderedBlocks[0].TotalQuantityOfShape().ToString()); - PrintRow("Triangle", base.OrderedBlocks[1].TotalQuantityOfShape().ToString()); - PrintRow("Circle", base.OrderedBlocks[2].TotalQuantityOfShape().ToString()); - PrintLine(); - } - public void PrintLine() - { - Console.WriteLine(new string('-', tableWidth)); - } - public void PrintRow(params string[] columns) - { - int width = (tableWidth - columns.Length) / columns.Length; - string row = "|"; - - foreach (string column in columns) - { - row += AlignCentre(column, width) + "|"; - } - - Console.WriteLine(row); + GenerateTable(); } - public string AlignCentre(string text, int width) + public void GenerateTable() { - text = text.Length > width ? text.Substring(0, width - 3) + "..." : text; - - if (string.IsNullOrEmpty(text)) - { - return new string(' ', width); - } - else - { - return text.PadRight(width - (width - text.Length) / 2).PadLeft(width); - } + ReportHelper.GenerateCuttingListReport(OrderedBlocks); } - - } -} +} \ No newline at end of file diff --git a/Order.Management/InvoiceReport.cs b/Order.Management/InvoiceReport.cs index 78443c3..50a24ef 100644 --- a/Order.Management/InvoiceReport.cs +++ b/Order.Management/InvoiceReport.cs @@ -1,18 +1,14 @@ using System; using System.Collections.Generic; -using System.Text; +using System.Linq; namespace Order.Management { - class InvoiceReport : Order + internal class InvoiceReport : Order { - public int tableWidth = 73; public InvoiceReport(string customerName, string customerAddress, string dueDate, List shapes) + : base(customerName, customerAddress, dueDate, shapes) { - base.CustomerName = customerName; - base.Address = customerAddress; - base.DueDate = dueDate; - base.OrderedBlocks = shapes; } public override void GenerateReport() @@ -20,79 +16,40 @@ public override void GenerateReport() Console.WriteLine("\nYour invoice report has been generated: "); Console.WriteLine(base.ToString()); GenerateTable(); - OrderSquareDetails(); - OrderTriangleDetails(); - OrderCircleDetails(); - RedPaintSurcharge(); - } + Console.WriteLine(); - public void RedPaintSurcharge() - { - Console.WriteLine("Red Color Surcharge " + TotalAmountOfRedShapes() + " @ $" + base.OrderedBlocks[0].AdditionalCharge + " ppi = $" + TotalPriceRedPaintSurcharge()); - } + var shapeTypes = Enum.GetValues(typeof(ShapeTypes)).Cast().ToList(); + foreach (var shapeType in shapeTypes) + { + OrderTypeDetails(shapeType); + } - public int TotalAmountOfRedShapes() - { - return base.OrderedBlocks[0].NumberOfRedShape + base.OrderedBlocks[1].NumberOfRedShape + - base.OrderedBlocks[2].NumberOfRedShape; + VariantSurcharge(); } - public int TotalPriceRedPaintSurcharge() - { - return TotalAmountOfRedShapes() * base.OrderedBlocks[0].AdditionalCharge; - } - public void GenerateTable() - { - PrintLine(); - PrintRow(" ", " Red ", " Blue ", " Yellow "); - PrintLine(); - PrintRow("Square", base.OrderedBlocks[0].NumberOfRedShape.ToString(), base.OrderedBlocks[0].NumberOfBlueShape.ToString(), base.OrderedBlocks[0].NumberOfYellowShape.ToString()); - PrintRow("Triangle", base.OrderedBlocks[1].NumberOfRedShape.ToString(), base.OrderedBlocks[1].NumberOfBlueShape.ToString(), base.OrderedBlocks[1].NumberOfYellowShape.ToString()); - PrintRow("Circle", base.OrderedBlocks[2].NumberOfRedShape.ToString(), base.OrderedBlocks[2].NumberOfBlueShape.ToString(), base.OrderedBlocks[2].NumberOfYellowShape.ToString()); - PrintLine(); - } - public void OrderSquareDetails() - { - Console.WriteLine("\nSquares " + base.OrderedBlocks[0].TotalQuantityOfShape() + " @ $" + base.OrderedBlocks[0].Price + " ppi = $" + base.OrderedBlocks[0].Total()); - } - public void OrderTriangleDetails() + public void VariantSurcharge() { - Console.WriteLine("Triangles " + base.OrderedBlocks[1].TotalQuantityOfShape() + " @ $" + base.OrderedBlocks[1].Price + " ppi = $" + base.OrderedBlocks[1].Total()); + Console.WriteLine("Shape Surcharge " + TotalQtyOfSurchargeShapes() + " @ $" + Constants.AdditionalCharge + " ppi = $" + TotalPriceSurcharge()); } - public void OrderCircleDetails() + + public int TotalQtyOfSurchargeShapes() { - Console.WriteLine("Circles " + base.OrderedBlocks[2].TotalQuantityOfShape() + " @ $" + base.OrderedBlocks[2].Price + " ppi = $" + base.OrderedBlocks[2].Total()); + return base.OrderedBlocks.Sum(b => b.AdditionalChargeQty()); } - public void PrintLine() + + public int TotalPriceSurcharge() { - Console.WriteLine(new string('-', tableWidth)); + return OrderedBlocks.Sum(b => b.AdditionalChargeTotal()); } - public void PrintRow(params string[] columns) + public void GenerateTable() { - int width = (tableWidth - columns.Length) / columns.Length; - string row = "|"; - - foreach (string column in columns) - { - row += AlignCentre(column, width) + "|"; - } - - Console.WriteLine(row); + ReportHelper.GenerateReport(OrderedBlocks); } - public string AlignCentre(string text, int width) + public void OrderTypeDetails(ShapeTypes shapeType) { - text = text.Length > width ? text.Substring(0, width - 3) + "..." : text; - - if (string.IsNullOrEmpty(text)) - { - return new string(' ', width); - } - else - { - return text.PadRight(width - (width - text.Length) / 2).PadLeft(width); - } + Console.WriteLine($"{shapeType.ToString()}s " + OrderedBlocks[(int)shapeType].TotalQuantityOfShape() + " @ $" + OrderedBlocks[(int)shapeType].Price + " ppi = $" + base.OrderedBlocks[(int)shapeType].Total()); } } -} +} \ No newline at end of file diff --git a/Order.Management/Order.cs b/Order.Management/Order.cs index 235c789..7400d82 100644 --- a/Order.Management/Order.cs +++ b/Order.Management/Order.cs @@ -1,22 +1,24 @@ using System; using System.Collections.Generic; -using System.Text; namespace Order.Management { - abstract class Order + internal class PaintingReport : Order { - public string CustomerName { get; set; } - public string Address { get; set; } - public string DueDate { get; set; } - public int OrderNumber { get; set; } - public List OrderedBlocks { get; set; } - - public abstract void GenerateReport(); + public PaintingReport(string customerName, string customerAddress, string dueDate, List shapes) + : base(customerName, customerAddress, dueDate, shapes) + { + } + public override void GenerateReport() + { + Console.WriteLine("\nYour painting report has been generated: "); + Console.WriteLine(base.ToString()); + GenerateTable(); + } - public string ToString() + public void GenerateTable() { - return "\nName: " + CustomerName + " Address: " + Address + " Due Date: " + DueDate + " Order #: " + OrderNumber; + ReportHelper.GenerateReport(OrderedBlocks); } } -} +} \ No newline at end of file diff --git a/Order.Management/Program.cs b/Order.Management/Program.cs index 1422f85..aa3226e 100644 --- a/Order.Management/Program.cs +++ b/Order.Management/Program.cs @@ -1,12 +1,13 @@ using System; using System.Collections.Generic; +using System.Linq; namespace Order.Management { class Program { - // Main entry - static void Main(string[] args) + // Main entry + private static void Main(string[] args) { var (customerName, address, dueDate) = CustomerInfoInput(); @@ -18,58 +19,33 @@ static void Main(string[] args) PaintingReport(customerName, address, dueDate, orderedShapes); } - - // Order Circle Input - public static Circle OrderCirclesInput() - { - Console.Write("\nPlease input the number of Red Circle: "); - int redCircle = Convert.ToInt32(userInput()); - Console.Write("Please input the number of Blue Circle: "); - int blueCircle = Convert.ToInt32(userInput()); - Console.Write("Please input the number of Yellow Circle: "); - int yellowCircle = Convert.ToInt32(userInput()); - - Circle circle = new Circle(redCircle, blueCircle, yellowCircle); - return circle; - } - - // Order Squares Input - public static Square OrderSquaresInput() - { - Console.Write("\nPlease input the number of Red Squares: "); - int redSquare = Convert.ToInt32(userInput()); - Console.Write("Please input the number of Blue Squares: "); - int blueSquare = Convert.ToInt32(userInput()); - Console.Write("Please input the number of Yellow Squares: "); - int yellowSquare = Convert.ToInt32(userInput()); - - Square square = new Square(redSquare, blueSquare, yellowSquare); - return square; - } - // Order Triangles Input - public static Triangle OrderTrianglesInput() + // Order Shape Input + public static Shape OrderShapeInput(ShapeTypes shapeType) { - Console.Write("\nPlease input the number of Red Triangles: "); - int redTriangle = Convert.ToInt32(userInput()); - Console.Write("Please input the number of Blue Triangles: "); - int blueTriangle = Convert.ToInt32(userInput()); - Console.Write("Please input the number of Yellow Triangles: "); - int yellowTriangle = Convert.ToInt32(userInput()); - - Triangle triangle = new Triangle(redTriangle, blueTriangle, yellowTriangle); - return triangle; + var shapeVariants = ShapeVariants(); + + Console.WriteLine(); + foreach (var variant in shapeVariants) + { + Console.Write($"Please input the number of {variant.ShapeColor.ToString()} {shapeType.ToString()}: "); + int.TryParse(UserInput(allowEmptyInput: true), out var qty); + variant.Qty = qty; + } + + var shape = ShapeFactory.GetShape((int)shapeType, shapeVariants); + return shape; } + // User Console Input - public static string userInput() + public static string UserInput(bool allowEmptyInput = false) { - string input = Console.ReadLine(); - while (string.IsNullOrEmpty(input)) + var input = Console.ReadLine(); + while (!allowEmptyInput && string.IsNullOrEmpty(input)) { Console.WriteLine("please enter valid details"); input = Console.ReadLine(); - } return input; } @@ -77,21 +53,21 @@ public static string userInput() // Generate Painting Report private static void PaintingReport(string customerName, string address, string dueDate, List orderedShapes) { - PaintingReport paintingReport = new PaintingReport(customerName, address, dueDate, orderedShapes); + var paintingReport = new PaintingReport(customerName, address, dueDate, orderedShapes); paintingReport.GenerateReport(); } // Generate Painting Report private static void CuttingListReport(string customerName, string address, string dueDate, List orderedShapes) { - CuttingListReport cuttingListReport = new CuttingListReport(customerName, address, dueDate, orderedShapes); + var cuttingListReport = new CuttingListReport(customerName, address, dueDate, orderedShapes); cuttingListReport.GenerateReport(); } // Generate Invoice Report private static void InvoiceReport(string customerName, string address, string dueDate, List orderedShapes) { - InvoiceReport invoiceReport = new InvoiceReport(customerName, address, dueDate, orderedShapes); + var invoiceReport = new InvoiceReport(customerName, address, dueDate, orderedShapes); invoiceReport.GenerateReport(); } @@ -99,26 +75,33 @@ private static void InvoiceReport(string customerName, string address, string du private static (string customerName, string address, string dueDate) CustomerInfoInput() { Console.Write("Please input your Name: "); - string customerName = userInput(); + var customerName = UserInput(); + Console.Write("Please input your Address: "); - string address = userInput(); + var address = UserInput(); + Console.Write("Please input your Due Date: "); - string dueDate = userInput(); + var dueDate = UserInput(); + return (customerName, address, dueDate); } // Get order input private static List CustomerOrderInput() { - Square square = OrderSquaresInput(); - Triangle triangle = OrderTrianglesInput(); - Circle circle = OrderCirclesInput(); - - var orderedShapes = new List(); - orderedShapes.Add(square); - orderedShapes.Add(triangle); - orderedShapes.Add(circle); - return orderedShapes; + var shapeTypes = Enum.GetValues(typeof(ShapeTypes)).Cast(); + return shapeTypes.Select(OrderShapeInput).ToList(); + } + + // Generate Default Shape Variants + public static List ShapeVariants() + { + var shapeVariants = Enum.GetValues(typeof(ShapeColors)).Cast() + .Select(v => new ShapeVariant(v, qty: 0, additionalCharge: v == ShapeColors.Red)).ToList(); + + // additionalCharge: Is Additional Charge Flag to Identify Surcharge Variants + + return shapeVariants; } } } diff --git a/Order.Management/ReportHelper.cs b/Order.Management/ReportHelper.cs new file mode 100644 index 0000000..21923c9 --- /dev/null +++ b/Order.Management/ReportHelper.cs @@ -0,0 +1,90 @@ +using System; +using System.Collections.Generic; +using System.Linq; + +namespace Order.Management +{ + public static class ReportHelper + { + public static void GenerateReport(List shapes) + { + var headerRows = Enum.GetValues(typeof(ShapeColors)).Cast().ToList(); + + var reportHeader = new List { " " }; + reportHeader.AddRange(headerRows.Select(headerRow => $" {headerRow.ToString()} ")); + + PrintLine(Constants.LargeTableWidth); + PrintRow(Constants.LargeTableWidth, reportHeader.ToArray()); + PrintLine(Constants.LargeTableWidth); + + var headerColumns = Enum.GetValues(typeof(ShapeTypes)).Cast().ToList(); + foreach (var shapeType in headerColumns) + { + var reportContent = new List { shapeType.ToString() }; + + var shapeColors = Enum.GetValues(typeof(ShapeColors)).Cast().ToList(); + reportContent.AddRange(shapeColors.Select(shapeColor => shapes[(int)shapeType].TotalQtyByShapeColor(shapeColor) > 0 + ? shapes[(int)shapeType].TotalQtyByShapeColor(shapeColor).ToString() + : "-")); + + PrintRow(Constants.LargeTableWidth, reportContent.ToArray()); + } + + PrintLine(Constants.LargeTableWidth); + } + + public static void GenerateCuttingListReport(List shapes) + { + var shapeTypes = Enum.GetValues(typeof(ShapeTypes)).Cast().ToList(); + var reportHeader = new List() { " ", " Qty " }; + + PrintLine(Constants.SmallTableWidth); + PrintRow(Constants.SmallTableWidth, reportHeader.ToArray()); + PrintLine(Constants.SmallTableWidth); + + foreach (var shapeType in shapeTypes) + { + PrintRow(Constants.SmallTableWidth, shapeType.ToString(), shapes[(int)shapeType].TotalQuantityOfShape().ToString()); + } + + PrintLine(Constants.SmallTableWidth); + } + + #region Hellper Function + + public static void PrintLine(int reportWidth) + { + Console.WriteLine(new string('-', reportWidth)); + } + + public static void PrintRow(int reportWidth, params string[] columns) + { + int width = (reportWidth - columns.Length) / columns.Length; + string row = "|"; + + foreach (string column in columns) + { + row += AlignCentre(column, width) + "|"; + } + + Console.WriteLine(row); + } + + public static string AlignCentre(string text, int width) + { + text = text.Length > width ? text.Substring(0, width - 3) + "..." : text; + + if (string.IsNullOrEmpty(text)) + { + return new string(' ', width); + } + else + { + return text.PadRight(width - (width - text.Length) / 2).PadLeft(width); + } + } + + #endregion + + } +} diff --git a/Order.Management/Shape.cs b/Order.Management/Shape.cs index 7f5c61c..6e18a23 100644 --- a/Order.Management/Shape.cs +++ b/Order.Management/Shape.cs @@ -1,27 +1,43 @@ -using System; -using System.Collections.Generic; -using System.Text; +using System.Collections.Generic; +using System.Linq; namespace Order.Management { - abstract class Shape + public abstract class Shape { - public string Name { get; set; } - public int Price { get; set; } - public int AdditionalCharge { get; set; } - public int NumberOfRedShape { get; set; } - public int NumberOfBlueShape { get; set; } - public int NumberOfYellowShape { get; set; } + protected Shape(List shapeVariants) + { + ShapeVariants = shapeVariants; + } + + internal string Name { get; set; } + internal int Price { get; set; } + protected List ShapeVariants { get; set; } + public int TotalQuantityOfShape() { - return NumberOfRedShape + NumberOfBlueShape + NumberOfYellowShape; + return ShapeVariants?.Sum(v => v.Qty) ?? 0; + } + + public int AdditionalChargeQty() + { + return (ShapeVariants?.Where(v => v.AdditionalCharge).Sum(v => v.Qty) ?? 0); } public int AdditionalChargeTotal() { - return NumberOfRedShape * AdditionalCharge; + return (ShapeVariants?.Where(v => v.AdditionalCharge).Sum(v => v.Qty) ?? 0) * Constants.AdditionalCharge; + } + + public int TotalQtyByShapeColor(ShapeColors shapeColor) + { + return (ShapeVariants?.Where(v => v.ShapeColor == shapeColor).Sum(v => v.Qty) ?? '-'); + } + + public int Total() + { + return (ShapeVariants?.Sum(v => v.Qty) ?? 0) * Price; } - public abstract int Total(); } -} +} \ No newline at end of file diff --git a/Order.Management/ShapeFactory.cs b/Order.Management/ShapeFactory.cs new file mode 100644 index 0000000..7232390 --- /dev/null +++ b/Order.Management/ShapeFactory.cs @@ -0,0 +1,19 @@ +using System; +using System.Collections.Generic; + +namespace Order.Management +{ + internal static class ShapeFactory + { + public static Shape GetShape(int shapeType, List shapeVariants) + { + return shapeType switch + { + (int)ShapeTypes.Circle => new Circle(shapeVariants), + (int)ShapeTypes.Square => new Square(shapeVariants), + (int)ShapeTypes.Triangle => new Triangle(shapeVariants), + _ => throw new NotImplementedException() // Fail Fast + }; + } + } +} \ No newline at end of file diff --git a/Order.Management/ShapeVariant.cs b/Order.Management/ShapeVariant.cs new file mode 100644 index 0000000..9d6b801 --- /dev/null +++ b/Order.Management/ShapeVariant.cs @@ -0,0 +1,16 @@ +namespace Order.Management +{ + public class ShapeVariant + { + public ShapeVariant(ShapeColors shapeColor, int qty, bool additionalCharge) + { + ShapeColor = shapeColor; + Qty = qty; + AdditionalCharge = additionalCharge; + } + + public ShapeColors ShapeColor { get; set; } + public int Qty { get; set; } + public bool AdditionalCharge { get; set; } + } +} \ No newline at end of file diff --git a/Order.Management/Square.cs b/Order.Management/Square.cs index 017601e..9a0b41d 100644 --- a/Order.Management/Square.cs +++ b/Order.Management/Square.cs @@ -1,40 +1,14 @@ -using System; -using System.Collections.Generic; -using System.Text; +using System.Collections.Generic; namespace Order.Management { - class Square : Shape + internal class Square : Shape { - - public int SquarePrice = 1; - - public Square(int numberOfRedSquares, int numberOfBlueSquares, int numberOfYellowSquares) + private const int SquarePrice = 1; + public Square(List shapeVariants) : base(shapeVariants) { Name = "Square"; - base.Price = SquarePrice; - AdditionalCharge = 1; - base.NumberOfRedShape = numberOfRedSquares; - base.NumberOfBlueShape = numberOfBlueSquares; - base.NumberOfYellowShape = numberOfYellowSquares; - } - - public override int Total() - { - return RedSquaresTotal() + BlueSquaresTotal() + YellowSquaresTotal(); - } - - public int RedSquaresTotal() - { - return (base.NumberOfRedShape * Price); - } - public int BlueSquaresTotal() - { - return (base.NumberOfBlueShape * Price); - } - public int YellowSquaresTotal() - { - return (base.NumberOfYellowShape * Price); + Price = SquarePrice; } } -} +} \ No newline at end of file diff --git a/Order.Management/Triangle.cs b/Order.Management/Triangle.cs index dbf48ff..6e787c3 100644 --- a/Order.Management/Triangle.cs +++ b/Order.Management/Triangle.cs @@ -1,39 +1,14 @@ -using System; -using System.Collections.Generic; -using System.Text; +using System.Collections.Generic; namespace Order.Management { - class Triangle : Shape + internal class Triangle : Shape { - public int TrianglePrice = 2; - public Triangle(int numberOfRedTriangles, int numberOfBlueTriangles, int numberOfYellowTriangles) + private const int TrianglePrice = 2; + public Triangle(List shapeVariants) : base(shapeVariants) { Name = "Triangle"; - base.Price = TrianglePrice; - AdditionalCharge = 1; - base.NumberOfRedShape = numberOfRedTriangles; - base.NumberOfBlueShape = numberOfBlueTriangles; - base.NumberOfYellowShape = numberOfYellowTriangles; + Price = TrianglePrice; } - - public override int Total() - { - return RedTrianglesTotal() + BlueTrianglesTotal() + YellowTrianglesTotal(); - } - - public int RedTrianglesTotal() - { - return (base.NumberOfRedShape * Price); - } - public int BlueTrianglesTotal() - { - return (base.NumberOfBlueShape * Price); - } - public int YellowTrianglesTotal() - { - return (base.NumberOfYellowShape * Price); - } - -} -} + } +} \ No newline at end of file