if/esle if not working correctly, but a method not working.

  • (2 Pages)
  • +
  • 1
  • 2

28 Replies - 1550 Views - Last Post: 15 July 2012 - 09:12 AM Rate Topic: -----

#1 brianborn1968  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 38
  • Joined: 15-November 11

if/esle if not working correctly, but a method not working.

Posted 10 July 2012 - 06:14 AM

This is a large program of a taxi sim, at least for me being that this is for an introductory course in Java. There are 2 problems that I'm having or at least that I have noticed so far. Let's start with the easiest to explain.

1) Once the taxi has traveled 100 miles, it needs to be repaired before it can continue to operate and the repairs cost $25. The maintenance cost should be deducted from the total fares, but it doesn't work. The method is deductRepairs.

2) I opened up a can of worms with learning about the setVisible method as this was not part of the assignment, but I wanted to learn it. Below are some screen captures of the issue as it might be easier than trying to explain.

3) There is a lot of code here and if anyone has suggestions on making this more efficient, it would be much appreciated.

NOTE: the gas tank is unlimited and the cab can run on a negative amount of gas. This was not specified in the instructions so I did not code for it. I wish my real car could operate on a negative amount of gas.

Brian


import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.text.NumberFormat;
import javax.swing.border.TitledBorder;

public class BoydBAssignment4_ver1 extends JApplet
{
    private Taxi		cab;			//create the pointer
    private Operation	ops;			//create the pointer

    private JLabel		lblName      	= new JLabel("Enter gas quantity:");	//Enter miles or gas:
    private JTextField	numEntry		= new JTextField("0", 6);
    private JLabel		resultLabelA	= new JLabel("Before you can begin, you must first create");
    private JLabel		resultLabelB	= new JLabel("your taxi cab object in the Operations View");
    private JLabel		resultLabelC	= new JLabel("below. You will need to add gas too.");
    private JButton		btnRecord		= new JButton("Record Trip (in miles)");
    private JButton		btnAddGas		= new JButton("Add Gas (in gallons)");
    private JButton		btnGasLeft		= new JButton("Gas Available");
    private JButton		btnMiles		= new JButton("Miles Traveled");
    private JButton		btnTotalRev		= new JButton("Total Fares Earned");
    private JButton		btnMilesForRepair		= new JButton("Miles For Repair");						//TEMP LINE. REMOVE BEFORE SUBMITTING
    private JButton		btnCreate		= new JButton("Create");
    private JButton		btnStart		= new JButton("Start");
    private JButton		btnRepair		= new JButton("Repair");
    private JButton		btnReports		= new JButton("View Report Buttons");
    private JButton		btnCloseReports	= new JButton("Close Report View");
    private JPanel		panelMessages	= new JPanel();
    private JPanel		panelDataEntry	= new JPanel();
    private JPanel		panelReportView	= new JPanel();
    private JPanel		panelOperations	= new JPanel();
    private JPanel		panelData		= new JPanel();
    
    ButtonHandler 		b_handle;

    public void init()
    {
		//cab = new Taxi();				//moved to ActionEvent
		ops = new Operation();			//instantiate the object

        setSize(300,550);
    	Container box = getContentPane( );
        box.setLayout(new FlowLayout( ) );
        //box.setLayout(new GridLayout(0, 1));		//experimenting
        panelMessages.setBackground(Color.WHITE);	//experimenting
        numEntry.setBackground(Color.YELLOW);		//experimenting

        panelMessages.setLayout(new BoxLayout(panelMessages, BoxLayout.Y_AXIS));
        panelDataEntry.setLayout(new BoxLayout(panelDataEntry, BoxLayout.Y_AXIS));
        panelReportView.setLayout(new BoxLayout(panelReportView, BoxLayout.Y_AXIS));
        panelOperations.setLayout(new BoxLayout(panelOperations, BoxLayout.Y_AXIS));

        panelMessages.setPreferredSize(new Dimension( 280, 110 ));
        panelDataEntry.setPreferredSize(new Dimension( 200, 110 ));
        panelReportView.setPreferredSize(new Dimension( 200, 160 ));
        panelOperations.setPreferredSize(new Dimension( 200, 110 ));        											
        btnAddGas.setMinimumSize(new Dimension(190, 25));
        btnAddGas.setMaximumSize(new Dimension(190, 25));
        btnGasLeft.setMinimumSize(new Dimension(190, 25));
        btnGasLeft.setMaximumSize(new Dimension(190, 25));
        btnMiles.setMinimumSize(new Dimension(190, 25));
        btnMiles.setMaximumSize(new Dimension(190, 25));
        btnTotalRev.setMinimumSize(new Dimension(190, 25));
        btnTotalRev.setMaximumSize(new Dimension(190, 25));
        btnStart.setMinimumSize(new Dimension(190, 25));
        btnStart.setMaximumSize(new Dimension(190, 25));
        btnRepair.setMinimumSize(new Dimension(190, 25));	
        btnRepair.setMaximumSize(new Dimension(190, 25));	
        btnCreate.setMinimumSize(new Dimension(190, 25)); 
        btnCreate.setMaximumSize(new Dimension(200, 25)); 
        btnCloseReports.setMinimumSize(new Dimension(190, 25));
        btnCloseReports.setMaximumSize(new Dimension(190, 25));
        btnRecord.setMinimumSize(new Dimension(190, 25));
        btnRecord.setMaximumSize(new Dimension(190, 25));
        btnMilesForRepair.setMinimumSize(new Dimension(190, 25));
        btnMilesForRepair.setMaximumSize(new Dimension(190, 25));
        
        /*************************
        *** Panel for Messages ***
        *************************/
        box.add(panelMessages);

        Font fontPanel = new Font("Dialog", Font.PLAIN, 12);
        panelMessages.setBorder(
			BorderFactory.createTitledBorder(
				BorderFactory.createEtchedBorder(),
				"Messages",
				TitledBorder.DEFAULT_JUSTIFICATION,
				TitledBorder.DEFAULT_POSITION,
				fontPanel
				)
			);

        panelMessages.add(panelData);

        panelMessages.add(resultLabelA);
        panelMessages.add(resultLabelB);
        panelMessages.add(resultLabelC);
        panelMessages.add(btnReports);

        /***************************
        *** Panel for Data Entry ***
        ***************************/
        box.add(panelDataEntry);

        panelDataEntry.setBorder(
			BorderFactory.createTitledBorder(
				BorderFactory.createEtchedBorder(),
				"Data Entry",
				TitledBorder.DEFAULT_JUSTIFICATION,
				TitledBorder.DEFAULT_POSITION,
				fontPanel
				)
			);

        lblName.setAlignmentX(CENTER_ALIGNMENT);
        btnRecord.setAlignmentX(CENTER_ALIGNMENT);
        btnAddGas.setAlignmentX(CENTER_ALIGNMENT);

        panelDataEntry.add(panelData);

        panelData.add(lblName);
        panelData.add(numEntry);

        panelDataEntry.add(btnRecord);
        panelDataEntry.add(btnAddGas);

        /****************************
        *** Panel for Report View ***
        ****************************/
        box.add(panelReportView);

        panelReportView.setBorder(
			BorderFactory.createTitledBorder(
				BorderFactory.createEtchedBorder(),
				"Reports View",
				TitledBorder.DEFAULT_JUSTIFICATION,
				TitledBorder.DEFAULT_POSITION,
				fontPanel
				)
			);
        
        btnGasLeft.setAlignmentX(CENTER_ALIGNMENT);
        btnMiles.setAlignmentX(CENTER_ALIGNMENT);
        btnTotalRev.setAlignmentX(CENTER_ALIGNMENT);
        btnMilesForRepair.setAlignmentX(CENTER_ALIGNMENT);													//TEMP LINE. REMOVE BEFORE SUBMITTING
        btnCloseReports.setAlignmentX(CENTER_ALIGNMENT);

        panelReportView.add(btnGasLeft);
        panelReportView.add(btnMiles);
        panelReportView.add(btnTotalRev);
        panelReportView.add(btnMilesForRepair);																//TEMP LINE. REMOVE BEFORE SUBMITTING
        panelReportView.add(btnCloseReports);

        /********************************
        *** Panel for Operations View ***
        ********************************/
        box.add(panelOperations);

        panelOperations.setBorder(
			BorderFactory.createTitledBorder(
				BorderFactory.createEtchedBorder(),
				"Operations View",
				TitledBorder.DEFAULT_JUSTIFICATION,
				TitledBorder.DEFAULT_POSITION,
				fontPanel
				)
			);

        btnCreate.setAlignmentX(CENTER_ALIGNMENT);
        btnStart.setAlignmentX(CENTER_ALIGNMENT);
        btnRepair.setAlignmentX(CENTER_ALIGNMENT);

        panelOperations.add(btnCreate);
        panelOperations.add(btnStart);
        panelOperations.add(btnRepair);
        
        /********************************
        ***                           ***
        ********************************/

        b_handle = new ButtonHandler( );

        btnRecord.addActionListener( b_handle);
        btnAddGas.addActionListener( b_handle);
        btnGasLeft.addActionListener( b_handle);
        btnMiles.addActionListener( b_handle);
        btnTotalRev.addActionListener( b_handle);
        btnCreate.addActionListener( b_handle);
        btnStart.addActionListener( b_handle);
        btnRepair.addActionListener( b_handle);
        btnReports.addActionListener( b_handle);
        btnCloseReports.addActionListener( b_handle);
        btnMilesForRepair.addActionListener( b_handle);														//TEMP LINE. REMOVE BEFORE SUBMITTING

        panelDataEntry.setVisible(true);
        panelReportView.setVisible(false);
        btnRepair.setVisible(false);
        btnStart.setVisible(false);
        btnRecord.setVisible(false);
        btnAddGas.setVisible(false);
        btnReports.setVisible(false);
    }

//****************************************************************************************
//*** The class below is for the button handler
//****************************************************************************************

