2016-04-15 18 views
-3

Ich verwende Visual Studio 2015 und C#. Ich versuche Credits basierend auf dem Residency-Typ zu berechnen. Ich habe eine Klasse, die die Berechnungen und Getters und Setter hat. Wenn ich jedoch auf Berechnen klicke, wird nur 0 als Summe angezeigt. Offensichtlich verbinden sich die Dinge nicht richtig. Ich bin mir jedoch nicht sicher, wo oder wie ich das beheben kann.C# Innerhalb einer Klasse berechnen

Dies ist meine Klassendatei:

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; 
       } 
      } 
     } 

    } 
} 

und dies ist meine Schaltfläche berechnen:

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 
} 

Wie erhalte ich meine Berechnungen den Gesamtunterricht in den TextBox auftreten und zeigen?

+1

Haben Sie schon debugged? Gehen Sie Schritt für Schritt durch und sehen Sie, wo es nicht mehr Corerte ist. – Mafii

+1

Hallo, siehe [Erstellen eines minimalen, vollständigen und überprüfbaren Beispiels] (http://stackoverflow.com/help/mcve). Es ist einfacher, dir zu helfen, wenn es nicht so viel Code gibt. :) – smead

+0

Ich rate, es nicht zu setzen Ihre CreditsTotal zu einem Wert sein – BugFinder

Antwort

1

Ihre Immobilie FullPrice ist nirgends in Ihrem Code festgelegt.

Im Getter Ihrer FullPrice geben Sie FullCost zurück. Aber diese FullCost ist auch nirgends gesetzt.

In Ihrem Code sind Sie nur die Business Klasse instanziieren, aber nichts zu berechnen.

Sie brauchen so etwas wie:

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

Rufen Sie Ihre Berechnungen in den Konstruktor Business Klasse und legen Wert auf die FullCost. FullCost sollte ein Feld wie creditsTotal sein.

+0

Ich habe die FullCost-Konstruktor-Setup, aber wie kann ich diese Berechnungen aufrufen? public double FullPrice { erhalten {return FullCost; } set {FullCost = Wert; } } – Someone

+0

'public Business (int Kredite, String Residenz) { creditsTotal = Kredite; Residency = Wohnsitz; ResidentCreditCost(); NonResidentUSCreditCost(); NonResidentNUSCreditCost(); } ' – mihkov

0

Sie rufen die Berechnungsfunktion überhaupt nicht auf. Rufen Sie diese Funktionen

Verwandte Themen