Results 1 to 7 of 7
  1. #1
    dimesnnix is offline Member
    Join Date
    Jun 2011
    Posts
    6
    Rep Power
    0

    Default 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?

  2. #2
    Junky's Avatar
    Junky is online now Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,782
    Rep Power
    7

    Default

    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?

  3. #3
    sibernewf is offline Member
    Join Date
    Mar 2011
    Posts
    48
    Rep Power
    0

    Default

    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
    Last edited by sibernewf; 06-02-2011 at 06:20 AM.

  4. #4
    dimesnnix is offline Member
    Join Date
    Jun 2011
    Posts
    6
    Rep Power
    0

    Default

    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"
    Last edited by dimesnnix; 06-02-2011 at 09:43 PM.

  5. #5
    dimesnnix is offline Member
    Join Date
    Jun 2011
    Posts
    6
    Rep Power
    0

    Default

    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);
    }
    }

  6. #6
    sibernewf is offline Member
    Join Date
    Mar 2011
    Posts
    48
    Rep Power
    0

    Default

    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
    Java 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:

    Java 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
    Last edited by sibernewf; 06-03-2011 at 01:57 AM.

  7. #7
    Junky's Avatar
    Junky is online now Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,782
    Rep Power
    7

    Default

    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.

Similar Threads

  1. Specific amount of character in each line?
    By hacikho in forum New To Java
    Replies: 6
    Last Post: 12-07-2010, 04:18 AM
  2. Formulas in methods or constructors?
    By kyameron in forum New To Java
    Replies: 11
    Last Post: 11-20-2010, 12:22 PM
  3. Vectors - accessing an unknown amount of objects
    By counterfox in forum New To Java
    Replies: 1
    Last Post: 05-07-2010, 10:45 PM
  4. Constructor with unknown amount of objects?
    By Bernard Robitaille in forum New To Java
    Replies: 5
    Last Post: 03-01-2009, 05:00 AM
  5. iteration on huge amount of files in a folder
    By tshaked in forum Advanced Java
    Replies: 1
    Last Post: 08-07-2007, 07:08 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •