In this article you will see how to generate a tutorial course invoice by using java programming language.
Java Program to Create a Tutorial Course Invoice
As per problem statement, you have to create an tutorial course invoice. You have provided with Course details specifying course id, course name, fee of the course.
Approach:
- Two classes created Main class and Course class.
- Main class is the driver class and Course class has all the properties of course.
- In Main class, it will ask the user to enter required details like student name, course, id, course duration in months, course fee per month etc.
- To create object of Course class we have created a constructor of it. All the course t properties are put in the constructor. This class contains two user defined methods displayFormat() and display() method to display bill details in console.
Program:
import java.util.ArrayList; import java.util.List; import java.util.Scanner; import java.text.SimpleDateFormat; import java.util.Date; import java.util.Calendar; class Course { //declaring variables private String course_id; private String course_name; private int course_duration; private double price; private double total_price; //constructor Course(String course_id, String course_name, int course_duration, double price, double total_price) { this.course_id=course_id; this.course_name = course_name; this.course_duration = course_duration; this.price = price; this.total_price = total_price; } //getter methods public String getId() { return course_id; } public String getPname() { return course_name; } public int getQty() { return course_duration; } public double getPrice() { return price; } public double getTotalPrice() { return total_price; } //displayFormat() method to display the column names public static void displayFormat() { System.out.format("---------------------------------------------------------------------------------------------------------------------------"); System.out.print("\nCourse ID \t\tName\t\tDuration(In Months)\t\tRate(Per Month) \tTotal Price\n"); System.out.format("---------------------------------------------------------------------------------------------------------------------------\n"); } //display() method to display the column values public void display() { System.out.format(" %-9s %-9s %5d %9.2f %14.2f\n" ,course_id, course_name, course_duration, price, total_price); } } public class Main { public static void main(String args[]) { //variables declared and initialized String courseId = null; String courseName = null; int duration = 0; double price = 0.0; double total_price = 0.0; double overAllPrice = 0.0; double cgst, sgst, subtotal=0.0, discount=0.0; char option = '\0'; System.out.println("\t\t\t\t--------------------BtechGeeks Course Invoice-----------------"); System.out.println("\t\t\t\t\t "+" "+"Hyderabad, India"); System.out.println("GSTIN: 03AYJKK932M762"+"\t\t\t\t\t\t\tContact: (+91) 9876543210"); //format of current date and time SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss"); Date date = new Date(); Calendar calendar = Calendar.getInstance(); String[] days = new String[] { "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Frcourse_iday", "Saturday" }; //Printing current date and time System.out.println("Date: "+formatter.format(date)+" "+days[calendar.get(Calendar.DAY_OF_WEEK) - 1]+"\t\t\t\t\t\t (+91) 9998887770"); Scanner sc = new Scanner(System.in); System.out.print("Enter Student Name: "); String customername=sc.nextLine(); //create Scanner class object //creating an ArrayList to store the course List<Course> course = new ArrayList<Course>(); do { //taking input values System.out.println("Enter the Course details: "); //Asking course ID System.out.print("Course ID: "); courseId = sc.nextLine(); //Asking course Name System.out.print("Course Name: "); courseName = sc.nextLine(); //Asking course Duration in months System.out.print("Duration (in month): "); duration = sc.nextInt(); //Asking course Price per month System.out.print("Price (per month): "); price = sc.nextDouble(); //calculating total price for a specific course total_price = price * duration; //calculating overall price overAllPrice = overAllPrice + total_price; //creating Course class object and adding it to the List course.add( new Course(courseId, courseName, duration, price, total_price) ); //asking for continue with other courses? System.out.print("Want to add more courses? (y or n): "); //reading a character y or Y or N or n option = sc.next().charAt(0); //read remaining characters, don't store (no use) sc.nextLine(); } while (option == 'y' || option == 'Y'); //display all course with its properties Course.displayFormat(); for (Course p : course) { p.display(); } //price calculation System.out.println("\n\t\t\t\t\t\t\t\t\t\tTotal Amount (Rs.) " +overAllPrice); //calculating discount amount //Suppose we are offering 10% discount on total course fee discount = overAllPrice*10/100; System.out.println("\n\t\t\t\t\t\t\t\t\t\t Discount (Rs.) " +discount); //calculating total amount after discount subtotal = overAllPrice-discount; System.out.println("\n\t\t\t\t\t\t\t\t\t\t SubTotal "+subtotal); //calculating tax amount sgst=overAllPrice*12/100; System.out.println("\n\t\t\t\t\t\t\t\t\t\t SGST (%) "+sgst); cgst=overAllPrice*12/100; System.out.println("\n\t\t\t\t\t\t\t\t\t\t CGST (%) "+cgst); //calculating final amount to be paid System.out.println("\n\t\t\t\t\t\t\t\t\t\t Invoice Total " +(subtotal+cgst+sgst)); System.out.println("\t\t\t\t All the Best for Your Bright Future"); System.out.println("\t\t\t\t----------------Thank You!!-----------------"); //Closing Scanner object sc.close(); } }
Output:
Have you mastered basic programming topics of java and looking forward to mastering advanced topics in a java programming language? Go with these ultimate Advanced java programs examples with output & achieve your goal in improving java coding skills.