    private class ButtonHandler implements ActionListener
    {
        public void actionPerformed(ActionEvent e)
        {
        	if (e.getSource( ) == btnRecord)
            {
        		if (cab.gasLeft() > 0 && ops.getMilesLastRepair() < 100 && ops.isStarted() == true) 
        		{
        			cab.addMiles(Double.parseDouble(numEntry.getText()));
        			ops.addMiles(Double.parseDouble(numEntry.getText()));
        			resultLabelA.setText(Double.parseDouble(numEntry.getText()) + " miles driven on this fare.");
        			resultLabelB.setText(" ");
        			resultLabelC.setText(" ");
        			numEntry.setText("0");
        		}
        		else if (ops.getMilesLastRepair() >= 100)
        		{
        			panelOperations.setVisible(true);
        			panelDataEntry.setVisible(false);
        			btnRepair.setVisible(true);
        			resultLabelA.setText("Maintenance needed, click the repair button.");
        			resultLabelB.setText(" ");
        			resultLabelC.setText(" ");
        			numEntry.setText("0");
        		}
        		else if (ops.isStarted() == false)
        		{
        			resultLabelA.setText("Click the start button first.");
        			resultLabelB.setText(" ");
        			resultLabelC.setText(" ");
        			numEntry.setText("0");
        		}
        		else if (cab.gasLeft() <= 0)
        		{
        			resultLabelA.setText("Gas tank is empty. Add gas.");
        			resultLabelB.setText(" ");
        			resultLabelC.setText(" ");
        			numEntry.setText("0");
        		}
            }

            //Add gas to the tank
            else if (e.getSource( ) == btnAddGas)
            {
            	if(ops.isStarted() != true)
            	{
        			btnStart.setVisible(true);
        			panelDataEntry.setVisible(false);
        			panelOperations.setVisible(true);
                    resultLabelA.setText(Double.parseDouble(numEntry.getText()) + " gallons of gas added.");
        			resultLabelB.setText(" ");
        			resultLabelC.setText("Now you need to start your cab.");
                    numEntry.setText("0");
            	}
            	else
            	{
            		cab.addGas(Double.parseDouble(numEntry.getText()));
                    resultLabelA.setText(Double.parseDouble(numEntry.getText()) + " gallons of gas added.");
        			resultLabelB.setText(" ");
        			resultLabelC.setText("is on");
                    numEntry.setText("0");
            	}
            }
        	
            //Show the available gas in the tank
            else if (e.getSource( ) == btnGasLeft)
            {
				Double availGas = cab.gasLeft();
				NumberFormat formatGas = NumberFormat.getNumberInstance();
				formatGas.setMaximumFractionDigits(1);
				String gasOut = formatGas.format(availGas);
            	resultLabelA.setText(gasOut + " Gallons Left in Tank");
    			resultLabelB.setText(" ");
    			resultLabelC.setText(" ");
    			numEntry.setText("0");
            }
        	
            //Show the miles traveled
            else if (e.getSource( ) == btnMiles)
            {
            	resultLabelA.setText(cab.getMiles() + " Total Miles Driven");
    			resultLabelB.setText(" ");
    			resultLabelC.setText(" ");
    			numEntry.setText("0");

            }
        	
            //Show the total amount of fares collected
            else if (e.getSource( ) == btnTotalRev)
            {
				Double totalFares = cab.calculateFaresEarned();
				NumberFormat formatFares = NumberFormat.getCurrencyInstance();
				String formatTotalFares = formatFares.format(totalFares);
            	resultLabelA.setText(formatTotalFares + " Total Fares Earned");
    			resultLabelB.setText(" ");
    			resultLabelC.setText(" ");
    			numEntry.setText("0");
            }
        	
            //Button used to create/instantiate the object
            else if (e.getSource( ) == btnCreate)
            {
            	cab = new Taxi(Double.parseDouble(numEntry.getText()), 0.0, 0.0);
    			btnCreate.setVisible(false);
    			btnStart.setVisible(true);
    			btnRecord.setVisible(true);
    			btnAddGas.setVisible(true);
    			panelDataEntry.setVisible(false);
    			resultLabelA.setText("Your taxi cab object has been created. Now");
    			resultLabelB.setText("you need to start your cab to begin");
    			resultLabelC.setText("accepting fares.");
    			numEntry.setText("0");
            }
        	
            //Button to start the vehicle
            else if (e.getSource( ) == btnStart)
            {
            	if (cab.gasLeft() > 0 && ops.isStarted() != true)
            	{
            		ops.setStarted(true);
        			btnStart.setVisible(false);
        			panelDataEntry.setVisible(true);
        			panelOperations.setVisible(false);
        			btnReports.setVisible(true);
        			resultLabelA.setText("Your cab is now ready for business. Enter");
        			resultLabelB.setText("either the miles of your trip or how many");						//this does not show. why???
        			resultLabelC.setText("gallons of gas you need to add to your tank.");					//this does not show. why???
        			numEntry.setText("0");
            	}
            	else if (cab.gasLeft() > 0 && ops.isStarted() == true)
            	{
            		ops.setStarted(true);
        			btnStart.setVisible(false);
        			panelDataEntry.setVisible(true);
        			panelOperations.setVisible(false);
        			btnReports.setVisible(true);
        			resultLabelA.setText("has gas and is already started");
        			resultLabelB.setText(" ");
        			resultLabelC.setText(" ");
        			numEntry.setText("0");
            	}
            	else
            	{
            		panelDataEntry.setVisible(true);
    				btnRecord.setVisible(false);
    				btnAddGas.setVisible(true);
        			panelOperations.setVisible(false);
            		resultLabelA.setText("You need to add gas before you can start");
        			resultLabelB.setText("your cab.");
        			resultLabelC.setText(" ");
        			numEntry.setText("0");
            	}            		
            }
        	
            //Button to repair the vehicle
            else if (e.getSource( ) == btnRepair)
            {
            	double REPAIR_COST = 25.00;
                ops.setMilesLastRepair(0.0);
                ops.setStarted(false);
                ops.deductRepairs(REPAIR_COST);																			//DOES NOT WORK
    			btnRepair.setVisible(false);
    			btnStart.setVisible(true);
            	resultLabelA.setText("Your cab has been repaired and $25 has been");
            	resultLabelB.setText("deducted from your total fares. You must ");
    			resultLabelC.setText("restart your cab before you can continue.");
    			numEntry.setText("0");
            }
        	
            //Show the Report Buttons
            else if (e.getSource( ) == btnReports)
            {
                panelReportView.setVisible(true);
                panelDataEntry.setVisible(false);
    			panelOperations.setVisible(false);
            	resultLabelA.setText(" ");
    			resultLabelB.setText(" ");
    			resultLabelC.setText(" ");
    			numEntry.setText("0");
            }
        	
            //Close the Reports View and show the Data Entry View
            else if (e.getSource( ) == btnCloseReports)
            {
            	if (ops.isStarted() != true)
            	{
	                panelReportView.setVisible(false);
	                panelDataEntry.setVisible(false);
	                panelOperations.setVisible(true);
	            	resultLabelA.setText("Your cab needs to be started before you.");
	    			resultLabelB.setText("can drive it.");
	    			resultLabelC.setText(" ");
	    			numEntry.setText("0");
            	}
            	else
            	{
            		panelReportView.setVisible(false);
                    panelDataEntry.setVisible(true);
                	resultLabelA.setText("Good to go");
        			resultLabelB.setText(" ");
        			resultLabelC.setText(" ");
        			numEntry.setText("0");
            	}
            }
        	
            //Show the miles traveled
            else if (e.getSource( ) == btnMilesForRepair)
            {
            	resultLabelA.setText(ops.getMilesLastRepair() + " Miles Since Last Repair");
    			resultLabelB.setText(" ");
    			resultLabelC.setText(" ");
    			numEntry.setText("0");
            }
        }
    }//END OF HANDLER CLASS
}//END OF PROGRAM APPLET CLASS

