public List <Instrument> SearchInstruments(InstrumentSpec searchSpec) { var matchingInstruments = new List <Instrument>(); foreach (var instrument in _inventory) { if (instrument.Spec.Matches(searchSpec)) { matchingInstruments.Add(instrument); } } return(matchingInstruments); }
private static void Main() { var inventory = new Inventory(); InitializeInventory(inventory); var properties = new Dictionary <string, object> { { "Builder", Builder.Gibson }, { "BackWood", Wood.Maple } }; var whatBryanLikes = new InstrumentSpec(properties); var matchingInstruments = inventory.SearchInstruments(whatBryanLikes); if (matchingInstruments.Any()) { Console.WriteLine("Bryan, you might like these instruments:"); foreach (var matchingInstrument in matchingInstruments) { var spec = matchingInstrument.Spec; Console.WriteLine("We have a {0} with the following properties:", spec.GetProperty("InstrumentType")); foreach (var property in spec.GetProperties()) { if (property.Key == "InstrumentType") { continue; } Console.WriteLine("{0}: {1}", property.Key, property.Value); } Console.WriteLine("You can have this {0} for ${1} \n------", spec.GetProperty("InstrumentType"), matchingInstrument.Price); } } else { Console.WriteLine("Sorry Bryan, we have nothing for you."); } Console.ReadKey(); }
public void AddInstrument(string serialNumber, double price, InstrumentSpec spec) { var instrument = new Instrument(serialNumber, price, spec); _inventory.Add(instrument); }
public Instrument(string serialNumber, double price, InstrumentSpec spec) { SerialNumber = serialNumber; Price = price; Spec = spec; }
public bool Matches(InstrumentSpec otherSpec) { return(otherSpec.GetProperties().Keys.All(propertyName => GetProperty(propertyName).ToString() == otherSpec.GetProperty(propertyName).ToString())); }