I am a making an app that calculates the future value of a given amount of money.
This is what my program looks like and I a trying to find out how I can validate the monthly payment, and yearly interest rate fields of my program. If the user clicks enter and does not input anything in monthly payment, interest rate, or both I would like the program to prompt the user that those are required fields.
This is the part of the code that outputs the data and process it. I have attempted to create a idValidData class that checks for valid data, but I don't know what to do with it.
public void actionPerformed(ActionEvent e)
{
Object source = e.getSource();
DefaultListModel futureValueListModel = new DefaultListModel();
if (source == exitButton)
{
System.exit(0);
}
else if (source == calculateButton)
{
try
{
double payment = Double.parseDouble(paymentTextField.getText());
double rate = Double.parseDouble(rateTextField.getText());
int years = (yearsComboBox.getSelectedIndex()) + 1;
NumberFormat currency = NumberFormat.getCurrencyInstance();
for(int i = 1; i <= years; i++)
{
double futureValue = FinancialCalculations.calculateFutureValue(payment, rate, i);
futureValueListModel.addElement("Year " + i + ": " + currency.format(futureValue));
}
futureValueList.setModel(futureValueListModel);
}
catch(Exception ee)
{
System.out.println("This program ended because you didn't enter any data."); //when the user doesn't input data and clicks enter, the program will throw this exception
}
}
}
//END OF ACTION PERFORMED METHOD
public boolean isValidData() //VALIDATES THE DATA
{
SwingValidator sv = new SwingValidator();
if (sv != null)
{
return sv.isPresent(paymentTextField, "Monthly Payment")
&& sv.isPresent(rateTextField, "Yearly Interest Rate");
}
else{
return sv.isPresent(rateTextField, "Yearly Interest Rate");
}
}
I also have a swing validator class that will return booleans when it checks for data entry, and data type. The code I have for it is:
import javax.swing.*;
import javax.swing.text.JTextComponent;
import java.math.BigDecimal;
public class SwingValidator
{
public static boolean isPresent(JTextComponent c, String title)
{
if (c.getText().length() == 0)
{
showMessage(c, title + " is a required field.\n"+ "Please re-enter.");
c.requestFocusInWindow();
return false;
}
return true;
}
public static boolean isInteger(JTextComponent c, String title)
{
try
{
int i = Integer.parseInt(c.getText());
return true;
}
catch (NumberFormatException e)
{
showMessage(c, title + " must be an integer.\n"+ "Please re-enter.");
c.requestFocusInWindow();
return false;
}
}
public static boolean isDouble(JTextComponent c, String title)
{
try
{
double d = Double.parseDouble(c.getText());
return true;
}
catch (NumberFormatException e)
{
showMessage(c, title + " must be a valid number number.\n"+ "Please re-enter.");
c.requestFocusInWindow();
return false;
}
}
private static void showMessage(JTextComponent c, String message)
{
JOptionPane.showMessageDialog(c, message, "Invalid Entry",JOptionPane.ERROR_MESSAGE);
}
}