public static void SortParcels(this Department[] departments, Parcel[] parcels) { List <Parcel> sortableParcels = parcels.ToList(); bool InsurDepExists = departments.ContainsInsuranceDepartment(); InsuranceDepartment inDep = null; if (InsurDepExists) { inDep = departments.GetInsuranceDepartment(); } foreach (Parcel parcel in parcels) { Parcel sortedParcel = null; //Sort into insurance department if (InsurDepExists) { if (!parcel.IsSignedOff() && parcel.Value >= inDep.MinSigningPrice) { inDep.AddParcel(parcel); sortedParcel = parcel; sortableParcels.Remove(sortedParcel); continue; } } //Sort based on weight into correct departments for (int i = 0; i < departments.Length; i++) { if (departments[i] is ProcessingDepartment) { ProcessingDepartment pD = (ProcessingDepartment)departments[i]; float weight = parcel.Weight; if (weight < pD.WeightClass) { pD.AddParcel(parcel); sortedParcel = parcel; sortableParcels.Remove(sortedParcel); break; } } } } ProcessingDepartment pDs = departments.GetProcessingDepartments().HighestWeightProcessingDepartment(); pDs.AddParcels(sortableParcels.ToArray()); }
public static Department[] RemoveProcessingDepartmentAndResort(this Department[] departments, int arrayRowNr) { if (departments[arrayRowNr] == null) { Debug.WriteLine("Department not found"); return(null); } if (departments[arrayRowNr] is InsuranceDepartment) { Debug.WriteLine("Can't remove insurance department"); return(null); } if (departments[arrayRowNr] is ProcessingDepartment) { ProcessingDepartment pDp = (ProcessingDepartment)departments[arrayRowNr]; if (pDp.SortsHigherWeights) { Debug.WriteLine("Can't remove max processing department"); return(null); } List <Department> deps = departments.ToList(); Parcel[] ResortParcels = departments[arrayRowNr].Parcels.ToArray(); int removedWeightClass = pDp.WeightClass; deps.RemoveAt(arrayRowNr); bool updateMaxWeights = false; if (arrayRowNr < deps.Count - 1) { if (deps[arrayRowNr] is ProcessingDepartment) { ProcessingDepartment NewPDp = (ProcessingDepartment)deps[arrayRowNr]; if (NewPDp.SortsHigherWeights) { updateMaxWeights = true; } } else if (deps[arrayRowNr + 1] is ProcessingDepartment) { ProcessingDepartment NewPDp = (ProcessingDepartment)deps[arrayRowNr + 1]; if (NewPDp.SortsHigherWeights) { updateMaxWeights = true; } } } if (updateMaxWeights) { ProcessingDepartment NewPDp = deps.ToArray().HighestWeightProcessingDepartment(); InsuranceDepartment NewIDp = deps.ToArray().GetInsuranceDepartment(); if (arrayRowNr > 0) { ProcessingDepartment lowerPDp = (ProcessingDepartment)deps[arrayRowNr - 1]; NewPDp.SetWeight(lowerPDp.WeightClass); } else { NewPDp.SetWeight(0); } deps.Remove(NewPDp); deps.Remove(NewIDp); deps.Add(NewPDp); deps.Add(NewIDp); } deps.ToArray().SortParcels(ResortParcels); return(deps.ToArray()); } else { Debug.WriteLine("Something went wrong?"); return(null); } }