public static ISet <Yarn> ExtractYarnCounts(List <Customer> customers) { HashSet <Yarn> yarnTotal = new HashSet <Yarn>(); List <Yarn> combinedYarnOrders = extractListOfYarn(customers); for (var i = 0; i < combinedYarnOrders.Count; i++) { var yarn1 = combinedYarnOrders[i]; for (var j = i + 1; j < combinedYarnOrders.Count; j++) { var yarn2 = combinedYarnOrders[j]; if (!ReferenceEquals(yarn1, yarn2)) { if (Yarn.AreEquivalent(yarn1, yarn2)) { yarn1 += yarn2; combinedYarnOrders.RemoveAt(j); } } } yarnTotal.Add(yarn1); } return(yarnTotal); }
public static bool AreEquivalent(Yarn yarn1, Yarn yarn2) { return(yarn1.IsMiniSkein == yarn2.IsMiniSkein && yarn1.YarnType == yarn2.YarnType && yarn1.YarnTypeDescription == yarn2.YarnTypeDescription && yarn1.Color == yarn2.Color); }
public static Yarn operator +(Yarn yarn, Yarn addend) { if (AreEquivalent(yarn, addend)) { double sum = yarn.NumberOfSkeins + addend.NumberOfSkeins; Yarn newYarn = new Yarn() { NumberOfSkeins = sum, YarnType = yarn.YarnType, YarnTypeDescription = yarn.YarnTypeDescription, Color = yarn.Color }; return(newYarn); } throw new ArgumentException("Yarn are different types"); }
public static (YarnType, double) ModifyValuesForMiniSkeins(string yarnDescription, double quantity, YarnType yarnType) { if (Yarn.DetermineIfMiniSkein(yarnDescription)) { double conversionConstant = yarnType.GetMiniConversionConstant(); quantity *= conversionConstant; YarnType miniType = yarnType.GetCorrespondingMiniType(); return(miniType, quantity); } return(yarnType, quantity); }
public static List <Customer> GenerateCustomers(Stream stream) { var customers = new List <Customer>(); using (var reader = new StreamReader(stream)) { using (var csv = new CsvReader(reader)) { bool yarnDataParsable = false; Customer currentCustomer = null; while (csv.Read()) { String[] line = csv.Parser.Context.Record; if (line.Contains("Quantity")) { csv.ReadHeader(); yarnDataParsable = true; } if (yarnDataParsable) { var firstEntryOfLine = csv.Parser.Context.Record[0]; if ((firstEntryOfLine != String.Empty) && (IsTotalLine(firstEntryOfLine) == false)) { string customerName = firstEntryOfLine; currentCustomer = new Customer() { Name = customerName }; customers.Add(currentCustomer); continue; } if (currentCustomer != null) { try { Yarn yarn = Yarn.CreateYarnFromCSV(csv); currentCustomer.Order.Add(yarn); } catch (MissingFieldException) { } } } } } } return(customers); }
public override bool Equals(object obj) { if ((obj == null) || this.GetType() != obj.GetType()) { return(false); } else { Yarn yarn = (Yarn)obj; return((this.YarnType == yarn.YarnType) && (this.Color == yarn.Color) && (this.YarnTypeDescription.Equals((yarn.YarnTypeDescription))) && (this.IsMiniSkein == yarn.IsMiniSkein)); } }
public static Yarn CreateYarnFromText(CsvReader reader) { double quantity = reader.GetField <double>(1); YarnType yarnType = YarnFactory.CreateYarnTypeFromText(reader.GetField <String>(2)); String yarnTypeDescription = reader.GetField <String>(3); (yarnType, quantity) = YarnFactory.ModifyValuesForMiniSkeins(yarnTypeDescription, quantity, yarnType); Yarn yarn = new Yarn(quantity, yarnType, yarnTypeDescription); if (reader.TryGetField <String>(4, out String colorName)) { yarn.Color = colorName; } return(yarn); }
public static Yarn CreateYarnFromText(string inputText) { inputText = inputText.TrimStart(','); String[] inputs = inputText.Split(','); double quantity = double.Parse(inputs[0]); YarnType yarnType = YarnFactory.CreateYarnTypeFromText(inputs[1]); String yarnTypeDescription = inputs[2]; (yarnType, quantity) = YarnFactory.ModifyValuesForMiniSkeins(yarnTypeDescription, quantity, yarnType); Yarn yarn = new Yarn(quantity, yarnType, yarnTypeDescription); if (inputs.Length > 3) { yarn.Color = inputs[3]; } return(yarn); }