import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import java.text.DecimalFormat;
public class BreakEven extends JApplet implements ActionListener
{
// initializing computation variables
private double newProfit, currentProfit, improvementCost, breakEven;
boolean validInputs;
// input radio buttons
JRadioButton[] timeUnits = new JRadioButton[4];
// input labels, textfields
JLabel lblNewProfit = new JLabel("Anticipated Profit: ");
JTextField txtNewProfit = new JTextField(7);
JLabel lblCurrentProfit = new JLabel("Current Profit: ");
JTextField txtCurrentProfit = new JTextField(7);
JLabel lblImprovementCost = new JLabel("The Cost of Improvement: ");
JTextField txtImprovementCost = new JTextField(7);
// input calculate button
JButton cmdCalculate = new JButton("Calculate Break Even");
// labels and textfields for time until break even
output
JLabel lblBreakEven = new JLabel("The Time Until Break Even: ");
JTextField txtBreakEven = new JTextField(8);
// textfields for output of time units
JTextField txtNewProfitTimeUnits = new JTextField(8);
JTextField txtCurrentProfitTimeUnits = new JTextField(8);
JTextField txtBreakEvenTimeUnits = new JTextField(8);
public void init( )
{
// instantiate the grid that will
coordinate the overall layout
JPanel panOverall = new JPanel();
GridBagLayout gblOverall = new GridBagLayout();
GridBagConstraints gbcOverall = new GridBagConstraints();
panOverall.setLayout(gblOverall);
// develop label and textbox for the cost of
improvement input
buildConstraints(gbcOverall, 0, 0, GridBagConstraints.NONE,
GridBagConstraints.EAST, 50, 100, 2);
panOverall.add(lblImprovementCost, gbcOverall);
buildConstraints(gbcOverall, 2, 0, GridBagConstraints.NONE,
GridBagConstraints.WEST, 50, 100, 2);
panOverall.add(txtImprovementCost, gbcOverall);
// develop the Radio Buttons
timeUnits[0] = new JRadioButton("years");
timeUnits[1] = new JRadioButton("quarters");
timeUnits[2] = new JRadioButton("months");
timeUnits[3] = new JRadioButton("weeks");
// defining the button group for the radio
buttons
ButtonGroup bgTimeUnits = new ButtonGroup();
// using a user defined class to handle radio
button events
RadioButtonHandler handler = new RadioButtonHandler();
// grouping, configuring and placing the radio
buttons into the GUI
for (int i = 0; i < timeUnits.length; i++)
{
bgTimeUnits.add(timeUnits[i]);
timeUnits[i].addItemListener(handler);
buildConstraints(gbcOverall, i, 1, GridBagConstraints.NONE,
GridBagConstraints.CENTER, 25, 100, 1);
panOverall.add(timeUnits[i], gbcOverall);
}
// develop label and textbox for current profit
input
buildConstraints(gbcOverall, 0, 2, GridBagConstraints.NONE,
GridBagConstraints.EAST, 50, 100, 2);
panOverall.add(lblCurrentProfit, gbcOverall);
buildConstraints(gbcOverall, 2, 2, GridBagConstraints.NONE,
GridBagConstraints.WEST, 25, 100, 1);
panOverall.add(txtCurrentProfit, gbcOverall);
buildConstraints(gbcOverall, 3, 2, GridBagConstraints.NONE,
GridBagConstraints.WEST, 25, 100, 1);
txtCurrentProfitTimeUnits.setEditable(false);
panOverall.add(txtCurrentProfitTimeUnits, gbcOverall);
// develop label and textbox for anticipated
profit input
buildConstraints(gbcOverall, 0, 3, GridBagConstraints.NONE,
GridBagConstraints.EAST, 50, 100, 2);
panOverall.add(lblNewProfit, gbcOverall);
buildConstraints(gbcOverall, 2, 3, GridBagConstraints.NONE,
GridBagConstraints.WEST, 25, 100, 1);
panOverall.add(txtNewProfit, gbcOverall);
buildConstraints(gbcOverall, 3, 3, GridBagConstraints.NONE,
GridBagConstraints.WEST, 25, 100, 1);
txtNewProfitTimeUnits.setEditable(false);
panOverall.add(txtNewProfitTimeUnits, gbcOverall);
// build the constraints for the command button
buildConstraints(gbcOverall, 0, 4, GridBagConstraints.NONE,
GridBagConstraints.CENTER, 100, 100, 4);
cmdCalculate.addActionListener(this);
panOverall.add(cmdCalculate, gbcOverall);
// develop label and textbox for Break Even
outputs
buildConstraints(gbcOverall, 0, 5, GridBagConstraints.NONE,
GridBagConstraints.EAST, 50, 100, 2);
panOverall.add(lblBreakEven, gbcOverall);
buildConstraints(gbcOverall, 2, 5, GridBagConstraints.NONE,
GridBagConstraints.WEST, 25, 100, 1);
txtBreakEven.setEditable(false);
panOverall.add(txtBreakEven, gbcOverall);
buildConstraints(gbcOverall, 3, 5, GridBagConstraints.NONE,
GridBagConstraints.WEST, 25, 100, 1);
txtBreakEvenTimeUnits.setEditable(false);
panOverall.add(txtBreakEvenTimeUnits, gbcOverall);
setContentPane(panOverall);
}
// a method to build the constraints for each cell
of a GridBagLayout
public void buildConstraints(GridBagConstraints gbc, int gx, int
gy, int fillStatus, int pos, int wx, int wy, int gw)
{
gbc.gridx = gx; // specify x location of
cell
gbc.gridy = gy; // specify y location of cell
gbc.fill = fillStatus; // specify whether or not
object should fill the cell
gbc.anchor = pos; // specify the object's
alignment in the cell
gbc.weightx = wx; // specify the weight
associated with the width of the cell
gbc.weighty = wy; // specify the weight
associated with the height of the cell
gbc.gridwidth = gw; // specify width of the cell
in the number of cells
}
// the actionEvent handler
public void actionPerformed(ActionEvent userClick)
{
if(validateInputs( ))
{
if (userClick.getSource( ) == cmdCalculate)
{
computeBreakEven(improvementCost, currentProfit, newProfit);
}
}
}
public void computeBreakEven( double cost, double currentP, double newP
)
{
breakEven = cost/(newP - currentP);
DecimalFormat twoDecimalPlaces = new DecimalFormat("0.00");
txtBreakEven.setText(twoDecimalPlaces.format(breakEven));
}
// a method to obtain inputs from the GUI
// and validate them
public boolean validateInputs( )
{
validInputs = true;
String nonNumericMessage = "";
// obtaining inputs from the GUI
// the try block is used to help insure the
// inputs are numeric of the appropriate types
try
{
improvementCost =
Double.parseDouble(txtImprovementCost.getText());
}
catch (NumberFormatException nfeCost)
{
nonNumericMessage = "You need to enter a number for the
improvement cost\n";
validInputs = false;
txtImprovementCost.setText("");
}
try
{
currentProfit = Double.parseDouble(txtCurrentProfit.getText());
}
catch (NumberFormatException nfeCurrent)
{
nonNumericMessage = nonNumericMessage + "You need to enter a
number for the current profit\n";
validInputs = false;
txtCurrentProfit.setText("");
}
try
{
newProfit = Double.parseDouble(txtNewProfit.getText());
}
catch (NumberFormatException nfeNew)
{
nonNumericMessage = nonNumericMessage + "You need to enter a
number for the anticipated profit\n";
validInputs = false;
txtNewProfit.setText("");
}
// this if statement displays an error message
associated with non-numeric inputs
if (!validInputs)
JOptionPane.showMessageDialog(null, nonNumericMessage, "Invalid
Inputs", JOptionPane.ERROR_MESSAGE);
return validInputs;
} // end method to validate the inputs
private class RadioButtonHandler implements ItemListener
{
public void itemStateChanged( ItemEvent event )
{
String timePeriod = "";
String timeRate = "";
if (event.getSource() == timeUnits[0])
{
timePeriod = "years";
timeRate = "per year";
}
else if (event.getSource() == timeUnits[1])
{
timePeriod = "quarters";
timeRate = "per quarter";
}
else if (event.getSource() == timeUnits[2])
{
timePeriod = "months";
timeRate = "per month";
}
else if (event.getSource() == timeUnits[3])
{
timePeriod = "weeks";
timeRate = "per week";
}
txtCurrentProfitTimeUnits.setText(timeRate);
txtNewProfitTimeUnits.setText(timeRate);
txtBreakEvenTimeUnits.setText(timePeriod);
}
} // ends radio button handler
} // ends the BreakEven class |