电话本的java程序代码 电话本的java程序代码怎么写( 四 )


addContact();
}
else if (e.getActionCommand().equals(SEARCH_BUTTON_DISPLAY)) {
searchContact();
}
else if (e.getActionCommand().equals(UPDATE_BUTTON_DISPLAY)) {
updatePhoneBook();
}
else if (e.getActionCommand().equals(REMOVE_BUTTON_DISPLAY)) {
removeContact();
}
}
private void addContact() {
String name = JOptionPane.showInputDialog(ASK_FOR_NAME);
if (name != null!name.trim().equals("")) {
String phone = JOptionPane.showInputDialog(ASK_FOR_PHONE);
if (phoneBook.add(name, phone)) {
JOptionPane.showMessageDialog(null, ADD_SUCCESSFUL);
}
else {
JOptionPane.showMessageDialog(null, ADD_FAILED_DUPLICATE);
}
display();
}
}
private void searchContact() {
int option = -1;
while (option == -1) {
option = JOptionPane.showOptionDialog(null, SEARCH_PROMPT, "",
JOptionPane.DEFAULT_OPTION,
JOptionPane.INFORMATION_MESSAGE,
null, SEARCH_OPTIONS, SEARCH_OPTIONS[0]);
}
if (option == 0) {
String name = JOptionPane.showInputDialog(ASK_FOR_NAME);
Contact c = phoneBook.getByName(name);
if (c != null) {
JOptionPane.showMessageDialog(null, phoneBook.getByName(name));
}
else {
JOptionPane.showMessageDialog(null, SEARCH_NOT_FOUND);
}
}
else {
String phone = JOptionPane.showInputDialog(ASK_FOR_PHONE);
Contact c = phoneBook.getByPhone(phone);
if (c != null!c.getPhone().equals(ContactImpl.EMPTY_PHONE)) {
JOptionPane.showMessageDialog(null, c);
}
else {
JOptionPane.showMessageDialog(null, SEARCH_NOT_FOUND);
}
}
}
private void updatePhoneBook() {
phoneBook.update();
display();
}
private void removeContact() {
String name = JOptionPane.showInputDialog(ASK_FOR_NAME);
if (name != null!name.trim().equals("")) {
int option = JOptionPane.showConfirmDialog(null, REMOVE_CONFIRM
+ name + "?", "Confirm", JOptionPane.YES_NO_OPTION);
if (option == 0) {
if (!phoneBook.remove(name)) {
JOptionPane.showMessageDialog(null, SEARCH_NOT_FOUND);
}
else {
JOptionPane.showMessageDialog(null, REMOVE_SUCCESSFUL);
}
}
}
display();
}
private void display() {
displayArea.selectAll();
displayArea.replaceSelection("");
for (Contact c : phoneBook.getPhoneBook()) {
displayArea.append(c.toString() + "\n");
}
}
private static void createAndShowGUI() {
JFrame mainFrame = new PhoneBookDemo();
mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
mainFrame.pack();
mainFrame.setVisible(true);
}
public static void main(String[] args) {
createAndShowGUI();
}
}
interface PhoneBookDemoConstants {
static final String ADD_BUTTON_DISPLAY = "Add";
static final String SEARCH_BUTTON_DISPLAY = "Search";
static final String UPDATE_BUTTON_DISPLAY = "Sort";
static final String REMOVE_BUTTON_DISPLAY = "Remove";
static final String ASK_FOR_NAME = "Please enter name of contact :";
static final String ASK_FOR_PHONE = "Please enter phone of contact :";
static final String ADD_SUCCESSFUL = "Contact has been added successfully.";
static final String ADD_FAILED_DUPLICATE = "Contact with same name has been dectected.";
static final String SEARCH_OPTIONS[] = {"By name", "By phone"};
static final String SEARCH_PROMPT = "Please Select a search method.";
static final String SEARCH_NOT_FOUND = "Contact is not found in your phone book.";
static final String REMOVE_CONFIRM = "Are you sure that you want to remove details of ";
static final String REMOVE_SUCCESSFUL = "Contact has been successfully removed.";
}
interface Contact {
public String getName();