Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 36 additions & 3 deletions AcmeApp/Acme.Biz/Product.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using Acme.Common;
using static Acme.Common.LoggingService;
using System;
using System.Collections.Generic;
using System.Linq;
Expand All @@ -16,7 +15,26 @@ public class Product
#region Constructors
public Product()
{
#region Generic Lists
var colorOptions = new List<string>()
{
"Red", "Espresso", "White", "Navy"
};

Console.WriteLine(colorOptions);

#endregion Generic Lists

var states = new Dictionary<string, string>
{
{ "CA", "California" },
{ "WA", "Washington DC" },
{ "NY", "New York" }
};

Console.WriteLine(states);
}

public Product(int productId,
string productName,
string description) : this()
Expand Down Expand Up @@ -84,9 +102,24 @@ public Vendor ProductVendor
/// </summary>
/// <param name="markupPercent">Percent used to mark up the cost.</param>
/// <returns></returns>
public decimal CalculateSuggestedPrice(decimal markupPercent) =>
this.Cost + (this.Cost * markupPercent / 100);
public OperationResult<decimal> 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<decimal>(value, message);
return operationResult;
}

public override string ToString()
{
return this.ProductName + " (" + this.ProductId + ")";
Expand Down
25 changes: 23 additions & 2 deletions AcmeApp/Acme.Biz/Vendor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public class Vendor
/// <param name="deliverBy">Requested delivery date.</param>
/// <param name="instructions">Delivery instructions.</param>
/// <returns></returns>
public OperationResult PlaceOrder(Product product, int quantity,
public OperationResult<bool> PlaceOrder(Product product, int quantity,
DateTimeOffset? deliverBy = null,
string instructions = "standard delivery")
{
Expand Down Expand Up @@ -61,7 +61,7 @@ public OperationResult PlaceOrder(Product product, int quantity,
{
success = true;
}
var operationResult = new OperationResult(success, orderText);
var operationResult = new OperationResult<bool>(success, orderText);
return operationResult;
}

Expand All @@ -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();
}

/// <summary>
/// Sends an email to welcome a new vendor.
Expand Down
58 changes: 58 additions & 0 deletions AcmeApp/Acme.Biz/VendorRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ namespace Acme.Biz
{
public class VendorRepository
{
private List<Vendor> vendors;

/// <summary>
/// Retrieve one vendor.
/// </summary>
Expand All @@ -29,6 +31,62 @@ public Vendor Retrieve(int vendorId)
return vendor;
}

public List<Vendor> Retrieve()
{
if(vendors == null)
{
vendors = new List<Vendor>();

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 Dictionary<string, Vendor> RetrieveWithKeys()
{
var vendors = new Dictionary<string, Vendor>()
{
{ "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<T>(string sql, T defaultValue)
{
T value = defaultValue;
return value;
}

/// <summary>
/// Save data for one vendor.
/// </summary>
Expand Down
9 changes: 4 additions & 5 deletions AcmeApp/Acme.Common/OperationResult.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,19 @@
/// Provides a success flag and message
/// useful as a method return type.
/// </summary>
public class OperationResult
public class OperationResult<T>
{
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; }
}

}
5 changes: 3 additions & 2 deletions AcmeApp/Tests/Acme.BizTests/ProductTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,14 @@ public void CalculateSuggestedPriceTest()
// Arrange
var currentProduct = new Product(1, "Saw", "");
currentProduct.Cost = 50m;
var expected = 55m;
var expected = new OperationResult<decimal>(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()]
Expand Down
75 changes: 75 additions & 0 deletions AcmeApp/Tests/Acme.BizTests/VendorRepositoryTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,81 @@ 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<int>("Select ...", 42);

//Assert
Assert.AreEqual(expected, actual);
}

[TestMethod()]
public void RetrieveStringValueTest()
{
//Arrange
var repository = new VendorRepository();
var expected = "test";

//Act
var actual = repository.RetrieveValue<string>("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<Vendor>("Select ...", vendor);

//Assert
Assert.AreEqual(expected, actual);
}

[TestMethod()]
public void RetrieveTest()
{
//Arrange
var repository = new VendorRepository();
var expected = new List<Vendor>();
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);
}

[TestMethod()]
public void RetrieveWithKeysTest()
{
//Arrange
var repository = new VendorRepository();
var expected = new Dictionary<string, Vendor>()
{
{ "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);
}
}
}
12 changes: 6 additions & 6 deletions AcmeApp/Tests/Acme.BizTests/VendorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,15 +63,15 @@ public void PlaceOrderTest()
// Arrange
var vendor = new Vendor();
var product = new Product(1, "Saw", "");
var expected = new OperationResult(true,
var expected = new OperationResult<bool>(true,
"Order from Acme, Inc\r\nProduct: Saw\r\nQuantity: 12" +
"\r\nInstructions: standard delivery");

// Act
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()]
Expand All @@ -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<bool>(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");
Expand All @@ -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);
}

Expand All @@ -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<bool>(true,
"Order from Acme, Inc\r\nProduct: Saw\r\nQuantity: 12" +
"\r\nInstructions: Deliver to Suite 42");

Expand All @@ -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);
}

Expand Down