我正在尝试从头开始创建一个find方法,看看两个对象是否相等,只要某些方法的结果相等,使用Equals方法就可以了。我知道使用Find / Contains方法会更快,但我不允许使用它们。该方法的签名为“static int Find(List c, Coffee x)
”在x
中查找搜索c
并返回有效索引(例如,0
,1
){{1存在于x
中,否则返回c
。必须使用equals方法来确定等效性。如果传递的对象不等于列表中当前的对象,则将其添加到列表中(该列表包含从基类派生的两种类型的对象,因此列表可以存储这两种类型)。等效性由-1
,name
,cost
,demand
和holding cost(h)
定义为常规和roasttype(M)
,name
,{{对于cost
,1}},demand
和holding 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;
}
}
}
假设我正确实现了equals方法,在哪里/如何实现“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;
}
}
”方法?
答案 0 :(得分:0)
因为它被覆盖的Equals方法将无法正常工作。等于测试参考相等性,即两个对象引用是否指向同一个对象。有关Equals的详细信息,请参阅MSDN。
如果您只是想确保不添加相同的两个咖啡(具有相同的名称,需求成本和类型),那么您可以对这些字段执行简单的值检查,例如(添加到您的方法)咖啡班)
public bool CoffeeIsSame(Coffee c2)
{
return (this.Name == c2.Name) && (this.Demand == this.Demand) && (this.Cost == c2.Cost) && (this.Type == c2.Type);
}
你的Find方法看起来像这样:
static bool Find(List c, Coffee x)
{
bool result = false;
foreach(Coffee coffee in c)
{
result = coffee.CoffeeIsSame(x);
if (result)
{
break;
}
}
return result;
}
要实现静态Find方法,可以将其添加到Coffee类中。然后你这样称呼它(使用你的Main方法中的一些代码作为例子):
...
else if (type.Contains("dark"))
{
M = 3;
regularCoffee = new Regular(name, D, C, M);
if (!Coffee.Find(inventory, regularCoffee))
{
inventory.Add(regularCoffee);
}
}
...
希望有所帮助,
欢呼声