I am having trouble with this following call, specially the last component:
Console.WriteLine(\"Taxpayer # {0} SSN: {1}, Income is {2:c}, Tax is {3:c}\", i + 1,
Your last decision point else taxOwed = 0;
will never execute, so it's not needed. I've ran your code as shown below and everything works. The problem must be with either the parameter being passed into the method being zero, or you are not setting the values like you think you are.
void Main()
{
var result = CalculateTax(40000);
Console.WriteLine(result);
}
public int CalculateTax(int income)
{
var incLimit = 50000;
var lowTaxRate = 0.10;
var highTaxRate = 0.25;
int taxOwed;
if (income < incLimit){
taxOwed = Convert.ToInt32(income * lowTaxRate); }
else if(income >= incLimit) {
taxOwed = Convert.ToInt32(income * highTaxRate);}
return taxOwed;
}
Now that you posted your full code, your problem is that you need to change the static GetRates()
method to return the rates, as Clark mentions. That static method is the only place calling rates.assignRates()
and those assigned rates are only good for that specific instance of rates
contained in that method and nowhere else. So change GetRates()
to to return the rates
instance as follows:
public static Rates GetRates()
{
...
Rates rates = new Rates();
...
return rates;
}
Then change the main method as follows:
static void Main(string[] args)
{
Taxpayer[] taxArray = new Taxpayer[5];
// Implement a for-loop that will prompt the user to enter
// the Social Security Number and gross income.
...
Rates taxRates = Taxpayer.GetRates();
// Implement a for-loop that will display each object as formatted
// taxpayer SSN, income and calculated tax.
for (int i = 0; i < taxArray.Length; ++i)
{
Console.WriteLine(
"Taxpayer # {0} SSN: {1}, Income is {2:c}, Tax is {3:c}",
i + 1,
taxArray[i].SSN,
taxArray[i].grossIncome,
taxRates.CalculateTax(50000));
}
...
}
I would add debug statements in your method to confirm that lowTaxRate and hightTaxRate are not zero/null.
No-repro. Using the simple program below, I get valid non-zero result (900
using my values):
internal class Program {
private static int incLimit = 30000;
private static float lowTaxRate = 0.18F;
private static float highTaxRate = 0.30F;
private static void Main(string[] args) {
var result = CalculateTax(5000);
}
public static int CalculateTax(int income) {
int taxOwed;
// If income is less than the limit then return the tax
// as income times low rate.
if (income < incLimit) {
taxOwed = Convert.ToInt32(income * lowTaxRate);
}
// If income is greater than or equal to the limit then
// return the tax as income times high rate.
else if (income >= incLimit) {
taxOwed = Convert.ToInt32(income * highTaxRate);
}
else taxOwed = 0;
return taxOwed;
}
}
Your problem is in your usage of the Rates.assignRates() method. You are only calling it from the static Taxpayer.GetRates() method. This method is acting on a local Rates object, and then then throwing the populated object away. You probably want to change Taxpayer.GetRates() to return a Rates object, returning the internally created (and populated) rates variable:
public static Rates GetRates() { ... return rates; }
And then in Main(), remove the existing call to Taxpayer.GetRates() and change the line where you declare the taxRates variable as follows:
Rates taxRates = Taxpayer.GetRates();
Also note that you should also be handling error cases due to bad/missing input somehow, but you don't seem to be doing that right now, so I didn't include any functional changes other than to get you back the populated Rates object.
Additionally, you might want to consider making the Rates class static, as you appear to only be using a single instance of it throughout.
You sure lowTaxRate and highTaxRate are not set to 0 because anything multiplied by 0 is 0. Put some debuggers/MessageBoxes to check that.