6 Replies - 6074 Views - Last Post: 11 May 2012 - 10:55 AM Rate Topic: -----

#1 beeWeeEugene   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 10-May 12

An Automotive GUI Program

Posted 10 May 2012 - 10:51 AM

I am writing a GUI program for a fictitious auto repair shop. I need to list services provided, and calculate their costs along with a 20 dollar service fee for each. I have this part done, with all calculations working properly and no compiler errors.

The second part (where I could use some advice) requires in addition to displaying the total cost, the services purchased and their price need to be displayed to make a full standard invoice listing. The code below compiles and works fine, but if anyone can give me direction to this second part, I would greatly appreciate your help. Thanks in advance! (This is also my first time posting here, so forgive me if the code doesn't look the way it should).

public class BillingDemo
{
	public static void main(String[] args)
	{
		new Billing();
	}
}


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) + Services.oilChange);
		}
	}
	
	private class ExitButtonListener implements ActionListener
	{
		public void actionPerformed(ActionEvent e)
		{
			System.exit(0);
		}
	}
}


import javax.swing.*;

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


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);
	}
}


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;
	}
	
	private static void main(String[] ars)
	{
		PartsAndLabor PAL = new PartsAndLabor();
	}
}


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;
	}
}



Is This A Good Question/Topic? 0
  • +

Replies To: An Automotive GUI Program

#2 beeWeeEugene   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 10-May 12

Re: An Automotive GUI Program

Posted 11 May 2012 - 08:36 AM

In my services class, I have public variables and some private JCheckBox.

I was thinking to add the selected items to the final JOptionPane MessageDialog box (in the Billing Class), I could do something like this:
if (oilChange.isSelected()==true)
{
   // Print variable 'OIL_CHANGE' and it's value;
}



Am I on the right track here? Thanks again in advance for any help and direction!
Was This Post Helpful? 0
  • +
  • -

#3 CasiOo   User is offline

  • D.I.C Lover
  • member icon

Reputation: 1578
  • View blog
  • Posts: 3,551
  • Joined: 05-April 11

Re: An Automotive GUI Program

Posted 11 May 2012 - 08:55 AM

How about you make a new service object which has the specific service and its cost :) ? Then you could, on your Services class, call getSelectedServices() or something like that.
Was This Post Helpful? 1
  • +
  • -

#4 beeWeeEugene   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 10-May 12

Re: An Automotive GUI Program

Posted 11 May 2012 - 08:59 AM

View PostCasiOo, on 11 May 2012 - 08:55 AM, said:

How about you make a new service object which has the specific service and its cost :) ? Then you could, on your Services class, call getSelectedServices() or something like that.


Thank you for the reply. I will work on your suggestion and hope it works! If I run into any issues, I may be posting them here later today... Thanks again!
Was This Post Helpful? 0
  • +
  • -

#5 CasiOo   User is offline

  • D.I.C Lover
  • member icon

Reputation: 1578
  • View blog
  • Posts: 3,551
  • Joined: 05-April 11

Re: An Automotive GUI Program

Posted 11 May 2012 - 09:23 AM

You might even take it a bit further, and make some more objects to make it easier for you.
You tend to put all of the information in your gui classes, but should they really be the ones containing this information? You should think about which classes should have which responsibilities.

In my head this design makes sense:
public class AutoRepairShop {
	private Service[] servicesProvided;
	...
}

public class Service {
	private String serviceName;
	private double laberCost;
	...
}


Was This Post Helpful? 0
  • +
  • -

#6 beeWeeEugene   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 10-May 12

Re: An Automotive GUI Program

Posted 11 May 2012 - 10:39 AM

View PostCasiOo, on 11 May 2012 - 09:23 AM, said:

You might even take it a bit further, and make some more objects to make it easier for you.
You tend to put all of the information in your gui classes, but should they really be the ones containing this information? You should think about which classes should have which responsibilities.

In my head this design makes sense:
public class AutoRepairShop {
	private Service[] servicesProvided;
	...
}

public class Service {
	private String serviceName;
	private double laberCost;
	...
}


CasiOo,

Thanks again for your help. I've implemented your idea of the getSelectedServices() method, and things are looking good. I haven't finalized my code yet, but will be able to do so soon thanks to your help. Should I post my code when I'm done, or just leave this thread as is? Happy Friday and have a great weekend!
Was This Post Helpful? 0
  • +
  • -

#7 CasiOo   User is offline

  • D.I.C Lover
  • member icon

Reputation: 1578
  • View blog
  • Posts: 3,551
  • Joined: 05-April 11

Re: An Automotive GUI Program

Posted 11 May 2012 - 10:55 AM

It would be a great help for people who read your question and have similar problems :) So yes do it if you want to share your code :D
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1