2012-01-15 64 views
-2

我無法實施Icomparable CompareTo根據稅收比較納稅人對象..有人可以幫助我實現納稅人類的icomparable實現? 我想實現像這裏的接口了IComparable是新話題,me..please幫助http://www.dotnetperls.com/icomparable實施對納稅人類的Ipaparable進行排序基於taxOwed

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 

namespace Taxes 
{ 
    class Rates 
    { 
     // Create a class named rates that has the following data members: 
     private int incomeLimit; 
     private double lowTaxRate; 
     private double highTaxRate; 

     public int IncomeLimit // use read-only accessor. 
     { get { return incomeLimit; } } 

     public double LowTaxRate // use read-only accessor. 
     { get { return lowTaxRate; } } 

     public double HighTaxRate // use read-only accessor. 
     { get { return highTaxRate; } } 

     //A class constructor that assigns default values of limit=30000, low rate = .15 and high rate = .28. 
     public Rates() 
     { 
      int limit = 30000; 
      double lowRate = .15; 
      double highRate = .28; 

      incomeLimit = limit; 
      lowTaxRate = lowRate; 
      highTaxRate = highRate; 
     } 

     //A class constructor that takes three parameters to assign input values for limit, low rate and high rate. 
     public Rates(int limit, double lowRate, double highRate) 
     { 

     } 

     // A CalculateTax method that takes an income parameter and computes the tax as follows: 
     public int CalculateTax(int income) 
     { 
      int limit = 0; 
      double lowRate = 0; 
      double highRate = 0; 
      int taxOwed = 0; 

      // If income is less than the limit then return the tax as income times low rate. 
      if (income < limit) 
       taxOwed = Convert.ToInt32(income * lowRate); 
      // If income is greater than or equal to the limit then return the tax as income times high rate. 
      if (income >= limit) 
       taxOwed = Convert.ToInt32(income * highRate); 
      return taxOwed; 
     } 
    } //end class Rates 

    // Create a class named Taxpayer that has the following data members: 
    public class Taxpayer 
    { 
     //Social Security number (use type string, no dashes between groups). Use get and set accessors. 
     string SSN 
     { get; set; } 

     int yearlyGrossIncome // Use get and set accessors. 
     { get; set; } 

     public int taxOwed // Use read-only accessor. 
     { 
      get { return taxOwed; } 
     } 

     // ** The Taxpayer class should be set up so that its objects are comparable to each other based on tax owed. 
     class taxpayer : IComparable 
     { 
      public int taxOwed { get; set; } 
      public int income { get; set; } 

      int IComparable.CompareTo(Object o) 
      { 
       int returnVal; 
       taxpayer temp = (taxpayer)o; 
       if (this.taxOwed > temp.taxOwed) 
        returnVal = 1; 
       else 
        if (this.taxOwed < temp.taxOwed) 
         returnVal = -1; 
        else 
         returnVal = 0; 
       return returnVal; 


      } // End IComparable.CompareTo 
     } //end taxpayer IComparable class 

     // **The tax should be calculated whenever the income is set. 
     // The Taxpayer class should have a getRates class method that has the following. 
     public static void GetRates() 
     { 
      // Local method data members for income limit, low rate and high rate. 
      int incomeLimit = 0; 
      double lowRate; 
      double highRate; 
      string userInput; 

      // Prompt the user to enter a selection for either default settings or user input of settings. 
      Console.Write("Would you like to enter your own values? (enter 0) or would you like to use the default values? (enter 1): "); 

      /* If the user selects default the default values you will instantiate a rates object using the default constructor 
      * and set the Taxpayer class data member for tax equal to the value returned from calling the rates object CalculateTax method.*/ 
      userInput = Convert.ToString(Console.ReadLine()); 

      if (userInput == "1") 
      { 
       Rates rates = new Rates(); 
       rates.CalculateTax(incomeLimit); 
      } // end if 

      /* If the user selects to enter the rates data then prompt the user to enter values for income limit, low rate and high rate, 
      * instantiate a rates object using the three-argument constructor passing those three entries as the constructor arguments and 
      * set the Taxpayer class data member for tax equal to the valuereturned from calling the rates object CalculateTax method. */ 

      if (userInput == "0") 
      { 
       Console.Write("Please enter the income limit: "); 
       incomeLimit = Convert.ToInt32(Console.ReadLine()); 
       Console.Write("Please enter the low rate: "); 
       lowRate = Convert.ToDouble(Console.ReadLine()); 
       Console.Write("Please enter the high rate: "); 
       highRate = Convert.ToDouble(Console.ReadLine()); 

       Rates rates = new Rates(incomeLimit, lowRate, highRate); 
       rates.CalculateTax(incomeLimit); 

      } // end if 
     } //end GetRates class 



