static void Main(string[] args) { Inventory inventory = new Inventory(); initializeInventory(inventory); GuitarSpec whatErinLikes = new GuitarSpec(Builder_Enum.FENDER, "Stratocastor", Type_Enum.ACOUSTIC, Wood_Enum.ALDER, Wood_Enum.ALDER); List <Guitar> matchingGuitar = inventory.search(whatErinLikes); if (matchingGuitar.Count != 0) { Console.WriteLine("Erin, you might like these guitar:"); for (int i = 0; i < matchingGuitar.Count; i++) { Guitar guitar = matchingGuitar[i]; Console.WriteLine(" We have a " + guitar.Spec.Builder + " " + guitar.Spec.Model + " " + guitar.Spec.Type + " guitar:\n " + guitar.Spec.BackWood + " back and sides,\n " + guitar.Spec.TopWood + " top.\n You can have it for only $" + guitar.Price + "!\n ----"); } } else { Console.WriteLine("Sorry, Erin, we have nothing for you."); } Console.ReadKey(); }
public PropertyInfo[] GetProperties(GuitarSpec searchSpec) { Type t = searchSpec.GetType(); PropertyInfo[] pis = t.GetProperties(); return(pis); }
public void Add(string serialNumber, double price, Builder_Enum builder, string model, Type_Enum type, Wood_Enum backWood, Wood_Enum topWood, String_Enum numString) { GuitarSpec guitarSpec = new GuitarSpec(builder, model, type, backWood, topWood, numString); Guitar guitar = new Guitar(serialNumber, price, guitarSpec); guitars.Add(guitar); }
private static void InitializeInventory(Inventory inventory) { GuitarSpec guitarSpec1 = new GuitarSpec(Builder_Enum.FENDER, "Stratocastor", Type_Enum.ACOUSTIC, Wood_Enum.ALDER, Wood_Enum.ALDER); inventory.AddGuitar("V95693", 1499.95, guitarSpec1); GuitarSpec guitarSpec2 = new GuitarSpec(Builder_Enum.FENDER, "Stratocastor", Type_Enum.ACOUSTIC, Wood_Enum.ALDER, Wood_Enum.ALDER); inventory.AddGuitar("V9512", 1549.95, guitarSpec2); }
public PropertyInfo[] GetProperties(GuitarSpec searchSpec) { Type t = searchSpec.GetType(); PropertyInfo[] pis = t.GetProperties(); //foreach (PropertyInfo pi in pis) //{ // object value1 = pi.GetValue(t, null); // string name1 = pi.Name; //} if (pis == null) { Console.WriteLine("Failed to get Properties!"); return(null); } return(pis); }
public List <Guitar> Search(GuitarSpec searchSpec) { List <Guitar> matchGuitar = new List <Guitar>(); for (int i = 0; i < guitars.Count; i++) { Guitar guitar = guitars[i]; //使用反射获取所有的属性,分别进入这个match进行比较,最后得出结果。 PropertyInfo[] pis = GetProperties(searchSpec); PropertyInfo[] pis2 = GetProperties(guitar.Spec); //反射比较所有的属性 //目前暂时存在7个属性 GetPropertyValues(pis); GetPropertyValues(pis2); //解决方法 //1.泛型match + 委托 //2.用反射获取所有的属性+委托构造一个方法的比较+获取一个类对象中的所有的属性的数值 //一种方法就是直接构建一个泛型比较,但是同样需要获取整个基础的属性进行比较。 //另外一种方法就是,直接获取所有属性进行比较,但是目前遇到的问题就是属性的值无法返回。。。。。 //Builder_Enum builder = searchSpec.Builder; //if (builder != guitar.Spec.Builder) // continue; //string model = searchSpec.Model.ToLower(); //if ((model != null) && (!model.Equals("")) && // (!model.Equals(guitar.Spec.Model.ToLower()))) // continue; //Type_Enum type = searchSpec.Type; //if (type != guitar.Spec.Type) // continue; //Wood_Enum backWood = searchSpec.BackWood; //if (backWood != guitar.Spec.BackWood) // continue; //Wood_Enum topWood = searchSpec.TopWood; //if (topWood != guitar.Spec.TopWood) // continue; } return(matchGuitar); }
public List <Guitar> search(GuitarSpec searchSpec) { List <Guitar> matchGuitar = new List <Guitar>(); for (int i = 0; i < guitars.Count; i++) { Guitar guitar = guitars[i]; //使用反射获取所有的属性,分别进入这个match进行比较,最后得出结果。 PropertyInfo[] pis = GetProperties(searchSpec); PropertyInfo[] pis2 = GetProperties(guitar.Spec); //反射比较所有的属性 foreach (object item in pis) { //这里还出现了大问题。。。。 Console.WriteLine(pis.GetValue()); } //解决方法 //1.泛型match + 委托 //2.用反射获取所有的属性+委托构造一个方法的比较+获取一个类对象中的所有的属性的数值 //Builder_Enum builder = searchSpec.Builder; //if (builder != guitar.Spec.Builder) // continue; //string model = searchSpec.Model.ToLower(); //if ((model != null) && (!model.Equals("")) && // (!model.Equals(guitar.Spec.Model.ToLower()))) // continue; //Type_Enum type = searchSpec.Type; //if (type != guitar.Spec.Type) // continue; //Wood_Enum backWood = searchSpec.BackWood; //if (backWood != guitar.Spec.BackWood) // continue; //Wood_Enum topWood = searchSpec.TopWood; //if (topWood != guitar.Spec.TopWood) // continue; } return(matchGuitar); }
static void Main(string[] args) { Inventory inventory = new Inventory(); InitializeInventory(inventory); GuitarSpec whatErinLikes = new GuitarSpec(Builder_Enum.FENDER, "Stratocastor", Type_Enum.ACOUSTIC, Wood_Enum.ALDER, Wood_Enum.ALDER); //Type t = whatErinLikes.GetType(); //PropertyInfo[] propertyInfos = t.GetProperties(whatErinLikes); //foreach (var prop in propertyInfos) //{ // Console.WriteLine(" {0} ({1}): {2}", prop.Name, // prop.PropertyType.Name, // prop.GetValue(whatErinLikes)); //} inventory.GetProperties(whatErinLikes); //List<Guitar> matchingGuitar = inventory.Search(whatErinLikes); //if (matchingGuitar.Count != 0) //{ // Console.WriteLine("Erin, you might like these guitar:"); // for (int i = 0; i < matchingGuitar.Count; i++) // { // Guitar guitar = matchingGuitar[i]; // Console.WriteLine(" We have a " + // guitar.Spec.Builder + " " + guitar.Spec.Model + " " + // guitar.Spec.Type + " guitar:\n " + // guitar.Spec.BackWood + " back and sides,\n " + // guitar.Spec.TopWood + " top.\n You can have it for only $" + // guitar.Price + "!\n ----"); // } //} //else //{ // Console.WriteLine("Sorry, Erin, we have nothing for you."); //} Console.ReadKey(); }
public List <Guitar> Search(GuitarSpec searchSpec) { List <Guitar> matchGuitar = new List <Guitar>(); for (int i = 0; i < guitars.Count; i++) { Guitar guitar = guitars[i]; Builder_Enum builder = searchSpec.Builder; if (builder != guitar.Spec.Builder) { continue; } string model = searchSpec.Model.ToString(); if ((model != null) && (!model.Equals("")) && (!model.Equals(guitar.Spec.Model.ToLower()))) { continue; } Type_Enum type = searchSpec.Type; if (type != guitar.Spec.Type) { continue; } Wood_Enum backWood = searchSpec.BackWood; if (backWood != guitar.Spec.BackWood) { continue; } Wood_Enum topWood = searchSpec.TopWood; if (topWood != guitar.Spec.TopWood) { continue; } String_Enum numString = searchSpec.NumString; if (numString != guitar.Spec.NumString) { continue; } matchGuitar.Add(guitar); } return(matchGuitar); }
//构造函数 public Guitar(string serialNumber, double price, GuitarSpec guitarSpec) { this.serialNumber = serialNumber; this.price = price; this.spec = guitarSpec; }
public void addGuitar(string serialNumber, double price, GuitarSpec guitarSpec) { Guitar guitar = new Guitar(serialNumber, price, guitarSpec); guitars.Add(guitar); }