Hi,
I'm staruggling with this program can I get some help please?
Write a program called CalculateInvoice that allows a user to enter multiple invoice items for multiple customers and calculates a total invoice amount. For each customer, enter a customer number and customer name. Then, for each customer allow the user to enter any number of items. Data to be entered for each item is the description, quantity, and price. For each item calculate the extended amount by multiplying the quantity times the price. Add each extended amount to a subtotal for the customer. If the subtotal is greater than or equal to $500, the customer gets a 5% discount. Calculate a sales tax of 8.25% on the subtotal less the discount. Display the customer number and name on the first line, followed by the subtotal, the amount of discount, amount of sales tax, and total invoice amount. You are not to list the individual items.
Hint: You will need two loops for this assignment, an outer loop for multiple customers and an inner loop for multiple items. Because you have multiple customers, you need to clear (set to zero) all fields used to store customer level totals) in order to prevent values from carrying over to the next customer.

For your test data, you should have at least two customers. Each customer should have at least two items. One of the customers should have an invoice subtotal equal to or greater than $500 to test your discount.
public class CalculateInvoice {

/**
 * @param args the command line arguments
 */
@SuppressWarnings("empty-statement")
public static void main(String[] args) {
    //   Declare variables
    String customerName, itemdescription;
    int customerID;
    double invoiceTotal = 0,subtotal = 0,discount,Quantity,Price,itemCheck,tax,customerCheck = 0,extandedAmount,total;
    boolean loopcustomers,loopItems = true;
 itemCheck=0;

    Scanner input1 = new Scanner(System.in);////Using a Scanner so we can easily pull in different data types. 
    Scanner input2 = new Scanner(System.in);
    Scanner input3 = new Scanner(System.in);

 //  outer loop for multiple customers

     System.out.print("Enter your customer ID: ");
     customerID=input1.nextInt();
     System.out.print("Enter your customer Name: ");
      customerName=input2.nextLine();

            //Inner loop for multiple items
             do 
             {  

                 System.out.print("Enter description of item: ");
                  itemdescription=input1.nextLine();
                System.out.print("Enter quantity of item: ");
                  Quantity=input2.nextDouble();
                 System.out.print ("Enter price of item: $ ");
                  Price=input3.nextDouble();

                        extandedAmount=Price*Quantity;
                        subtotal=subtotal+extandedAmount;
                         int ifsubtotal;

               while (loopItems)
                   if(subtotal>500);{

                       discount=(subtotal*0.05);}

                   {
                   discount=0;}

               total = subtotal-discount;            
               tax=(total*0.0825);
               invoiceTotal=total+tax;

                        itemCheck+=1;
                        Price=0;
                        Quantity=0;
                        extandedAmount=0;

             System.out.print("Enter more customers: ");

                loopcustomers=input3.hasNextBoolean();  

                while (loopcustomers=='Y');

                   customerID=0;
                   customerName=null;
    customerCheck+=1;
    return
     System.out.println("Customer ID: "+ customerID + "  Customer Name: " + customerName);
            System.out.println("Subtotal: " + subtotal +"Discount: " +discount +"Tax: "+ tax +"Invoice Total: " +invoiceTotal);   

Recommended Answers

All 10 Replies

First read https://www.daniweb.com/programming/threads/435023/read-this-before-posting-a-question

I think you wrote one original line in your text (the first line) then copied your assignment here.

Essentially you are asking folk to read your code and make it work. Instead, tell where you think it's gone wrong and if there is an error, what line number.

Finally, I don't see any debugging code. That is, if you get the wrong number, did you print out the values to see if you had a GIGO problem?

Thank you for your response,
Yes I just wrote one line and the rest it is the homework.
this is the massage I got when I run my program:

Quoted Text Here

Exception in thread "main" java.lang.RuntimeException: Uncompilable source code - Erroneous tree type: <any>
at calculateinvoice.CalculateInvoice.main(CalculateInvoice.java:34)
  C:\Users\Owner\AppData\Local\NetBeans\Cache\8.1\executor-snippets\run.xml:53: Java returned: 1
BUILD FAILED (total time: 2 seconds)

Quoted Text Here
This is my first class in Java A Beginner's Guide ,and I still confused with code and methods.

You could use some class methods to aid console string input as per here;

https://www.daniweb.com/programming/software-development/threads/506093/java-output

Also a class for Customer with methods
and a class for Item with methods

maybe each could have takeInInfo methods also ?

Then all you need is a loop with in a loop as per your instructions to take in each customer ... and for each customer ... to take in all the items requested ... then to process data for for each customer ... as you go ... and output nicely formatted output

I hope your realize that line 34 in your top post is a blank line. I may be asking a lot here in that your error message and code match up for the line numbers.

Since you seem a little confuzzed ...

the following may help to jump-start your Java coding ...

Firstly ... recall in Java we like to use classes ... but here I am using classes more like we use a C++ struct ... this is to make the coding simpler ...
(i.e. NO need here to code for getters )

So I borrowed and EDITED a bit the Console class from the other program I referred you to earlier ...

So in the first file called: "Console.java", you could use these input methods ...

import java.util.Scanner;

public class Console {

    private static Scanner sc = new Scanner(System.in);

    public static String getString(String prompt) {
        System.out.print(prompt);
        String s = sc.nextLine();
        return s;
    }

    public static int getInt(String prompt) {
        int i = 0;
        while (true) {
            System.out.print(prompt);
            try {
                i = Integer.parseInt(sc.nextLine());
                break;
            } catch (NumberFormatException e) {
                System.out.println("Error! Invalid integer. Try again.");
            }
        }
        return i;
    }

    public static double getDouble(String prompt) {
        double d = 0;
        while (true) {
            System.out.print(prompt);
            try {
                d = Double.parseDouble(sc.nextLine());
                break;
            } catch (NumberFormatException e) {
                System.out.println("Error! Invalid decimal. Try again.");
            }
        }
        return d;
    }
}

Then you could use these next two classes (each file name is the same as the class name with a .java at the end of the file name as per this: 'FileName.java':

 class Customer
{
    static Console con = new Console();

    int number = 0;
    String name = "";

    void takeInInfo()
    {
        number = con.getInt(  "Enter customer number : " );
        name = con.getString( "Enter customer name   : " ).toUpperCase();
    }
}

And:

class Item
{
    static Console con = new Console();

    String type = "";
    int quantity = 0;
    double price = 0.0;

    double extended() { return quantity * price; }

    void takeInInfo()
    {
        type = con.getString(  "Enter type     : " );
        quantity = con.getInt( "Enter quantity : " );
        price = con.getDouble( "Enter price    : " );
    }
}

Note that these each use the class Console at the top.

Finally place this MAIN file called Invoice.java in the same folder as the above 3 files
and compile all (from a command line) by calling:

javac Invoice.java

Then ... run by calling, (from a command line) :

java Invoice

NOTE the CaSe MATTERs VERY mUcH in Java file names ... also !

Ok ... here is the 4th file and note how much easier it is to see and follow (and maintain) the code flow ... now :

public class Invoice
{  
    static final double  DISCOUNT_BEGINS = 500.00; 
    static final double  DISCOUNT_RATE   = 0.05;
    static final double  NET_TAX_RATE    = 0.0825;

    static String HEADER = "A NEW CUSTOMER INVOICE BEGINS ...";

    // Note: these next two obj's get new values on each pass in loops below //
    static Customer cust = new Customer();
    static Item item     = new Item();

    public static void main(String[] args)
    {
        //Outer loop for Customer input ...
        do 
        {
            // Note: in the below format string, the %n means print a newline //
            System.out.format( "%n%s%n%n", HEADER ); 

            double subTot = 0.0;
            cust.takeInInfo();

            //Inner loop to handle multiple item input ...
            do
            {
                item.takeInInfo();
                subTot += item.extended();
            }
            while( !Console.getString("More items (y/n ? ").equalsIgnoreCase("N") );

            System.out.println();
            System.out.format( "ID: %10d  Name: %s%n", cust.number, cust.name );
            System.out.format( "Sub toatl is  : %10.2f%n", subTot );

            double discount = 0.0;
            if( subTot >= DISCOUNT_BEGINS )
                discount = subTot * DISCOUNT_RATE;
            System.out.format( "Discount is   : %10.2f%n", discount );

            subTot -= discount; // updated //
            System.out.format( "New total is  : %10.2f%n", subTot );

            double salesTax = subTot * NET_TAX_RATE;
            System.out.format( "Sales tax is  : %10.2f%n", salesTax );

            double total = subTot + salesTax;
            System.out.format( "amount due is : %10.2f%n", total );

        }
        while( !Console.getString("More customers (y/n ? ").equalsIgnoreCase("N") );

    }
}    

/*
    Program spec's for: class Invoice (that Calculates a customer Invoice) 

    that allows a user to enter multiple invoice items 
    for multiple customers 

    and calculates a total invoice amount. 

    For each customer, 

    enter a customer number and customer name. 

    Then, for each customer allow the user to enter any number of items. 

    Data to be entered for each item is 
    the description, quantity, and price. 

    For each item calculate the extended amount by 
    multiplying the quantity times the price. 

    Add each extended amount to a subtotal for the customer. 

    If the subtotal is greater than or equal to $500, the customer gets a 5% discount. 

    Calculate a sales tax of 8.25% on the subtotal less the discount. 

    Display the customer number and name on the first line, 
    followed by the subtotal, 
    the amount of discount, 
    amount of sales tax, and 
    total invoice amount. 

    You are not to list the individual items.

    Hint: You will need two loops for this assignment, 
    an outer loop for multiple customers 
    and an inner loop for multiple items. 

    Because you have multiple customers, 
    you need to clear (set to zero) all fields used to store customer level totals) 
    in order to prevent values from carrying over to the next customer.

    For your test data, you should have at least two customers. 

    Each customer should have at least two items. 

    One of the customers should have an invoice subtotal 
    equal to or greater than $500 to test your discount.
*/

And ... If you prefer to use private data members in your Java classes, you could sub in the below three classes (files) where appropriate ...

public class Customer
{
    private static Console con = new Console();

    private int number = 0;
    private String name = "";

    public int getNumber()  { return number; }
    public String getName() { return name; }

    public void takeInInfo()
    {
        number = con.getInt(  "Enter customer number : " );
        name = con.getString( "Enter customer name   : " ).toUpperCase();
    }
}

And ...

public class Item
{
    private static Console con = new Console();

    private String type = "";
    private int quantity = 0;
    private double price = 0.0;

    public double extended() { return quantity * price; }

    public void takeInInfo()
    {
        type = con.getString(  "Enter type     : " );
        quantity = con.getInt( "Enter quantity : " );
        price = con.getDouble( "Enter price    : " );
    }
}

And ...

 public class Invoice
{  
    private static final double  DISCOUNT_BEGINS = 500.00; 
    private static final double  DISCOUNT_RATE   = 0.05;
    private static final double  NET_TAX_RATE    = 0.0825;

    private static String HEADER = "A NEW CUSTOMER INVOICE BEGINS ...";

    // Note: these next two obj's get new values on each pass in loops below //
    private static Customer cust = new Customer();
    private static Item item     = new Item();

    public static void main(String[] args)
    {
        //Outer loop for Customer input ...
        do 
        {
            // Note: in the below format string, the %n means print a newline //
            System.out.format( "%n%s%n%n", HEADER ); 

            double subTot = 0.0;
            cust.takeInInfo();

            //Inner loop to handle multiple item input ...
            do
            {
                item.takeInInfo();
                subTot += item.extended();
            }
            while( !Console.getString("More items (y/n ? ").equalsIgnoreCase("N") );

            System.out.println();
            System.out.format( "ID: %10d  Name: %s%n", cust.getNumber(), cust.getName() );
            System.out.format( "Sub toatl is  : %10.2f%n", subTot );

            double discount = 0.0;
            if( subTot >= DISCOUNT_BEGINS )
                discount = subTot * DISCOUNT_RATE;
            System.out.format( "Discount is   : %10.2f%n", discount );

            subTot -= discount; // updated //
            System.out.format( "New total is  : %10.2f%n", subTot );

            double salesTax = subTot * NET_TAX_RATE;
            System.out.format( "Sales tax is  : %10.2f%n", salesTax );

            double total = subTot + salesTax;
            System.out.format( "amount due is : %10.2f%n", total );

        }
        while( !Console.getString("More customers (y/n ? ").equalsIgnoreCase("N") );

    }
}

Happy coding in Java :)

Thank you very very much I approciated your help.
I just finishted it.

If you "finished it" by using the hundreds of lines of code from David W then you are deceiving yourself. All you wil have done is to get through this exercise without learning the skills it was supposed to teach, which means you will be in an even worse state for the next lesson. David's well-intentioned posts unfortunately don't teach anything.

Maybe you did solve this youself, by understanding what you needed to do and doing it yourself, in which case I apologise for doubting you. If you copied/pasted any of David's code then I very strongly suggest you go back and do it again yourself.

Thank you for your advise. I didn't copied David's code because I want to understand and with David's code I arrived to understand what I need to do.

OK, that's great. I spoke separately with David and asked him to provide more explanation with his code suggestions next time, but people helping each other like this is exactly what DaniWeb is all about. I look forward to seeing you both here in future.

commented: Absolutely, agreed 100% +15
commented: Agree also +15
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.