c#设置纳税人类数据成员等于另一种方法

时间:2011-04-17 16:25:57

标签: c# class interface methods

我对c#很陌生,对于如何设置数据成员然后用不同的方法计算税收感到困惑。以下是该计划应该做的部分:

“如果用户选择默认值,则使用默认构造函数选择默认值,并将Taxpayer类数据成员设置为等于调用rates对象CalculateTax方法返回的值。”

- 如果他们选择'D',我有一个switch语句调用默认构造函数,我不知道如何将纳税人类数据设置为默认值,我不确定CalculateTax方法是否正确。< / p>

如果他们选择'O'

,那么就做同样的事情

这是我到目前为止所做的:

using System;
interface Icomparable
{
    int CompareTo(Object o);
}
    class rates
    {
        public double Limit{get;}
        public double LowRate{get;}
        public double HighRate{get;}
        private rates()
        {
            Limit = 30000;
            LowRate = 0.15;
            HighRate = 0.28;
        }
        public rates(double Limit; double LowRate; double HighRate;)
        {
            Console.Write("Enter dollar limit: ");              
            Limit = Convert.ToDouble(Console.ReadLine());
            Console.Write("Enter the low rate: ");
            LowRate = Convert.ToDouble(Console.ReadLine());
            Console.Write("Enter the high rate: ");
            HighRate = Convert.ToDouble(Console.ReadLine());
        }
        public CalculateTax()
        {
            if(Income < Limit)
            {TaxOwed = (Income * LowRate)}
            else
            {TaxOwed = (Income * HighRate)}
        }
    }

    public class taxpayer : IComparable
    {
        public string SSN{get; set;}
        public double Income{get; set;}
        public double TaxOwed{get;}

        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;
        }
    public getRates()
    {
    double Limit;
    double LowRate;
    double HighRate;
    Console.WriteLine("Do you want default values(enter D) or enter your own (enter O)?");
    entry = Convert.ToChar(Console.ReadLine());

    switch (entry)  //set switch
    {
        case'D':
            rates();
            break;
        case'O':
            rates(double Limit; double LowRate; double HighRate;)
            break;
        default:
            Console.WriteLine("Invalid input");
            goto getRates();
    }


    }
    }

非常感谢任何帮助。

2 个答案:

答案 0 :(得分:1)

我不明白为什么你会继续创建一个拥有所有值的对象。 这是一个替代模式,静态预设+工厂

class Rates
{
    public double Limit{get;}
    public double LowRate{get;}
    public double HighRate{get;}

    static readonly Rates default = new Rates(30000,0.15,0.28);
    static readonly Rates govna = new Rates(300000,0.1,0.2);
    static readonly Rates priest = new Rates(300,0.05,0.07);

    public static Rates createRates()
    {
        double Limit; double LowRate; double HighRate;
        Console.Write("Enter dollar limit: ");              
        Limit = Convert.ToDouble(Console.ReadLine());
        Console.Write("Enter the low rate: ");
        LowRate = Convert.ToDouble(Console.ReadLine());
        Console.Write("Enter the high rate: ");
        HighRate = Convert.ToDouble(Console.ReadLine());

        return new Rates( Limit, LowRate, HighRate );
    }

    private Rates(double limit; double lowRate; double highRate;)
    {
        Limit = limit;
        LowRate = lowRate;
        HighRate = highRate;
    }

    public double CalculateTax( double Income)
    {
        if(Income < Limit)
        {return (Income * LowRate)}
        else
        {return (Income * HighRate)}
    }
}

哦,还有什么与goto?

while( null == ratesResult ){
    Console.WriteLine("Do you want default values(enter D) or enter your own (enter O)?");
    entry = Convert.ToChar(Console.ReadLine());

    switch (entry)  //set switch
    {
        case'D':
            ratesResult = Rates.default;
            break;
        case'O':
            ratesResult = Rates.createRates();
            break;
        default:
            Console.WriteLine("Invalid input");
    }

}

答案 1 :(得分:0)

你的代码有很多问题,伙计。以下是一些可以指导您正确方向的建议。

在以下属性中,您还应该有一个集合;访问者能够存储这些值。

    public double Limit{get;}
    public double LowRate{get;}
    public double HighRate{get;}

在以下功能中,是否要提示用户输入值?如果是这样,请从

更改第一行
public rates(double Limit; double LowRate; double HighRate;)

public rates(bool dummy)

现在您的代码看起来像

    public rates(bool dummy) //Note that dummy is not used in this function.  It just distinguishes rates() from rates(bool)
    {
        Console.Write("Enter dollar limit: ");              
        Limit = Convert.ToDouble(Console.ReadLine());
        Console.Write("Enter the low rate: ");
        LowRate = Convert.ToDouble(Console.ReadLine());
        Console.Write("Enter the high rate: ");
        HighRate = Convert.ToDouble(Console.ReadLine());
    }

假人只是为了让代码区分你的两个不同的功能。

在你的switch语句中,只需调用rate(bool dummy)函数而不是另一个函数。对于假人,分配它是真还是假,谁在乎......它看起来如下。

switch (entry)  //set switch
{
    case'D':
        rates();
        break;
    case'O':
        rates(true)
        break;
    default:
        Console.WriteLine("Invalid input");
        goto getRates();
}

您的代码存在更多问题,但在我们可以为您提供进一步帮助之前,您必须对其进行修改。您应该尝试在VS中使用它,因为编辑器可以通过为您提供特定错误来帮助您解决问题。