using com.knapp.KCC2017.data; using com.knapp.KCC2017.entities; using com.knapp.KCC2017.util; using System.Collections.Generic; using System.Collections.ObjectModel; using System.IO; namespace com.knapp.KCC2017 { /// /// Container class for all input into the solution /// public class InputData { /// /// List of all products that are used in this contest /// private readonly List products = new List(); /// /// List of all containers (storage and workstation) /// private readonly List containers = new List(); /// /// Get a read-only collection of all products /// /// readon-only collection with all products public ReadOnlyCollection GetProducts() { return new ReadOnlyCollection( products ); } /// /// Get a read-only collection of all containes (storage and workstation) /// /// read-onyl collection of all containers public ReadOnlyCollection GetContainers() { return new ReadOnlyCollection( containers ); } /// /// Create from outside only via CreateFromCsv /// private InputData( ) {/* EMPTY */ } /// /// Load all input data from the csv files and create instance (and composite instances) /// /// a newly created ínstance of the inout public static InputData CreateFromCsv() { InputData input = new InputData(); input.LoadProductsFromCsv( System.IO.Path.Combine( Settings.DataPath, Settings.InProductFilename ) ); input.LoadContainersFromCsv( System.IO.Path.Combine( Settings.DataPath, Settings.InContainerFilename ) ); return input; } /// /// Load all products from the CSV /// /// full path of the csv private void LoadProductsFromCsv( string fullFilename ) { KContract.Requires( !string.IsNullOrWhiteSpace( fullFilename ), "fullFilename mandatory but is null or whitespace" ); foreach ( Product product in CsvReader.ReadCsvFile( fullFilename ) ) { products.Add( product ); } System.Console.Out.WriteLine( "+++ loaded: {0} products", products.Count ); } /// /// Load all containers from csv /// /// full path of the csv private void LoadContainersFromCsv( string fullFilename ) { KContract.Requires( !string.IsNullOrWhiteSpace( fullFilename ), "fullFilename mandatory but is null or whitespace" ); using ( StreamReader streamReader = new StreamReader( fullFilename ) ) { string line; while ( ( line = streamReader.ReadLine() ) != null ) { if ( !string.IsNullOrWhiteSpace( line ) && !line.StartsWith( "#" ) ) { string[] fields = line.Split(new[] { ';' }); string code = fields[ 0 ].Trim(); ContainerType type = ContainerType.Get( fields[1].Trim() ); Container container = new Container( code, type ); for ( int i = 2, s = 0; i < fields.Length; i += 2, ++s ) { if ( !string.IsNullOrWhiteSpace( fields[ i ] ) ) { Product p = FindProductByCode( fields[i].Trim() ); int quantity = int.Parse( fields[i+1]); container.GetSlots()[ s ]._SetProduct( p ); container.GetSlots()[ s ]._SetQuantity( quantity ); } } containers.Add( container ); } } } System.Console.Out.WriteLine( "+++ loaded: {0} containers", containers.Count ); } /// /// Helper function to find product /// /// /// product wuth goven code /// private Product FindProductByCode( string productCode ) { Product result = products.Find( p => p.Code.Equals( productCode ) ); if( result == null ) { throw new KeyNotFoundException("no product found for code " + productCode ); } return result; } } }