2016-04-15 13 views
-3

私はVisual Studio 2015とC#を使用しています。私はレジデンスタイプに基づいてクレジットを計算しようとしています。計算とゲッターとセッターを持つクラスがあります。しかし、計算をクリックすると、合計として0が表示されます。明らかに、物事は正しく接続されていません。しかし、私はどこでこれを解決するかについては不明です。C#クラス内で計算する

これは私のクラスファイルです:

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で、総授業料を発生して表示してもらうにはどうすればよいですか?

+1

まだデバッグしましたか?一歩一歩進み、もうコアラクトではないところを見てください。 – Mafii

+1

こんにちは、[最小限の、完全で検証可能なサンプルの作成方法](http://stackoverflow.com/help/mcve)を参照してください。あまりにも多くのコードがなければ、あなたを助けるほうが簡単です。 :) – smead

+0

あなたのクレジットを設定していないと推測しています。合計値は – BugFinder

答えて

1

プロパティFullPriceはあなたのコードのどこにも設定されていません。

FullPriceのゲッターでは、FullCostが返されます。しかし、このFullCostもどこにも設定されていません。

コードでは、Businessクラスをインスタンス化するだけですが、何も計算しません。 FullCostからBusinessクラスのコンストラクタと設定した値で、あなたの計算を呼び出します

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

はあなたのような何かを必要としています。 FullCostcreditsTotalのようなフィールドにする必要があります。

+0

FullCostコンストラクタの設定がありますが、その計算をどのように呼びますか?パブリックダブルFullPrice { get {return FullCost; } {fullCost = value; } } – Someone

+0

'publicビジネス(intクレジット、文字列レジデンシー) { クレジット合計=クレジット; 居住=レジデンス; ResidentCreditCost(); NonResidentUSCreditCost(); NonResidentNUSCreditCost(); } ' – mihkov

0

計算機能は全く呼び出されていません。これらの関数を呼び出します。