//****************************************************************************************
//*** The class below is for the taxi
//****************************************************************************************

class Taxi
{
	protected	double	tank		= 0.0;
	protected	double	miles		= 0.0;
	protected	double	fares		= 0.0;
	protected	double	MPG			= 17.8;
	protected	double	FARE_BASE	= 2.00;
	protected	double	FARE_ADD	= .585;
    static		int		numOfTrips	= 0;

//Constructors
    public Taxi()	//constructor with default values
    {
		this(0.0, 0.0, 0.0);
    }

    public Taxi(double tank, double miles, double fares)	//constructor to set values
    {
		this.tank = tank;		//	setTank(tank);
		this.miles = miles;		//	setMiles(miles);
		this.fares = fares;		//	setFares(fares);
    }

//Setters
    public void setTank(double tank)
    {
		this.tank = tank;
    }

    public void setMiles(double miles)
    {
		this.miles = miles;
		numOfTrips++;
    }

    public void setFares(double fares)
    {
		this.fares = fares;
    }

    public void addMiles(double m)
    {
        setMiles(getMiles()+m);
    }

    public void addGas(double g)
    {
        setTank(getTank()+g);
    }

//Getters
    public double getTank()
    {
		return tank;
    }

    public double getMiles()
    {
		return miles;
    }

    public double getFares()
    {
		return fares;
    }

    public double getTotalFaresEarned()
    {
        double faresTotal = 0.0;
        return faresTotal;
    }

//Calculations
    public double  calculateGasUsed()
    {
        double gasUsed = miles / MPG;
        return gasUsed;
    }

    public double gasLeft()
    {
        return getTank()-calculateGasUsed();
    }

