I am trying to build a very quick invoice type program, and I can't seem to get the final total to print. I am calling the function for it, but it's not printing anything to the screen...any suggestions?

public class Invoice 
{
	   private double Total;
	  

	   	//Constructor
	public Invoice( double initialBalance )
	   {
		// validate that initialBalance is greater than 0.0; 
	      // if it is not, balance is initialized to the default value 0.0
	      if ( initialBalance >= 0.00 ) 
	         Total = initialBalance; 
	   } // end Account constructor

	   // Find amount 
	   public void price(double Price, double NumberOfItem )
	   {      
	      Total = Total + (NumberOfItem * Price); // add amount to balance 
	   } // end method credit

	   // return the account balance
	   public double getTotal()
	   {
		return Total; // gives the value of balance to the calling method
	   } // end method getBalance
	} // end class Account

and an invoice testing program:

public class InvoiceTest 
{
	   // main method begins execution of Java application
	   public static void main( String[] args ) 
	   {
	      Invoice account1 = new Invoice(0.00); // create Invoice object

	      // display initial balance
	      System.out.printf( "Current Account Balance is: $%.2f\n", 
	         account1.getTotal() );
	      
	      // create Scanner to obtain input from command window
	      Scanner input = new Scanner( System.in );
	      double Price; //Price of each item
	      int NumberOfItem; // Number of Item Being Purchased
	      String Tool; //name of tool read from user
	      String Description; //description of item
	      
	      //Intake Name of Item From customer
	      System.out.println("Enter Name of Item: ");
	      Tool = input.nextLine();
	      
	      //Intake Description of Item From Customer
	      System.out.println("Enter Description of Item: ");
	      Description = input.nextLine();
	      
	      //Enter Number of Items Being Purchased
	      System.out.println( "Enter number of " + Tool + "(s)" + " being purchased:" ); 
	      NumberOfItem = input.nextInt();

	      //Enter Price For Each Tool
	      System.out.println( "Enter Price For Each " + Tool + " :" );
	      Price = input.nextDouble();
	    
	      //Find Final Price For Invoice
	      System.out.printf("Total of Purchase is:", account1.getTotal());
	      
	   } // end main

}

This is the output that I'm getting:

Current Account Balance is: $0.00
Enter Name of Item: 
asdf
Enter Description of Item: 
asdf
Enter number of asdf(s) being purchased:
10
Enter Price For Each asdf :
5
Total of Purchase is:

The fault is in your understanding of creating object classes, how to initialized the class and how to populate/set data to variables of the object that did not received data on initialization. Meaning at the start you did call new Invoice(0.00); that set balance, however you failed to call price(double Price, double NumberOfItem ) therefore your account1.getTotal() has nothing to return

Strange that it wouldn't print a 0.0?

Try adding a $%.2f\n to format the data. System.out.printf("Total of Purchase is:$%.2f\n"", account1.getTotal();

commented: Answered quickly and efficiently! Thanks! +1
commented: Kudos for the thread solver :) +8

Look at your invoice classes constructor. You are telling the program to set the Total to whatever the initialbalance is.

public Invoice( double initialBalance )//This means when you create a new //invoice object, it will need 1 argument of type double
Invoice account1 = new Invoice(0.00);//You have initialBalance as 0, therefore total //will always be 0.

//so you need to use the price method from the invoice class
account1.price(price,numberOfItem);
//then try and get the total
 System.out.printf("Total of Purchase is:", account1.getTotal());

The fault is in your understanding of creating object classes, how to initialized the class and how to populate/set data to variables of the object that did not received data on initialization. Meaning at the start you did call new Invoice(0.00); that set balance, however you failed to call price(double Price, double NumberOfItem ) therefore your account1.getTotal() has nothing to return

How do I call

price(double price,double NumberOfItem)

? I have everything declared for those item, so I'm a little confused....

Strange that it wouldn't print a 0.0?

Try adding a $%.2f\n to format the data. System.out.printf("Total of Purchase is:$%.2f\n"", account1.getTotal();

Thanks! That was it!

Thanks! That was it!

That printed the correct total? That's impossible unless you called the price method. Or am I missing something here? :)

>>That printed the correct total? That's impossible unless you called the price method.
True.

>>Or am I missing something here?
Unless I'm missing it too I can confirm that you're not missing anything.

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.