(a) Create a class named Invoice containing fields for an item number, name, quantity, price, and total cost. Create a constructor to pass the value of the item name, quantity, and price. Also include displayLine() method that calculates the total cost for the item (As price times quantity) then displays the item number, name, quantity price, and total cost. Save the class as Invoice.java.
(b) Create a class named TestInvoice whose main() method declares three invoice items. Provide values for each, and display them. Save the application as TestInvoice.java.
class Invoice {
private String itemNumber;
private String name;
private int quantity;
private double price;
private double totalCost;
public Invoice() {
}
public Invoice(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 newInvoice1 = new Invoice("111", "chips", 2, 1);
Invoice newInvoice2 = new Invoice("222", "Monitor", 5, 5);
Invoice newInvoice3 = new Invoice("333", "CD Disk", 2, 6);
newInvoice1.displayLine();
newInvoice2.displayLine();
newInvoice3.displayLine();
}
}
Comments
Leave a comment