Reputation: 327
I'm trying to write a very simple program that produces an invoice for an item purchased. I have a driver class with my setters and getters, and my object class which is supposed to call them. However, when I print out the invoice, it just produces null responses for everything.
Here is my driver class:
public class Invoice {
private String partNumber; //part number of item
private String description; //description of item
private int quantity; //quantity being purchased
private double itemPrice; //price of item
//constructor
public Invoice(String partNumber, String description, int quantity, double itemPrice) {
partNumber = partNumber;
description = description;
if (quantity < 0) {
quantity = 0;
}
if (itemPrice < 0.0) {
itemPrice = 0.0;
}
}
public void setPartNumber(String number) {
partNumber = number; //store the partNumber
}
public void setDescription(String description) {
description = description;//store the description
}
public void setQuantity(int quantity) {
quantity = quantity;//store the quantity
}
public void setItemPrice(double price) {
itemPrice = price;//store the itemPrice
}
public String getPartNumber() {
return partNumber;//retrieve the partNumber
}
public String getDescription() {
return description;//retrieve the description
}
public int getQuantity() {
return quantity;//retrieve the quantity
}
public double getItemPrice() {
return itemPrice;//retrieve the itemPrice
}
//get price for item purchased.
public double getInvoiceAmount(double amount) {
amount = itemPrice * quantity;
if (amount < 0) {
amount = 0.0;
}
return amount;
}
}
And here is my object class:
public class InvoiceTest {
public static void main(String[] args) {
Invoice invoice1 = new Invoice ("0001", "Hammer: used to hit nails.", 1, 10.0);
System.out.println("Item purchased: " + invoice1.getPartNumber() +
"\n Description: " + invoice1.getDescription() +
"\n Amount:" + invoice1.getQuantity() +
"\n price: " + invoice1.getQuantity());
System.out.println();
System.out.println("Total price: " + invoice1.getInvoiceAmount(amount));
}
}
Upvotes: 0
Views: 3993
Reputation: 201447
Your setters are not setting the values into the object instance, you need to make changes like below
public Invoice(String partNumber, String description, int quantity,
double itemPrice) {
this.partNumber = partNumber;
this.description = description;
this.quantity = quantity;
if (this.quantity < 0) {
this.quantity = 0;
}
this.itemPrice = itemPrice;
if (this.itemPrice < 0.0) {
this.itemPrice = 0.0;
}
}
public void setPartNumber(String number) {
this.partNumber = number; //store the partNumber
}
public void setDescription(String description) {
this.description = description;//store the description
}
public void setQuantity(int quantity) {
this.quantity = quantity;//store the quantity
}
public void setItemPrice(double price) {
this.itemPrice = price;//store the itemPrice
}
Upvotes: 1
Reputation: 85779
You forgot to assign the variables to the fields in constructor. Use this
keyword to refer to the class fields:
public Invoice(String partNumber, String description, int quantity, double itemPrice) {
//here's an example
//partNumber = partNumber
this.partNumber = partNumber;
//description = description;
this.description = description;
//similar for other assignments inside the constructor...
}
You must also do this in your setters as well:
public void setPartNumber(String partNumber) {
this.partNumber = partNumber; //store the partNumber
}
Upvotes: 8
Reputation: 17248
Your class's member fields are not being initialized as you intended, because of this:
public Invoice(String partNumber, String description, int quantity, double itemPrice)
{
partNumber = partNumber;
Here, partNumber
refers to the function argument, not the member field. To set the member field, use this.partNumber = partNumber
.
Upvotes: 2