Changing the Track-bar's Maximum Value - c#

I have a text-box with the intention of entering integers. This integer is supposed to affect all 6 track-bars I have connected with each other, with the track-bar's maximum value all become addends to the entered integer.
For example; If I were to enter 318 into the first text-box, all 6 track-bar's maximum should be 318. If one track-bar's value is 67, then all of the bar's maximum should be 251, and so forth.
I have written all the code for this except that all 6 Track-Bar's Maximum value will not change when starting the program from it's default status. I've tried changing the event to be public instead of private, and putting the code in the public Form1() instead of an event but to no avail. What do I do? Here is an example of an event for one of the sliders I have.
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
Foo();
}
public void Foo()
{
basetotal.Text = "600";
hpvalue.Text = "0";
attackvalue.Text = "0";
defensevalue.Text = "0";
speedvalue.Text = "0";
specialattackvalue.Text = "0";
specialdefensevalue.Text = "0";
hpslider.Minimum = 0;
hpslider.TickFrequency = 100;
hpslider.LargeChange = 1;
double total, hp, attack, defense, speed, spa, spd, max;
total = double.Parse(basetotal.Text);
hp = double.Parse(hpvalue.Text);
attack = double.Parse(attackvalue.Text);
defense = double.Parse(defensevalue.Text);
speed = double.Parse(speedvalue.Text);
spa = double.Parse(specialattackvalue.Text);
spd = double.Parse(specialdefensevalue.Text);
max = total - hp - attack - defense - speed - spa - spd;
hpslider.Maximum = (int)max;
attackslider.Maximum = (int)max;
defenseslider.Maximum = (int)max;
speedslider.Maximum = (int)max;
specialattackslider.Maximum = (int)max;
specialdefenseslider.Maximum = (int)max;
}
private void Form1_Load(object sender, EventArgs e)
{
}
public void hpslider_ValueChanged(object sender, EventArgs e)
{
Foo();
}

Related

'double' does not contain a definition for 'Text' and no extension method

