From 2566804da8d290e74c9fdae0034b5a6faf1eb5d6 Mon Sep 17 00:00:00 2001 From: Vartika06 Date: Sat, 2 Feb 2019 14:48:15 +0530 Subject: [PATCH 1/4] Working with arrays --- AcmeApp/Acme.Biz/Product.cs | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/AcmeApp/Acme.Biz/Product.cs b/AcmeApp/Acme.Biz/Product.cs index d923536..0d300a7 100644 --- a/AcmeApp/Acme.Biz/Product.cs +++ b/AcmeApp/Acme.Biz/Product.cs @@ -16,7 +16,22 @@ public class Product #region Constructors public Product() { + var colorOptions = new string[4] { "Red", "Espresso", "White", "Navy" }; + + var brown = Array.IndexOf(colorOptions, "Espresso"); + colorOptions.SetValue("Blue", 3); + + for (int i = 0; i < colorOptions.Length; i++) + { + colorOptions[i] = colorOptions[i].ToLower(); + } + + foreach (var color in colorOptions) + { + Console.WriteLine($"The color is {color}"); + } } + public Product(int productId, string productName, string description) : this() From 3bc28ca0b71f9b970c82e916325c89a267961c6e Mon Sep 17 00:00:00 2001 From: Vartika06 Date: Sat, 2 Feb 2019 16:05:07 +0530 Subject: [PATCH 2/4] Working with Generics --- AcmeApp/Acme.Biz/Product.cs | 19 ++++++++- AcmeApp/Acme.Biz/Vendor.cs | 4 +- AcmeApp/Acme.Biz/VendorRepository.cs | 6 +++ AcmeApp/Acme.Common/OperationResult.cs | 9 ++-- AcmeApp/Tests/Acme.BizTests/ProductTests.cs | 5 ++- .../Acme.BizTests/VendorRepositoryTests.cs | 41 +++++++++++++++++++ AcmeApp/Tests/Acme.BizTests/VendorTests.cs | 12 +++--- 7 files changed, 79 insertions(+), 17 deletions(-) diff --git a/AcmeApp/Acme.Biz/Product.cs b/AcmeApp/Acme.Biz/Product.cs index 0d300a7..035f283 100644 --- a/AcmeApp/Acme.Biz/Product.cs +++ b/AcmeApp/Acme.Biz/Product.cs @@ -99,9 +99,24 @@ public Vendor ProductVendor /// /// Percent used to mark up the cost. /// - public decimal CalculateSuggestedPrice(decimal markupPercent) => - this.Cost + (this.Cost * markupPercent / 100); + public OperationResult CalculateSuggestedPrice(decimal markupPercent) + { + var message = string.Empty; + if(markupPercent <= 0m) + { + message = "Invalid markup percentage"; + } + else if(markupPercent < 10) + { + message = "Below recommended markup percentage"; + } + + var value = this.Cost + (this.Cost * markupPercent / 100); + var operationResult = new OperationResult(value, message); + return operationResult; + } + public override string ToString() { return this.ProductName + " (" + this.ProductId + ")"; diff --git a/AcmeApp/Acme.Biz/Vendor.cs b/AcmeApp/Acme.Biz/Vendor.cs index f12c22e..f130650 100644 --- a/AcmeApp/Acme.Biz/Vendor.cs +++ b/AcmeApp/Acme.Biz/Vendor.cs @@ -24,7 +24,7 @@ public class Vendor /// Requested delivery date. /// Delivery instructions. /// - public OperationResult PlaceOrder(Product product, int quantity, + public OperationResult PlaceOrder(Product product, int quantity, DateTimeOffset? deliverBy = null, string instructions = "standard delivery") { @@ -61,7 +61,7 @@ public OperationResult PlaceOrder(Product product, int quantity, { success = true; } - var operationResult = new OperationResult(success, orderText); + var operationResult = new OperationResult(success, orderText); return operationResult; } diff --git a/AcmeApp/Acme.Biz/VendorRepository.cs b/AcmeApp/Acme.Biz/VendorRepository.cs index 20e3b63..8270f3c 100644 --- a/AcmeApp/Acme.Biz/VendorRepository.cs +++ b/AcmeApp/Acme.Biz/VendorRepository.cs @@ -29,6 +29,12 @@ public Vendor Retrieve(int vendorId) return vendor; } + public T RetrieveValue(string sql, T defaultValue) + { + T value = defaultValue; + return value; + } + /// /// Save data for one vendor. /// diff --git a/AcmeApp/Acme.Common/OperationResult.cs b/AcmeApp/Acme.Common/OperationResult.cs index 511bb4e..1c7bd73 100644 --- a/AcmeApp/Acme.Common/OperationResult.cs +++ b/AcmeApp/Acme.Common/OperationResult.cs @@ -4,20 +4,19 @@ /// Provides a success flag and message /// useful as a method return type. /// - public class OperationResult + public class OperationResult { public OperationResult() { } - public OperationResult(bool success, string message) : this() + public OperationResult(T result, string message) : this() { - this.Success = success; + this.Result = result; this.Message = message; } - public bool Success { get; set; } + public T Result { get; set; } public string Message { get; set; } } - } diff --git a/AcmeApp/Tests/Acme.BizTests/ProductTests.cs b/AcmeApp/Tests/Acme.BizTests/ProductTests.cs index 344f787..d974398 100644 --- a/AcmeApp/Tests/Acme.BizTests/ProductTests.cs +++ b/AcmeApp/Tests/Acme.BizTests/ProductTests.cs @@ -19,13 +19,14 @@ public void CalculateSuggestedPriceTest() // Arrange var currentProduct = new Product(1, "Saw", ""); currentProduct.Cost = 50m; - var expected = 55m; + var expected = new OperationResult(55m, ""); // Act var actual = currentProduct.CalculateSuggestedPrice(10m); // Assert - Assert.AreEqual(expected, actual); + Assert.AreEqual(expected.Result, actual.Result); + Assert.AreEqual(expected.Message, actual.Message); } [TestMethod()] diff --git a/AcmeApp/Tests/Acme.BizTests/VendorRepositoryTests.cs b/AcmeApp/Tests/Acme.BizTests/VendorRepositoryTests.cs index cfd87df..386d86e 100644 --- a/AcmeApp/Tests/Acme.BizTests/VendorRepositoryTests.cs +++ b/AcmeApp/Tests/Acme.BizTests/VendorRepositoryTests.cs @@ -11,6 +11,47 @@ namespace Acme.Biz.Tests [TestClass()] public class VendorRepositoryTests { + [TestMethod()] + public void RetrieveValueTest() + { + //Arrange + var repository = new VendorRepository(); + var expected = 42; + //Act + var actual = repository.RetrieveValue("Select ...", 42); + + //Assert + Assert.AreEqual(expected, actual); + } + + [TestMethod()] + public void RetrieveStringValueTest() + { + //Arrange + var repository = new VendorRepository(); + var expected = "test"; + + //Act + var actual = repository.RetrieveValue("Select ...", "test"); + + //Assert + Assert.AreEqual(expected, actual); + } + + [TestMethod()] + public void RetrieveValueObjectTest() + { + //Arrange + var repository = new VendorRepository(); + var vendor = new Vendor(); + var expected = vendor; + + //Act + var actual = repository.RetrieveValue("Select ...", vendor); + + //Assert + Assert.AreEqual(expected, actual); + } } } \ No newline at end of file diff --git a/AcmeApp/Tests/Acme.BizTests/VendorTests.cs b/AcmeApp/Tests/Acme.BizTests/VendorTests.cs index 91b70ec..6622d25 100644 --- a/AcmeApp/Tests/Acme.BizTests/VendorTests.cs +++ b/AcmeApp/Tests/Acme.BizTests/VendorTests.cs @@ -63,7 +63,7 @@ public void PlaceOrderTest() // Arrange var vendor = new Vendor(); var product = new Product(1, "Saw", ""); - var expected = new OperationResult(true, + var expected = new OperationResult(true, "Order from Acme, Inc\r\nProduct: Saw\r\nQuantity: 12" + "\r\nInstructions: standard delivery"); @@ -71,7 +71,7 @@ public void PlaceOrderTest() var actual = vendor.PlaceOrder(product, 12); // Assert - Assert.AreEqual(expected.Success, actual.Success); + Assert.AreEqual(expected.Result, actual.Result); Assert.AreEqual(expected.Message, actual.Message); } [TestMethod()] @@ -80,7 +80,7 @@ public void PlaceOrder_3Parameters() // Arrange var vendor = new Vendor(); var product = new Product(1, "Saw", ""); - var expected = new OperationResult(true, + var expected = new OperationResult(true, "Order from Acme, Inc\r\nProduct: Saw\r\nQuantity: 12" + "\r\nDeliver By: " + new DateTime(2018,10,25).ToString("d") + "\r\nInstructions: standard delivery"); @@ -90,7 +90,7 @@ public void PlaceOrder_3Parameters() new DateTimeOffset(2018, 10, 25, 0, 0, 0, new TimeSpan(-7, 0, 0))); // Assert - Assert.AreEqual(expected.Success, actual.Success); + Assert.AreEqual(expected.Result, actual.Result); Assert.AreEqual(expected.Message, actual.Message); } @@ -114,7 +114,7 @@ public void PlaceOrder_NoDeliveryDate() // Arrange var vendor = new Vendor(); var product = new Product(1, "Saw", ""); - var expected = new OperationResult(true, + var expected = new OperationResult(true, "Order from Acme, Inc\r\nProduct: Saw\r\nQuantity: 12" + "\r\nInstructions: Deliver to Suite 42"); @@ -123,7 +123,7 @@ public void PlaceOrder_NoDeliveryDate() instructions: "Deliver to Suite 42"); // Assert - Assert.AreEqual(expected.Success, actual.Success); + Assert.AreEqual(expected.Result, actual.Result); Assert.AreEqual(expected.Message, actual.Message); } From c997c576a09c15c25b19d7073957b92dca796c19 Mon Sep 17 00:00:00 2001 From: Vartika06 Date: Sun, 3 Feb 2019 15:32:26 +0530 Subject: [PATCH 3/4] Generic Lists --- AcmeApp/Acme.Biz/Product.cs | 17 +++--------- AcmeApp/Acme.Biz/Vendor.cs | 21 +++++++++++++++ AcmeApp/Acme.Biz/VendorRepository.cs | 26 +++++++++++++++++++ .../Acme.BizTests/VendorRepositoryTests.cs | 16 ++++++++++++ 4 files changed, 67 insertions(+), 13 deletions(-) diff --git a/AcmeApp/Acme.Biz/Product.cs b/AcmeApp/Acme.Biz/Product.cs index 035f283..45933a3 100644 --- a/AcmeApp/Acme.Biz/Product.cs +++ b/AcmeApp/Acme.Biz/Product.cs @@ -1,5 +1,4 @@ using Acme.Common; -using static Acme.Common.LoggingService; using System; using System.Collections.Generic; using System.Linq; @@ -16,20 +15,12 @@ public class Product #region Constructors public Product() { - var colorOptions = new string[4] { "Red", "Espresso", "White", "Navy" }; - - var brown = Array.IndexOf(colorOptions, "Espresso"); - colorOptions.SetValue("Blue", 3); - - for (int i = 0; i < colorOptions.Length; i++) + var colorOptions = new List() { - colorOptions[i] = colorOptions[i].ToLower(); - } + "Red", "Espresso", "White", "Navy" + }; - foreach (var color in colorOptions) - { - Console.WriteLine($"The color is {color}"); - } + Console.WriteLine(colorOptions); } public Product(int productId, diff --git a/AcmeApp/Acme.Biz/Vendor.cs b/AcmeApp/Acme.Biz/Vendor.cs index f130650..4cffac4 100644 --- a/AcmeApp/Acme.Biz/Vendor.cs +++ b/AcmeApp/Acme.Biz/Vendor.cs @@ -70,6 +70,27 @@ public override string ToString() return $"Vendor: {this.CompanyName} ({this.VendorId})"; } + public override bool Equals(object obj) + { + if(obj == null || this.GetType() != obj.GetType()) + { + return false; + } + + if (obj is Vendor compareVendor && + this.VendorId == compareVendor.VendorId && + string.Equals(this.CompanyName, compareVendor.CompanyName, StringComparison.InvariantCultureIgnoreCase) && + this.Email == compareVendor.Email) + { + return true; + } + return base.Equals(obj); + } + + public override int GetHashCode() + { + return base.GetHashCode(); + } /// /// Sends an email to welcome a new vendor. diff --git a/AcmeApp/Acme.Biz/VendorRepository.cs b/AcmeApp/Acme.Biz/VendorRepository.cs index 8270f3c..8c8aac6 100644 --- a/AcmeApp/Acme.Biz/VendorRepository.cs +++ b/AcmeApp/Acme.Biz/VendorRepository.cs @@ -8,6 +8,8 @@ namespace Acme.Biz { public class VendorRepository { + private List vendors; + /// /// Retrieve one vendor. /// @@ -29,6 +31,30 @@ public Vendor Retrieve(int vendorId) return vendor; } + public List Retrieve() + { + if(vendors == null) + { + vendors = new List(); + + vendors.Add(new Vendor() { VendorId = 1, CompanyName = "ABC Corp", Email = "abc@abc.com" }); + vendors.Add(new Vendor() { VendorId = 2, CompanyName = "XYZ Inc", Email = "xyx@xyz.com" }); + } + + foreach (var vendor in vendors) + { + vendor.CompanyName = vendor.CompanyName.ToUpper(); + Console.WriteLine(vendor); + } + + for (int i = 0; i < vendors.Count; i++) + { + Console.WriteLine(vendors[i]); + } + + return vendors; + } + public T RetrieveValue(string sql, T defaultValue) { T value = defaultValue; diff --git a/AcmeApp/Tests/Acme.BizTests/VendorRepositoryTests.cs b/AcmeApp/Tests/Acme.BizTests/VendorRepositoryTests.cs index 386d86e..e615959 100644 --- a/AcmeApp/Tests/Acme.BizTests/VendorRepositoryTests.cs +++ b/AcmeApp/Tests/Acme.BizTests/VendorRepositoryTests.cs @@ -53,5 +53,21 @@ public void RetrieveValueObjectTest() //Assert Assert.AreEqual(expected, actual); } + + [TestMethod()] + public void RetrieveTest() + { + //Arrange + var repository = new VendorRepository(); + var expected = new List(); + expected.Add(new Vendor() { VendorId = 1, CompanyName = "ABC Corp", Email = "abc@abc.com" }); + expected.Add(new Vendor() { VendorId = 2, CompanyName = "XYZ Inc", Email = "xyx@xyz.com" }); + + //Act + var actual = repository.Retrieve(); + + //Assert + CollectionAssert.AreEqual(expected, actual); + } } } \ No newline at end of file From 14cbc7af22da04b50066eef665a19a6c19ab0596 Mon Sep 17 00:00:00 2001 From: Vartika06 Date: Sun, 3 Feb 2019 16:25:50 +0530 Subject: [PATCH 4/4] Generic Dictionaries --- AcmeApp/Acme.Biz/Product.cs | 12 +++++++++ AcmeApp/Acme.Biz/VendorRepository.cs | 26 +++++++++++++++++++ .../Acme.BizTests/VendorRepositoryTests.cs | 18 +++++++++++++ 3 files changed, 56 insertions(+) diff --git a/AcmeApp/Acme.Biz/Product.cs b/AcmeApp/Acme.Biz/Product.cs index 45933a3..0cc705d 100644 --- a/AcmeApp/Acme.Biz/Product.cs +++ b/AcmeApp/Acme.Biz/Product.cs @@ -15,12 +15,24 @@ public class Product #region Constructors public Product() { + #region Generic Lists var colorOptions = new List() { "Red", "Espresso", "White", "Navy" }; Console.WriteLine(colorOptions); + + #endregion Generic Lists + + var states = new Dictionary + { + { "CA", "California" }, + { "WA", "Washington DC" }, + { "NY", "New York" } + }; + + Console.WriteLine(states); } public Product(int productId, diff --git a/AcmeApp/Acme.Biz/VendorRepository.cs b/AcmeApp/Acme.Biz/VendorRepository.cs index 8c8aac6..72dbb89 100644 --- a/AcmeApp/Acme.Biz/VendorRepository.cs +++ b/AcmeApp/Acme.Biz/VendorRepository.cs @@ -55,6 +55,32 @@ public List Retrieve() return vendors; } + public Dictionary RetrieveWithKeys() + { + var vendors = new Dictionary() + { + { "ABC Corp", new Vendor{ VendorId = 1, CompanyName = "ABC Corp", Email = "abc@abc.com" } }, + { "XYZ Inc", new Vendor{ VendorId = 2, CompanyName = "XYZ Inc", Email = "xyz@xyz.com" } }, + }; + + foreach (var element in vendors) + { + Console.WriteLine($"Key : {element.Key} & Value : {element.Value}"); + } + + //foreach (var companyName in vendors.Keys) + //{ + // Console.WriteLine(vendors[companyName]); + //} + + //if(vendors.TryGetValue("XYZ Inc", out var vendor)) + //{ + //Console.WriteLine(vendor); + //} + + return vendors; + } + public T RetrieveValue(string sql, T defaultValue) { T value = defaultValue; diff --git a/AcmeApp/Tests/Acme.BizTests/VendorRepositoryTests.cs b/AcmeApp/Tests/Acme.BizTests/VendorRepositoryTests.cs index e615959..c2d5a2c 100644 --- a/AcmeApp/Tests/Acme.BizTests/VendorRepositoryTests.cs +++ b/AcmeApp/Tests/Acme.BizTests/VendorRepositoryTests.cs @@ -69,5 +69,23 @@ public void RetrieveTest() //Assert CollectionAssert.AreEqual(expected, actual); } + + [TestMethod()] + public void RetrieveWithKeysTest() + { + //Arrange + var repository = new VendorRepository(); + var expected = new Dictionary() + { + { "ABC Corp", new Vendor{ VendorId = 1, CompanyName = "ABC Corp", Email = "abc@abc.com" } }, + { "XYZ Inc", new Vendor{ VendorId = 2, CompanyName = "XYZ Inc", Email = "xyz@xyz.com" } }, + }; + + //Act + var actual = repository.RetrieveWithKeys(); + + //Assert + CollectionAssert.AreEqual(expected, actual); + } } } \ No newline at end of file