0

I'm working on a java project, as far as I can tell, I've finished it all, but when i run the program, the first set of coding on the application does what it's supposed to, but the updated amounts give me 0 for the price and the amount. The same thing goes for the coding for the second object, the original invoice information works fine, except for the fact that but for the updated code, it gives me negative values when they're supposed to be zero besides that, it works fine. for the updated invoice amounts, it gives me 0 for the price and final amount. I can't see any errors, it all compiles and runs. Here is the class code.

public class Invoice
{
private String partNumber;
private String partDescription;
private int quantity;
private double price;

public Invoice (String part,String description, int quan, double value)
{
    partNumber = part;
    partDescription = description;

    if(quan > 0);
        quantity = quan;

    if(price > 0.0);
        price = value;

}
public void setpartNumber(String part)
{
    partNumber = part;
}
public String getpartNumber()
{
    return partNumber;
}
public void setpartDescription(String description)
{
    partDescription = description;      
}
public String getpartDescription()
{
    return partDescription;
}
public void setquantity(int quan)
{
    if(quan > 0);
        quantity = quan;
    if(quan <= 0);
        quan = 0;
}

public int getquantity()
{

    return quantity;
}
public void setprice(double value)
{
    if(value > 0.0);
        price = value;

    if(value <= 0.0);
        price = 0.0;

}
public double getprice()
{
    return price;
}
public double getInvoiceAmount()
{
    return (getquantity() * getprice());

}

}

And here is the application code.

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


    System.out.println("Original Invoice Information");         
    System.out.printf("Part Number: %s\n", invoiceA.getpartNumber());
    System.out.printf("Description: %s\n",invoiceA.getpartDescription());
    System.out.printf("Quantity: %d\n", invoiceA.getquantity());
    System.out.printf("Price: %.2f\n", invoiceA.getprice());
    System.out.printf("Invoice Amount: %.2f\n",invoiceA.getInvoiceAmount());


    invoiceA.setpartNumber("001234");
    invoiceA.setpartDescription("Yellow Hammer");
    invoiceA.setquantity(3);
    invoiceA.setprice(19.49);
    System.out.println();

    System.out.println("Updated Invoice Information");
    System.out.printf("Part Number: %s\n", invoiceA.getpartNumber());
    System.out.printf("Description: %s\n",invoiceA.getpartDescription());
    System.out.printf("Quantity: %d\n",invoiceA.getquantity());
    System.out.printf("Price: %.2f\n",invoiceA.getprice());
    System.out.printf("Invoice Amount: %.2f\n",invoiceA.getInvoiceAmount());

    Invoice invoiceB = new Invoice("5678","Paint Brush",-5,-9.99);


    System.out.println("\nOriginal Invoice Information");           
    System.out.printf("Part Number: %s\n", invoiceB.getpartNumber());
    System.out.printf("Description: %s\n",invoiceB.getpartDescription());
    System.out.printf("Quantity: %d\n", invoiceB.getquantity());
    System.out.printf("Price: %.2f\n", invoiceB.getprice());
    System.out.printf("Invoice Amount: %.2f\n",invoiceB.getInvoiceAmount());


    invoiceB.setquantity(3);
    invoiceB.setprice(9.49);
    System.out.println();

    System.out.println("Updated Invoice Information");
    System.out.printf("Part Number: %s\n", invoiceB.getpartNumber());
    System.out.printf("Description: %s\n",invoiceB.getpartDescription());
    System.out.printf("Quantity: %d\n",invoiceB.getquantity());
    System.out.printf("Price: %.2f\n",invoiceB.getprice());
    System.out.printf("Invoice Amount: %.2f\n",invoiceB.getInvoiceAmount());


}
}
AJ114
  • 1
  • 3
  • 2
    "_Here's a bunch of code, debug it_". Sorry, but I must be too lazy. Maybe try using a debugger/print statements to step through your code and see when and where things get off track. – takendarkk Feb 07 '14 at 22:43
  • Why do you have "if (price > 0.0)" when it is a declared instance variable? – Alex Johnson Feb 07 '14 at 22:45
  • @Takendarkk I don't know how to use a debugger... my teacher never said that was an option... If I could use it, I would have... – AJ114 Feb 07 '14 at 22:48
  • 1
    Use print statements instead, that's the manual way of doing it. Print your variables at multiple steps along the way and check to see if they are what you think they should be. This will at least narrow your problem down a bit. – takendarkk Feb 07 '14 at 22:50
  • 1
    Yep, as @Takendarkk states, you'll want to at least learn how to use a "poor-man's debugger" by checking the state of your variables at critical points of your program using println statements. Then removing these statements when the code is fixed. And then take the time to learn to use a debugger. You certainly don't need your teacher's permission or blessing to do this, trust me. – Hovercraft Full Of Eels Feb 07 '14 at 22:52

4 Answers4

5

You have extra semicolons after your if conditions throughout your code. The one that actually caused your bug:

public void setprice(double value)
{
    if(value > 0.0);
        price = value;

    if(value <= 0.0);
        price = 0.0;

}

The semicolon acts as the body for your if, so the subsequent statements are always executed, and price is always set to 0. Remove the semicolons:

public void setprice(double value)
{
    if(value > 0.0)
        price = value;

    if(value <= 0.0)
        price = 0.0;

}

Change the other ifs in your code similarly.

rgettman
  • 176,041
  • 30
  • 275
  • 357
0

In your setQuantity method, you are setting the value of quan if the arg is <= 0. You should be setting quantity.

public void setquantity(int quan)
{
    if(quan > 0)
        quantity = quan;
    if(quan <= 0)
        quan = 0;            // should be quantity
}

EDIT: and, as user1066946 & rgettman have pointed out, you need to remove the semicolons from your if statement.

mdl
  • 406
  • 2
  • 9
  • Yup, good eye. I saw OP was setting the arg & just copied and pasted their code. – mdl Feb 07 '14 at 22:49
0

Change

if(price > 0.0){
    price = value;

}

to

if(value> 0.0) {
    price = value;

}

EDIT: and remove the semicolons from if statements.

Alex Johnson
  • 504
  • 5
  • 15
0

In your constructor, if(price > 0.0); price = value; should say if (value > 0.0) { price = value }

You have an errant semicolon after your condition. It's also good practice to use braces in your if statements - it makes it much more clear about what code will be executed when the condition is true.

(As an aside, you're also trying to invent two words to mean the same thing. Instead, use price as the name in the method signature, and say this.price = price; in the constructor. this.price refers to the one defined in the class.)

David Koelle
  • 20,726
  • 23
  • 93
  • 130