Me and my teacher cannot figure this out. It's a windows forms app with all the appropriate fields linked, this is my only issue.
Error 1 'double' does not contain a definition for 'Text' and no extension method
here's my code:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void calculate_Click(object sender, EventArgs e)
{
double total = 0; //total amount
double tip = 0; //tip percentage
double meal = 0; //meal amount
tip = Convert.ToDouble(tip.Text) / 100;
meal = Convert.ToDouble(meal.Text);
total = (meal * tip);
total.Text = "$ " + Convert.ToString(total);
}
private void Form1_Load(object sender, EventArgs e)
{
}
}
It looks like you named your class-level Textboxes the exact same as your method-scoped variables. Or at least, that's my best assumption without knowing what your textboxes are actually called.
Your problem is you are trying to find a property Text on a double, which certainly does not exist. If you actually did name your textboxes the same (which is legal in C#), you'll want to reference them by using this.total and this.tip when trying to set the Text property.
It's worth noting that I'm concerned for your education if your teacher is unable to debug this.
I think you are trying to display the result into a Textbox; but Here in the code you declared total as double. please use the textbox name here (total.Text) in place of the variable total; And also use .ToString() instead of .Text inside your Convert.ToDouble(
Few more tips for you:
use double.TryParse for converting string to double
use total.ToString("$#00.00"); to get the the number prefixed with a $ symbol and rounded to 2 digits after the decimal
Let me assume the UI elements are :
//txtTip be the Textbox for tip percentage
//txtMeal be the Textbox for meal amount
//txtTotal be the Textbox for Total Amount
Then your code will be like the following:
double total = 0; //total amount
double tip = 0; //tip percentage
double meal = 0; //meal amount
if(! double.TryParse(txtTip.Text,out tip))
{
// show conversion failed error
}
if(! double.TryParse(txtMeal.Text,out meal))
{
// show conversion failed error
}
tip = tip / 100;
total = (meal * tip);
txtTotal.Text =total.ToString("$#00.00") ;
I think this is a mistake of use local variable and global variable.
class Test
{
// global variable.
int va = 1;
int vb = 2;
public void local()
{
bool va = false; // local variable
Console.WriteLine(va); // va is bool here.use local value.
Console.WriteLine(vb); // vb is int, use global value.
}
}
in your code, you declared local variables.
public partial class Form1 : Form
{
private void calculate_Click(object sender, EventArgs e)
{
// declare local variables...
// overwrite global varables.
double total = 0; //total amount
double tip = 0; //tip percentage
double meal = 0; //meal amount
// maybe, you want to invoke textbox.Text.
// but, tip(TextBox) object is overwrote by double.
// double has not the Text property, throw exception.
tip = Convert.ToDouble(tip.Text) / 100;
meal = Convert.ToDouble(meal.Text);
total = (meal * tip);
total.Text = "$ " + Convert.ToString(total);
}
}
how to fix it? just declare different variable name, like this:
private void calculate_Click(object sender, EventArgs e)
{
// make the local variable name is different with global.
double d_total = 0; //total amount
double d_tip = 0; //tip percentage
double d_meal = 0; //meal amount
d_tip = Convert.ToDouble(tip.Text) / 100; // tip is TextBox here
d_meal = Convert.ToDouble(meal.Text);
d_total = (d_meal * d_tip);
total.Text = "$ " + Convert.ToString(d_total);
}
or use this like this:
private void calculate_Click(object sender, EventArgs e)
{
double total = 0; //total amount
double tip = 0; //tip percentage
double meal = 0; //meal amount
tip = Convert.ToDouble(this.tip.Text) / 100;
meal = Convert.ToDouble(this.meal.Text);
total = (meal * tip);
this.total.Text = "$ " + Convert.ToString(total);
}

Point buy system for a tabletop rpg

I'm trying to make a point buy system using numeric up/downs. Here's the idea:
There are six numeric up/downs, one for each trait (Strength, Dexterity, Constitution, Intelligence, Wisdom and Charisma). Each trait begins at 10 points. You can't bring a trait below 7 or above 18.
I'm a total noob, but I managed to do this:
private void numericUpDown1_ValueChanged(object sender, EventArgs e)
{
numericUpDown1.Maximum = 18;
numericUpDown1.Minimum = 7;
}
I did this one six times. In my form, there is now six numeric up/downs. Now I'm trying to do something which is way too much for my minuscule knowledge.
I want a system in which the value of the six numeric up downs is combined and cannot be exceeded, which means that in this case, we would have 60 points and couldn't increase any score unless we decreased one. I would add 15 points to that "Point pool", so the user doesn't have to decrease a stat straight away, in order to increase another one.
Example: I have 1 point left, and my scores go as follows: 15, 15, 14, 10, 10, 10. I increase the third score by 1 point. I now have this:
15, 15, 15, 10, 10, 10.
Now I have nothing left, but I want my fourth score at 15 points. In order to achieve this, I have to decrease the fifth and sixth score until I have 5 points freed up. I now have this:
15, 15, 15, 15, 7, 8.
Having a Lil' box in my form to display how many points are left would be a cherry on top.
I did my best to explain this. Please take note that English is not my native language and I sometimes do struggle with it.
I'm clueless as to how I can achieve this, as I barely have any knowledge of C#. What would be the code missing ?
It would easier if you create a Character class
You could define defaults for each property in constructor, and individual methods to increase or decrease its points.
public class Character
{
private int totalPointsMax = 60;
private int maxPoints = 18;
private int minPoints = 7;
public int Strength { get; set; }
public int Dexterity { get; set; }
public int Constitution { get; set; }
public int Intelligence { get; set; }
public int Wisdom { get; set; }
public int Charisma { get; set; }
public Character()
{
// create new Character defaults...
Strength = 10;
Dexterity = 10;
Constitution = 10;
Intelligence = 10;
Wisdom = 10;
Charisma = 10;
}
private int GetTotalCharacterPoints()
{
return Strength + Dexterity + Constitution + Intelligence + Wisdom + Charisma;
}
//example of method to increase Strength
public void IncreaseStrength()
{
int availablePoints = totalPointsMax - GetTotalCharacterPoints();
if (availablePoints > 0 && Strength < maxPoints)
Strength++;
}
//example of method to decrease Strength
public void DecreaseStrength()
{
if (Strength >= minPoints)
Strength--;
}
//missing the other increase/decrease methods for the rest of features...
}
You just need to instance at beginning and your UI buttons only need to invoke the CharacterFoo.IncreaseStrength() or CharacterFoo.DecreaseWisdom() ... etc.
Also, with this option you can allways reuse this in any part of the game..
(ex: if your character finds any special potion .. then CharacterFoo.IncreaseStrength() )
Hope this helps...
There's a couple of ways to do it.
Firstly, move these outside of the event function:
numericUpDown1.Maximum = 18;
numericUpDown1.Minimum = 7;
My recommendation would be to set a variable with the maximum points:
private int MAX_POINTS = 60;
Then when one of them is changed, you can call another method that adds up all of the current boxes, and determines whether it is over the limit or not:
private bool TotalOfStatsIsOverLimit() {
int total = GetTotalOfStats();
return total > MAX_POINTS;
}
private int GetTotalOfStats() {
int total = 0;
// TODO: Go through each number box and add to the total
return total;
}
private void numericUpDown1_ValueChanged(object sender, EventArgs e) {
if(TotalOfStatsIsOverLimit()) {
// TODO: Decrease the value of the stat box that was updated
}
}
One advantage of doing it this way, is that you can reuse the same event method numericUpDown1_ValueChanged for all 6 of your stat boxes.
If I were you, I would go with a class for character since it will greatly simplify your future works, though if you are new, it may be harder at the start.
Still, you can follow up the basic approach as follows:
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
private const int totalPoints = 60 + 15; // sum of each trait plus the pool bonus
public Form1()
{
InitializeComponent();
foreach (Control control in this.Controls)
{
if (control is NumericUpDown)
{
NumericUpDown numControl = control as NumericUpDown;
numControl.Minimum = 7;
numControl.Maximum = 18;
numControl.Value = 10;
numControl.ValueChanged += nud_ValueChanged;
lblPointsLeft.Text = "15"; // bonus points
}
}
}
private void nud_ValueChanged(object sender, EventArgs e)
{
int sum = (int)(nudCha.Value + nudCon.Value + nudDex.Value + nudInt.Value + nudStr.Value + nudWis.Value);
int pointsLeft = totalPoints - sum;
NumericUpDown nudSender = (NumericUpDown)sender;
if (pointsLeft < 0)
{
MessageBox.Show("No points left");
// restore last change
// undo the last change
nudSender.Value = nudSender.Value - 1;
pointsLeft++;
}
lblPointsLeft.Text = pointsLeft.ToString();
}
}
}
Here's some Pseudocode:
You want to create a variable to hold your current points. Create some labels to hold that variable, and make sure you do AJAX calls, otherwise every time you update you are going to be calling this from the server again. This is probably better done in Javascript/Jquery.
int pointsUsed = numericUpDown1.value + numericUpDown2.value + numericUpDown6.value; //add all 6 of your values.
//for your textbox:
label1.text = "points left is:"
label2.text = 75 - pointsUsed;
private void checkBox1_Click(Object sender, EventArgs e)
{//to add points
if (pointsUsed < 75)
{
numericUpDown1.Value += 1;
pointsUsed += 1;
}
}
Check MSDN NumericUpDown for more info.

