Valhalla Legends Forums Archive | Java Programming | Help: Creating a basic invoice

AuthorMessageTime
ViLeNT
I've been working on this for the past 3-4 hours. I've made a lot of progress but it's clear much is to be done. Can someone please point me in the right direction as to why this is not working? ??? ???

Invoice.java

[code]
//********************************************************************
//  Invoice.java      Author:
//
//  Creates a new invoice and calculates the customers total.
//
//********************************************************************

import java.text.*;

public class Invoice
{

//declares instance variables for: part number, part description, quantity of items, and price per item
private String PartNumber, PartDescription;
private int Quantity;
private double PricePerItem;

//-----------------------------------------------------------------
//  Sets up the an invoice by defining the part number, part description,
//  the quantity of items and the price per item
//-----------------------------------------------------------------
public Invoice (String PartNumber,String PartDescription, int Quantity, double PricePerItem)
  {

  }

 
  //  Setter method for part number variable
  public void setPartNumber (String PartNumber)
  {
  }

  //  Getter method for part number variable
  public String getPartNumber()
  {
      return PartNumber;
  }
 
 
  //  Setter method for part description variable
  public void setPartDescription (String PartDescription)
  {
  }

  //  Getter method for part description variable
  public String getPartDescription()
  {
      return PartDescription;
  }
 
 
  //  Setter method for quantity variable
  public void setQuantity (int Quantity)
  {
  }

 
  //  Getter method for quantity variable
  public int getQuantity()
  {
      return Quantity;
  }
 
 
  //  Setter method for price per item
  public void setPricePerItem (double PricePerItem)
  {
  }


  //  Getter method for price per item
  public double getPricePerItem()
  {
      return PricePerItem;
  }

//calculates and returns the invoice amount
public double getInvoiceAmount()
{
double sum = Quantity * PricePerItem;

return sum;
}



} // end class Invoice
[/CODE]


Transaction.java

[code]
public class Transaction
{
public static void main( String args[] )
{
Invoice invoice1 = new Invoice( "1234", "Hammer", 2, 14.95 );

// display invoice1
System.out.println( "Original invoice information" );

System.out.printf( "Part number: %s\n", invoice1.getPartNumber() );

System.out.printf( "Description: %s\n",

invoice1.getPartDescription() );

System.out.printf( "Quantity: %d\n", invoice1.getQuantity() );

System.out.printf( "Price: %.2f\n", invoice1.getPricePerItem() );

System.out.printf( "Invoice amount: %.2f\n",

invoice1.getInvoiceAmount() );

System.out.println();

System.out.println("***** Testing for negative values");

// change invoice1's data
invoice1.setPartNumber( "001234" );

invoice1.setPartDescription( "Yellow Hammer" );

invoice1.setQuantity( -2 );

invoice1.setPricePerItem( -5.95 );

System.out.printf( "Invoice amount: %.2f\n",
invoice1.getInvoiceAmount() );
}
}

[/code]


Additionally I am missing the following:

With respect to quantity of the item and price per item, create this rule

•If the quantity is not positive, it should be set to 0. If the price per item is not positive, it should be set to 0.0.


----------------------------------------------------------------------

Right now when I run the program I am getting the following output:

Original invoice information
Part number: null
Description: null
Quantity: 0
Price: 0.00
Invoice amount: 0.00

***** Testing for negative values
Invoice amount: 0.00
February 25, 2008, 4:05 AM
ViLeNT
Well I managed to get a little further.

[code]

//********************************************************************
//  Invoice.java      Author:
//
//  Creates a new invoice and calculates the customers total.
//
//********************************************************************

import java.text.*;

public class Invoice
{

//declares instance variables for: part number, part description, quantity of items, and price per item
private String partNumber, partDescription;
private int quantity;
private double pricePerItem;

//-----------------------------------------------------------------
//  Sets up the an invoice by defining the part number, part description,
//  the quantity of items and the price per item
//-----------------------------------------------------------------
public Invoice (String serial, String partDesc, int amount, double itemprice)
  {
  partNumber = serial;
  partDescription = partDesc;
  quantity = amount;
  pricePerItem = itemprice;
  }

 
  //  Setter method for part number variable
  public void setpartNumber (String partNumber)
  {
  }

  //  Getter method for part number variable
  public String getpartNumber()
  {
      return partNumber;
  }
 
 
  //  Setter method for part description variable
  public void setpartDescription (String partDescription)
  {
  }

