个人通讯录管理系统
问题描述:
题目描述:设计一个个人通讯录管理系统,以图形化界面及菜单方式工作。
功能需求
1) 设计一个个人信息类,类中包含字段序号、姓名、手机号码、地址和邮箱等。
2) 可以添加、删除和修改个人信息
3) 根据姓名或地址可以查找出相关以信息,如查找不到也提示“查无此人。”(要求可以实现精确查找和模糊查找)
4) 使用外部文件或数据库存储相关数据。
类的设计
设计八个类:一个通讯类(tongxun)、一个主方法(Test)、一个查询类(Select)、一个新增类(Insert)、一个展示类(Display)、一个修改类(dfds)、一个删除类(Delete)、一个复制类(Copy)。
主方法(Test):程序主方法。
通讯类(tongxun):这一个类是功能面板,通过选择不同按钮来实现功能。
查询类(Select):查询联系人。
新增类(Insert):新增加联系人。
展示类(Display):展示所有联系人的信息。
修改类(dfds):修改特定联系人信息。
删除类(Delete):删除特定联系人。
复制类(Copy):备份。
主类则为运行程序的入口,调用通讯类,完成程序功能
开发环境:
Eclipse
解题思路:
这题旨在通过判断成绩的分数段得到相应的等级,主要考察知识点1为如何从控制台接收一个数字型的数据,2是利用选择结构的if…else if语句来对成绩进行分段。最后一点是一定不要忘了对输入的错误的处理(数字型且分数的范围限制。)
程序设计:
1.主方法
package tongxunlu;
public class Test {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
tongxun run = new tongxun ();
}
}
2.通讯录
public class tongxun extends JFrame implements ActionListener {
private TextField text_1; //单行文本输入框 (姓名)
private TextField text_2; //单行文本输入框 (电话)
private RandomAccessFile raf; //""""""获取对象文件 raf """""""""
public tongxun() {
JFrame mainFrame = new JFrame("通讯录");
mainFrame.setLocation(800, 600);
mainFrame.setSize(250, 220);
mainFrame.setLayout(null);
Button bt1 = new Button("新增联系人");
mainFrame.add(bt1);
bt1.setLocation(10, 30);
bt1.setSize(80, 25);
bt1.addActionListener(this);
Button bt2 = new Button("删除联系人");
mainFrame.add(bt2);
bt2.setLocation(120, 30);
bt2.setSize(80, 25);
bt2.addActionListener(this);
Button bt3 = new Button("显示所有记录");
mainFrame.add(bt3);
bt3.setLocation(120, 65);
bt3.setSize(80, 25);
bt3.addActionListener(this);
Button bt4 = new Button("查询个人信息");
mainFrame.add(bt4);
bt4.setLocation(10, 65);
bt4.setSize(80, 25);
bt4.addActionListener(this);
Button bt5 = new Button("文件备份");
mainFrame.add(bt5);
bt5.setLocation(120, 100);
bt5.setSize(80, 25);
bt5.addActionListener(this);
Button bt6 = new Button("退出");
mainFrame.add(bt6);
bt6.setLocation(65, 135);
bt6.setSize(80, 25);
bt6.addActionListener(this);
Button bt13 = new Button("修改联系人");
mainFrame.add(bt13);
bt13.setLocation(10, 100);
bt13.setSize(80, 25);
bt13.addActionListener(this);
mainFrame.setVisible(true);
}
public void actionPerformed(ActionEvent e) {
String bt = e.getActionCommand();
if (bt.equals("新增联系人")) {
Insert m = new Insert();
}
if (bt.equals("删除联系人")) {
Delete n = new Delete();
}
if (bt.equals("查询个人信息")) {
Select a = new Select();
}
if (bt.equals("显示所有记录")) {
Display b = new Display();
}
if(bt.equals("修改联系人")){
dfds c = new dfds();
}
if (bt.equals("文件备份")) {
Copy v = new Copy();
}
if (bt.equals("退出")) {
System.exit(0);
}
}
}
3.查询类
class Select extends JFrame implements ActionListener {
public String s1,s2,s3, s4;
public TextField text_1;
public TextField text_2;
public TextField text_3;
public TextField text_4;
public TextField text_5;
Select() {
setTitle("查询个人信息");
setSize(400,300);
setLocation(600, 400);
setLayout(new GridLayout(6, 2));
text_1 = new TextField();
text_2 = new TextField();
text_3 = new TextField();
text_4 = new TextField();
text_5 = new TextField();
Label lab_1 = new Label("请输入要查找人的姓名:");
Label lab_2 = new Label("该联系人手机号码是:");
Label lab_3 = new Label("该联系人地址是:");
Label lab_4 = new Label("该联系人邮箱是:");
Label lab_5 = new Label("备注:");
Button bt11 = new Button("确定");
Button bt12 = new Button("清空");
bt11.addActionListener(this);
bt12.addActionListener(this);
add(lab_1);
add(text_1);
add(lab_2);
add(text_2);
add(lab_3);
add(text_3);
add(lab_4);
add(text_4);
add(lab_5);
add(text_5);
add(bt11);
add(bt12);
setVisible(true);
}
public void actionPerformed(ActionEvent e) {
String bt = e.getActionCommand();
if (bt.equals("确定")) {
String fileName = "E:/Contacts.txt";
String line;
try {
BufferedReader in = new BufferedReader(new FileReader(fileName));
line = in.readLine();
while (line != null) {
if (line.equals(text_1.getText())) {
s1 = line;
s2 = in.readLine();
s3 = in.readLine();
s4 = in.readLine();
text_1.setText("" + s1);
text_2.setText("" + s2);
text_3.setText("" + s3);
text_4.setText("" + s4);
text_5.setText("" + "无");
}
line = in.readLine();
}
if(text_5.getText().equals("") ){
text_5.setText("" + "查无此人");
}
in.close();
} catch (IOException d) {
System.out.println(d);
}
}
if (bt.equals("清空")) {
text_1.setText("");
text_2.setText("");
text_3.setText("");
text_4.setText("");
text_5.setText("");
}
}
}
Insert(){
setTitle("新增联系人");
setSize(300,300);
setLocation(600,400);
setLayout(new GridLayout(6,2));
text_1=new TextField();
text_2=new TextField();
text_3=new TextField();
text_4=new TextField();
text_5=new TextField();
Label lab_1=new Label("请输入联系人序号:");
Label lab_2=new Label("请输入联系人姓名:");
Label lab_3=new Label("请输入联系人手机号码 :");
Label lab_4=new Label("请输入联系人地址:");
Label lab_5=new Label("请输入联系人邮箱:");
Button bt7=new Button("确定");
Button bt8=new Button("清空");
bt7.addActionListener(this);
bt8.addActionListener(this);
add(lab_1);add(text_1);
add(lab_2);add(text_2);
add(lab_3);add(text_3);
add(lab_4);add(text_4);
add(lab_5);add(text_5);
add(bt7); add(bt8);
setVisible(true);
}
public void actionPerformed(ActionEvent e){
String bt=e.getActionCommand();
if(bt.equals("确定")){try{
FileWriter raf=new FileWriter("E:/Contacts.txt",true);
String s1=text_1.getText();
String s2=text_2.getText();
String s3=text_3.getText();
raf.close();
}
catch(IOException d){
System.out.println(d)
}
}
5.展示类
Display() {
setTitle("显示所有记录");
setSize(800, 300);
setLocation(600, 400);
setVisible(true);
}
public void paint(Graphics g) {
super.paint(g);
String fileName = "E:/Contacts.txt";
String line;
int i;
i = 0;
try {
BufferedReader in = new BufferedReader(new FileReader(fileName));
line = in.readLine();
while (line != null) {
i = i + 1;
g.drawString("序号:" + line, 10, 40 * i);
g.drawString("姓名:" + in.readLine(), 90, 40 * i);
g.drawString("电话:" + in.readLine(), 185, 40 * i);
g.drawString("地址:" + in.readLine(), 315, 40 * i);
g.drawString("邮箱:" + in.readLine(), 415, 40 * i);
line = in.readLine();
}
in.close();
} catch (IOException d) {
System.out.println(d);
}
}
}
6.修改类
dfds() {
public void actionPerformed(ActionEvent e) {
String fileName = "E:/Contacts.txt";
String bt = e.getActionCommand();
if (bt.equals("显示该序号联系人信息")) {
try{
BufferedReader in = new BufferedReader(new FileReader(fileName));
String line;
line = in.readLine();
while (line != null) {
if (line.equals(text_1.getText())) {
s1 = line;
s2 = in.readLine();
s3 = in.readLine();
s4 = in.readLine();
s5 = in.readLine();
text_1.setText("" + s1);
text_2.setText("" + s2);
text_3.setText("" + s3);
text_4.setText("" + s4);
text_5.setText("" + s5);
}
line = in.readLine();
}
in.close();
} catch (IOException d) {
System.out.println(d);
}
}
if (bt.equals("确定修改该数据")) {
try{
BufferedReader in = new BufferedReader(new FileReader(fileName));
int i = 0;
String[] copy = new String[10000];
String[] assist = new String[5];
String b;
b = in.readLine();
while (b != null) {
//copy[]数组储存着要保存的数据
i++;
}
b = in.readLine();
}
in.close();
assist[0] = text_6.getText();
assist[1] = text_7.getText();
assist[2] = text_8.getText();
assist[3] = text_9.getText();
assist[4] = text_10.getText();
for(int d = 0;d <= 4;d++){
copy[i] = assist[d];
i++;
}
i--;
String 重写文件 = "E:/Contacts.txt"; //对整个Contacts.txt进行刷新重写
try {
FileWriter writer = new FileWriter(重写文件);
for (int a = 0; a <= i ; a++) {
writer.write(copy[a] + "\r\n");
}
writer.close();
} catch (IOException e1) {
}
}catch (IOException d) {
System.out.println(d);
}
}
}
7.删除类
Delete() {
setTitle("删除联系人");
setSize(350, 150);
setLocation(600, 400);
setLayout(new GridLayout(3, 2));
text_1 = new TextField();
text_2 = new TextField();
Label lab_3 = new Label("请输入要删除联系人的序号是:");
Label lab_4 = new Label("备注:");
Button bt9 = new Button("确定");
Button bt10 = new Button("清空");
bt9.addActionListener(this);
bt10.addActionListener(this);
add(lab_3);
add(text_1);
add(lab_4);
add(text_2);
add(bt9);
add(bt10);
setVisible(true);
}
public void actionPerformed(ActionEvent e) {
String bt = e.getActionCommand();
if (bt.equals("确定")) {
String[] copy = new String[10000];
int i = 0, a, t = 0;
String b;
String fileName = "E:/Contacts.txt";
try {
BufferedReader in = new BufferedReader(new FileReader(fileName));
b = in.readLine();
while (b != null) {
if (b.equals(text_1.getText())) {
in.readLine();
in.readLine();
in.readLine();
in.readLine();
text_2.setText("已成功删除该联系人");
t = 1;
} else {
i++;
copy[i] = b;
}
b = in.readLine();
}
if (t == 0) {
text_2.setText("该通讯录无此人的信息");
}
in.close();
} catch (IOException d) {
System.out.println(d);
}
String 重写文件 = "E:/Contacts.txt";
try {
FileWriter writer = new FileWriter(重写文件);
for (a = 1; a < i + 1; a++) {
writer.write(copy[a] + "\r\n");
}
writer.close();
} catch (IOException e1) {
}
}
if (bt.equals("清空")) {
text_1.setText("");
text_2.setText("");
}
}
}
8.备份类:
Copy() {
setTitle("文件备份");
setSize(300, 150);
setLocation(600, 400);
setVisible(true);
String[] copy = new String[10000];
int i = 0, a;
String fileName = "E:\\Contacts.txt";
try {
BufferedReader in = new BufferedReader(new FileReader(fileName));
copy[i] = in.readLine();
while (copy[i] != null) {
i++;
copy[i] = in.readLine();
}
in.close();
} catch (IOException d) {
System.out.println(d);
}
String wenJan = "E:\\备份.txt";
try {
FileWriter writer = new FileWriter(wenJan);
for (a = 0; a < i; a++) {
writer.write(copy[a] + "\r\n");
}
writer.close();
} catch (IOException e1) {
}
}
public void paint(Graphics g) {
super.paint(g);
g.drawString("已经成功备份到(E:\\备份.txt)", 90, 90);
}
}
运行结果:
1.功能面板:
2.新增联系人:
3.删除联系人:
4.查询联系人:
5.修改联系人:
6.文件备份:
7.展示联系人: