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

New Topic/Question
Reply




MultiQuote





|