Create a class named INVOICE that contains fields for an item number, name, quantity, price, and total cost. Create instance methods that set the item name, quantity, and price. Whenever the price or quantity is set , recalculate the total (price times quantity ). Also include a DISPLAYLine() method that displays the item number, name, quantity, price for each INVOICE. Save the class as INVOICE.java
Sample output
item # : 111
Item name: chips
Quantity:2
Price: 1.00
Total Cost: 2.00
class INVOICE {
private String itemNumber;
private String name;
private int quantity;
private double price;
private double totalCost;
public INVOICE() {
}
public void SETDATA(String itemNumber, String name, int quantity, double price) {
this.itemNumber = itemNumber;
this.name = name;
this.quantity = quantity;
this.price = price;
this.totalCost = this.price * this.quantity;
}
public void DISPLAYLine() {
System.out.println("Item #: " + itemNumber);
System.out.println("Item name: " + name);
System.out.println("Quantity: " + quantity);
System.out.printf("Price: %.2f\n", price);
System.out.printf("Total cost: %.2f\n\n", totalCost);
}
}
class App {
public static void main(String[] args) {
INVOICE iNVOICE = new INVOICE();
iNVOICE.SETDATA("111", "chips", 2, 1);
iNVOICE.DISPLAYLine();
}
}
Comments
Leave a comment