-3

The user inputs date of birth of an employee, but the task says you have to validate the entries.

What I want the application to display is an error when the user inputs day of birthdate as 33 or something as there is no such day in B.O.D.

Also, when the user clicks save, the data in the textfields should be saved into an arraylist. And when the user clicks print, the data that is saved in the arraylist should be displayed as previous information.

public class JavaGUI {

    ArrayList<JavaMethods> arl10 = new ArrayList<JavaMethods>();
    ArrayList<JavaMethods> arl11 = new ArrayList<JavaMethods>();

    JFrame frame;
    JPanel panel;

    JLabel labelA = new JLabel("Employee Information");
    JLabel labelB = new JLabel("Employee Birthdate");

    JLabel label1 = new JLabel("Employee Name:");
    JTextArea txt1 = new JTextArea("                ");
    JLabel label2 = new JLabel("Employee Address:");
    JTextArea txt2 = new JTextArea("                ");
    JLabel label3 = new JLabel("Employee Age:");
    JTextArea txt3 = new JTextArea("                ");
    JLabel label4 = new JLabel("Day:");
    JTextArea txt4 = new JTextArea("                ");
    JLabel label5 = new JLabel("Month:");
    JTextArea txt5 = new JTextArea("                ");
    JLabel label6 = new JLabel("Year:");
    JTextArea txt6 = new JTextArea("                ");

    JButton btn1 = new JButton("Clear");
    JButton btn2 = new JButton("Save");
    JButton btn3 = new JButton("Print");

    JRadioButton male = new JRadioButton("Male");
    JRadioButton female = new JRadioButton("Female");

    ButtonGroup g1 = new ButtonGroup();

    ActionListener act1 = new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {

            txt1.setText("                ");
            txt2.setText("                ");
            txt3.setText("                ");
            txt4.setText("                ");
            txt5.setText("                ");
            txt6.setText("                ");

        }

    };

    ActionListener act2 = new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            try {
                int day = Integer.parseInt(txt4.getText());
                if(day <= 32) {
            
            JOptionPane.showMessageDialog(frame, "Employee Information has been saved, Click PRINT to show them!");
            JavaMethods arl10 = new JavaMethods(txt1.getText().trim(), txt2.getText().trim(), male.getText(),Integer.parseInt(txt3.getText().trim()), Integer.parseInt(txt4.getText().trim()),Integer.parseInt(txt5.getText().trim()), Integer.parseInt(txt6.getText().trim()));
                
            arl11.add(arl10);
                }
                else {
                    System.out.println("wrong day");
                }
            }
            catch(NumberFormatException ex)
            {
                System.out.println("Exception : "+ex);
            }
        }
    };

    ActionListener act3 = new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {

            try {
    System.out.println(arl11);

                    JavaMethods arl10 = new JavaMethods(txt1.getText().trim(), txt2.getText().trim(), female.getText(),
                            Integer.parseInt(txt3.getText().trim()), Integer.parseInt(txt4.getText().trim()),
                            Integer.parseInt(txt5.getText().trim()), Integer.parseInt(txt6.getText().trim()));

                    arl11.add(arl10);

                    System.out.print("Previously Stored Information" + " " + arl11);
                    System.out.println();
                    System.out.println(arl10.toString());

                }

             catch (Exception e1) {
                System.out.println(
                        "Unable to perform the task, Please make sure you have entered the right DataType, Or make sure you have filled all the blanks.");

            }
        }

    };

    public JavaGUI() {
        frame = new JFrame();
        panel = new JPanel();

        frame.setPreferredSize(new Dimension(500, 500));
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        frame.setTitle("HR System");

        btn1.addActionListener(act1);
        btn2.addActionListener(act2);
        btn3.addActionListener(act3);

        panel.add(labelA);
        panel.add(label1);
        panel.add(txt1);
        panel.add(label2);
        panel.add(txt2);
        panel.add(label3);
        panel.add(txt3);
        panel.add(labelB);
        panel.add(label4);
        panel.add(txt4);
        panel.add(label5);
        panel.add(txt5);
        panel.add(label6);
        panel.add(txt6);

        g1.add(male);
        g1.add(female);

        panel.add(btn1);
        panel.add(btn2);
        panel.add(btn3);
        panel.add(male);
        panel.add(female);
        frame.add(panel);
        frame.pack();
        frame.setVisible(true);

    }

}
khelwood
  • 55,782
  • 14
  • 81
  • 108
javabeh
  • 1
  • 1
  • What exactly is the problem you face with the validation ? Can you please specify ? Thanks. – vivek Jan 07 '22 at 13:46
  • @vivek I want to stop the user from entering a wrong date of birth, by displaying an error in the console So the user is not allowed to enter day as 32, there is no such day of birth – javabeh Jan 07 '22 at 13:47
  • Then you should change the expression to < 32 instead of <= 32. – vivek Jan 07 '22 at 13:50
  • It's a string, I can't compare it with an Integer value – javabeh Jan 07 '22 at 13:52
  • 1
    But I see you are doing a parseInt so you will have the int value then. – vivek Jan 07 '22 at 13:56
  • the textfields accepts the int value, but comparing it in a if system will give the following error The operator <= is undefined for the argument type(s) String, int – javabeh Jan 07 '22 at 14:01
  • See my code here: https://www.jdoodle.com/iembed/v0/lEL – Abra Jan 07 '22 at 15:52
  • References for my code: [Laying Out Components Within a Container](https://docs.oracle.com/javase/tutorial/uiswing/layout/index.html) , [How to Use Spinners](https://docs.oracle.com/javase/tutorial/uiswing/components/spinner.html) , [Date Time](https://docs.oracle.com/javase/tutorial/datetime/index.html) , [Method References](https://docs.oracle.com/javase/tutorial/java/javaOO/methodreferences.html) , [Enum Types](https://docs.oracle.com/javase/tutorial/java/javaOO/enum.html) , [Record Classes](https://docs.oracle.com/en/java/javase/17/language/records.html) – Abra Jan 07 '22 at 16:00
  • More references: [Unicode Division Slash character](https://unicode-table.com/en/2215/) , [Convert java.util.Date to java.time.LocalDate](https://stackoverflow.com/questions/21242110/convert-java-util-date-to-java-time-localdate) – Abra Jan 07 '22 at 16:02

1 Answers1

1

Compare numbers

To compare numbers input as text, parse.

String dayInput = "42" ;
int day = Integer.parseInt( dayInput ) ;
if( day < 1 ) { … handle invalid input }
if( day > 31 ) { … handle invalid input }

And month.

String monthInput = "13" ;
int month = Integer.parseInt( monthInput ) ;
if( month < 1 ) { … handle invalid input }
if( month > 12 ) { … handle invalid input }

Validate a date

To validate a date, instantiate a LocalDate object while trapping for DateTimeParseException.

try
{
    LocalDate ld = LocalDate.of( year , month , day ) ;
}
catch ( DateTimeParseException e )
{ 
    … handle invalid input
}

Eventually, you may want to learn about Jakarta Bean Validation to help with such work.

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154