private static void findInstrument(string name, InstrumentSpec instrumentSpec, Inventory inventory) { string choiceInstrument = ""; List <Instrument> instruments = new List <Instrument>(); if (instrumentSpec is GuitarSpec) { List <Guitar> guitars = inventory.search((GuitarSpec)instrumentSpec); instruments = new List <Instrument>(guitars); choiceInstrument = "guitar"; } else if (instrumentSpec is MandolinSpec) { List <Mandolin> mandolins = inventory.search((MandolinSpec)instrumentSpec); instruments = new List <Instrument>(mandolins); choiceInstrument = "mandolin"; } if (instruments.Count > 0) { try { string msgSuccess = string.Format("{0}, you might like these {1}s: ", name, choiceInstrument); foreach (Instrument instrument in instruments) { InstrumentSpec spec = null; if (instrumentSpec is GuitarSpec) { Guitar guitar = (Guitar)instrument; spec = guitar.spec; } else if (instrumentSpec is MandolinSpec) { Mandolin mandolin = (Mandolin)instrument; spec = mandolin.spec; } if (spec == null) { messageFail(name); } msgSuccess += "\nWe have a " + Enumerations.GetEnumDescription(spec.builder) + " " + spec.model + " " + Enumerations.GetEnumDescription(spec.type) + " " + choiceInstrument + ":\n " + Enumerations.GetEnumDescription(spec.backWood) + " back and sides,\n " + Enumerations.GetEnumDescription(spec.topWood) + " top.\nYou can have it for only $" + instrument.price + "!\n ----"; } Console.WriteLine(msgSuccess); Console.ReadKey(); return; } catch (Exception) { // No action taken. Default fail message will be triggered at end of method. } } messageFail(name); }
public void addInstrument(string serialNumber, double price, InstrumentSpec spec) { Instrument instrument = null; if (spec is GuitarSpec) { instrument = new Guitar(serialNumber, price, (GuitarSpec)spec); } else if (spec is MandolinSpec) { instrument = new Mandolin(serialNumber, price, (MandolinSpec)spec); } _inventory.Add(instrument); }
public List <Guitar> search(GuitarSpec searchSpec) { List <Guitar> matchingGuitars = new List <Guitar>(); for (int i = 0; i < _inventory.Count; i++) { if (!(_inventory[i] is Guitar)) { continue; } Guitar guitar = (Guitar)_inventory[i]; if (guitar.spec.matches(searchSpec)) { matchingGuitars.Add(guitar); } } return(matchingGuitars); }