Why Is Chart Control Not Showing Any Data?

I want to make a graph of instant charge against time but the chart is not displaying anything.
What I am doing wrong?
Here is my code and snapshot of my output:
namespace WindowsFormsApplication21
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
double max = 24000000, min = 23999999.85;
chart1.ChartAreas.Add("0");
chart1.ChartAreas[0].AxisY.Minimum = min;
chart1.ChartAreas[0].AxisY.Maximum = max;
chart1.Series[0].Color = Color.Red;
}
private void button1_Click(object sender, EventArgs e)
{
double[] q = new double[10];
for (int i = 0; i < q.Length; i++)
{
int t = i + 1;
q[i] = (24 * Math.Pow(10, 6)) * Math.Exp(t / (2000 * Math.Pow(10,6)));
chart1.Series[0].Points.AddXY(t, q[i]);
}
}
}
}
This is my output
As you can see, your data simply don't fit the values; all are above 24M, which is the maximum you even show.
You should not try to hard-code these values imo. They may work today and stop working with the next set of data..
Here is how you can set them in code, after adding the DataPoints:
ChartArea CA = chart1.ChartAreas[0];
Series S1 = chart1.Series[0];
CA.AxisY.Maximum = S1.Points.Max(x => x.YValues[0]);
CA.AxisY.Minimum = S1.Points.Min(x => x.YValues[0]);
CA.AxisY.LabelStyle.Format = "###,###,###,##0.000";
(My system shows the decimal points in German localization.)

How to go about making a variable in a class output to a label after clicking on a picturebox?

