Answer to Question #324177 in Java | JSP | JSF for bela

Question #324177

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.


1
Expert's answer
2022-04-05T13:17:52-0400
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());
    }
}

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