samedi 28 mars 2015

Using an ActionListener to check to a specific instance from and then add to an ArrayList



I have created an GUI that accepts the following fields (name, street, city, state, zip, account number). In addition you are able to choose between 3 customer Types from a panel and finally a course from a drop down course ComboBox.


When the user types in the above information and clicks “SUBMIT”, the program will create the customer if they don’t exist and put them on the customer list, find the selected course on the course list and ask the user if they wish to enroll in the course with a confirmation pop up dialog. If the user selects YES, the course is added to that customer’s course list, the fields are cleared, the cursor is placed in the name field, and another entry is possible. If the customer already exits, a new customer is not created, but the course is added to that customer’s course list. When the user clicks “FINISH”, the program writes to the prompt each customer and their course list, displays the dialog box of invoices, then exits. Anonymous actionListeners are used. This is as far as I have got.



submitButton.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent ae)
{
if (confirm())
{
customerList.add(new Customer(nameField.getText(),new Address(streetField.getText(), cityField.getText(), stateField.getText(), Integer.parseInt(zipField.getText())), Integer.parseInt(accountNumberField.getText())));
}

clearFields();
}
});

finishButton.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent ae)
{
for(Customer c: customerList)
System.out.println(c.toString());

System.exit(0);
}
});

add(nameLabel);
add(nameField);
add(streetLabel);
add(streetField);
add(cityLabel);
add(cityField);
add(stateLabel);
add(stateField);
add(zipLabel);
add(zipField);
add(accountNumberLabel);
add(accountNumberField);

add(customerLabel);
add(myPanel);

add(courseLabel);
add(courseBox);

add(submitLabel);
add(submitButton);

add(finishLabel);
add(finishButton);
}

public static void main(String args[])
{
CourseGUI g = new CourseGUI();

g.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

g.setLocationRelativeTo(null);

g.setSize(650,350);

g.setVisible(true);
}

public void clearFields()
{
nameField.setText("");
streetField.setText("");
cityField.setText("");
stateField.setText("");
zipField.setText("");
accountNumberField.setText("");
myGroup.clearSelection();
courseBox.setSelectedIndex(0);

nameField.requestFocus();
}

public String getCourse()
{
return courses[courseBox.getSelectedIndex()];
}

public String getClassification()
{
if (studentButton.isSelected())
return studentButton.getText();
else if (facultyButton.isSelected())
return facultyButton.getText();
else
return governmentButton.getText();
}

public boolean confirm()
{
JFrame frame = new JFrame();

int result;

String message;

message = String.format("%s %s %s %s %s %d %d", nameField.getText(), streetField.getText(), cityField.getText(), stateField.getText(), zipField.getText(), accountNumberField.getText());

result = JOptionPane.showConfirmDialog(frame, message);

if (result == JOptionPane.YES_OPTION)
return true;
return false;
}


What I need help with is the coding inside my submitButton. addActionListener. How would I check to see if a customer already exist and if not then add them to the customerList. Assume all other classes and arrayList needed are already created.




Aucun commentaire:

Enregistrer un commentaire