    public double calculateFaresEarned()
    {
        double f=(getMiles()*FARE_ADD) + (numOfTrips* FARE_BASE);
        setFares(f);
        return f;
    }
}//END OF TAXI CLASS

//****************************************************************************************
//*** The class below is for the operation of the cab 
//****************************************************************************************

class Operation extends Taxi
{
	protected double milesLastRepair = 0.0;
	protected boolean started = false;
	
//Constructors
    public Operation()	//constructor with default values
    {
		this(0.0);
    }

    public Operation(double milesLastRepair)	//constructor to set values
    {
		this.milesLastRepair = milesLastRepair;		//	setmilesLastRepair(milesLastRepair);
    }

//Setters

	public void setMilesLastRepair(double milesLastRepair) 
	{
		this.milesLastRepair = milesLastRepair;
	}

    public void addMiles(double m)
    {
    	setMilesLastRepair(getMilesLastRepair()+m);
    }

	public void setStarted(boolean started) 
	{
		this.started = started;
	}

    public void deductRepairs(double r)
    {
    	setFares(getTotalFaresEarned()-r);																	//DOES NOT WORK
    }

//Getters
	public double getMilesLastRepair() 
	{
		return milesLastRepair;
	}

	public boolean isStarted() 
	{
		return started;
	}

}//END OF OPERATION CLASS

Attached image(s)

  • Attached Image


Is This A Good Question/Topic? 0
  • +

Replies To: if/esle if not working correctly, but a method not working.

#2 synlight  Icon User is offline

  • D.I.C Regular

Reputation: 44
  • View blog
  • Posts: 259
  • Joined: 14-September 11

Re: if/esle if not working correctly, but a method not working.

Posted 10 July 2012 - 06:21 AM

LOL we are in the same class. Stupid taxi.

Okay, what you are doing is light years ahead of what I'm doing. And I may be wayyy off base here. But why can;t you just deduct 25 from your fares variable instead of calling the get function, then deducting 25 from that?

It seems overly complicated?
Was This Post Helpful? 0
  • +
  • -

#3 brianborn1968  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 38
  • Joined: 15-November 11

Re: if/esle if not working correctly, but a method not working.

Posted 10 July 2012 - 06:28 AM

Mickie, it's not that I'm light years ahead of you, I'm just taking one class this semester and you are taking 4 classes. I have more time to experiment. This will change once fall semester starts.

Anyway, lets see what I can do with your suggestion.
Was This Post Helpful? 0
  • +
  • -

#4 CasiOo  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 994
  • View blog
  • Posts: 2,203
  • Joined: 05-April 11

Re: if/esle if not working correctly, but a method not working.

Posted 10 July 2012 - 06:40 AM

You do never add the gas to the cab here.
if(ops.isStarted() != true) {
	btnStart.setVisible(true);
	panelDataEntry.setVisible(false);
	panelOperations.setVisible(true);
	resultLabelA.setText(Double.parseDouble(numEntry.getText()) + " gallons of gas added.");
	resultLabelB.setText(" ");
	resultLabelC.setText("Now you need to start your cab.");
	numEntry.setText("0");
}



You can change your if statements from if(ops.isStarted() != true) to just if(!ops.isStarted())

I don't understand why you have an if-else there anyway, isn't it supposed to add the gas in both situations?

Also you could use a CardLayout to change the view so you don't have to set a lot of panels visible/invisible.
Was This Post Helpful? 1
  • +
  • -

#5 CasiOo  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 994
  • View blog
  • Posts: 2,203
  • Joined: 05-April 11

Re: if/esle if not working correctly, but a method not working.

Posted 10 July 2012 - 06:51 AM

Is this method yet to be implemented?
public double getTotalFaresEarned()
{
	double faresTotal = 0.0;
	return faresTotal;
}



Instead of writing your gasUsed as:
public double  calculateGasUsed()
{
	double gasUsed = miles / MPG;
	return gasUsed;
}



You could reduce it to one line
public double  calculateGasUsed()
{
	return miles / MPG;
}


Was This Post Helpful? 2
  • +
  • -

#6 brianborn1968  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 38
  • Joined: 15-November 11

Re: if/esle if not working correctly, but a method not working.

Posted 10 July 2012 - 06:52 AM

View PostCasiOo, on 10 July 2012 - 09:40 AM, said:

You do never add the gas to the cab here.

You can change your if statements from if(ops.isStarted() != true) to just if(!ops.isStarted())

I don't understand why you have an if-else there anyway, isn't it supposed to add the gas in both situations?

Also you could use a CardLayout to change the view so you don't have to set a lot of panels visible/invisible.


Thanks! I commented out the if-else and added the method to add gas and it work. I'm not ready to delete the if-else as I cannot remember why I added it and it might come to me later. Maybe it isn't needed and after all the testing, I can remove it before I submit it.

In class we have only covered FlowLayout and have experimented with GridLayout, but have decided to wait until the next assignment to change the layout. I have not looked in the CardLayout, but will definitely check it out.

Thanks again for the tips!
Was This Post Helpful? 0
  • +
  • -

#7 brianborn1968  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 38
  • Joined: 15-November 11

Re: if/esle if not working correctly, but a method not working.

Posted 10 July 2012 - 07:11 AM

View PostCasiOo, on 10 July 2012 - 09:51 AM, said:

Is this method yet to be implemented?
public double getTotalFaresEarned()
{
	double faresTotal = 0.0;
	return faresTotal;
}



You're awesome! Thanks again for helping with shortening the calculateGasUsed method and for spotting the getTotalFaresEarned method has not been implemented yet. This could be the reason why the $25 for the repairs were not deducted. I will work on that and see what happens.
Was This Post Helpful? 0
  • +
  • -

#8 brianborn1968  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 38
  • Joined: 15-November 11

Re: if/esle if not working correctly, but a method not working.

Posted 14 July 2012 - 07:30 AM

I'm starting to feel like an idiot as I cannot solve the issue of having the $25 deducted from the fares when repairs are made. I'm sure that it is something simple, but I just cannot seem to figure it out. Even with the suggestions already given.

If someone is able and willing to help with this problem, the entire program is available here, but the issue should be located on...

Lines 364-376 & 569-584

The above is the only issue that remains before I can submit this assignment which is due on Monday.

The following 2 questions are not important to this assignment; I'm just trying to find the better way.

1) Is there are better way to specify the width of the buttons so they are all the sames size? Lines 57-76

2) Which is a better way to use the constructor? Lines 452-454 & 549


import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.text.NumberFormat;
import javax.swing.border.TitledBorder;

