Question: Create a class named Invoice with field names and item number, quantity, price and total cost. Create instance methods that set the item name, quantity

Create a class named Invoice with field names and item number, quantity, price and total cost. Create instance methods that set the item name, quantity and price. Also include a displayLine() method that calculate the total cost of the item (as price times quantity), then displays the item number, name, quantity, price, and total cost. Save class as Invoice.java. Create a class named TestInvoice whose main method() declares three Invoice objects. Accept as a user input (using Scanner or JOptionPane) of each Invoice field values and assign those to an instance of Invoice class (there should be three series of inputs to fill the fields of each of three Invoice instances). Display values for each Invoice objects using JOptionPane class. When doing so format the output in orderly fashion, one invoice data in a separate line. Save the class as TestInvoice.java. Create an UML class diagram prior to coding the application.

Here is my code for Invoice.java:

public class Invoice {

// instance variables

private String name;

private int itemNumber;

private int quantity;

private double price;

private double totalCost;

// setters

public void setName(String name) {

this.name = name;

}

public void setQuantity(int quantity) {

this.quantity = quantity;

}

public void setPrice(double price) {

this.price = price;

}

public void setItemNumber(int itemNumber){

this.itemNumber = itemNumber;

}

//display method

public void displayLine(){

totalCost = price*quantity;

System.out.println("Name: "+name);

System.out.println("Item Number: "+itemNumber);

System.out.println("Quantity: "+quantity);

System.out.println("Price: "+price);

System.out.println("Total Cost: "+totalCost);

}

}

My code for TestInvoice.java:

import java.util.Scanner;

public class TestInvoice {

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

// creating three methods

Invoice i1 = new Invoice();

Invoice i2 = new Invoice();

Invoice i3 = new Invoice();

// input for first invoice

System.out.print("Enter name of invoice1: ");

i1.setName(sc.next());

System.out.print("Enter item number: ");

i1.setItemNumber(sc.nextInt());

System.out.print("Enter quantity: ");

i1.setQuantity(sc.nextInt());

System.out.print("Enter price: ");

i1.setPrice(sc.nextDouble());

System.out.println();

// input for second invoice

System.out.print("Enter name of invoice2: ");

i2.setName(sc.next());

System.out.print("Enter item number: ");

i2.setItemNumber(sc.nextInt());

System.out.print("Enter quantity: ");

i2.setQuantity(sc.nextInt());

System.out.print("Enter price: ");

i2.setPrice(sc.nextDouble());

System.out.println();

// input for third invoice

System.out.print("Enter name of invoice3: ");

i3.setName(sc.next());

System.out.print("Enter item number: ");

i3.setItemNumber(sc.nextInt());

System.out.print("Enter quantity: ");

i3.setQuantity(sc.nextInt());

System.out.print("Enter price: ");

i3.setPrice(sc.nextDouble());

System.out.println();

sc.close();

// displaying three invoice information

i1.displayLine();

System.out.println();

i2.displayLine();

System.out.println();

i3.displayLine();

System.out.println();

}

}

For my Invoice.java code, I am receiving the error "No main methods, applets, or MDlets found in file" when attempting to run the code. I also need assistance with the UML class diagram. Any assistance would be greatly appreciated.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!