protected JButton createSaveButton() { JButton button = new JButton("Save"); button.addActionListener( actionEvent -> { if (actionEvent.getSource() == saveButton) { try { AddViewController addController = (AddViewController) controller; User user = new User(); String fullName = fullNameField.getText(); String age = ageField.getText(); String email = emailField.getText(); String phone = phoneNumberField.getText(); boolean fullNameIsCorrect = false; boolean phoneIsCorrect = false; boolean emailIsCorrect = false; boolean ageIsCorrect = false; try { user.setFullName(checkFullName(fullNameField.getText())); fullNameIsCorrect = true; } catch (IllegalArgumentException e) { fullNameField.setText("Must be not empty"); } try { user.setAge(checkAge(ageField.getText())); ageIsCorrect = true; } catch (IllegalArgumentException e) { ageField.setText("Incorrect age, must be 0-100"); } try { user.setEmail(checkEmail(emailField.getText())); emailIsCorrect = true; } catch (IllegalArgumentException e) { emailField.setText("Incorrect email"); } try { user.setPhoneNumber(checkPhoneNumber(phoneNumberField.getText())); phoneIsCorrect = true; } catch (IllegalArgumentException e) { phoneNumberField.setText("Phone must be XXXXXXXXXX"); } if (!(fullNameIsCorrect && ageIsCorrect && phoneIsCorrect && emailIsCorrect)) return; user.setFullName(fullName); user.setAge(Integer.parseInt(age)); user.setEmail(email); user.setPhoneNumber(phone); addController.save(user); BaseView.updateUI(model.getAllSorted()); addController.cancel(); clearTextFields(); } catch (StorageException e) { JOptionPane.showMessageDialog(frame, e.getMessage()); } } }); return button; }
protected String checkEmail(String email) throws IllegalArgumentException { User user = new User(); user.setEmail(email); return user.getEmail(); }