Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

JAVA GUI program to calculate future investments. You will need to ask for an In

ID: 3572779 • Letter: J

Question

JAVA GUI program to calculate future investments.

You will need to ask for an Investment Amount, Number of Years, and the Annual Interest Rate expressed as a percentage. (3.55 = 3.55%). The user will input these via text fields.

There will be a Calculate button that when pressed will calculate the Future Value using this formula:

futureValue = investmentAmount * (1 + monthlyInterestRate)(years*12)

You will need to convert the Annual Interest Rate to be the decimal equivalent. (3.55 = 0.0355) Also note that the formula above calls for the monthly interest rate.

The Future Value will be displayed in a field on the form.

Error Checking

Inputs

Make sure all fields are numbers.

The Investment Amount can be between $1 and $100,000.

The Number of Years can be between 1 and 20.

The Annual Interest Rate can be between 1% and 12.5%.

All fields need to be correct when the Calculate button is pressed to determine Future Value.

Use a JOptionPane to tell the user what the error is.

The user will be able to change any of the fields and press Calculate.

The user should not be able to change the value in the Future Value field.

USE A TRY-CATCH BLOCK FOR THE CALCULATION

Explanation / Answer


import javax.swing.JOptionPane;


public class FutureValueCalculator extends javax.swing.JFrame {

/**
* Creates new form FutureValueCalculator
*/
public FutureValueCalculator() {
initComponents();
}
// Variables declaration
private javax.swing.JLabel NumOfYearLabel;
private javax.swing.JLabel annualIntRateLabel;
private javax.swing.JTextField annualIntRateText;
private javax.swing.JButton calculate;
private javax.swing.JLabel errorLabel;
private javax.swing.JLabel futureValueLabel;
private javax.swing.JTextField futureValueText;
private javax.swing.JLabel investmentAmountLabel;
private javax.swing.JTextField investmentAmountText;
private javax.swing.JTextField numOfYearsText;
// End of variables declaration
  
/**
* This method is called from within the constructor to initialize the form.
* .
*/
@SuppressWarnings("unchecked")
private void initComponents() {

NumOfYearLabel = new javax.swing.JLabel();
investmentAmountLabel = new javax.swing.JLabel();
annualIntRateLabel = new javax.swing.JLabel();
investmentAmountText = new javax.swing.JTextField();
numOfYearsText = new javax.swing.JTextField();
annualIntRateText = new javax.swing.JTextField();
futureValueLabel = new javax.swing.JLabel();
futureValueText = new javax.swing.JTextField();
calculate = new javax.swing.JButton();
errorLabel = new javax.swing.JLabel();

setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setTitle("Future Value Calculator");

NumOfYearLabel.setText("Number of Years : ");

investmentAmountLabel.setText("Investment Amount : ");

annualIntRateLabel.setText("Annual Interest Rate :");

futureValueLabel.setText("Future Value :");

futureValueText.setEditable(false);

calculate.setText("Calculate");
calculate.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
calculateActionPerformed(evt);
}
});

javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(60, 60, 60)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(futureValueLabel)
.addComponent(investmentAmountLabel)
.addComponent(annualIntRateLabel)
.addComponent(NumOfYearLabel))
.addGap(39, 39, 39)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(investmentAmountText)
.addComponent(numOfYearsText)
.addComponent(annualIntRateText)
.addComponent(futureValueText, javax.swing.GroupLayout.DEFAULT_SIZE, 115, Short.MAX_VALUE))
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addGap(23, 23, 23)
.addComponent(errorLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 248, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 27, Short.MAX_VALUE)
.addComponent(calculate)
.addGap(25, 25, 25))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(64, 64, 64)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(investmentAmountLabel)
.addComponent(investmentAmountText, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(18, 18, 18)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(NumOfYearLabel)
.addComponent(numOfYearsText, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(18, 18, 18)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(annualIntRateLabel)
.addComponent(annualIntRateText, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(47, 47, 47)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(futureValueLabel)
.addComponent(futureValueText, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 29, Short.MAX_VALUE)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(calculate)
.addComponent(errorLabel))
.addGap(21, 21, 21))
);

pack();
}   

private void calculateActionPerformed(java.awt.event.ActionEvent evt) {
errorLabel.setText("");
if(isValidationPass()){
double investmentAmount = 0;
int year = 0;
double annualInterestRate = 0;
try{
investmentAmount = Double.parseDouble(investmentAmountText.getText());
year = Integer.parseInt(numOfYearsText.getText());
annualInterestRate = Double.parseDouble(annualIntRateText.getText());
double monthlyInterestRate = annualInterestRate/100.00;
double futureValue = investmentAmount * (1 + monthlyInterestRate)*(year * 12);
futureValueText.setText(""+futureValue);
  
}catch(Exception e){
JOptionPane.showMessageDialog(rootPane, "something went wrong!!! ");   

}
}

  

}   

private boolean isValidationPass() {


if(numOfYearsText.getText().contains(".")){

JOptionPane.showMessageDialog(rootPane, "year should be in integer ");
return false;
}
double investmentAmount = 0;
int year = 0;
double annualInterestRate = 0;
try{
investmentAmount = Double.parseDouble(investmentAmountText.getText());
year = Integer.parseInt(numOfYearsText.getText());
annualInterestRate = Double.parseDouble(annualIntRateText.getText());
}catch(NumberFormatException e){
JOptionPane.showMessageDialog(rootPane, "Input should be in proper format ");   
investmentAmountText.setText("");
numOfYearsText.setText("");
annualIntRateText.setText("");
return false;
}
if(investmentAmount <1
|| investmentAmount > 100000 ){
JOptionPane.showMessageDialog(rootPane, "investment amount in between $1 to $100,000");   
investmentAmountText.setText("");
return false;

}
else if(year < 1 || year > 20){
JOptionPane.showMessageDialog(rootPane, "year in between 1 to 20");
numOfYearsText.setText("");
return false;
}
else if(annualInterestRate < 1 || annualInterestRate > 12.5){
JOptionPane.showMessageDialog(rootPane, "annual interest rate in between 1% to 12.5%");
annualIntRateText.setText("");
return false;
}
return true;
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {

try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(FutureValueCalculator.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(FutureValueCalculator.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(FutureValueCalculator.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(FutureValueCalculator.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
  
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new FutureValueCalculator().setVisible(true);
}
});
}

  

  
}