This from Murach exercise 16-1 on Page 534 Number of Years text field replaced w
ID: 3844868 • Letter: T
Question
This from Murach exercise 16-1 on Page 534
Number of Years text field replaced with combobox
Future Value text field replaced with a list having a vertical scrollbar
Calculate button event listener calculates the future values for each year selected in the combobox
Calculate button event listener displays the calculated the future values for each year selected in the list
_____________________
These are the Forms gives
murach.business>FinancialCalculations.java
package murach.business;
public class FinancialCalculations
{
public static final int MONTHS_IN_YEAR = 12;
public static double calculateFutureValue(double monthlyPayment,
double yearlyInterestRate, int years)
{
int months = years * MONTHS_IN_YEAR;
double monthlyInterestRate = yearlyInterestRate/MONTHS_IN_YEAR/100;
double futureValue = 0;
for (int i = 1; i <= months; i++)
{
futureValue = (futureValue + monthlyPayment) *
(1 + monthlyInterestRate);
}
return futureValue;
}
}
murach.ui>SwingValidator.java
package murach.ui;
import javax.swing.*;
import javax.swing.text.JTextComponent;
public class SwingValidator
{
public boolean isPresent(JTextComponent c, String fieldName)
{
if (c.getText().length() == 0)
{
showMessage(c, fieldName + " is a required field.");
c.requestFocusInWindow();
return false;
}
return true;
}
public boolean isInteger(JTextComponent c, String fieldName)
{
try
{
int i = Integer.parseInt(c.getText());
return true;
}
catch (NumberFormatException e)
{
showMessage(c, fieldName + " must be an integer.");
c.requestFocusInWindow();
return false;
}
}
public boolean isDouble(JTextComponent c, String fieldName)
{
try
{
double d = Double.parseDouble(c.getText());
return true;
}
catch (NumberFormatException e)
{
showMessage(c, fieldName + " must be a valid number.");
c.requestFocusInWindow();
return false;
}
}
private void showMessage(JTextComponent c, String message)
{
JOptionPane.showMessageDialog(c, message, "Invalid Entry",
JOptionPane.ERROR_MESSAGE);
}
}
____________________________
Thank you.
Explanation / Answer
murach.business > FinancialCalculations.java
package murach.ui;
import java.text.NumberFormat;
import murach.business.FinancialCalculations;
public class FutureValueFrame extends javax.swing.JFrame {
/** Creates new form FutureValueFrame */
public FutureValueFrame() {
initComponents();
}
/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jLabel1 = new javax.swing.JLabel();
jLabel2 = new javax.swing.JLabel();
jLabel3 = new javax.swing.JLabel();
jLabel4 = new javax.swing.JLabel();
monthlyPaymentTextField = new javax.swing.JTextField();
yearlyRateTextField = new javax.swing.JTextField();
calculateButton = new javax.swing.JButton();
exitButton = new javax.swing.JButton();
YearsComboBox = new javax.swing.JComboBox();
jScrollPane1 = new javax.swing.JScrollPane();
FutureValueList = new javax.swing.JList();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setTitle("Future Value Calculator");
setResizable(false);
jLabel1.setText("Monthly Payment:");
jLabel2.setText("Yearly Interest Rate:");
jLabel3.setText("Number of Years:");
jLabel4.setText("Future Value:");
calculateButton.setMnemonic('c');
calculateButton.setText("Calculate");
calculateButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
calculateButtonActionPerformed(evt);
}
});
exitButton.setMnemonic('x');
exitButton.setText("Exit");
exitButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
exitButtonActionPerformed(evt);
}
});
YearsComboBox.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20" }));
YearsComboBox.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
YearsComboBoxActionPerformed(evt);
}
});
FutureValueList.setModel(new javax.swing.AbstractListModel() {
String[] strings = { "Year 1", "Year 2", "Year 3", "Year 4", "Year 5" };
public int getSize() { return strings.length; }
public Object getElementAt(int i) { return strings[i]; }
});
jScrollPane1.setViewportView(FutureValueList);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jLabel1)
.addComponent(jLabel2)
.addComponent(jLabel3)
.addComponent(jLabel4))
.addGap(16, 16, 16)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(yearlyRateTextField)
.addComponent(monthlyPaymentTextField, javax.swing.GroupLayout.DEFAULT_SIZE, 115, Short.MAX_VALUE)
.addComponent(YearsComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, 67, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(layout.createSequentialGroup()
.addGap(6, 6, 6)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(calculateButton, javax.swing.GroupLayout.PREFERRED_SIZE, 90, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 26, Short.MAX_VALUE)
.addComponent(exitButton, javax.swing.GroupLayout.PREFERRED_SIZE, 65, javax.swing.GroupLayout.PREFERRED_SIZE))
.addComponent(jScrollPane1))))
.addGap(36, 36, 36))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel1)
.addComponent(monthlyPaymentTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel2)
.addComponent(yearlyRateTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel3)
.addComponent(YearsComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jLabel4)
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 56, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(18, 18, 18)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(calculateButton)
.addComponent(exitButton))
.addContainerGap(100, Short.MAX_VALUE))
);
pack();
}// </editor-fold>
private void calculateButtonActionPerformed(java.awt.event.ActionEvent evt) {
if (isValidData())
{
double p = Double.parseDouble(monthlyPaymentTextField.getText());
double r = Double.parseDouble(yearlyRateTextField.getText());
int y = Integer.parseInt(YearsComboBox.getItemCount());
double fv = FinancialCalculations.calculateFutureValue(p, r, y);
NumberFormat currency = NumberFormat.getCurrencyInstance();
FutureValueList.setText(currency.format(fv));
}
}
private void exitButtonActionPerformed(java.awt.event.ActionEvent evt) {
System.exit(0);
}
private void YearsComboBoxActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
int y = YearsComboBox.getSelectedIndex();
}
private boolean isValidData()
{
SwingValidator sv = new SwingValidator();
return
sv.isPresent(monthlyPaymentTextField, "Monthly Investment") &&
sv.isDouble(monthlyPaymentTextField, "Monthly Investment") &&
sv.isPresent(yearlyRateTextField, "Interest Rate") &&
sv.isDouble(yearlyRateTextField, "Interest Rate") &&
sv.isPresent(YearsComboBox, "Number of Years") &&
sv.isInteger(YearsComboBox, "Number of Years");
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
// new FutureValueFrame().setVisible(true); // generated code
FutureValueFrame frame = new FutureValueFrame();
frame.setVisible(true);
frame.setLocationRelativeTo(null);
}
});
}
// Variables declaration - do not modify
private javax.swing.JList FutureValueList;
public javax.swing.JComboBox YearsComboBox;
private javax.swing.JButton calculateButton;
private javax.swing.JButton exitButton;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JLabel jLabel4;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JTextField monthlyPaymentTextField;
private javax.swing.JTextField yearlyRateTextField;
// End of variables declaration
}
swingvalidator.java:
package murach.ui;
import javax.swing.*;
import javax.swing.text.JTextComponent;
public class SwingValidator
{
public boolean isPresent(JTextComponent c, String fieldName)
{
if (c.getText().length() == 0)
{
showMessage(c, fieldName + " is a required field.");
c.requestFocusInWindow();
return false;
}
return true;
}
public boolean isInteger(JTextComponent c, String fieldName)
{
try
{
int i = Integer.parseInt(c.getText());
return true;
}
catch (NumberFormatException e)
{
showMessage(c, fieldName + " must be an integer.");
c.requestFocusInWindow();
return false;
}
}
public boolean isDouble(JTextComponent c, String fieldName)
{
try
{
double d = Double.parseDouble(c.getText());
return true;
}
catch (NumberFormatException e)
{
showMessage(c, fieldName + " must be a valid number.");
c.requestFocusInWindow();
return false;
}
}
private void showMessage(JTextComponent c, String message)
{
JOptionPane.showMessageDialog(c, message, "Invalid Entry",
JOptionPane.ERROR_MESSAGE);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.