Invoice class using Java
Posted by Samath
Last Updated: January 17, 2024

Create a class named Invoice that contains fields for an item number, name, quantity, price, and total cost. Create instance methods that set the item name, quantity, and price. Use a constructor to initialize the values for Item number, name, quantity, price and totalcost should be set to zero. Whenever the price or quantity is set, recalculate the total (price times quantity). Also include a displayLine() method that displays the item number, name, quantity, price, and total cost.

Create a class named InvoiceDriver whose main() method declares three Invoice items. Create a method that prompts the user for and accepts values for the item number, name, quantity, and price for each Invoice. Then display each completed object.

Invoice Class:

public class Invoice 
{
    private int item_number;
    private String name;
    private int quantity;
    private double price;
    private double total_cost;
    
    public Invoice(int item_num, String nm, int quan, double pri)
    {
        item_number = item_num;
        name = nm;
        quantity = quan;
        price = pri;
        total_cost = 0;
    }
    
    public int getitem_number()
    {
        return item_number;
    }
    public String getname()
    {
        return name;
    }
    public int getquantity()
    {
        return quantity;
    }
    public double getprice()
    {
        return price;
    }
    public double gettotal_cost()
    {
        return total_cost;
    }  
        
    public void setitem_number( int item_n)
    {
        item_number = item_n;
    }
    public void setname(String n)
    {
        name = n;
    }
    public void setquantity(int quan)
    {
        quantity = quan;
        total();
    }
    public void setprice(double pri)
    {
        price = pri;
        total();
    }
    public void total()
    {
        total_cost = price * quantity;
    } 
    public void displayLine()
    {
        System.out.println("Item Number: "+item_number);
        System.out.println("Name: "+name);
        System.out.println("Quantity: "+quantity);
        System.out.println("Price: "+price);
        System.out.println("Total Cost: "+total_cost);
    } 
}

 

Invoice Driver Class:

import java.util.Scanner;
public class InvoiceDriver {

    public static void main(String[] args) {
        
      Invoice check1 = new Invoice(112, "Book", 3, 125.98);
      Invoice check2 = new Invoice(101, "Phone", 2, 456.35);
      Invoice check3 = new Invoice(187, "Laptop", 1, 2345.68);
      
      items(check1,check2, check3);
    }
    
    public static void items(Invoice chk1, Invoice chk2, Invoice chk3)
    {
        Scanner sn = new Scanner(System.in);
        //-----------------------------------------------------------
        System.out.println("Please enter Item Number: ");
        chk1.setitem_number(sn.nextInt());
        
        System.out.println("Please enter Item Name: ");
        chk1.setname(sn.next());
        
        System.out.println("Please enter quantity: ");
        chk1.setquantity(sn.nextInt());
        
        System.out.println("Please enter price: ");
        chk1.setprice(sn.nextDouble());
        //-----------------------------------------------------------
        
        //-----------------------------------------------------------
        System.out.println("Please enter Item Number: ");
        chk2.setitem_number(sn.nextInt());
        
        System.out.println("Please enter Item Name: ");
        chk2.setname(sn.next());
        
        System.out.println("Please enter quantity: ");
        chk2.setquantity(sn.nextInt());
        
        System.out.println("Please enter price: ");
        chk2.setprice(sn.nextDouble());
        //-----------------------------------------------------------
        
        //-----------------------------------------------------------
        System.out.println("Please enter Item Number: ");
        chk3.setitem_number(sn.nextInt());
        
        System.out.println("Please enter Item Name: ");
        chk3.setname(sn.next());
        
        System.out.println("Please enter quantity: ");
        chk3.setquantity(sn.nextInt());
        
        System.out.println("Please enter price: ");
        chk3.setprice(sn.nextDouble());
        //-----------------------------------------------------------
        
        chk1.displayLine();
        chk2.displayLine();
        chk3.displayLine();
    }
    
}

 

Related Content
Simple Data Class using C++
Simple Data Class using C++
Samath | Jan 07, 2021
Account Class in C++
Account Class in C++
Samath | Jan 07, 2021
Car Class using C++
Car Class using C++
Samath | Jan 07, 2021
Circle Class in C++
Circle Class in C++
Samath | Jan 07, 2021
Movie Class in C++
Movie Class in C++
Samath | Jan 20, 2024
Rectangle Class in C++
Rectangle Class in C++
Samath | Jan 17, 2024
Movie Class in Java
Movie Class in Java
Samath | Jan 20, 2024
Student Class in Java
Student Class in Java
Samath | Jan 20, 2024
Glossary Class using Java
Glossary Class using Java
Samath | Jan 20, 2024
Vehicle Class using Java
Vehicle Class using Java
Samath | Jan 18, 2024
Phone Class using Java
Phone Class using Java
Samath | Jan 17, 2024
Airplane Class in Java
Airplane Class in Java
Samath | Jan 17, 2024
Square and Cube Class in C++
Square and Cube Class in C++
Samath | Jan 01, 2015