C#在一个类中计算

时间:2016-04-15 07:09:45

标签: c# visual-studio-2015

我正在使用Visual Studio 2015和C#。我正在尝试根据居住类型计算学分。我有一个有计算,吸气和设定的课程。但是,当我点击计算时,它只显示0作为总数。显然,事情没有正确连接。但是,我不确定在何处或如何解决此问题。

这是我的班级档案:

namespace QuarterTuition
{
    class Business
    {

        private int creditsTotal;
        private int creditsPriceGuide;
        public static double FullCost;


        public Business(int credits, string residency)
        {
            creditsTotal = credits;
            Residency = residency;

        }


        public double FullPrice
        {
            get { return FullCost; }

        }



        // Checking that credits is greater than zero
        public static bool IsValidCreditAmount(int testValue)
        {
            return testValue > 0;
        }


        // Method for getting and setting Credit Totals.  
        public int CreditsTotal
        {
            get { return creditsTotal; }
            set
            {
                if (IsValidCreditAmount(value))
                {
                    creditsTotal = value;
                }
                else
                {
                    throw new ApplicationException("Invalid!  Credits must be greater than zero.");
                }
            }
        }

        // Method for determining which pricing section the credit amount is in.
           public int CreditAmountSection
        {

            get { return creditsTotal; }
            set
            {

                    if (creditsTotal <= 10)
                    {
                        creditsPriceGuide = 1;
                    }

                    if (creditsTotal > 10 && creditsTotal <= 18)
                    {
                        creditsPriceGuide = 2;
                    }
                    if (creditsTotal > 18)
                    {
                        creditsPriceGuide = 3;
                    }
                else
                {
                    throw new ApplicationException("Invalid Credit Amount!");
                }

            }
        }

        // Checking that the string is empty
        public static bool IsEmptyString(string testValue)
        {
            return testValue == "";
        }


        // Checking that the residency is not an empty string
        public static bool IsValidResidency(string testValue)
        {
            return !IsEmptyString(testValue);
        }

        //variable
        private string residentType;


        // Method to check Residency Type
        public string Residency
        {
            get { return residentType; }
            set
            {
                if (IsValidResidency(value))
                    residentType = value;
                else
                    throw new ApplicationException("Invalid Residency");

            }
        }





        // 1-10 CREDITS
        // Cost for 1-10 Credits
        const double Resident110 = 104.11;
        const double NResUS110 = 117.11;
        const double NResNUS110 = 276.11;

        // 11-18 CREDITS
        // Cost for 11-18 Credits
        const double Resident118Base = 1041.10;
        const double NResUS1118Base = 1171.10;
        const double NResNUS1118Base = 2761.10;
        // Cost for each credit 11-18
        const double Resident11218 = 51.40;
        const double NResUS11218 = 52.09;
        const double NResNUS11218 = 56.41;

        // EXCESS CREDITS
        // Cost for 19+ Credits
        const double Resident18Base = 1452.30;
        const double NResUS18Base = 1587.82;
        const double NResNUS18Base = 3212.38;
        // Cost for each credit beyond 18
        const double Resident18More = 96.26;
        const double NResUS18More = 96.26;
        const double NResNUS18More = 268.26;



            /// <summary>
            /// CALCULATIONS BELOW
            /// </summary>
        public void ResidentCreditCost()
        {
            int credits = CreditAmountSection;
            int credSection = CreditAmountSection;
            string resType = Residency;

            if (resType == "Resident")
            {
                if (credSection == 1)
                {
                    FullCost = Resident110 * credits;

                }

                if (credSection == 2)
                {
                    double temp = Resident118Base * 10;
                    int tempCred = credits - 10;
                    double tempAdditional = tempCred * Resident11218;
                    FullCost = temp + tempAdditional;

                }
                if (credSection == 3)
                {
                    double temp = Resident18Base * 18;
                    int tempCred = credits - 18;
                    double tempAdditional = tempCred * Resident18More;
                    FullCost = temp + tempAdditional;
                }
            }
        }

        public void NonResidentUSCreditCost()
        {

            int credits = CreditAmountSection;
            int credSection = CreditAmountSection;
            string resType = Residency;

            if (resType == "NResUS")
            {
                if (credSection == 1)
                {
                    FullCost = NResUS110 * credits;
                }

                if (credSection == 2)
                {
                    double temp = NResUS1118Base * 10;
                    int tempCred = credits - 10;
                    double tempAdditional = tempCred * NResUS11218;
                    FullCost = temp + tempAdditional;

                }
                if (credSection == 3)
                {
                    double temp = NResUS18Base * 18;
                    int tempCred = credits - 18;
                    double tempAdditional = tempCred * NResUS18More;
                    FullCost = temp + tempAdditional;
                }
            }
        }

        public void NonResidentNUSCreditCost()
        {

            int credits = CreditAmountSection;
            int credSection = CreditAmountSection;
            string resType = Residency;

            if (resType == "NResNUS")
            {
                if (credSection == 1)
                {
                    FullCost = NResNUS110 * credits;
                }

                if (credSection == 2)
                {
                    double temp = NResNUS1118Base * 10;
                    int tempCred = credits - 10;
                    double tempAdditional = tempCred * NResNUS11218;
                    FullCost = temp + tempAdditional;

                }
                if (credSection == 3)
                {
                    double temp = NResUS18Base * 18;
                    int tempCred = credits - 18;
                    double tempAdditional = tempCred * NResNUS18More;
                    FullCost = temp + tempAdditional;
                }
            }
        }

    }
}

这是我的计算按钮:

private void btnCalculate_Click(object sender, EventArgs e)
{
    int credits = Convert.ToInt16(tbCredits.Text);
    string residency = "";

    if (rbResident.Checked == true)
    {
        residency = "Resident";
    }
    if (rbNResUS.Checked == true)
    {
        residency = "NResUS";
    }
    if (rbNResNUS.Checked == true)
    {
        residency = "NResNUS";
    }

    Business bus = new Business(credits, residency);

    double totalComplete = bus.FullPrice;

    // Test that it is obtaining the credits
    tbTotal.Text = Convert.ToString(totalComplete);
    // End Test
}

如何进行计算并显示TextBox中的总学费?

3 个答案:

答案 0 :(得分:1)

您的媒体资源FullPrice无法在您的代码中设置。

在您FullPrice的获取者中,您返回FullCost。但是这个FullCost也没有设置。

在您的代码中,您只是实例化Business类,但不计算任何内容。

您需要以下内容:

Business bus = new Business(credits, residency);
bus.ComputeCosts();
double totalComplete = bus.FullPrice;

答案 1 :(得分:0)

Business类的构造函数中调用您的计算,并将值设置为FullCostFullCost应该是creditsTotal

之类的字段

答案 2 :(得分:0)

你根本没有调用计算函数.Pls调用那些函数