저는 Find 메서드를 처음부터 만들었습니다. Equals 메서드를 사용하여 특정 멤버가 동일한 경우 두 개체가 같은지 확인합니다. Find/Contains 메서드를 사용하는 것이 더 빠를 것임을 알고 있지만 사용할 수는 없습니다. 메소드의 서명은 "static int Find (List c, Coffee x)"입니다. Find는 x를 c에 넣고 x가 c에 있으면 유효한 색인 (예 : 0, 1)을 반환하고 그렇지 않으면 -1을 반환합니다. 동등성을 판별하려면 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;
}
어떻게 찾기 방법을 사용 가야합니까 : 나는 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());
이것은 : 여기에 지금까지 가지고 무엇인가?
Coffee 클래스에 equals 비교기를 구현해야합니다. –