I'm pretty new to c# and am confused on how to set the data members and then calculate the tax with a different method. Here is what this part of the program should do:
"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."
-I have a switch statement calling for the default constructor if they choose 'D', I'm not sure how to set the taxpayer class data to default, i'm not sure if the CalculateTax method is correct either.
and then do the same type of thing if they choose 'O'
Here is what I have so far:
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("Ente开发者_开发百科r 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();
}
}
}
Any help with this would be very much appreciated.
I don't see why you would keep re creating an object that you have all the values for. Here is an alternate pattern, static presets + factory
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)}
}
}
Oh and whats with 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");
}
}
You have many problems with your code, buddy. Here is some advice that might guide you in the right direction.
In the following properties, you should also have a set; accessor to be able to store those values.
public double Limit{get;}
public double LowRate{get;}
public double HighRate{get;}
In the following function, do you want to prompt the user to enter values? If so, change the first line from
public rates(double Limit; double LowRate; double HighRate;)
to
public rates(bool dummy)
Now your code will look like
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());
}
the dummy is just to allow the the code to distinguish your two different functions.
In your switch statement, just call the rates(bool dummy) function instead of the other one. For the dummy, assign it either true or false, who cares.... It'd look like the following.
switch (entry) //set switch
{
case'D':
rates();
break;
case'O':
rates(true)
break;
default:
Console.WriteLine("Invalid input");
goto getRates();
}
There's more problems with your code, but you have to revise it before we can help you further. You should actually try to use it in VS, since the editor can help you fix problems by giving you specific errors.
精彩评论