2013-04-29 137 views
-2

我試圖從頭開始創建一個查找方法,以查看兩個對象是否相等,如果某些方法的結果相同,則使用Equals方法執行此操作。我知道使用Find/Contains方法會更快,但我不允許使用它們。該方法的簽名是「static int Find(List c, Coffee x)」查找尋求在cx並返回有效的索引(例如,01)如果存在於cx,返回-1否則。必須使用equals方法來確定等同性。如果傳遞的對象不等於當前在列表中的對象,它將被添加到列表中(該列表包含從基類派生的兩種類型的對象,因此列表可以存儲這兩種類型)。等效性是通過namecostdemandholding cost(h)roasttype(M)定期和namecostdemandholding cost(h)minimum quantity(also M)decaf定義。這是我迄今(這是一個很大的代碼,很可能被寫越好):測試兩個對象是否相等

class EOQCalculator4 
{ 
    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 ($)  T(weeks) "); 
     for (int j = 0; j < inventory.Count; j++) 
     { 
      Console.WriteLine("{0}", sortedList[j].toString()); 
     } 

     Console.WriteLine(obv.toStringQ()); 

    } 

#region CheckMethods 
    // Data validation methods 
    public static float CheckDemand(String R) 
    { 
     float number; 
     while (!float.TryParse(R, out number) || number <= 0) 
     { 
      Console.Write("Please enter a number greater than 0 for demand: "); 
      R = Console.ReadLine(); 

     } return number; 

    } 

    public static float CheckCost(String R) 
    { 
     float number; 
     while (!float.TryParse(R, out number) || number <= 0) 
     { 
      Console.Write("Please enter a number greater than 0 for cost: "); 
      R = Console.ReadLine(); 

     } return number; 

    } 

    public static float CheckMin(String R) 
    { 
     float number; 
     while (!float.TryParse(R, out number) || number <= 0) 
     { 
      Console.Write("Please enter a number greater than 0 for minimum quantity: "); 
      R = Console.ReadLine(); 

     } return number; 

    } 

public class Coffee 
{ 
    // Members 
    private static float sumQ = 0; 
    private static int mcount; 
    private float k = 20; 
    private float mc; 
    private string mName; 
    private float md; 
    private float q; 
    private float mh; 
    private float tac; 
    private float min = 0; 
    private float type = 0; 
    Coffee i = new Coffee(); 

    public override bool Equals(object obj) 
    { 
     if (obj is Coffee) 
     { 
      bool isNameEqual = i.Name.Equals(this.Name); 
      bool isuCostEqual = i.Cost.Equals(this.Cost); 
      bool isDemandEqual = i.Demand.Equals(this.Demand); 
      bool ishCostEqual = i.h.Equals(this.h); 
      bool isMinEqual = i.getMin.Equals(this.min); 
      return (isNameEqual && isuCostEqual && isDemandEqual && ishCostEqual && isMinEqual); 
     } 
     return false; 
    } 

    // Default Constructor 
    public Coffee() 
    { mcount = 0; } 

    // Full Constructor 
    public Coffee(string Name, float d, float c, float m) 
    { 
     mName = Name; 
     md = d; 
     mc = c; 
     mh = (float).30 * mc; 
     type = m; 
     min = m; 
     mcount++; 
    } 

    public Coffee(string Name, float d, float c) 
    { 
     mName = Name; 
     md = d; 
     mc = c; 
     mh = (float).30 * mc; 
     mcount++; 
    } 

    // Copy Constructor 
    public Coffee(Coffee e) 
    { 
     this.mName = e.mName; 
     this.md = e.md; 
     this.mh = e.mh; 
     this.mc = e.mc; 
     this.q = e.q; 
     mcount++; 
    } 

    // Destructor 
    ~Coffee() 
    { mcount--; } 

    // Properties 
    #region Properties 

    public float getMin 
    { 
     get { return min; } 
    } 

    public float getType 
    { 
     get { return type; } 
    } 

    public string Name 
    { 
     get { return mName; } 
    } 

    public float h 
    { 
     get { return mh; } 
    } 

    public float Cost 
    { 
     get { return mc; } 
    } 

    public float Demand 
    { 
     get { return md; } 
    } 

    public float getQ 
    { 
     get { return q; } 
    } 

    public float K 
    { 
     get { return k; } 
    } 

    public float getSumQ 
    { 
     get { return sumQ; } 
     set { sumQ = value; } 
    } 
    #endregion 

