Wednesday 27 July 2016

Java program code for car servicing

This java program is used to demonstrate car servicing Billing System.

 // BillingDemo.java ---------------------------------------------------- 1
public class BillingDemo
{
    public static void main(String[] args)
    {
        new Billing();
    }
}

//Billing.java ------------------------------------------------------------- 2
import java.awt.*;
import java.awt.event.*;
import java.text.DecimalFormat;
import javax.swing.*;

public class Billing extends JFrame
{
    private final double TAX_RATE = 0.06;
   
    private Label label;
    private Services service;
    private PartsAndLabor PAL;
    private Greeting greet;
   
    private JPanel buttonPanel;
    private JButton calcButton;
    private JButton exitButton;
   
    public Billing()
    {
        super("Oreder Calculator");
       
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
       
        setLayout(new BorderLayout());
       
        greet = new Greeting();
        label = new Label();
        service = new Services();
        PAL = new PartsAndLabor();
       
        buildButtonPanel();
       
        add(greet, BorderLayout.NORTH);
        add(label, BorderLayout.WEST);
        add(service, BorderLayout.CENTER);
        add(PAL, BorderLayout.EAST);
        add(buttonPanel, BorderLayout.SOUTH);
       
        pack();
        setVisible(true);
    }
   
    private void buildButtonPanel()
    {
        buttonPanel = new JPanel();
       
        calcButton = new JButton("Calculate");
        exitButton = new JButton("Exit");
       
        calcButton.addActionListener(new CalcButtonListener());
        exitButton.addActionListener(new ExitButtonListener());
       
        buttonPanel.add(calcButton);
        buttonPanel.add(exitButton);
    }
   
    private class CalcButtonListener implements ActionListener
    {
        public void actionPerformed(ActionEvent e)
        {
            double total;
           
            total = service.getServiceCost() +
                          PAL.getLaborCost();
           
            DecimalFormat dollar = new DecimalFormat("0.00");
           
            JOptionPane.showMessageDialog(null, "\nTotal Amount Due: $" + dollar.format(total) );
        }
    }
   
    private class ExitButtonListener implements ActionListener
    {
        public void actionPerformed(ActionEvent e)
        {
            System.exit(0);
        }
    }
}


// Greeting.java -----------------------------------------------------------3



 import javax.swing.*;

public class Greeting extends JPanel
{
    public Greeting()
    {
        JLabel greeting =
            new JLabel("Welcome to Joe's Automotive Service Page!");
        add(greeting);
    }
}

// Label.java -------------------------------------------------------------4
import java.awt.*;
import javax.swing.*;

public class Label extends JPanel
{   
    private JLabel label1;
   
    public Label()
    {
        setLayout(new GridLayout(1,1));
        label1 = new JLabel("Choose options here:");
        add(label1);
    }
}

//PartsAndLabor.java -------------------------------------------------5
import java.awt.*;
import javax.swing.*;

public class PartsAndLabor extends JPanel
{
    public final double OIL_CHANGE_LABOR = 20.00;
    public final double LUBE_JOB_LABOR = 20.00;
    public final double RADIATOR_FLUSH_LABOR = 20.00;
    public final double TRANSMISSION_FLUSH_LABOR = 20.00;
    public final double INSPECTION_LABOR = 20.00;
    public final double MUFFLER_REPLACEMENT_LABOR = 20.00;
    public final double TIRE_ROTATION_LABOR = 20.00;
   
    private JCheckBox oilChange;
    private JCheckBox lubeJob;
    private JCheckBox radiatorFlush;
    private JCheckBox transmissionFlush;
    private JCheckBox inspection;
    private JCheckBox mufflerReplacement;
    private JCheckBox tireRotation;
   
   
   