I'm fairly new to OOP and am not sure how I would go about implementing something in my program. My program is pretty much similar to whack a mole and has an array of picture boxes with an image in and an image of a monster moves randomly between the picture boxes with a time interval applied or will move to a new random picture box whenever the user clicks on the monster in time. I have created an monster and a player sub class to try and add some OOP concepts to the program but am not sure how to implement what I want. Basically I have a label for score on my main form and a score variable in my animal class with a value. I want to be able to add the value of score from the label on my form when the user clicks on the picture box with the mole in and take away the value of score from the label when they don't click on it in time.
Here is my code:
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
PictureBox[] boxes;
int initialscore = 0;
int time = 0;
int randPos;
public Form1()
{
InitializeComponent();
}
private void boxes_MouseClick(object sender, MouseEventArgs e)
{
PictureBox pb2 = new PictureBox() { Image = Image.FromFile("sword2.png") };
this.Cursor = new Cursor(((Bitmap)pb2.Image).GetHicon());
for (int x = 0; x < 27; x++)
{
if (sender.Equals(boxes[x]))
{
Image grass = Image.FromFile("swamp.png");
PictureBox temp = (PictureBox)sender;
temp.Image = grass;
}
if (sender.Equals(boxes[x]))
{
PictureBox pb = (PictureBox)sender;
if (pb.Tag == "skeleton.png")
initialscore++;
}
}
label1.Text = " Score: " +initialscore.ToString();
}
public void timer1_Tick(object sender, EventArgs e)
{
boxes[randPos].Image = Image.FromFile("swamp.png");
boxes[randPos].Tag = "swamp.png";
Random r = new Random();
randPos=r.Next(0, 27);
boxes[randPos].Image = Image.FromFile("skeleton.png");
boxes[randPos].Tag = "skeleton.png";
}
private void Form1_Load(object sender, EventArgs e)
{
boxes = new PictureBox[27];
int top = 100;
int left = 100;
for (int x = 0; x < 27; x++)
{
boxes[x] = new PictureBox();
boxes[x].Image = Image.FromFile("swamp.png");
boxes[x].Height = 100;
boxes[x].Width = 100;
if (x % 9 == 0)
{
top += 120;
left = 120;
}
else
left += 120;
boxes[x].Top = top;
boxes[x].Left = (50 + left);
Controls.Add(boxes[x]);
this.boxes[x].MouseClick += new
System.Windows.Forms.MouseEventHandler(this.boxes_MouseClick);
label1.Text = " Score: " + initialscore.ToString();
label2.Text = " Time: " + time.ToString();
}
}
}
namespace WindowsFormsApplication1
{
class Monster
{
protected int score;
public Monster()
{
score = 10;
}
}
}
namespace WindowsFormsApplication1
{
class Player:Monster
{
}
}
Nothing has been added in the player class yet.
What do I need to add or change to be able to get the initial score to change by the value of the score in the monster class when clicking on the moving image?
To unify the updating/incrementing and visualization of the score you should extract that to a method:
public void incrementScore(int increment)
{
initialscore += increment;
label1.Text = " Score: " + initialscore.ToString();
}
in the Form1_Load you call this like:
incrementScore(0);
for the click on the monster you have different possibilities:
if all the monsters have the same points you can make it a static variable in the Monster class.
protected static int Score = 10;
which allows you to use it in the boxes_MouseClick event handler:
incrementScore(Monster.Score);
in case all monsters have another value you have to hold the score variable as an instance variable, identify somehow the instance of the monster class you clicked on and increment with this value

My code is not returning the correct value