    // Methods 
    public virtual float Q() 
    { 
     q = (float)Math.Sqrt(2 * md * k/mh); 
     sumQ += q; 
     return q; 
    } 

    public virtual float TAC() 
    { 
     tac = q/2 * mh + md * k/q + md * mc; 
     return tac; 
    } 

    public virtual float T() 
    { 
     float t = (q/(md/52)); 
     return t; 
    } 

    public virtual string toString() 
    { 
     string a = String.Format("{0,-10:s} {1,-10:f2} {2,-13:f0} {3,-11:f0} {4,-11:f2} {5,-0:f2}", mName, mc, md, Q(), TAC(), T()); 
     return a; 
    } 

    public virtual string toStringQ() 
    { 
     string c = String.Format("\nIf you purchase all of the coffee you will need space to hold {0,-0:f2} of coffee", sumQ); 
     return c; 
    } 
} 

}

public class Decaf : Coffee 
{ 
    // Members 
    private float k = 30; 
    private float min; 
    private float q; 

    // Constructor 
    public Decaf(string Name, float d, float c, float m) 
     : base(Name, d, c) 
    { 
     min = m; 
    } 

    // Methods 
    public override float Q() 
    { 
     q = (float)Math.Sqrt(2 * Demand * k/h); 
     if (q < min) 
     { 
      return min; 
     } 
     else return q; 
    } 

    public override float TAC() 
    { 
     getSumQ += Q(); 
     return Q()/2 * h + Demand * k/Q() + Demand * Cost; 
    } 

    public override float T() 
    { 
     return (Q()/(Demand/52)); 
    } 

    public override string toString() 
    { 
     string a = String.Format("{0,-11:s}{1,-11:f2}{2,-12:f0}{3,-9:f0}{4,-12:f0}{5,-13:f2}{6,-10:f2}", Name, Cost, Demand, min, Q(), TAC(), T()); 
     return a; 
    } 
} 

}

// Enumerator 
[Flags] enum RoastType { light = 1, medium = 2, dark = 3 } 

public class Regular : Coffee 
{ 
    // Members 
    RoastType roast; 
    private float q; 
    private float k = 20; 

    // Constructor 
    public Regular(string Name, float d, float c, float r) 
     : base(Name, d, c) 
    { 
     int x = (int) r; 
     roast = (RoastType) x; 
     roast.ToString(); 

    } 
    // Methods 
    public override float Q() 
    { 
     q = (float)Math.Sqrt(2 * Demand * k/h); 
     return q; 
    } 

    public override float TAC() 
    { 
     getSumQ += Q(); 
     return Q()/2 * h + Demand * k/Q() + Demand * Cost; 
    } 

    public override float T() 
    { 
     return (Q()/(Demand/52)); 
    } 

    public override string toString() 
    { 
     string b = String.Format("{0,-11:s}{1,-11:f2}{2,-12:f0}{3,-9:s}{4,-12:f0}{5,-13:f2}{6,-10:f2}", Name, Cost, Demand, roast.ToString(), Q(), TAC(), T()); 
     return b; 
    } 
} 

}

假設我正確執行equals方法,在哪裏/如何實現「static int Find(List<Coffee> c, Coffee x)」方法?

回答

0

因爲它站在重寫的等於方法將無法正常工作。等於引用相等的測試,即如果兩個對象引用指向同一個對象。有關Equals的更多信息,請參閱MSDN

如果你只是想確保沒有添加相同的兩個咖啡(同名,需求成本和類型),那麼你可以對這些字段執行一個簡單的值檢查,例如(方法添加到你的咖啡類)

public bool CoffeeIsSame(Coffee c2) 
{ 
    return (this.Name == c2.Name) && (this.Demand == this.Demand) && (this.Cost == c2.Cost) && (this.Type == c2.Type); 
} 

你的查找方法可能是這個樣子:

static bool Find(List c, Coffee x) 
{ 
    bool result = false; 
    foreach(Coffee coffee in c) 
    { 
     result = coffee.CoffeeIsSame(x); 
     if (result) 
     { 
      break; 
     } 
    } 
    return result; 
} 

要實現你的靜態查找方法,你可以把它添加到您的咖啡類。然後,你這樣稱呼它(使用一些代碼從你的主要方法爲例):

... 
else if (type.Contains("dark")) 
{ 
    M = 3; 
    regularCoffee = new Regular(name, D, C, M); 
    if (!Coffee.Find(inventory, regularCoffee)) 
    { 
     inventory.Add(regularCoffee); 
    } 
} 
... 

希望幫助,

歡呼

相關問題