    public PartsAndLabor()
    {
        setLayout(new GridLayout(7,1));
       
        oilChange = new JCheckBox("Oil Change Labor");
        lubeJob = new JCheckBox("Lube Job Labor");
        radiatorFlush = new JCheckBox("Radiator Flush Labor");
        transmissionFlush = new JCheckBox("Transmission Flush Labor");
        inspection = new JCheckBox("Inspection Labor");
        mufflerReplacement = new JCheckBox("Muffler Replacement Labor");
        tireRotation = new JCheckBox("Tire Rotation Labor");
       
        setBorder(BorderFactory.createTitledBorder("Parts and Labor"));
       
        add(oilChange);
        add(lubeJob);
        add(radiatorFlush);
        add(transmissionFlush);
        add(inspection);
        add(mufflerReplacement);
        add(tireRotation);
    }
   
    public double getLaborCost()
    {
        double laborCost = 0.0;
       
        if (oilChange.isSelected())
            laborCost += OIL_CHANGE_LABOR;
        if (lubeJob.isSelected())
            laborCost += LUBE_JOB_LABOR;
        if (radiatorFlush.isSelected())
            laborCost += RADIATOR_FLUSH_LABOR;
        if (transmissionFlush.isSelected())
            laborCost += TRANSMISSION_FLUSH_LABOR;
        if (inspection.isSelected())
            laborCost += INSPECTION_LABOR;
        if (mufflerReplacement.isSelected())
            laborCost += MUFFLER_REPLACEMENT_LABOR;
        if (tireRotation.isSelected())
            laborCost += TIRE_ROTATION_LABOR;
       
        return laborCost;
    }
   
    public static void main(String[] ars)
    {
        PartsAndLabor PAL = new PartsAndLabor();
    }
}

//Services.java ----------------------------------------------------6
import java.awt.*;
import javax.swing.*;

public class Services extends JPanel
{
    public final static double OIL_CHANGE = 26.00;
    public final double LUBE_JOB = 18.00;
    public final double RADIATOR_FLUSH = 30.00;
    public final double TRANSMISSION_FLUSH = 80.00;
    public final double INSPECTION = 15.00;
    public final double MUFFLER_REPLACEMENT = 100.00;
    public final double TIRE_ROTATION = 20.00;
   
    private JCheckBox oilChange;
    private JCheckBox lubeJob;
    private JCheckBox radiatorFlush;
    private JCheckBox transmissionFlush;
    private JCheckBox inspection;
    private JCheckBox mufflerReplacement;
    private JCheckBox tireRotation;
   
    public Services()
    {
        setLayout(new GridLayout(7,1));
       
        oilChange = new JCheckBox("Oil Change");
        lubeJob = new JCheckBox("Lube Job");
        radiatorFlush = new JCheckBox("Radiator Flush");
        transmissionFlush = new JCheckBox("Transmission Flush");
        inspection = new JCheckBox("Inspection");
        mufflerReplacement = new JCheckBox("Muffler Replacement");
        tireRotation = new JCheckBox("Tire Rotation");
       
        setBorder(BorderFactory.createTitledBorder("Services"));
       
        add(oilChange);
        add(lubeJob);
        add(radiatorFlush);
        add(transmissionFlush);
        add(inspection);
        add(mufflerReplacement);
        add(tireRotation);
    }
   
    public double getServiceCost()
    {
        double serviceCost = 0.0;
       
        if (oilChange.isSelected())
            serviceCost += OIL_CHANGE;
        if (lubeJob.isSelected())
            serviceCost += LUBE_JOB;
        if (radiatorFlush.isSelected())
            serviceCost += RADIATOR_FLUSH;
        if (transmissionFlush.isSelected())
            serviceCost += TRANSMISSION_FLUSH;
        if (inspection.isSelected())
            serviceCost += INSPECTION;
        if (mufflerReplacement.isSelected())
            serviceCost += MUFFLER_REPLACEMENT;
        if (tireRotation.isSelected())
            serviceCost += TIRE_ROTATION;
           
        return serviceCost;
    }
}

OUTPUT:-