-
February 2nd, 2013, 09:15 PM
#1
Find Minimum and Maximum values
I have a form that has people enter an amount, and it calculates a discount for the invoice and displays it, as well as total of all invoices, number of invoices, and average invoice amount. I need it to also display the smallest and largest invoices. I am still quite new to C#, so any hints would be greatly appreciated. Here's the code I have so far, not sure how to start on the last part, looking into using Math.Max and Math.Min.
Code:
public partial class frmInvoiceTotal : Form
{
public frmInvoiceTotal()
{
InitializeComponent();
}
int numberOfInvoices = 0;
decimal totalOfInvoices = 0m;
decimal invoiceAverage = 0m;
private void btnCalculate_Click(object sender, EventArgs e)
{
decimal subtotal = Convert.ToDecimal(txtEnterSubtotal.Text);
decimal discountPercent = .25m;
decimal discountAmount = Math.Round(subtotal * discountPercent, 2);
decimal invoiceTotal = subtotal - discountAmount;
txtEnterSubtotal.Text = subtotal.ToString("c");
txtDiscountPercent.Text = discountPercent.ToString("p1");
txtDiscountAmount.Text = discountAmount.ToString("c");
txtTotal.Text = invoiceTotal.ToString("c");
numberOfInvoices++;
totalOfInvoices += invoiceTotal;
invoiceAverage = totalOfInvoices / numberOfInvoices;
txtNumberOfInvoices.Text = numberOfInvoices.ToString();
txtTotalOfInvoices.Text = totalOfInvoices.ToString("c");
txtInvoiceAverage.Text = invoiceAverage.ToString("c");
txtEnterSubtotal.Text = "";
txtEnterSubtotal.Focus();
}
private void btnClearTotals_Click(object sender, System.EventArgs e)
{
numberOfInvoices = 0;
totalOfInvoices = 0m;
invoiceAverage = 0m;
txtNumberOfInvoices.Text = "";
txtTotalOfInvoices.Text = "";
txtInvoiceAverage.Text = "";
txtEnterSubtotal.Focus();
}
private void btnExit_Click(object sender, EventArgs e)
{
this.Close();
}
private void label10_Click(object sender, EventArgs e)
{
}
}
}
Last edited by BioPhysEngr; February 3rd, 2013 at 01:20 AM.
Reason: add code tags
-
February 3rd, 2013, 01:27 AM
#2
Re: Find Minimum and Maximum values
With these lines:
Code:
numberOfInvoices++;
totalOfInvoices += invoiceTotal;
invoiceAverage = totalOfInvoices / numberOfInvoices;
You are keeping track of invoice-to-invoice parameters. To keep track of min and max, you can either do:
Code:
minInvoice = Math.Min(minInvoice, invoiceTotal);
maxInvoice = Math.Max(maxInvoice, invoiceTotal);
Or:
Code:
if( invoiceTotal < minInvoice ) {
minInvoice = invoiceTotal;
}
if( invoiceTotal > maxInvoice ) {
maxInvoice = invoiceTotal;
}
I tend to prefer the second style, but either is fine.
Though: if you wrote the rest of that code, you should be entirely competent to see solution for yourself. We're happy to help, of course, but I think you will find these sorts of solutions will become apparent if you just work through them. MSDN is your friend. Here are, for example, the documents for Math.Min an Math.Max.
I've also surrounded your code with [code] and [/code] tags; this will preserve formatting, making it easier (and more likely) for us to read your code.
Hope that helps!
Best Regards,
BioPhysEngr
http://blog.biophysengr.net
--
All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.
-
February 3rd, 2013, 03:25 PM
#3
Re: Find Minimum and Maximum values
Thanks for your help. Sorry for not formatting the code as preferred, as I said I'm still quite new to programming. I appreciate your time.
-
February 3rd, 2013, 04:24 PM
#4
Re: Find Minimum and Maximum values
You can loop:
Code:
decimal[] invoices = { 1234.24m, 3456.64m, 500.75m, 468.90m, 797.95m, 3035.40m };
//Method1
decimal minValue = invoices[0];
for (int i = 1; i < invoices.Length; i++)
{
if (invoices[i] < minValue)
{
minValue = invoices[i];
}
}
Console.WriteLine(minValue); // 468.90
Perhaps you would want to check the number of invoices before indexing the first invoice, depending on how the rest of your program works.
You can use LINQ:
Code:
decimal[] invoices = { 1234.24m, 3456.64m, 500.75m, 468.90m, 797.95m, 3035.40m };
decimal minValue = invoices.Min();
Console.WriteLine(minValue); // 468.90
Same principle with finding the highest values.
-
February 3rd, 2013, 04:48 PM
#5
Re: Find Minimum and Maximum values
So, with your help I was able to get much further. I declared the variables for minInvoice and maxInvoice. I added the lines of code using the math.min and math.max syntax. I then attached the 2 variables to my text boxes for minimum and maximum invoice. The code runs, but it just displays the subtotal in the min and max invoice fields. I will continue working on it. I appreciate your help thus far.
Code:
private void btnCalculate_Click(object sender, EventArgs e)
{
decimal subtotal = Convert.ToDecimal(txtEnterSubtotal.Text);
decimal discountPercent = .25m;
decimal discountAmount = Math.Round(subtotal * discountPercent, 2);
decimal invoiceTotal = subtotal - discountAmount;
decimal minInvoice = decimal.MaxValue;
decimal maxInvoice = 0m;
txtEnterSubtotal.Text = subtotal.ToString("c");
txtDiscountPercent.Text = discountPercent.ToString("p1");
txtDiscountAmount.Text = discountAmount.ToString("c");
txtTotal.Text = invoiceTotal.ToString("c");
numberOfInvoices++;
totalOfInvoices += invoiceTotal;
invoiceAverage = totalOfInvoices / numberOfInvoices;
minInvoice = Math.Min(minInvoice, invoiceTotal);
maxInvoice = Math.Max(maxInvoice, invoiceTotal);
txtNumberOfInvoices.Text = numberOfInvoices.ToString();
txtTotalOfInvoices.Text = totalOfInvoices.ToString("c");
txtInvoiceAverage.Text = invoiceAverage.ToString("c");
txtMinInvoice.Text = minInvoice.ToString("c");
txtMaxInvoice.Text = maxInvoice.ToString("c");
txtEnterSubtotal.Text = "";
txtEnterSubtotal.Focus();
}
-
February 3rd, 2013, 05:04 PM
#6
Re: Find Minimum and Maximum values
What do you need further help on?
Code:
decimal subtotal = Convert.ToDecimal(txtEnterSubtotal.Text);
Here you should be using TryParse() though, this is a textBox control i'm presuming, therefore invalid input can result in a runtime exception if the input is not validated first.
-
February 3rd, 2013, 05:07 PM
#7
Re: Find Minimum and Maximum values
Realize, that this is all the ToDecimal() function does, for the string param overload:
Code:
public static decimal ToDecimal(string value)
{
if (value == null)
{
return 0M;
}
return decimal.Parse(value, CultureInfo.CurrentCulture);
}
A textBox's Text property, will NEVER be null, so really, all it's doing is calling decimal.Parse() for you, and without validation on the input. The most "nothingness" value a textBox can have is string.Empty.
-
February 3rd, 2013, 05:07 PM
#8
Re: Find Minimum and Maximum values
The min and max invoice boxes are just displaying whatever the current invoice total is. Still working on what the problem could be.
-
February 3rd, 2013, 05:19 PM
#9
Re: Find Minimum and Maximum values
Originally Posted by Inigo Montoya
The min and max invoice boxes are just displaying whatever the current invoice total is. Still working on what the problem could be.
You're comparing the total against values that aren't relevant from a first glance.
Look at these lines:
Code:
decimal invoiceTotal = subtotal - discountAmount;
decimal minInvoice = decimal.MaxValue;
decimal maxInvoice = 0m;
minInvoice = Math.Min(minInvoice, invoiceTotal);
maxInvoice = Math.Max(maxInvoice, invoiceTotal);
minInvoice will ALWAYS be the invoiceTotal because minInvoice is as high as things get. There can be no larger decimal value than decimal.MaxValue, and you're comparing for the smallest of those 2 numbers. It doesn't make sense. It's the complete reverse with the Math.Max line, unless your invoiceTotal is a negative value, you can't get a smaller value than 0...
Last edited by AceInfinity; February 3rd, 2013 at 05:25 PM.
-
February 3rd, 2013, 06:53 PM
#10
Re: Find Minimum and Maximum values
I see what you mean Ace. It was suggested I use MaxValue so the correct value is returned the first time. If I had declared it as 0, then the first time an invoice is entered it would always display 0 as the min invoice.
-
February 3rd, 2013, 06:55 PM
#11
Re: Find Minimum and Maximum values
Originally Posted by Inigo Montoya
I see what you mean Ace. It was suggested I use MaxValue so the correct value is returned the first time. If I had declared it as 0, then the first time an invoice is entered it would always display 0 as the min invoice.
That's why if you look at my looping method, I automatically set it to the first invoice, and then I start looping from there, without looking at the one I already assigned as the minimum value to find any values smaller than the first invoice that I assigned to that variable. Doing it that way, avoids you from having to assign the variable to a useless value that doesn't really do anything for you.
-
February 3rd, 2013, 06:57 PM
#12
Re: Find Minimum and Maximum values
**It double posted for some reason, so i'll provide an analogy here:
It would be like picking a number from a bag that has a whole bunch of paper slips, and then taking each other piece of paper out of that bag individually comparing to the first one you took out, and keeping the smaller value from each comparison as you went along until no more slips of paper were left.
Logically, this makes much more sense, than for instance, what you're doing, which is almost like having an initial piece of paper, writing a ridiculously huge number on it. And using that as a stepping stone to compare the same way as described earlier in my post here. Being able to skip steps is always good in programming, so long as you get a good reliable result.
-
February 3rd, 2013, 07:02 PM
#13
Re: Find Minimum and Maximum values
I haven't gotten as far as loops yet. Here's what I am supposed to do:
Use the Math.Min method to compare each invoice total to a variable that contains the smallest invoice total to that point. Then, replace the variable with the smaller of the two invoice totals. To make this work for the first invoice, you can initialize this variable to a large number like the MaxValue member of the Decimal class.
-
February 3rd, 2013, 07:14 PM
#14
Re: Find Minimum and Maximum values
This is a homework thing? Sort out your knowns for what they want you to do then:
- Use Math.Min() to compare each invoice total to a variable that contains the smallest invoice total
I wouldn't suggest setting it to MaxValue if there's no reason. Unless it's possible to get an invoice of $0, you can set it to the first invoice total, then you have an immediate starting point to compare against future invoices.
You know what you're supposed to do, but I can't help and I won't write code for you especially if this is homework, so what are you stuck on then?
[sigpic][/sigpic]
Microsoft MVP .NET Programming (2012 - Present)
®Crestron DMC-T Certified Automation Programmer
-
February 3rd, 2013, 08:06 PM
#15
Re: Find Minimum and Maximum values
I don't expect anyone to write code for me, just looking for outside opinions as well. My classmates in this online course aren't posting any suggestions either, and neither is my instructor. I thank you for your help anyways.
It seems to be doing the first invoice right, it should display invoiceTotal in both the smallest and largest invoice fields the first time. Just can't work out how to get it to compare the next input to the previous one. We cover loops in the next chapter, so I'm guessing I shouldn't be using those yet.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|