2013-04-26 55 views
0

我试图从头开始创建一个查找方法,以查看两个对象是否相等,因为某些成员是相等的,使用Equals方法来做到这一点。我知道使用Find/Contains方法会更快,但我无法使用它们。方法的签名是“static int Find(List c,Coffee x)”如果x在c中存在,Find查找x在c中并返回一个有效索引(例如0,1),否则返回-1。必须使用equals方法来确定等同性。如果传递的对象不等于列表中的当前对象,它将被添加到列表中(该列表包含从基类派生的两种类型的对象,因此列表可以存储这两种类型)。等值换算由名称,成本,需求,持有成本和焙烤类型定义,用于定期和名称,成本,需求,持有成本和最小数量的无水咖啡。这是我到目前为止有:创建查找方法

static void Main(string[] args) 
    { 

     // Create objects and references 
     Coffee obv = new Coffee(); 
     Decaf decafCoffee = null; 
     Regular regularCoffee = null; 
     List<Coffee> inventory = new List<Coffee>(); 


     // Prompt user for input and store it as a string 
     Console.Write("Enter q to quit or the whole data as a comma delimited string using the following format Name,D,C,D:minQ or R:roast "); 
     string s = Console.ReadLine(); 

     // Loop 
     while (!s.ToLower().Equals("q")) 
     { 
      // Split string up and assign componets to variables 
      string[] values = s.Split(",".ToCharArray(), StringSplitOptions.RemoveEmptyEntries); 
      string name = values[0]; 
      string demand = (values[1]); 
      string cost = (values[2]); 
      string type = values[3]; 

      // Check for > 0 and convert to numbers 
      float D = CheckDemand(demand); 
      float C = CheckCost(cost); 
      float M = 0; 

      if (type.StartsWith("D:")) 
      { 
       type = Regex.Match(type, @"\d+").Value; 
       M = CheckMin(type); 
       decafCoffee = new Decaf(name, D, C, M); 
       inventory.Add(decafCoffee); 
      } 

      else if (type.StartsWith("R:")) 
      { 
       if (type.Contains("light")) 
       { 
        M = 1; 
        regularCoffee = new Regular(name, D, C, M); 
        inventory.Add(regularCoffee); 
       } 
       else if (type.Contains("medium")) 
       { 
        M = 2; 
        regularCoffee = new Regular(name, D, C, M); 
        inventory.Add(regularCoffee); 
       } 

       else if (type.Contains("dark")) 
       { 
        M = 3; 
        regularCoffee = new Regular(name, D, C, M); 
        inventory.Add(regularCoffee); 
       } 
       else Console.WriteLine("\nError, please enter all lower case \"dark\", \"medium\", or \"light\" next time."); 
      } 

      else Console.WriteLine("\nError, please enter either \"D:\" followed by a number or \"R:\" followed by roast type next time."); 
      Console.Write("\nEnter q to quit or the whole data as a comma delimited string using the following format Name,D,C,D:minQ or R:roast: "); 
      s = Console.ReadLine(); 
     } // End loop 

     // Sort and display values 
     var sortedList = inventory.OrderBy(i => i.Q()).ToList(); 
     Console.WriteLine("\nName \t C ($)  Demand \t Detail Q(lbs.)  TAC 
     for (int j = 0; j < inventory.Count; j++) 
     { 
      Console.WriteLine("{0}", sortedList[j].toString()); 
     } 

     Console.WriteLine(obv.toStringQ()); 

这就是我对equals方法:

public override bool Equals(object obj) 
    { 
     if (obj is Coffee) 
     { 
      bool isNameEqual = Name.Equals(this.Name); 
      bool isuCostEqual = Cost.Equals(this.Cost); 
      bool isDemandEqual = Demand.Equals(this.Demand); 
      bool ishCostEqual = h.Equals(this.h); 
      bool isMinEqual = getQ.Equals(this.getQ); 

      return (isNameEqual && isuCostEqual && isDemandEqual && ishCostEqual && isMinEqual); 
     } 
     return false; 
    } 

如何去使用find方法?

+0

您需要在Coffee类中实现等于比较器。 – 2013-04-26 20:19:57

回答

1

在Coffee上实现一个'Equals'方法,并且可选择在其Decaf,Regular等子类上执行。现在,您可以直接在列表上使用Contains方法。

+0

我将不得不手动创建该方法。 – 2013-04-26 20:24:57

+0

是的,你将不得不。因为问题陈述中提到:“等同性是根据名称,成本,需求,持有成本和烘焙类型来定义的,包括常规和名称,成本,需求,持有成本和最小数量的无菌咖啡” - 这应该在实施“ ' 方法。让我们知道,如果你遇到问题,而实施这一点。祝你好运! – aquaraga 2013-04-26 20:26:07

+0

好的,谢谢! – 2013-04-26 20:32:23