private static Inventory initializeInventory(Inventory inventory) { string[] keys = {"instrumentType", "builder", "model", "type", "numStrings", "topWood", "backWood"}; Dictionary<string, object> properties = new Dictionary<string, object>(); properties.Add("instrumentType", InstrumentType.GUITAR); properties.Add("builder", Builder.COLLINGS); properties.Add("model", "CJ"); properties.Add("type", PlayType.ACOUSTIC); properties.Add("numStrings", 6); properties.Add("topWood", Wood.INDIAN_ROSEWOOD); properties.Add("backWood", Wood.SITKA); InstrumentSpec instrumentSpec = new InstrumentSpec(properties); inventory.addInstrument("11277", 3999.95, instrumentSpec); properties["builder"] = Builder.MARTIN; properties["model"] = "D-18"; properties["topWood"] = Wood.MAHOGANY; properties["backWood"] = Wood.ADIRONDACK; inventory.addInstrument("122784", 5495.95, new InstrumentSpec(properties)); properties["builder"] = Builder.FENDER; properties["model"] = "Stratocastor"; properties["type"] = PlayType.ELECTRIC; properties["topWood"] = Wood.ALDER; properties["backWood"] = Wood.ALDER; inventory.addInstrument("V95693", 1499.95, new InstrumentSpec(properties)); inventory.addInstrument("V9512", 1549.95, new InstrumentSpec(properties)); properties["builder"] = Builder.GIBSON; properties["model"] = "Les Paul"; properties["topWood"] = Wood.MAPLE; properties["backWood"] = Wood.MAPLE; inventory.addInstrument("70108276", 2295.95, new InstrumentSpec(properties)); properties["model"] = "SG '61 Reissue"; properties["topWood"] = Wood.MAHOGANY; properties["backWood"] = Wood.MAHOGANY; inventory.addInstrument("82765501", 1890.95, new InstrumentSpec(properties)); properties["instrumentType"] = InstrumentType.MANDOLIN; properties["type"] = PlayType.ACOUSTIC; properties["model"]= "F-5G"; properties["backWood"]= Wood.MAPLE; properties["topWood"]= Wood.MAPLE; properties.Remove("numStrings"); inventory.addInstrument("9019920", 5495.99, new InstrumentSpec(properties)); properties["instrumentType"] = InstrumentType.BANJO; properties["model"] = "RB-3 Wreath"; properties.Remove("topWood"); properties.Add("numStrings", 5); inventory.addInstrument("8900231", 2945.95, new InstrumentSpec(properties)); return inventory; }
public virtual bool matches(InstrumentSpec otherSpec) { List<string> keyList = new List<string>(otherSpec.getProperties().Keys); foreach (string key in keyList) { if (!properties[key].Equals(otherSpec.getProperty(key))) return false; } return true; }
// The search method compares each property of the Guitar object it's passed in to each Guitar object in Rick's inventory // Returns a list of guitars that match the customers specifications of the instrument. public List<Instrument> search(InstrumentSpec searchSpec) { // Delegate responsibility to compare the specifications of instruments List<Instrument> matchingInstruments = new List<Instrument>(); foreach (Instrument instrument in inventory) { if (instrument.getSpec().matches(searchSpec)) matchingInstruments.Add(instrument); } return matchingInstruments; }
// matches() uses the superclass's matches() and then performs additional checks to make sure the spec is the right type // and matches the guitar-specific properties public override bool matches(InstrumentSpec searchInstrumentSpec) { if (!base.matches(searchInstrumentSpec)) return false; if (!(searchInstrumentSpec is GuitarSpec)) return false; GuitarSpec spec = (GuitarSpec)searchInstrumentSpec; if (numStrings != spec.numStrings) return false; return true; }
public virtual bool matches(InstrumentSpec otherSpec) { List <string> keyList = new List <string>(otherSpec.getProperties().Keys); foreach (string key in keyList) { if (!properties[key].Equals(otherSpec.getProperty(key))) { return(false); } } return(true); }
// Just like GuitarSpec, MandolinSpec uses its base class to do basic comparison // and then casts MandolinSpec and compares mandolin-specific properties public override bool matches(InstrumentSpec searchInstrumentSpec) { if (!(base.matches(searchInstrumentSpec))) { return(false); } if (!(searchInstrumentSpec is MandolinSpec)) { return(false); } MandolinSpec spec = (MandolinSpec)searchInstrumentSpec; if (style != spec.style) { return(false); } return(true); }
public void RunSimulator() { // Guitar Customer // Setup Rick's Guitar inventory Inventory inventory = new Inventory(); inventory = initializeInventory(inventory); Dictionary<string, object> properties = new Dictionary<string, object>(); properties.Add("builder", Builder.GIBSON); properties.Add("backWood", Wood.MAPLE); InstrumentSpec clientSpec = new InstrumentSpec(properties); List<Instrument> matchingInstruments = inventory.search(clientSpec); if (matchingInstruments.Any()) { foreach (Instrument instrument in matchingInstruments) { InstrumentSpec instrumentSpec = instrument.getSpec(); Console.WriteLine("We have a {0} with the following properties:", instrumentSpec.getProperty("instrumentType")); var specProperties = instrumentSpec.getProperties(); foreach (string property in specProperties.Keys) { if (property.Equals("instrumentType")) continue; Console.WriteLine("{0}: {1}", property, instrumentSpec.getProperty(property)); } Console.WriteLine("You can have this {0} for ${1}", instrumentSpec.getProperty("instrumentType"), instrument.getPrice()); Console.WriteLine("---------------------------"); } } else Console.WriteLine("Sorry customer, we don't have anything for you."); }
public Instrument(string serialNumber, double price, InstrumentSpec instrumentSpec) { this.serialNumber = serialNumber; this.price = price; this.instrumentSpec = instrumentSpec; }
// addGuitar() takes in all properties required to create a new Guitar instance, creates one, and adds it to the inventory public void addInstrument(string serialNumber, double price, InstrumentSpec spec) { Instrument instrument = new Instrument(serialNumber, price, spec); inventory.Add(instrument); }
public MandolinSpec(Style style, InstrumentSpec spec) { this.style = style; }