public class BoydBAssignment4_ver1 extends JApplet
{
    private Taxi		cab;			//create the pointer
    private Operation	ops;			//create the pointer

    private JLabel		lblName      		= new JLabel("Enter gas quantity:");
    private JTextField	numEntry			= new JTextField("0", 6);
    private JLabel		resultLabelA		= new JLabel("Before you can begin, you must first create");
    private JLabel		resultLabelB		= new JLabel("your taxi cab object in the Operations View");
    private JLabel		resultLabelC		= new JLabel("below. You will need to add gas too.");
    private JButton		btnRecord			= new JButton("Record Trip (in miles)");
    private JButton		btnAddGas			= new JButton("Add Gas (in gallons)");
    private JButton		btnGasLeft			= new JButton("Gas Available");
    private JButton		btnMiles			= new JButton("Miles Traveled");
    private JButton		btnTotalRev			= new JButton("Total Fares Earned");
    private JButton		btnMilesForRepair	= new JButton("Miles For Repair");
    private JButton		btnCreate			= new JButton("Create");
    private JButton		btnStart			= new JButton("Start");
    private JButton		btnRepair			= new JButton("Repair");
    private JButton		btnReports			= new JButton("View Report Buttons");
    private JButton		btnCloseReports		= new JButton("Close Report View");
    private JPanel		panelMessages		= new JPanel();
    private JPanel		panelDataEntry		= new JPanel();
    private JPanel		panelReportView		= new JPanel();
    private JPanel		panelOperations		= new JPanel();
    private JPanel		panelData			= new JPanel();
    
    ButtonHandler 		b_handle;

    public void init()
    {
		//cab = new Taxi();				//moved to ActionEvent
		ops = new Operation();			//instantiate the object

        setSize(300,550);
    	Container box = getContentPane( );
        box.setLayout(new FlowLayout( ) );
        //box.setLayout(new GridLayout(0, 1));		//experimenting
        panelMessages.setBackground(Color.WHITE);	//experimenting
        numEntry.setBackground(Color.YELLOW);		//experimenting

        panelMessages.setLayout(new BoxLayout(panelMessages, BoxLayout.Y_AXIS));
        panelDataEntry.setLayout(new BoxLayout(panelDataEntry, BoxLayout.Y_AXIS));
        panelReportView.setLayout(new BoxLayout(panelReportView, BoxLayout.Y_AXIS));
        panelOperations.setLayout(new BoxLayout(panelOperations, BoxLayout.Y_AXIS));

        panelMessages.setPreferredSize(new Dimension( 280, 110 ));
        panelDataEntry.setPreferredSize(new Dimension( 200, 110 ));
        panelReportView.setPreferredSize(new Dimension( 200, 160 ));
        panelOperations.setPreferredSize(new Dimension( 200, 110 ));        											
        btnAddGas.setMinimumSize(new Dimension(190, 25));
        btnAddGas.setMaximumSize(new Dimension(190, 25));
        btnGasLeft.setMinimumSize(new Dimension(190, 25));
        btnGasLeft.setMaximumSize(new Dimension(190, 25));
        btnMiles.setMinimumSize(new Dimension(190, 25));
        btnMiles.setMaximumSize(new Dimension(190, 25));
        btnTotalRev.setMinimumSize(new Dimension(190, 25));
        btnTotalRev.setMaximumSize(new Dimension(190, 25));
        btnStart.setMinimumSize(new Dimension(190, 25));
        btnStart.setMaximumSize(new Dimension(190, 25));
        btnRepair.setMinimumSize(new Dimension(190, 25));	
        btnRepair.setMaximumSize(new Dimension(190, 25));	
        btnCreate.setMinimumSize(new Dimension(190, 25)); 
        btnCreate.setMaximumSize(new Dimension(200, 25)); 
        btnCloseReports.setMinimumSize(new Dimension(190, 25));
        btnCloseReports.setMaximumSize(new Dimension(190, 25));
        btnRecord.setMinimumSize(new Dimension(190, 25));
        btnRecord.setMaximumSize(new Dimension(190, 25));
        btnMilesForRepair.setMinimumSize(new Dimension(190, 25));
        btnMilesForRepair.setMaximumSize(new Dimension(190, 25));
        
        /*************************
        *** Panel for Messages ***
        *************************/
        box.add(panelMessages);

        Font fontPanel = new Font("Dialog", Font.PLAIN, 12);
        panelMessages.setBorder(
			BorderFactory.createTitledBorder(
				BorderFactory.createEtchedBorder(),
				"Messages",
				TitledBorder.DEFAULT_JUSTIFICATION,
				TitledBorder.DEFAULT_POSITION,
				fontPanel
				)
			);

        panelMessages.add(panelData);

        panelMessages.add(resultLabelA);
        panelMessages.add(resultLabelB);
        panelMessages.add(resultLabelC);
        panelMessages.add(btnReports);

        /***************************
        *** Panel for Data Entry ***
        ***************************/
        box.add(panelDataEntry);

        panelDataEntry.setBorder(
			BorderFactory.createTitledBorder(
				BorderFactory.createEtchedBorder(),
				"Data Entry",
				TitledBorder.DEFAULT_JUSTIFICATION,
				TitledBorder.DEFAULT_POSITION,
				fontPanel
				)
			);

        lblName.setAlignmentX(CENTER_ALIGNMENT);
        btnRecord.setAlignmentX(CENTER_ALIGNMENT);
        btnAddGas.setAlignmentX(CENTER_ALIGNMENT);

        panelDataEntry.add(panelData);

        panelData.add(lblName);
        panelData.add(numEntry);

        panelDataEntry.add(btnRecord);
        panelDataEntry.add(btnAddGas);

        /****************************
        *** Panel for Report View ***
        ****************************/
        box.add(panelReportView);

        panelReportView.setBorder(
			BorderFactory.createTitledBorder(
				BorderFactory.createEtchedBorder(),
				"Reports View",
				TitledBorder.DEFAULT_JUSTIFICATION,
				TitledBorder.DEFAULT_POSITION,
				fontPanel
				)
			);
        
        btnGasLeft.setAlignmentX(CENTER_ALIGNMENT);
        btnMiles.setAlignmentX(CENTER_ALIGNMENT);
        btnTotalRev.setAlignmentX(CENTER_ALIGNMENT);
        btnMilesForRepair.setAlignmentX(CENTER_ALIGNMENT);
        btnCloseReports.setAlignmentX(CENTER_ALIGNMENT);

        panelReportView.add(btnGasLeft);
        panelReportView.add(btnMiles);
        panelReportView.add(btnTotalRev);
        panelReportView.add(btnMilesForRepair);
        panelReportView.add(btnCloseReports);

        /********************************
        *** Panel for Operations View ***
        ********************************/
        box.add(panelOperations);

        panelOperations.setBorder(
			BorderFactory.createTitledBorder(
				BorderFactory.createEtchedBorder(),
				"Operations View",
				TitledBorder.DEFAULT_JUSTIFICATION,
				TitledBorder.DEFAULT_POSITION,
				fontPanel
				)
			);

        btnCreate.setAlignmentX(CENTER_ALIGNMENT);
        btnStart.setAlignmentX(CENTER_ALIGNMENT);
        btnRepair.setAlignmentX(CENTER_ALIGNMENT);

        panelOperations.add(btnCreate);
        panelOperations.add(btnStart);
        panelOperations.add(btnRepair);
        
        /********************************
        *** Separator                 ***
        ********************************/

        b_handle = new ButtonHandler( );

        btnRecord.addActionListener( b_handle);
        btnAddGas.addActionListener( b_handle);
        btnGasLeft.addActionListener( b_handle);
        btnMiles.addActionListener( b_handle);
        btnTotalRev.addActionListener( b_handle);
        btnCreate.addActionListener( b_handle);
        btnStart.addActionListener( b_handle);
        btnRepair.addActionListener( b_handle);
        btnReports.addActionListener( b_handle);
        btnCloseReports.addActionListener( b_handle);
        btnMilesForRepair.addActionListener( b_handle);

        panelDataEntry.setVisible(true);
        panelReportView.setVisible(false);
        btnRepair.setVisible(false);
        btnStart.setVisible(false);
        btnRecord.setVisible(false);
        btnAddGas.setVisible(false);
        btnReports.setVisible(false);
    }

//****************************************************************************************
//*** The class below is for the button handler
//****************************************************************************************

