我正在开发一个 java 项目,据我所知,我已经完成了这一切,但是当我运行程序时,应用程序上的第一组编码完成了它应该做的事情,但是更新后的数量给了我0 表示价格和金额。第二个对象的编码也是如此,原始发票信息工作正常,除了更新的代码,当它们应该为零时它给我负值除此之外,它工作正常。对于更新的发票金额,它给我的价格和最终金额为 0。我看不到任何错误,它全部编译并运行。这是课程代码。
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());
}
}
这是应用程序代码。
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());
}
}