Answer to Question #323516 in Java | JSP | JSF for niko

Question #323516

(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. 



1
Expert's answer
2022-04-04T16:11:33-0400


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();
	}
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS