Table of Contents
Java program for billing system of tour and travels
A Java program for simple tours and travels billing system.
Java program for billing system
import java.awt.event.*; import javax.swing.*; import java.util.*; //itvoyagers.in public class Ticket extends JFrame implements ActionListener { int space = 30, age; float cost, total_service_tax = 0.0f, final_amount; JLabel ticket_name_label, non_ac_price_label, ac_price_label; JLabel name_label, age_label; JLabel name_list_label, age_list_label, cost_list_label; JLabel service_tax_text_label, service_tax, total_text_label, total; //itvoyagers.in JTextField name_text_field[] = new JTextField[3]; JTextField age_text_field[] = new JTextField[3]; JCheckBox ac; JButton book; JList name_list, age_list, price_list; DefaultListModel<String> name_bill, age_bill, price_bill; //itvoyagers.in public Ticket() { ticket_name_label = new JLabel("Mumbai To Pune"); non_ac_price_label = new JLabel("Non-AC : ₹400/-"); ac_price_label = new JLabel("AC : ₹550/-"); name_label = new JLabel("Name"); age_label = new JLabel("Age"); name_list_label = new JLabel("Name"); age_list_label = new JLabel("Age"); cost_list_label = new JLabel("Cost"); ac = new JCheckBox("AC"); book = new JButton("Book"); //itvoyagers.in service_tax_text_label = new JLabel("4% Service Tax"); total_text_label = new JLabel("Total Amount"); service_tax = new JLabel(); total = new JLabel(); name_bill = new DefaultListModel<>(); age_bill = new DefaultListModel<>(); price_bill = new DefaultListModel<>(); name_list = new JList(name_bill); age_list = new JList(age_bill); price_list = new JList(price_bill); for (int i = 0; i < 3; i++) { name_text_field[i] = new JTextField(); name_text_field[i].setBounds(10, (i+6)*10+space, 200, 20); add(name_text_field[i]); age_text_field[i] = new JTextField(); age_text_field[i].setBounds(250, (i+6)*10+space, 100, 20); add(age_text_field[i]); space += 30; } //itvoyagers.in ticket_name_label.setBounds(140, 10, 200, 20); non_ac_price_label.setBounds(10, 40, 200, 20); ac_price_label.setBounds(250, 40, 100, 20); name_label.setBounds(10, 70, 50, 20); age_label.setBounds(250, 70, 50, 20); ac.setBounds(100, 199, 50, 20); book.setBounds(175, 195, 100, 30); name_list_label.setBounds(10, 230, 50, 20); age_list_label.setBounds(230, 230, 30, 20); cost_list_label.setBounds(300, 230, 50, 20); name_list.setBounds(10, 260, 200, 100); age_list.setBounds(230, 260, 50, 100); price_list.setBounds(300, 260, 50, 100); service_tax_text_label.setBounds(100, 370, 100, 20); total_text_label.setBounds(100, 390, 100, 20); service_tax.setBounds(300, 370, 100, 20); total.setBounds(300, 390, 100, 20); //itvoyagers.in add(ticket_name_label); add(non_ac_price_label); add(ac_price_label); add(name_label); add(age_label); add(ac); add(book); add(name_list_label); add(age_list_label); add(cost_list_label); add(name_list); add(age_list); add(price_list); add(service_tax_text_label); add(total_text_label); add(service_tax); add(total); book.addActionListener(this); //itvoyagers.in setSize(375,460); setLayout(null); setVisible(true); setTitle("My Ticket"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } @Override public void actionPerformed(ActionEvent e) { if(e.getSource()==book) { float amount_without_service_tax = 0.0f; clear_all(); cost = ac.isSelected() ? 550 : 400; //itvoyagers.in for(int i = 0; i < 3; i++) { try { age = Integer.parseInt(age_text_field[i].getText()); if((age < 5 || age > 65) && age<100) { price_bill.addElement(Float.toString(cost/2)); } else if(age>=5 && age<=65) { price_bill.addElement(Float.toString(cost)); } else { JOptionPane.showMessageDialog(this, "Please enter valid age"); age_text_field[i].setText(""); break; } name_bill.addElement(name_text_field[i].getText()); age_bill.addElement(age_text_field[i].getText()); } catch(NumberFormatException exp) { JOptionPane.showMessageDialog(this, "Something went worngn1. Please check for blank fieldsn2. Please enter numeric values in age"); age_text_field[i].setText(""); clear_all(); break; } } //itvoyagers.in for(int i=0;i<price_bill.getSize();i++) { amount_without_service_tax += Float.parseFloat(price_bill.getElementAt(i)); } total_service_tax = amount_without_service_tax * 0.04f; final_amount = amount_without_service_tax + total_service_tax; service_tax.setText(Float.toString(total_service_tax)); total.setText(Float.toString(final_amount)); } } //itvoyagers.in public void clear_all() { name_bill.clear(); age_bill.clear(); price_bill.clear(); } //itvoyagers.in public static void main(String[] args) { Ticket ticket = new Ticket(); } }
Output

PRACTICALS/PRACTICE PROGRAM IN Java
CHECKOUT OTHER RELATED TOPICS
We are aiming to explain all concepts of Java in easiest terms as possible.

ITVoyagers
Author