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.
import java.util.Scanner;
class Invoice{
int num;
String name;
int qual;
int cost;
int allCost;
public Invoice(int num, String name, int qual, int cost) {
this.num = num;
this.name = name;
this.qual = qual;
this.cost = cost;
displayLine();
}
public void displayLine(){
allCost = qual * cost;
}
@Override
public String toString() {
return "Invoice{" +
"num=" + num +
", name='" + name + '\'' +
", qual=" + qual +
", cost=" + cost +
", allCost=" + allCost +
'}';
}
}
public class Main {
public static void main(String[] args){
Scanner in = new Scanner(System.in);
int num = in.nextInt();
String name = in.next();
int qual = in.nextInt();
int cost = in.nextInt();
System.out.println(new Invoice(num, name, qual, cost).toString());
}
}
Comments
Leave a comment