    private class ButtonHandler implements ActionListener
    {
        public void actionPerformed(ActionEvent e)
        {
        	if (e.getSource( ) == btnRecord)
            {
        		if (cab.gasLeft() > 0 && ops.getMilesLastRepair() < 100 && ops.isStarted()) 
        		{
        			double miles = Double.parseDouble(numEntry.getText());
        			cab.addFare(miles);
        			cab.addMiles(miles);
        			ops.addMiles(miles);
        			resultLabelA.setText(miles + " miles driven on this fare.");
        			resultLabelB.setText(" ");
        			resultLabelC.setText(" ");
        			numEntry.setText("0");
        		}
        		else if (ops.getMilesLastRepair() >= 100)
        		{
        			panelOperations.setVisible(true);
        			panelDataEntry.setVisible(false);
        			btnRepair.setVisible(true);
        			resultLabelA.setText("Maintenance needed, click the repair button.");
        			resultLabelB.setText(" ");
        			resultLabelC.setText(" ");
        			numEntry.setText("0");
        		}
        		else if (!ops.isStarted())
        		{
        			resultLabelA.setText("Click the start button first.");
        			resultLabelB.setText(" ");
        			resultLabelC.setText(" ");
        			numEntry.setText("0");
        		}
        		else if (cab.gasLeft() <= 0)
        		{
        			btnRecord.setVisible(false);
                    lblName.setText("Enter gas quantity:");
        			resultLabelA.setText("Gas tank is empty. Add gas.");
        			resultLabelB.setText(" ");
        			resultLabelC.setText(" ");
        			numEntry.setText("0");
        		}
            }

            //Add gas to the tank
            else if (e.getSource( ) == btnAddGas)
            {
				double gallons = Double.parseDouble(numEntry.getText());
    			btnStart.setVisible(true);
    			panelDataEntry.setVisible(false);
    			panelOperations.setVisible(true);
        		cab.addGas(gallons);
                resultLabelA.setText(gallons + " gallons of gas added.");
    			resultLabelB.setText(" ");
    			resultLabelC.setText("Now you need to start your cab.");
                numEntry.setText("0");
            }
        	
            //Show the available gas in the tank
            else if (e.getSource( ) == btnGasLeft)
            {
				Double availGas = cab.gasLeft();
				NumberFormat formatGas = NumberFormat.getNumberInstance();
				formatGas.setMaximumFractionDigits(1);
				String gasOut = formatGas.format(availGas);
            	resultLabelA.setText(gasOut + " Gallons Left in Tank");
    			resultLabelB.setText(" ");
    			resultLabelC.setText(" ");
    			numEntry.setText("0");
            }
        	
            //Show the miles traveled
            else if (e.getSource( ) == btnMiles)
            {
            	resultLabelA.setText(cab.getMiles() + " Total Miles Driven");
    			resultLabelB.setText(" ");
    			resultLabelC.setText(" ");
    			numEntry.setText("0");

            }
        	
            //Show the total amount of fares collected
            else if (e.getSource( ) == btnTotalRev)
            {
            	Double totalFares = cab.getFares();
//				Double totalFares = cab.calculateFaresEarned();
				NumberFormat formatFares = NumberFormat.getCurrencyInstance();
				String formatTotalFares = formatFares.format(totalFares);
            	resultLabelA.setText(formatTotalFares + " Total Fares Earned");
    			resultLabelB.setText(" ");
    			resultLabelC.setText(" ");
    			numEntry.setText("0");
            }
        	
            //Button used to create/instantiate the object
            else if (e.getSource( ) == btnCreate)
            {
            	cab = new Taxi(Double.parseDouble(numEntry.getText()), 0.0, 0.0);
    			btnCreate.setVisible(false);
    			btnStart.setVisible(true);
    			btnRecord.setVisible(true);
    			btnAddGas.setVisible(true);
    			panelDataEntry.setVisible(false);
    			resultLabelA.setText("Your taxi cab object has been created. Now");
    			resultLabelB.setText("you need to start your cab to begin");
    			resultLabelC.setText("accepting fares.");
    			numEntry.setText("0");
            }
        	
            //Button to start the vehicle
            else if (e.getSource( ) == btnStart)
            {
            	if (cab.gasLeft() > 0 && !ops.isStarted())
            	{
            		ops.setStarted(true);
        			btnStart.setVisible(false);
        			panelDataEntry.setVisible(true);
        			panelOperations.setVisible(false);
        			btnReports.setVisible(true);
        			btnRecord.setVisible(true);
                    lblName.setText("Enter miles or gas");
        			resultLabelA.setText("Your cab is now ready for business. Enter");
        			resultLabelB.setText("either the miles of your trip or how many");
        			resultLabelC.setText("gallons of gas you need to add to your tank.");
        			numEntry.setText("0");
            	}
            	else if (cab.gasLeft() > 0 && ops.isStarted())
            	{
            		ops.setStarted(true);
        			btnStart.setVisible(false);
        			panelDataEntry.setVisible(true);
        			panelOperations.setVisible(false);
        			btnReports.setVisible(true);
        			btnRecord.setVisible(true);
                    lblName.setText("Enter miles or gas");
        			resultLabelA.setText("Your cab is now ready for business. Enter");
        			resultLabelB.setText("either the miles of your trip or how many");
        			resultLabelC.setText("gallons of gas you need to add to your tank.");
        			numEntry.setText("0");
            	}
            	else
            	{
            		panelDataEntry.setVisible(true);
    				btnRecord.setVisible(false);
    				btnAddGas.setVisible(true);
        			panelOperations.setVisible(false);
            		resultLabelA.setText("You need to add gas before you can start");
        			resultLabelB.setText("your cab.");
        			resultLabelC.setText(" ");
        			numEntry.setText("0");
            	}            		
            }
        	
            //Button to repair the vehicle
            else if (e.getSource( ) == btnRepair)
            {
            	double REPAIR_COST = 25.00;
                ops.setMilesLastRepair(0.0);
                ops.setStarted(false);
//                ops.deductRepairs();
                ops.deductRepairs(REPAIR_COST);											//DOES NOT WORK
    			btnRepair.setVisible(false);
    			btnStart.setVisible(true);
            	resultLabelA.setText("Your cab has been repaired and $25 has been");
            	resultLabelB.setText("deducted from your total fares. You must ");
    			resultLabelC.setText("restart your cab before you can continue.");
    			numEntry.setText("0");
            }
        	
            //Show the Report Buttons
            else if (e.getSource( ) == btnReports)
            {
                panelReportView.setVisible(true);
                panelDataEntry.setVisible(false);
    			panelOperations.setVisible(false);
    			btnReports.setVisible(false);
            	resultLabelA.setText("Click one of the buttons in the Report View");
    			resultLabelB.setText("window below.");
    			resultLabelC.setText(" ");
    			numEntry.setText("0");
            }
        	
            //Close the Reports View and show the Data Entry View
            else if (e.getSource( ) == btnCloseReports)
            {
            	if (!ops.isStarted())
            	{
	                panelReportView.setVisible(false);
	                panelDataEntry.setVisible(false);
	                panelOperations.setVisible(true);
	            	resultLabelA.setText("Your cab needs to be started before you.");
	    			resultLabelB.setText("can drive it.");
	    			resultLabelC.setText(" ");
	    			numEntry.setText("0");
            	}
            	else	//cab is started
            	{
            		panelReportView.setVisible(false);
                    panelDataEntry.setVisible(true);
	                btnReports.setVisible(true);
        			resultLabelA.setText("Your cab is now ready for business. Enter");
        			resultLabelB.setText("either the miles of your trip or how many");
        			resultLabelC.setText("gallons of gas you need to add to your tank.");
        			numEntry.setText("0");
            	}
            }
        	
            //Show the miles traveled
            else if (e.getSource( ) == btnMilesForRepair)
            {
            	resultLabelA.setText(ops.getMilesLastRepair() + " Miles Since Last Repair");
    			resultLabelB.setText(" ");
    			resultLabelC.setText(" ");
    			numEntry.setText("0");
            }
        }
    }//END OF HANDLER CLASS
}//END OF PROGRAM APPLET CLASS

