java 电话本程序

2024-12-30 06:08:17
推荐回答(3个)
回答1:

只有最基本的功能,用户界面很粗糙,联系人明细也只有名字和一个电话,试着修改/增加一些功能以及配合数据库使用吧.

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextArea;

@SuppressWarnings("serial")
public class PhoneBookDemo extends JFrame implements ActionListener,
PhoneBookDemoConstants {
private PhoneBook phoneBook;
JTextArea displayArea;
public PhoneBookDemo () {
phoneBook = new PhoneBookImpl();
displayArea = new JTextArea(20, 40);
displayArea.setEditable(false);
BoxLayout layout = new BoxLayout(getContentPane(), BoxLayout.Y_AXIS);
JPanel buttonPanel = new JPanel();
JButton addButton = new JButton(ADD_BUTTON_DISPLAY);
JButton searchButton = new JButton(SEARCH_BUTTON_DISPLAY);
JButton updateButton = new JButton(UPDATE_BUTTON_DISPLAY);
JButton removeButton = new JButton(REMOVE_BUTTON_DISPLAY);
addButton.addActionListener(this);
searchButton.addActionListener(this);
updateButton.addActionListener(this);
removeButton.addActionListener(this);
buttonPanel.add(addButton);
buttonPanel.add(searchButton);
buttonPanel.add(updateButton);
buttonPanel.add(removeButton);
display();
add(displayArea);
add(buttonPanel);
setLayout(layout);
}

@Override
public void actionPerformed(ActionEvent e) {
if (e.getActionCommand().equals(ADD_BUTTON_DISPLAY)) {
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();
public void setName(String name);
public String getPhone();
public void setPhone(String phone);
public String toString();
}

interface PhoneBook {
public ArrayList getPhoneBook();
public boolean add(String name);
public boolean add(String name, String phone);
public Contact getByName(String name);
public Contact getByPhone(String phone);
public void update();
public boolean remove(String name);
boolean isExist(String name);
}

class ContactImpl implements Contact {
static final String EMPTY_PHONE = "Empty";
private String name;
private String phone;

public ContactImpl (String name) {
this.name = name;
this.phone = null;
}
public ContactImpl (String name, String phone) {
this.name = name;
this.phone = phone;
}
@Override
public String getName() {
return name;
}
@Override
public void setName(String name) {
this.name = name;
}
@Override
public String getPhone() {
return (phone == null ? EMPTY_PHONE : phone);
}
@Override
public void setPhone(String phone) {
this.phone = phone;
}
@Override
public String toString() {
return getName() + "\t\t: " + getPhone();
}
}

class PhoneBookImpl implements PhoneBook {
ArrayList phoneBook;
public PhoneBookImpl () {
phoneBook = new ArrayList();
}
@Override
public ArrayList getPhoneBook() {
return phoneBook;
}
@Override
public boolean add(String name) {
if (isExist(name)) {
return false;
}
else {
phoneBook.add(new ContactImpl(name));
return true;
}
}
@Override
public boolean add(String name, String phone) {
if (isExist(name)) {
return false;
}
else {
if (phone == null || phone.equals("") || phone.trim().equals("")) {
add(name);
}
else {
phoneBook.add(new ContactImpl(name, phone));
}
return true;
}
}
@Override
public Contact getByName(String name) {
for (Contact c : phoneBook) {
if (c.getName().equals(name)) {
return c;
}
}
return null;
}
@Override
public Contact getByPhone(String phone) {
for (Contact c : phoneBook) {
if (c.getPhone().equals(phone)) {
return c;
}
}
return null;
}
@Override
public boolean remove(String name) {
for (Contact c : phoneBook) {
if (c.getName().equals(name)) {
phoneBook.remove(c);
return true;
}
}
return false;
}
@Override
public void update() {
for (int i = 0; i < phoneBook.size() - 1; i++) {
for (int j = i + 1; j < phoneBook.size(); j++) {
if (phoneBook.get(i).getName().toLowerCase().compareTo(
phoneBook.get(j).getName().toLowerCase()) > 0) {
Contact c = phoneBook.get(i);
phoneBook.set(i, phoneBook.get(j));
phoneBook.set(j, c);
}
}
}
}
@Override
public boolean isExist(String name) {
for (Contact c : phoneBook) {
if (c.getName().equals(name)) {
return true;
}
}
return false;
}
}

回答2:

不知道你学过数据库没有,学过数据库和没学过写出的程序是不太一样的。
主要思想是在主界面上添加几个按钮,然后实现增删改查的操作,不是很难,蛋绝不是一句两句能说的清楚的,自己在好好看看书,好好琢磨琢磨。

回答3:

既然是小程序嘛~简简单单啦,O(∩_∩)O哈哈~ package testPackage; import java.io.BufferedReader; import java.io.IOException; import java.io.