java - Getters and Setters returning null -
i'm trying write simple program produces invoice item purchased. have driver class setters , getters, , object class supposed call them. however, when print out invoice, produces null responses everything.
here 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 partnumber } public void setdescription(string description) { description = description;//store description } public void setquantity(int quantity) { quantity = quantity;//store quantity } public void setitemprice(double price) { itemprice = price;//store itemprice } public string getpartnumber() { return partnumber;//retrieve partnumber } public string getdescription() { return description;//retrieve description } public int getquantity() { return quantity;//retrieve quantity } public double getitemprice() { return itemprice;//retrieve itemprice } //get price item purchased. public double getinvoiceamount(double amount) { amount = itemprice * quantity; if (amount < 0) { amount = 0.0; } return amount; } }
and here object class:
public class invoicetest { public static void main(string[] args) { invoice invoice1 = new invoice ("0001", "hammer: used 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)); } }
you forgot assign variables fields in constructor. use this
keyword refer class fields:
public invoice(string partnumber, string description, int quantity, double itemprice) { //here's example //partnumber = partnumber this.partnumber = partnumber; //description = description; this.description = description; //similar other assignments inside constructor... }
you must in setters well:
public void setpartnumber(string partnumber) { this.partnumber = partnumber; //store partnumber }
Comments
Post a Comment