0

Equals メソッドを使用して、特定のメンバーが等しい場合に 2 つのオブジェクトが等しいかどうかを確認するために、find メソッドを最初から作成しようとしています。Find/Contains メソッドを使用した方が高速であることはわかっていますが、使用できません。メソッドのシグネチャは "static int Find(List c, Coffee x)" です。Find は c で x を探し、x が c に存在する場合は有効なインデックス (0、1 など) を返し、そうでない場合は -1 を返します。equals メソッドを使用して等価性を判断する必要があります。渡されたオブジェクトがリスト内の現在のオブジェクトと等しくない場合は、リストに追加されます (リストには基本クラスから派生した 2 種類のオブジェクトが含まれているため、リストには両方の種類を格納できます)。同等性は、レギュラーの場合は名前、コスト、需要、保持コスト、および焙煎タイプによって定義され、デカフェの場合は名前、コスト、需要、保持コスト、および最小量によって定義されます。これが私がこれまでに持っているものです:

  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 メソッドを使用するにはどうすればよいですか?

4

1 に答える 1