-2

Equals メソッドを使用して、特定のメソッドの結果が等しい場合に 2 つのオブジェクトが等しいかどうかを確認するために、find メソッドをゼロから作成しようとしています。Find/Contains メソッドを使用した方が高速であることはわかっていますが、使用は許可されていません。メソッドのシグネチャは " static int Find(List c, Coffee x)" です。Find は をシークxし、 に存在する場合cは有効なインデックス ( 、0など1)を返し、そうでない場合は返します。equals メソッドを使用して等価性を判断する必要があります。渡されたオブジェクトが現在リストにあるオブジェクトと等しくない場合は、リストに追加されます (リストには基本クラスから派生した 2 種類のオブジェクトが含まれているため、リストには両方の種類を格納できます)。同等性は、、、およびによって定義されます。xc-1namecostdemandholding 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)ますか?

4

1 に答える 1