     static void Main(string[] args) 
     { 
      // instantiate an array of five (5) Taxpayer objects. 

      string SSN = "0"; 
      int income = 0; 
      int tax = 0; 
      int x = 1; 
      Taxpayer[] taxArray = new Taxpayer[5]; 


      // Implement a for-loop that will prompt the user to enter the Social Security Number and gross income. 
      for (x = 1; x < taxArray.Length; x++) 
      { 
       taxArray[x] = new Taxpayer(); 
       Console.Write("Please enter the Social Security Number for taxpayer {0}: ", x); 
       taxArray[x].SSN = Console.ReadLine(); 

       //SSN = String.Format("{0:000-00-0000}"); 
       Console.Write("Please enter the gross income for taxpayer {0}: ", x); 
       taxArray[x].yearlyGrossIncome = Convert.ToInt32(Console.ReadLine()); 

       Taxpayer.GetRates(); 

      } //end for 

      // Implement a for-loop that will display each object as formatted taxpayer SSN, income and calculated tax. 
      for (int i = 0; i < 5; i++) 
      { 

       Console.WriteLine("Taxpayer # {0} SSN: {1}, Income is {2:c}, Tax is {3:c}", i, SSN, income, tax); 
      } // end for 



      // Implement a for-loop that will sort the five objects in order by the amount of tax owed and then display 
      //each object as formatted taxpayer SSN, income and calculated tax. 

      Array.Sort(taxArray); 
      Console.WriteLine("Sorted by tax owed"); 
      for (int i = 0; x < taxArray.Length; i++) 
      { 
       Console.WriteLine("Taxpayer # {0} SSN: {1}, Income is {2:c}, Tax is {3:c}", i, SSN, income, tax); 


      } 


     } //end main 
    } // end Taxpayer class 


} //end namespace 

// Internal documentation 

+0

太多的代碼。請僅張貼_relevant_部分。另請解釋_why_您無法實現這些接口。究竟是什麼問題? – Oded 2012-01-15 22:44:48

+0

@Oded是對的,因爲如果你想實現IComparable <>,你只需要你想要比較的類。根據我的理解,你想比較納稅人的對象,所以你可以列出納稅人類。檢查下面的答案,你需要更多的幫助嗎? – Mzn 2012-01-16 11:33:35

回答

1

您需要在公開課上實施IComparable<Taxpayer>並取消私人taxpayer類。

這裏是納稅人類的第一部分的重寫:

public class Taxpayer : IComparable<Taxpayer> 
{ 
    //Social Security number (use type string, no dashes between groups). Use get and set accessors. 
    string SSN 
    { get; set; } 

    int yearlyGrossIncome // Use get and set accessors. 
    { get; set; } 

    public int taxOwed // Use read-only accessor. 
    { 
     get { return taxOwed; } 
    } 

    #region IComparable<Taxpayer> Members 

    int IComparable<Taxpayer>.CompareTo(Taxpayer other) 
    { 
     if (this.taxOwed > other.taxOwed) 
      return 1; 
     else 
      if (this.taxOwed < other.taxOwed) 
       return -1; 
      else 
       return 0; 
    } 

    #endregion 

    // **The tax should be calculated whenever the income is set. 
    // The Taxpayer class should have a getRates class method that has the following. 
    public static void GetRates() 
0

你已經實現了IComparable接口。我建議你使用IComparable<taxpayer>

這是你如何能在taxpayer類實現IComparable<taxpayer>: 你並不需要添加CompareTo方法內if語句。如果this的值較小且other較大,則返回負值。如果情況正好相反,則返回正值。如果它們相等,則返回零。這是相互this.taxOwed - other.taxOwed

class taxpayer : IComparable<taxpayer> 
{ 
    public int taxOwed { get; set; } 
    public int income { get; set; } 

    public int CompareTo(taxpayer other) 
    { 
     return taxOwed - other.taxOwed; 
    } 
} //end taxpayer IComparable 

只是減去他們的情況下,如果你使用這樣的:

public static List<taxpayer> taxPayers = new List<taxpayer>() 
    { 
     new taxpayer() { income = 1, taxOwed = 1000 }, 
     new taxpayer() { income = 2, taxOwed = 120 }, 
     new taxpayer() { income = 3, taxOwed = 7812 }, 
     new taxpayer() { income = 4, taxOwed = 4210 }, 
     new taxpayer() { income = 5, taxOwed = 400 }, 
     new taxpayer() { income = 6, taxOwed = 230 }, 
    }; 

    static void Main() 
    { 
     taxPayers.Sort(); 

     foreach (var t in taxPayers) 
      Console.WriteLine(t.taxOwed); 
    } 

輸出將是:

120 
230 
400 
1000 
4210 
7812 

更新我由代碼困惑,我沒有注意到,有兩類taxpayerTaxpayer。你爲什麼擁有他們兩個?你只需要一個,你可以像上面那樣實現IComparable<Taxpayer>

+0

如果你想要它們按照降序排列而不是像上面那樣升序,return other.taxOwed - taxOwed;在比較中。 – Mzn 2012-01-15 23:03:10

相關問題