I am trying to program a Form Pay Estimator and it calculates the gross pay, taxes owed, and net pay for an individual employee. For some reason my CalculateTaxes method in my Pay() class is not returning the correct value. This is the code I have here:
In my Pay() class:
{
//declare variables
private static int dependants;
private static double grossPay;
//property that gets and sets the dependants
public int NumOfDependents
{
get
{
return dependants;
}
set
{
dependants = value;
}
}
//calculates the gross pay for the production worker employee, using their hours
//worked times their wage and overtime is calculated for the employee, if
//applicable
public double CalculateGrossPay(double hoursWorked, double wageRate)
{
grossPay = hoursWorked * wageRate;
if (hoursWorked > 40)
{
grossPay += (.5 * wageRate) * (hoursWorked - 40);
}
return grossPay;
}
//calculates the gross pay for a salesperson, using their hours worked times their
//wage rate and then commission is calculated for the employee, based on their
//sales amount
public double CalculateGrossPay(double hoursWorked, double wageRate, double salesAmount)
{
grossPay = hoursWorked * wageRate;
if (salesAmount <= 10000)
{
grossPay += .02 * salesAmount;
}
else if (salesAmount > 10000)
{
grossPay += .04 * salesAmount;
}
return grossPay;
}
//calculates the taxes the employee has to pay
public static double CalculateTaxes()
{
int payCutoff = 100 + (100 * dependants);
if (grossPay <= payCutoff)
{
double taxesPaid = .1 * grossPay;
return taxesPaid;
}
else
{
double taxesPaid = (.1 * payCutoff) + (.2 * (grossPay - payCutoff));
return taxesPaid;
}
}
}
And in my Form class:
{
public FormPayEstimator()
{
InitializeComponent();
}
//closes the application
private void exitToolStripMenuItem_Click(object sender, EventArgs e)
{
Application.Exit();
}
//closes the application
private void buttonExit_Click(object sender, EventArgs e)
{
Application.Exit();
}
//clears all text boxes, unchecks the salesperson check box, makes the sales amount
//text box and label invisible and sets the focus back to the hours worked text box
private void buttonClearForm_Click(object sender, EventArgs e)
{
textBoxDependants.Text = "";
textBoxGrossPay.Text = "";
textBoxHourlyWageRate.Text = "";
textBoxHoursWorked.Text = "";
textBoxNetPay.Text = "";
textBoxTaxes.Text = "";
checkBoxSalesperson.Checked = false;
textBoxSalesAmount.Visible = false;
labelSalesAmount.Visible = false;
textBoxHoursWorked.Focus();
}
//displays information about the program
private void aboutToolStripMenuItem_Click(object sender, EventArgs e)
{
MessageBox.Show("Pay Estimator - Version 1.0", "Pay Estimator", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
//if the user checks the salesperson check box the sales amount text box and label
//become visible to the user and it sets the focus to the sales amount text box
private void checkBoxSalesperson_CheckedChanged(object sender, EventArgs e)
{
textBoxSalesAmount.Visible = true;
labelSalesAmount.Visible = true;
textBoxSalesAmount.Focus();
}
//displays the font dialog box and allows user to change their font
private void fontToolStripMenuItem_Click(object sender, EventArgs e)
{
fontDialog1.Font = textBoxHoursWorked.Font;
fontDialog1.Font = textBoxHourlyWageRate.Font;
fontDialog1.Font = textBoxDependants.Font;
fontDialog1.Font = textBoxGrossPay.Font;
fontDialog1.Font = textBoxTaxes.Font;
fontDialog1.Font = textBoxNetPay.Font;
fontDialog1.Font = textBoxSalesAmount.Font;
if (fontDialog1.ShowDialog() != DialogResult.Cancel)
{
textBoxHoursWorked.Font = fontDialog1.Font;
textBoxHourlyWageRate.Font = fontDialog1.Font;
textBoxDependants.Font = fontDialog1.Font;
textBoxGrossPay.Font = fontDialog1.Font;
textBoxTaxes.Font = fontDialog1.Font;
textBoxNetPay.Font = fontDialog1.Font;
textBoxSalesAmount.Font = fontDialog1.Font;
}
}
//displays the color dialog box and allows user to change thei font color
private void colorToolStripMenuItem_Click(object sender, EventArgs e)
{
colorDialog1.Color = textBoxHoursWorked.ForeColor;
colorDialog1.Color = textBoxHourlyWageRate.ForeColor;
colorDialog1.Color = textBoxDependants.ForeColor;
colorDialog1.Color = textBoxGrossPay.ForeColor;
colorDialog1.Color = textBoxTaxes.ForeColor;
colorDialog1.Color = textBoxNetPay.ForeColor;
colorDialog1.Color = textBoxSalesAmount.ForeColor;
if (colorDialog1.ShowDialog() != DialogResult.Cancel)
{
textBoxHoursWorked.ForeColor = fontDialog1.Color;
textBoxHourlyWageRate.ForeColor = fontDialog1.Color;
textBoxDependants.ForeColor = fontDialog1.Color;
textBoxGrossPay.ForeColor = fontDialog1.Color;
textBoxTaxes.ForeColor = fontDialog1.Color;
textBoxNetPay.ForeColor = fontDialog1.Color;
textBoxSalesAmount.ForeColor = fontDialog1.Color;
}
}
//calculates the users total gross pay, their taxes owed and their net pay
private void buttonCompute_Click(object sender, EventArgs e)
{
//declares variables
string inValue;
double hours, rate, dependants, salesAmount, grossPay, taxes, netPay;
//assigns variables to values user entered in the hours worked, hourly wage
//rate and dependants text boxes
inValue = textBoxHoursWorked.Text;
hours = double.Parse(inValue);
inValue = textBoxHourlyWageRate.Text;
rate = double.Parse(inValue);
inValue = textBoxDependants.Text;
dependants = int.Parse(inValue);
//creates an instance of the Pay class and runs the CalculateGrossPay method
//for the production workers
Pay p1 = new Pay();
grossPay = p1.CalculateGrossPay(hours, rate);
//checks to see if the sales amount checkbox is checked and if it is, a value
//is assigned to the salesAmount text box, an instance of the pay class is
// createdand the CalculateGrossPay method for a salesperson is run
if (checkBoxSalesperson.Checked == true)
{
inValue = textBoxSalesAmount.Text;
salesAmount = double.Parse(inValue);
Pay p2 = new Pay();
grossPay = p2.CalculateGrossPay(hours, rate, salesAmount);
}
//displays the answer in the Gross Pay text box
textBoxGrossPay.Text = String.Format("{0:c}", grossPay).ToString();
//runs the CalculateTaxes method from the Pay class and displays the result in
//the taxes text box
taxes = Pay.CalculateTaxes();
textBoxTaxes.Text = String.Format("{0:c}", taxes).ToString();
//calculates the net pay for an employee and displays the result in the net pay
//text box
netPay = grossPay - taxes;
textBoxNetPay.Text = String.Format("{0:c}", netPay).ToString();
}
}
}
When I compute the values I got $70 for taxes when it is only meant to be $40. Can anyone tell me why this is happening?
I think one of your problems is that you never set the static property Pay.NumOfDependents. It's hard to tell. Your code is very confusing, what with mixing static properties and such. You would be better off changing those static properties and the static CalculateTaxes method so that they're instance properties and methods. Then, in your code where you calculate the pay based on the employee type, you can write:
Pay p1 = new Pay();
// Here, set the number of dependents.
p1.NumOfDependents = dependents;
if (checkBoxSalesperson.Checked == true)
{
inValue = textBoxSalesAmount.Text;
salesAmount = double.Parse(inValue);
grossPay = p2.CalculateGrossPay(hours, rate, salesAmount);
}
else
{
grossPay = p1.CalculateGrossPay(hours, rate);
}
Now when you want to calculate the taxes, you can write:
taxes = p1.CalculateTaxes();
A cleaner design would have you put all of the pertinent properties (hours worked, sales amount, etc.) into the Pay class and then make a single call to calculate gross pay, taxes, etc. That method would set properties on the object like taxes, grossPay, etc. Then you could write:
// code to set properties here ...
// now calculate everything
p1.Calculate();
// and then access the computed properties
textboxGrossPay.Text = string.Format("{0:c}", p1.grossPay);
textboxTaxes.Text = string.Format("{0:c}", p1.taxes);
The idea here it to give the Pay object instance all the information it needs (hours worked, rate, sales amount, number of dependents) and then let it decide how to calculate the pay. That way your user interface code just needs to be concerned with getting data from the user and presenting the results of the calculations.
I'm not going to debug the code for you, but I do have an observation that may help...
CalculateTaxes() seems to rely on the value of private fields such as grossPay. If you don't call your methods in the correct order, those private fields will not be initialized, or may have the wrong value from a previous run. It's generally bad practice to rely on side effects like this for correct calculation. Suggest rewriting your methods not to rely on the previous state of private fields.
paycutoff should be a double, or cast to a double when you do non-integer calculations.
This is an ideal situation for unit tests.
You can write simple function calls to your calculation methods with some numbers, calculate the value you expect, and see if they match.
It would also help for the maintenance part as well as development.
You declared grosspay as static. Value of grosspay reflects in each object. you created p2 and called p2.CalculateGrossPay(hours, rate, salesAmount); It will overwrite the value of grosspay for object p1. When u call calculatetax(), it is estimated on the basis of latest grosspay value.

Categories