2014-11-24 54 views
-1

在文本文件中欠下的人麻烦diplaying税我无法弄清楚如何做我使用的文本文件里这个税收欠款计算。当我运行程序时,它会显示每个人的收入和申请状态,但所欠的税款不会显示。具有取决于他们的收入和报税身份

class Program 
{ 
    static void Main(string[] args) 
    { 

     Customers.CustomerTax(DataIO.TextFileToString(@"C:\Users\Matt\Desktop\tax.txt")); 

     foreach (Customers item in Customers._taxList) 

     { 

      Console.WriteLine(item.ToString()); 

      FilingStatus fs = (FilingStatus)Enum.Parse(typeof(FilingStatus), "Single"); 
     } 
    } 
} 

这里就是我做我的计算

class Customers 
{ 

    String _marriage; 
    double _income; 
    double _taxOwed; 
    private FilingStatus _filingstatus; 

    public static List<Customers> _taxList = new List<Customers>(); 



    public Customers(String marriage, int income) 
    { 
     Income = income; 
     Marriage = marriage; 

    } 

    public String Marriage 
    { 
     get { return _marriage; } 
     set 
     { 

      _marriage = value; 

     } 
    } 



    public double Income 
    { 
     get { return _income; } 
     set 
     { 
      if (value <= 0) 
      { 
       throw new ArgumentException(String.Format("{0} must be > 0", value)); 
      } 
      _income = value; 

     } 
    } 

    public double CalculateTax() 
    { 
     double taxOwed = 0.0; 

     if (_filingstatus == FilingStatus.Single) 
     { 
      if (Income <= TaxCalculations.Single10) 
      { 
       taxOwed = .1 * Income; 
      } 

      else if (Income <= TaxCalculations.Single15) 
      { 
       taxOwed = .15 * Income; 
      } 
      else if (Income <= TaxCalculations.Single25) 
      { 
       taxOwed = .25 * Income; 
      } 
      else if (Income <= TaxCalculations.Single28) 
      { 
       taxOwed = .28 * Income; 
      } 
      else if (Income <= TaxCalculations.Single33) 
      { 
       taxOwed = .33 * Income; 
      } 
      else if (Income <= TaxCalculations.Single35) 
      { 
       taxOwed = .35 * Income; 
      } 
      else Console.WriteLine("Derp"); 
     } 
     else if (_filingstatus == FilingStatus.MarriedJoint) 
     { 
      if (Income <= TaxCalculations.MarriedJoint10) 
      { 
       taxOwed = .1 * Income; 
      } 

      else if (Income <= TaxCalculations.MarriedJoint15) 
      { 
       taxOwed = .15 * Income; 
      } 
      else if (Income <= TaxCalculations.MarriedJoint25) 
      { 
       taxOwed = .25 * Income; 
      } 

      else if (Income <= TaxCalculations.MarriedJoint28) 
      { 
       taxOwed = .28 * Income; 
      } 
      else if (Income <= TaxCalculations.MarriedJoint33) 
      { 
       taxOwed = .33 * Income; 
      } 

      else if (Income <= TaxCalculations.MarriedJoint35) 
      { 
       taxOwed = .35 * Income; 
      } 
      else Console.WriteLine("Derp"); 
     } 
     else if (_filingstatus == FilingStatus.MarriedSeparate) 
     { 
      if (Income <= TaxCalculations.MarriedSeparate10) 
      { 
       taxOwed = .1 * Income; 
      } 

      else if (Income <= TaxCalculations.MarriedSeparate15) 
      { 
       taxOwed = .15 * Income; 
      } 
      else if (Income <= TaxCalculations.MarriedSeparate25) 
      { 
       taxOwed = .25 * Income; 
      } 

      else if (Income <= TaxCalculations.MarriedSeparate28) 
      { 
       taxOwed = .28 * Income; 
      } 
      else if (Income <= TaxCalculations.MarriedSeparate33) 
      { 
       taxOwed = .33 * Income; 
      } 

      else if (Income <= TaxCalculations.MarriedSeparate35) 
      { 
       taxOwed = .35 * Income; 
      } 
      else Console.WriteLine("Derp"); 
     } 
     else if (_filingstatus == FilingStatus.HeadOfHouse) 
     { 
      if (Income <= TaxCalculations.HeadOfHouse10) 
      { 
       taxOwed = .1 * Income; 
      } 

      else if (Income <= TaxCalculations.HeadOfHouse15) 
      { 
       taxOwed = .15 * Income; 
      } 
      else if (Income <= TaxCalculations.HeadOfHouse25) 
      { 
       taxOwed = .25 * Income; 
      } 

      else if (Income <= TaxCalculations.HeadOfHouse28) 
      { 
       taxOwed = .28 * Income; 
      } 
      else if (Income <= TaxCalculations.HeadOfHouse33) 
      { 
       taxOwed = .33 * Income; 
      } 

      else if (Income <= TaxCalculations.HeadOfHouse35) 
      { 
       taxOwed = .35 * Income; 
      } 
      else Console.WriteLine("Derp"); 

     } 
     else Console.WriteLine("Not a Valid Filing Status"); 
     return taxOwed; 

    }  


    public static void CustomerTax(String s) 
    { 

     String[] customersArr = s.Split('\n'); 

     foreach (string item in customersArr) 
     { 
      if (item.Equals("")) continue; 

      String[] customer = item.Split(','); 
      Customers p = new Customers(customer[1], int.Parse(customer[0])); 
      _taxList.Add(p); 


     } 
    } 
     public override string ToString() 
     { 
      return String.Format("Income: {0} Marriage Status: {1} Tax Owed: {2}", _income, _marriage, _taxOwed); 

     } 
    } 
} 

的StreamReader,数据IO

class DataIO 
{ 
    public static String TextFileToString(String path) 
    { 
     String output = ""; 
     String record = ""; 
     using (StreamReader sr = new StreamReader(path)) 
     { 

      while ((record = sr.ReadLine()) != null) 
      { 
       output += record + "\n"; 
      } 
     } 
     return output; 
    } 
} 
+1

您的代码中有很多额外的'}'。你可能想解决这个问题。 – gunr2171 2014-11-24 21:12:22

回答

2

或许这与你的ToString函数有问题吗?

public override string ToString() 
{ 
    return String.Format("Income: {0} Marriage Status: {1} Tax Owed:", _income, _marriage, _taxOwed); 
} 

应该是这样的: 添加您的括号在格式字符串中的第三个参数?

public override string ToString() 
{ 
    return String.Format("Income: {0} Marriage Status: {1} Tax Owed: {2}", _income, _marriage, _taxOwed); 
} 
+0

我修正了这些错误,但是对于我的文本文件中的每个人来说,欠款显示为0。 – Matt 2014-11-24 22:52:48

+0

起初我对自己有点困惑,但是你甚至都没有在你的代码中的任何地方设置'_taxOwed'变量。这可能是您要解决的下一个问题。 – Bobort 2014-11-25 16:15:57

+0

然后你的下一个问题就是你没有在你的代码中的任何地方设置你的'_filingstatus'。因为它没有设置,你的'taxOwed'变量仍然不会被设置。这些都是你缺少的基本原则。一个导师或一个班级可能是一个更好的地方去更好地理解你在做什么。当然,我自己也没有真正用C#编程,所以我也可能错过了一些东西。 – Bobort 2014-11-25 16:19:02