  //  Getter method for part description variable
  public String getpartDescription()
  {
      return partDescription;
  }
 
 
  //  Setter method for quantity variable
  public void setquantity (int quantity)
  {
  }

 
  //  Getter method for quantity variable
  public int getquantity()
  {
      return quantity;
  }
 
 
  //  Setter method for price per item
  public void setpricePerItem (double pricePerItem)
  {
  }


  //  Getter method for price per item
  public double getpricePerItem()
  {
      return pricePerItem;
  }

//calculates and returns the invoice amount
public double getinvoiceAmount()
{
return (quantity * pricePerItem);

}



} // end class Invoice
[/code]

[code]

public class Transaction
{
public static void main( String args[] )
{
Invoice invoice1 = new Invoice( "1234", "Hammer", 2, 14.95 );

// display invoice1
System.out.println( "Original invoice information" );

System.out.printf( "Part number:\n", invoice1.getpartNumber() );

System.out.printf( "Description: %s\n", invoice1.getpartDescription() );

System.out.printf( "Quantity: %d\n", invoice1.getquantity() );

System.out.printf( "Price: %.2f\n", invoice1.getpricePerItem() );

System.out.printf( "Invoice amount: %.2f\n", invoice1.getinvoiceAmount() );

System.out.println();

System.out.println("***** Testing for negative values");

// change invoice1's data
invoice1.setpartNumber("001234");

invoice1.setpartDescription( "Yellow Hammer");

invoice1.setquantity(-2);

invoice1.setpricePerItem(-5.95);

System.out.printf( "Invoice amount: %.2f\n", invoice1.getinvoiceAmount() );
}
}
[/code]


Output:

Original invoice information
Part number: 1234
Description: Hammer
Quantity: 2
Price: 14.95
Invoice amount: 29.90

***** Testing for negative values
Invoice amount: 29.90
February 25, 2008, 6:09 AM
Myndfyr
The reason it's not working as intended is because all of your setter methods are stubs; you don't actually have any code.  Java isn't just going to say, "Oh, he has a method called 'setPartNumber', and a variable called 'partNumber,' - obviously he means to set the partNumber variable when he calls setPartNumber!"  Computers do exactly what you tell them to do when you're programming.  As Steve McConnell says, you are your own worst enemy when it comes to programming.
February 25, 2008, 3:20 PM
ViLeNT
thank you ;) I fixed this

[code]
//********************************************************************
//  Invoice.java      Author: Vince Guadalupe
//
//  Creates a new invoice and calculates the customers total.
//
//********************************************************************

public class Invoice
{

//declares instance variables for: part number, part description, quantity of items, and price per item
private String partNumber, partDescription;
private int quantity;
private double pricePerItem;

//-----------------------------------------------------------------
//  Sets up the an invoice by defining the part number, part description,
//  the quantity of items and the price per item
//-----------------------------------------------------------------
public Invoice (String partNumber, String partDescription, int quantity, double pricePerItem)
  {
  this.partNumber = partNumber;
  this.partDescription = partDescription;
  this.quantity = quantity;
  this.pricePerItem = pricePerItem;
}

 
  //  Setter method for part number variable
  public void setpartNumber (String partNumber)
  {
  this.partNumber = partNumber;
  }

  //  Getter method for part number variable
  public String getpartNumber()
  {
      return partNumber;
  }
 
 
  //  Setter method for part description variable
  public void setpartDescription (String partDescription)
  {
  this.partDescription = partDescription;
  }

  //  Getter method for part description variable
  public String getpartDescription()
  {
      return partDescription;
  }
 
 
  //  Setter method for quantity variable
  public void setquantity (int quantity)
  {
  this.quantity = quantity;
  if(quantity < 0) {
  this.quantity = 0;
}
  }

 
  //  Getter method for quantity variable
  public int getquantity()
  {
     
  if(quantity < 0) {
  this.quantity = 0;
  }
  return quantity;
  }
 
 
  //  Setter method for price per item
  public void setpricePerItem (double pricePerItem)
  {
  this.pricePerItem = pricePerItem;
  if(pricePerItem < 0) {
  this.pricePerItem = 0.0;
}
  }


  //  Getter method for price per item
  public double getpricePerItem()
  {
  if(pricePerItem < 0) {
  this.pricePerItem = 0.0;
  }
      return pricePerItem;
  }

//calculates and returns the invoice amount
public double getinvoiceAmount()
{
return (quantity * pricePerItem);

}



} // end class Invoice

[/code]
February 26, 2008, 1:19 AM
steve0
Good  info  ! Thank you  !
August 23, 2008, 4:48 AM

Search