You are on page 1of 3

@Override

public void actionPerformed(ActionEvent event)


{
Object source = event.getSource();
if(source == submitButton)
{
String message = "";
String name = null , licenseNo = null, phoneNo = null;
int invalid = 0;
try
{
name = validate("Name", nameInput.getText(), tru
e, 15);
}
catch(Exception ex)
{
message += "\n" + ex.getMessage();
invalid++;
}
try
{
licenseNo = validate("License Number", licenseNo
Input.getText(), true, 100);
}
catch(Exception ex)
{
message += "\n" + ex.getMessage();
invalid++;
}
try
{
phoneNo = validate("Phone Number", phoneNoInput.
getText(), true, 15);
}
catch(Exception ex)
{
message += "\n" + ex.getMessage();
invalid++;
}
if(invalid !=0)
{
if (invalid == 1)
JOptionPane.showMessageDialog(this,messa
ge.substring(3)+ "." , getTitle(), JOptionPane.WARNING_MESSAGE);
else
JOptionPane.showMessageDialog(this, "Please make
sure the following fields are correct:." +message, getTitle(), JOptionPane.WARN
ING_MESSAGE);
}
else
{
if(addOperation )
customer = new Customer();
customer.setName(name);
customer.setLicenseNo(licenseNo);
customer.setPhoneNo(phoneNo);
try(Facade facade = new Facade())
{
if(addOperation)
{
int add = facade.addCustomer(customer);
if(add !=0)
JOptionPane.showMessageDialog(th
is, "Successfully added a new customer.", getTitle(), JOptionPane.INFORMATION_ME
SSAGE);
else
JOptionPane.showMessageDialog(th
is, "Unable to add a new customer.", getTitle(), JOptionPane.WARNING_MESSAGE);
resetInput();
}
else
{
int update = facade.updateCustom
er(customer);
if(update !=0)
JOptionPane.showMessageD
ialog(this, "Successfully updated an existing new customer.", getTitle(), JOptio
nPane.INFORMATION_MESSAGE);
else
JOptionPane.showMessageD
ialog(this, "Unable to update an existing customer.", getTitle(), JOptionPane.WA
RNING_MESSAGE);
}
}
catch(SQLException ex)
{
JOptionPane.showMessageDialog(this, "Una
ble to save a customer." + ex.getMessage(), getTitle(), JOptionPane.ERROR_MESSAG
E);
}
}
}
else if(source == resetButton)
{
resetInput();
}
}
private void resetInput()
{
nameInput.grabFocus();
if(addOperation )
{
nameInput.setText("");
licenseNoInput.setText("");
phoneNoInput.setText("");
}
else
{
nameInput.setText(customer.getName());
licenseNoInput.setText(customer.getLicenseNo());
phoneNoInput.setText(customer.getPhoneNo());
}
}
}

You might also like