//****************************************************************************************
//*** The class below is for the taxi
//****************************************************************************************

class Taxi
{
	protected	double	tank		= 0.0;
	protected	double	miles		= 0.0;
	protected	double	fares		= 0.0;
//	protected	double	faresTotal	= 0.0;
	protected	double	MPG			= 17.8;
	protected	double	FARE_BASE	= 2.00;
	protected	double	FARE_ADD	= .585;
//	protected	int		numOfTrips	= 0;

//Constructors
    public Taxi()	//constructor with default values
    {
		this(0.0, 0.0, 0.0);
    }

    public Taxi(double tank, double miles, double fares)	//constructor to set values
    {
		this.tank  = tank;		//	setTank(tank);		??? which way is better ???
		this.miles = miles;		//	setMiles(miles);	??? which way is better ???
		this.fares = fares;		//	setFares(fares);	??? which way is better ???
    }

//Setters
    public void setTank(double tank)
    {
		this.tank = tank;
    }

    public void setMiles(double miles)
    {
		this.miles = miles;
//		numOfTrips++;						//REMOVED 7/14/12
    }

    public void setFares(double fares)
    {
		this.fares = fares;
    }

    public void addFare(double miles)		//ADDED 7/14/12
    {
		double addFare = 0.00;
		addFare = miles * .585 + 2;
		fares += addFare;
    }

    public void addMiles(double m)
    {
        setMiles(getMiles()+m);
    }

    public void addGas(double g)
    {
        setTank(getTank()+g);
    }

//Getters
    public double getTank()
    {
		return tank;
    }

    public double getMiles()
    {
		return miles;
    }

    public double getFares()
    {
		return fares;
    }

//    public double getTotalFaresEarned()	//REMOVED 7/14/12
//    {
//        return faresTotal;
//    }

//Calculations
    public double calculateGasUsed()
    {
    	return miles / MPG;
    }

    public double gasLeft()
    {
        return getTank()-calculateGasUsed();
    }

//    public double calculateFaresEarned()	//REMOVED 7/14/12
//    {
//        double f=(getMiles()*FARE_ADD) + (numOfTrips* FARE_BASE);
//        setFares(f);
//        return f;
//    }

}//END OF TAXI CLASS

//****************************************************************************************
//*** The class below is for the operation of the cab 
//****************************************************************************************

class Operation extends Taxi
{
	protected double milesLastRepair = 0.0;
	protected boolean started = false;
	
//Constructors
    public Operation()	//constructor with default values
    {
		this(0.0);
    }

    public Operation(double milesLastRepair)	//constructor to set values
    {
		this.milesLastRepair = milesLastRepair;		//	setmilesLastRepair(milesLastRepair);	??? which way is better ???
    }

//Setters

	public void setMilesLastRepair(double milesLastRepair) 
	{
		this.milesLastRepair = milesLastRepair;
	}

    public void addMiles(double m)
    {
    	setMilesLastRepair(getMilesLastRepair()+m);
    }

	public void setStarted(boolean started) 
	{
		this.started = started;
	}

//	public void deductRepairs()												//DOES NOT WORK
//	{
//		fares-=25;
//	}
	
//    public void deductRepairs()											//DOES NOT WORK
//    {
//             double r = 25.00;
//             setFares(getFares()-r);
//            //repairMiles = 0;
//    }

