Skip to content

Instantly share code, notes, and snippets.

@ScottLilly
Created April 9, 2017 23:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ScottLilly/85091b9f61e66256a69a7909a05337fd to your computer and use it in GitHub Desktop.
Save ScottLilly/85091b9f61e66256a69a7909a05337fd to your computer and use it in GitHub Desktop.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Engine.Questions.Question1
{
class CallingClass
{
public void MyFunction()
{
var report1 =
ReportGenerator
.CreateStandardSalesReport(DateTime.UtcNow.AddMonths(-1), DateTime.UtcNow)
.GroupBy(ReportGenerator.GroupingMethod.Department)
.SortBy(ReportGenerator.SortMethod.DateAscending)
.BuildReport();
var report2 =
ReportGenerator
.CreateSalesReport(DateTime.UtcNow.AddMonths(-1), DateTime.UtcNow)
.IncludeAllSalespeople()
.IncludeAllCategories()
.ExcludeReturnedOrders()
.IncludeUnshippedOrders()
.GroupBy(ReportGenerator.GroupingMethod.Department)
.SortBy(ReportGenerator.SortMethod.DateAscending)
.BuildReport();
}
}
}
using System;
using System.Collections.Generic;
namespace Engine.Questions.Question1
{
public class ReportGenerator : ICanSetAllOrOneSalesperson,
ICanSetAllOrOneCategory, ICanAddSalespersonIDOrSetCategories,
ICanSetReturnedOrdersInclusion,
ICanAddCategoryIDOrSetReturnedOrdersInclusion,
ICanSetUnshippedOrdersInclusion,
ICanSetGroupBy, ICanSetSortBy, ICanBuildReport
{
#region Enums
public enum GroupingMethod
{
None,
Salesperson,
Office,
Department
}
public enum SortMethod
{
DateAscending,
DateDescending,
OrderTotalAscending,
OrderTotalDescending
}
#endregion
private bool _includeAllSalespeople;
private List<int> _includedSalespeopleIDs = new List<int>();
private bool _includeAllCategories;
private List<int> _includedCategoryIDs = new List<int>();
private bool _includeReturnedOrders;
private bool _includeUnshippedOrders;
private DateTime _from;
private DateTime _to;
private GroupingMethod _groupBy;
private SortMethod _sortBy;
private ReportGenerator(DateTime from, DateTime to)
{
_from = from;
_to = to;
}
// Instantiating functions
public static ICanSetGroupBy CreateStandardSalesReport(DateTime from, DateTime to)
{
var standardReport = new ReportGenerator(from, to);
standardReport._includeAllSalespeople = true;
standardReport._includeAllCategories = true;
standardReport._includeReturnedOrders = false;
standardReport._includeUnshippedOrders = true;
return standardReport;
}
public static ICanSetAllOrOneSalesperson CreateSalesReport(DateTime from, DateTime to)
{
return new ReportGenerator(from, to);
}
// Chaining functions
public ICanSetAllOrOneCategory IncludeAllSalespeople()
{
_includeAllSalespeople = true;
return this;
}
public ICanAddSalespersonIDOrSetCategories IncludeSalespersonID(int id)
{
_includedSalespeopleIDs.Add(id);
return this;
}
public ICanSetReturnedOrdersInclusion IncludeAllCategories()
{
_includeAllCategories = true;
return this;
}
public ICanAddCategoryIDOrSetReturnedOrdersInclusion IncludeCategoryID(int id)
{
_includedCategoryIDs.Add(id);
return this;
}
public ICanSetUnshippedOrdersInclusion IncludeReturnedOrders()
{
_includeReturnedOrders = true;
return this;
}
public ICanSetUnshippedOrdersInclusion ExcludeReturnedOrders()
{
_includeReturnedOrders = false;
return this;
}
public ICanSetGroupBy IncludeUnshippedOrders()
{
_includeUnshippedOrders = true;
return this;
}
public ICanSetGroupBy ExcludeUnshippedOrders()
{
_includeUnshippedOrders = false;
return this;
}
public ICanSetSortBy GroupBy(GroupingMethod groupBy)
{
_groupBy = groupBy;
return this;
}
public ICanBuildReport SortBy(SortMethod sortBy)
{
_sortBy = sortBy;
return this;
}
// Ending functions
public object BuildReport()
{
// This would have the report-generating code here.
return new object();
}
}
public interface ICanSetAllOrOneSalesperson
{
ICanSetAllOrOneCategory IncludeAllSalespeople();
ICanAddSalespersonIDOrSetCategories IncludeSalespersonID(int id);
}
public interface ICanSetAllOrOneCategory
{
ICanSetReturnedOrdersInclusion IncludeAllCategories();
ICanAddCategoryIDOrSetReturnedOrdersInclusion IncludeCategoryID(int id);
}
public interface ICanAddSalespersonIDOrSetCategories
{
ICanAddSalespersonIDOrSetCategories IncludeSalespersonID(int id);
ICanSetReturnedOrdersInclusion IncludeAllCategories();
ICanAddCategoryIDOrSetReturnedOrdersInclusion IncludeCategoryID(int id);
}
public interface ICanSetReturnedOrdersInclusion
{
ICanSetUnshippedOrdersInclusion IncludeReturnedOrders();
ICanSetUnshippedOrdersInclusion ExcludeReturnedOrders();
}
public interface ICanAddCategoryIDOrSetReturnedOrdersInclusion
{
ICanAddCategoryIDOrSetReturnedOrdersInclusion IncludeCategoryID(int id);
ICanSetUnshippedOrdersInclusion IncludeReturnedOrders();
ICanSetUnshippedOrdersInclusion ExcludeReturnedOrders();
}
public interface ICanSetUnshippedOrdersInclusion
{
ICanSetGroupBy IncludeUnshippedOrders();
ICanSetGroupBy ExcludeUnshippedOrders();
}
public interface ICanSetGroupBy
{
ICanSetSortBy GroupBy(ReportGenerator.GroupingMethod groupBy);
}
public interface ICanSetSortBy
{
ICanBuildReport SortBy(ReportGenerator.SortMethod sortBy);
}
public interface ICanBuildReport
{
object BuildReport();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment