I just started a Java class a few weeks ago and we're learning to create our own classes. I think I understand it a little, but I cannot figure out why I can't get the class to multiply the price of an item by the quantity. It either always equals 0.00, or i get a <identifier> expected error. Any help would be great. Thanks!
public class Invoice { //Part Number private String partNumber; public void setPartNumber( String name ) { partNumber = name; } public String getPartNumber() { return partNumber; } //End Number //Part Description private String partDesc; public void setPartDesc( String name ) { partDesc = name; } public String getPartDesc() { return partDesc; } //End Description //Quantity public int partQuantity; public void setPartQuantity( int name ) { partQuantity = name; } public int getPartQuantity() { return partQuantity; } //End Quantity //Part Price public double partPrice; public void setPartPrice( double name ) { partPrice = name; } public double getPartPrice() { return partPrice; } //End Price //Calculate Invoice Amount double amount = partQuantity * partPrice; //End Amount //Output public void displayMessage() { System.out.printf("Part Number: %s\n", getPartNumber() ); System.out.printf("Part Description: %s\n", getPartDesc() ); System.out.printf("Part Quantity: %d\n", getPartQuantity() ); System.out.printf("Part Price: %.2f\n", getPartPrice() ); System.out.printf("Total Invoice Amount: %.2f\n", amount ); } }
import java.util.Scanner; public class InvoiceTest { public static void main(String[] args) { Scanner input = new Scanner( System.in ); int theQuantity; double thePrice; Invoice myInvoice = new Invoice(); //Get and Set Part Number System.out.println("Please enter the part number: "); String theNum = input.nextLine(); myInvoice.setPartNumber( theNum ); //Get and Set Part Description System.out.println("Please enter the part description: "); String theDesc = input.nextLine(); myInvoice.setPartDesc( theDesc ); //Get and Set Part Quantity System.out.println("Please enter the part quantity: "); theQuantity = input.nextInt(); myInvoice.setPartQuantity( theQuantity ); //Get and Set Part Price System.out.println("Please enter the part price: "); thePrice = input.nextDouble(); myInvoice.setPartPrice( thePrice ); myInvoice.displayMessage(); } }