    public void deductRepairs(double r)										//DOES NOT WORK
    {
    	setFares(getFares()-r);
    }

//Getters
	public double getMilesLastRepair() 
	{
		return milesLastRepair;
	}

	public boolean isStarted() 
	{
		return started;
	}

}//END OF OPERATION CLASS


Was This Post Helpful? 0
  • +
  • -

#9 CasiOo  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 994
  • View blog
  • Posts: 2,203
  • Joined: 05-April 11

Re: if/esle if not working correctly, but a method not working.

Posted 14 July 2012 - 07:54 AM

I don't think anything is wrong with the repair part, maybe you dont add fares :P ?
Was This Post Helpful? 0
  • +
  • -

#10 brianborn1968  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 38
  • Joined: 15-November 11

Re: if/esle if not working correctly, but a method not working.

Posted 14 July 2012 - 08:00 AM

View PostCasiOo, on 14 July 2012 - 10:54 AM, said:

I don't think anything is wrong with the repair part, maybe you dont add fares :P ?


I'm not understanding. I have tested the program and the fares add up correctly. When I click the repair button after 100 miles, it does not deduct anything from the fares.

EDIT: Also I tried Mickie's suggestion "But why can't you just deduct 25 from your fares variable instead of calling the get function, then deducting 25 from that" but that didn't work either.

This post has been edited by brianborn1968: 14 July 2012 - 08:06 AM

Was This Post Helpful? 0
  • +
  • -

#11 brianborn1968  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 38
  • Joined: 15-November 11

Re: if/esle if not working correctly, but a method not working.

Posted 14 July 2012 - 08:14 AM

View PostCasiOo, on 14 July 2012 - 10:54 AM, said:

I don't think anything is wrong with the repair part, maybe you dont add fares :P ?


I'm trying to understand your reply and so I tried the following, but that doesn't work either. Were you trying to help or confuse me more?

	public void deductRepairs()
	{
		addFare(-25);
	}


Was This Post Helpful? 0
  • +
  • -

#12 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8019
  • View blog
  • Posts: 31,126
  • Joined: 06-March 08

Re: if/esle if not working correctly, but a method not working.

Posted 14 July 2012 - 08:17 AM

Effectively kind of a waste to create 10 Dimesnion objects if you only need 1

Dimension min = new Dimension(190, 25);
btnX.setMinimumSize(min);
btnY.setMinimumSize(min);
btnZ.setMinimumSize(min);

is a better approach and you have only one line of code to change if you decide to change the size of all your JButton
Was This Post Helpful? 1
  • +
  • -

#13 brianborn1968  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 38
  • Joined: 15-November 11

Re: if/esle if not working correctly, but a method not working.

Posted 14 July 2012 - 08:46 AM

View Postpbl, on 14 July 2012 - 11:17 AM, said:

Effectively kind of a waste to create 10 Dimesnion objects if you only need 1

Dimension min = new Dimension(190, 25);
btnX.setMinimumSize(min);
btnY.setMinimumSize(min);
btnZ.setMinimumSize(min);

is a better approach and you have only one line of code to change if you decide to change the size of all your JButton


This sounded great and would make this more efficient, but maybe I did something wrong because now all the buttons are of different sizes.

        Dimension min = new Dimension(190, 25);
        btnAddGas.setMinimumSize(min);
        btnGasLeft.setMinimumSize(min);
        btnMiles.setMinimumSize(min);
        btnTotalRev.setMinimumSize(min);
        btnStart.setMinimumSize(min);
        btnRepair.setMinimumSize(min);
        btnCreate.setMinimumSize(min);
        btnCloseReports.setMinimumSize(min);
        btnRecord.setMinimumSize(min);
        btnMilesForRepair.setMinimumSize(min);



I changed the code to the following and the buttons are the size I want them now. This doesn't cut down on the amount of code, but it sure will be helpful if I decide to change the size.

Thanks!

        Dimension size = new Dimension(190, 25);
        btnAddGas.setMinimumSize(size);
        btnAddGas.setMaximumSize(size);
        btnGasLeft.setMinimumSize(size);
        btnGasLeft.setMaximumSize(size);
        btnMiles.setMinimumSize(size);
        btnMiles.setMaximumSize(size);
        btnTotalRev.setMinimumSize(size);
        btnTotalRev.setMaximumSize(size);
        btnStart.setMinimumSize(size);
        btnStart.setMaximumSize(size);
        btnRepair.setMinimumSize(size);	
        btnRepair.setMaximumSize(size);	
        btnCreate.setMinimumSize(size); 
        btnCreate.setMaximumSize(size); 
        btnCloseReports.setMinimumSize(size);
        btnCloseReports.setMaximumSize(size);
        btnRecord.setMinimumSize(size);
        btnRecord.setMaximumSize(size);
        btnMilesForRepair.setMinimumSize(size);
        btnMilesForRepair.setMaximumSize(size);


Was This Post Helpful? 0
  • +
  • -

#14 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8019
  • View blog
  • Posts: 31,126
  • Joined: 06-March 08

Re: if/esle if not working correctly, but a method not working.

Posted 14 July 2012 - 09:06 AM

If your goal is to reduce code you can

    Dimension size = new Dimension(190, 25); 

    JButton btnA = createButton("This is first button");
    JButton btnB = createButton("This is second button");
  
...


    // creates my button
    private JButton createButton(String txt) {
       JButton btn = new JButton(txt);
       btn.setMinimumSize(size);
       btn.setMaximumSize(size);
       btn.addActionListener(bHandle);   // assuming it has been created
       btn.setAlignmen....
       return btn;
    }


Was This Post Helpful? 2
  • +
  • -

#15 brianborn1968  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 38
  • Joined: 15-November 11

Re: if/esle if not working correctly, but a method not working.

Posted 14 July 2012 - 09:25 AM

View Postpbl, on 14 July 2012 - 12:06 PM, said:

If your goal is to reduce code you can


This is the kind of info that I look for as this never occurred to me. This type of help opens up a lot of other possibilities. Not only can this be used for buttons, but modified for textfields, labels, panels, etc. I will just have to experiment. Thanks again for the tips.

BTW... would you happen to have any suggestions about the $25 deduction not working in my code? This is my main goal today because if that can be fixed, I can spend more time trying to improve on the rest of the code or even make it more realistic like not allowing the cab to operate with a negative amount of fuel or not having an unlimited size fuel tank.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2