Help with average amount formulas

• 06-02-2011, 01:46 AM
dimesnnix
Help with average amount formulas
Just finished the code for an Invoice application. I think my formulas for calculating the average invoice amount and the average discount amount are wrong, my numbers are not matching up. Can anyone help with the formulas?

import java.util.Scanner;

public class ModifiedInvoiceApp
{
public static void main(String[] args)
{
// welcome the user to the program
System.out.println("Welcome to the Invoice Total Calculator");
System.out.println(); // print a blank line

// create a Scanner object named sc
Scanner sc = new Scanner(System.in);

// perform invoice calculations until choice is equal to "n" or "N"
String choice = "y";
int InvoiceCount = 0;
while (!choice.equalsIgnoreCase("n"))
{
// get the invoice subtotal from the user
System.out.print("Enter subtotal: ");
double subtotal = sc.nextDouble();

// calculate the discount amount and total
double discountPercent= 0.0;
if (subtotal >= 500)
discountPercent = .25;
else if (subtotal >= 100)
discountPercent = .1;
else
discountPercent = 0.0;
double discountAmount = subtotal * discountPercent;
double total = subtotal - discountAmount;

// display the discount amount and total
String message1 = "Discount percent: " + discountPercent + "\n"
+ "Discount amount: " + discountAmount + "\n"
+ "Invoice total: " + total + "\n";
System.out.println(message1);

// see if the user wants to continue
System.out.print("Continue? (y/n): ");
choice = sc.next();

// display the number of invoices, average invoice amount, and average discount amount
InvoiceCount = InvoiceCount + 1;
double averageInvoiceAmount = total / InvoiceCount;
double averageDiscountAmount = discountAmount / InvoiceCount;
String message2 = "\n" +
"Number Of Invoices: " + InvoiceCount + "\n"
+ "Average Invoice Amount: " + averageInvoiceAmount + "\n"
+ "Average Discount Amount: " + averageDiscountAmount + "\n";
System.out.println(message2);
}
}
}

Also, the message displaying the number of invoices, average invoice amount, and average discount is only supposed to show when the user ends the program. What am I doing wrong?
• 06-02-2011, 01:49 AM
Junky
If you have a bunch of tasks that only need to be done once why do you have them inside a loop where they get done everytime around the loop?
• 06-02-2011, 06:16 AM
sibernewf
HI dimesnnix,

I havent got a solution for you, but I can help with the last part. You are asking why message2 is displayed when you only want to see it when the user exits the program.

Have another look at your while loop. This is where all your logic is. Now when the user is finished and say "n" at the prompt, the program should EXIT THE LOOP and then display the message.

Have another look and see where you have put message2 and see if you can figure out what I'm saying.

Also just a suggestion, but all of your variable declarations eg: double discountPercent= 0.0;
would probably be better off declared outside of your loop if you want the rest of the program to use them.

Good luck
• 06-02-2011, 09:40 PM
dimesnnix
Ok, I see what you're saying. The problem is when I put message2 outside of the loop, I get two errors;

Basically it says "cannot find symbol" and points to the variables "total" and "discountAmount"
• 06-02-2011, 09:52 PM
dimesnnix
Ok. I put the message2 outside of the loop and now the that part is working in the sense that it's not showing message2 after each entry. HOWEVER, now it's not showing message2 at all. When the user enters N (which should display message2 and close the application), nothing happens. Help? Here's the new code.

import java.util.Scanner;

public class ModifiedInvoiceApp
{
public static void main(String[] args)
{
// welcome the user to the program
System.out.println("Welcome to the Invoice Total Calculator");
System.out.println(); // print a blank line

// create a Scanner object named sc
Scanner sc = new Scanner(System.in);

// perform invoice calculations until choice is equal to "n" or "N"
String choice = "y";
int InvoiceCount = 0;
while (!choice.equalsIgnoreCase("n"))
{
// get the invoice subtotal from the user
System.out.print("Enter subtotal: ");
double subtotal = sc.nextDouble();

// calculate the discount amount and total
double discountPercent= 0.0;
if (subtotal >= 500)
discountPercent = .25;
else if (subtotal >= 100)
discountPercent = .1;
else
discountPercent = 0.0;
double discountAmount = subtotal * discountPercent;
double total = subtotal - discountAmount;

// display the discount amount and total
String message1 = "Discount percent: " + discountPercent + "\n"
+ "Discount amount: " + discountAmount + "\n"
+ "Invoice total: " + total + "\n";
System.out.println(message1);

// see if the user wants to continue
System.out.print("Continue? (y/n): ");
choice = sc.next();
}
double discountPercent= 0.0;
double subtotal = sc.nextDouble();
if (subtotal >= 500)
discountPercent = .25;
else if (subtotal >= 100)
discountPercent = .1;
else
discountPercent = 0.0;
double discountAmount = subtotal * discountPercent;
double total = subtotal - discountAmount;

// display the number of invoices, average invoice amount, and average discount amount
InvoiceCount = InvoiceCount + 1;
double averageInvoiceAmount = total / InvoiceCount;
double averageDiscountAmount = discountAmount / InvoiceCount;
String message2 = "\n" +
"Number Of Invoices: " + InvoiceCount + "\n"
+ "Average Invoice Amount: " + averageInvoiceAmount + "\n"
+ "Average Discount Amount: " + averageDiscountAmount + "\n";
System.out.println(message2);
}
}
• 06-03-2011, 01:41 AM
sibernewf
Ok, you may want to look up variable scope.
I'm pretty sure that is what the problem is.

In a nutshell, variables defined within a loop/if are only visible inside of that loop.

Try moving all your variable definitions eg double averageInvoiceAmouint = blah outside of your while loop. This will make the variable visible to the whole main method. Just remember that IF these variables are ONLY going to be used INSIDE the loop, there's nothing wrong with defining them inside the loop.

a quick example
Code:

```public class foo {     public static void main(String[] args) {     private int x = 10;     if(x == 10) {         // this is defined inside the if so is only visible inside the if           int y = 20;           // do other things...     }     // this will either cause an error  or display 0  because its not visible outside the if block.     System.out.println(y);   }    }```
Instead try something like this:

Code:

```public class foo {     public static void main(String[] args) {     // these are defined outside any loop     int x = 10;     int y = 0;     if(x == 10) {           y = 20;           // do other things...     }     // now it works because y is defined outside of the if block     System.out.println(y);   }    }```

Hope this moves you a little further forward.

Good luck
• 06-03-2011, 01:45 AM
Junky
Why do you have the code asking for user input duplicated inside and outside the loop? The reason you do not see message2 displayed is because of this duplication. The program is sitting there waiting for the user to input more data.