面向对象程序设计课程设计电话号码管理系统.docx
- 文档编号:2228698
- 上传时间:2023-05-02
- 格式:DOCX
- 页数:15
- 大小:129.28KB
面向对象程序设计课程设计电话号码管理系统.docx
《面向对象程序设计课程设计电话号码管理系统.docx》由会员分享,可在线阅读,更多相关《面向对象程序设计课程设计电话号码管理系统.docx(15页珍藏版)》请在冰点文库上搜索。
面向对象程序设计课程设计电话号码管理系统
课程考核
课程名称
面向对象程序设计
姓名
学号
专业
年级
班级
系别
网络工程
11
1
计科系
分数
任课教师签字
批改日期
备注
电话号码管理系统
一、课程设计目标
此次课程设计的目的是通过对一学期JAVA程序设计学习,使自己能够独立地运用相关软件进行JAVA程序的编写,并在此次课程设计中检验自己的掌握程度。
1、运用eclipse软件编写电话号码管理系统程序。
2、具体目的是:
编写的电话号码管理程序能够进行保存,修改,删除等功能(不使用数据库)。
开发环境:
Windows7
Eclipse
二、课程设计内容
1、使用Eclipse编写一个窗口,在窗口里有按钮,每个按钮对应相关的功能
2、编写的程序可以进行电话号码的增加,删除,修改等功能,并且在子目录生成一个TXT格式的文件,文件里面保存着输入的电话号码信息。
三、过程设计
1、进行程序最初的框架构思。
2、进行窗口程序的编写。
代码如下:
3、窗口内按键的编写。
代码如下:
当输入姓名和电话号码并且点添加时会在子文件夹里出现名为JmStar的TXT文件,文件里面保存着添加的电话号码信息如下;
源代码:
importjava.awt.event.ActionEvent;
importjava.awt.event.ActionListener;
importjava.awt.event.WindowAdapter;
importjava.awt.event.WindowEvent;
importjava.io.File;
importjava.io.FileInputStream;
importjava.io.FileNotFoundException;
importjava.io.FileOutputStream;
importjava.io.FileWriter;
importjava.io.IOException;
importjava.io.ObjectInputStream;
importjava.io.ObjectOutputStream;
importjava.util.Calendar;
importjava.util.HashMap;
importjava.util.Iterator;
importjava.util.Map;
importjava.util.Set;
importjava.util.Map.Entry;
importjavax.swing.JButton;
importjavax.swing.JFrame;
importjavax.swing.JLabel;
importjavax.swing.JTextField;
publicclassPhoneextendsJFrame{
Filefile1=newFile("return.ser");
Filefile2=newFile("JmStar.txt");
Filefile3=newFile("phone.ser");
FileOutputStreamfout;
FileWriterout;
FileInputStreamfin;
ObjectOutputStreamoout;
ObjectInputStreamoin;
Calendarca=Calendar.getInstance();
JButtonbtn1=newJButton("添加");
JButtonbtn2=newJButton("删除");
JButtonbtn3=newJButton("查询");
JButtonbtn4=newJButton("修改");
JLabellab1=newJLabel("姓名:
");
JLabellab2=newJLabel("电话号码:
");
JLabelshow=newJLabel("输出结果");
JTextFieldname=newJTextField();
JTextFieldphone=newJTextField();
HashMap
publicPhone(){
setSize(400,400);
setLocation(200,100);
setLayout(null);
setTitle("电话簿");
lab1.setSize(40,40);
lab1.setLocation(40,10);
lab2.setSize(80,40);
lab2.setLocation(40,50);
name.setSize(200,40);
name.setLocation(100,10);
phone.setSize(200,40);
phone.setLocation(100,50);
name.setSize(200,40);
name.setLocation(100,10);
btn1.setSize(80,40);
btn1.setLocation(100,100);
btn2.setSize(80,40);
btn2.setLocation(200,100);
btn3.setSize(80,40);
btn3.setLocation(100,150);
btn4.setSize(80,40);
btn4.setLocation(200,150);
show.setSize(400,200);
show.setLocation(40,150);
try{
load();
}catch(IOExceptione1){
e1.printStackTrace();
}
btn1.addActionListener(newActionListener(){
publicvoidactionPerformed(ActionEventarg0){
if(!
name.getText().isEmpty()&&!
phone.getText().isEmpty()){
if(!
pn.containsKey(name.getText())){
pn.put(name.getText(),phone.getText());
show.setText("成功添加联系人"+name.getText());
try{
if(!
file2.exists())
file2.createNewFile();
out=newFileWriter(file2,true);
out.write(ca.getTime()+"成功添加联系人"+"联系人姓名是"+name.getText()+"联系人号码是"+pn.get(name.getText())+"\r\n");
out.close();
}catch(FileNotFoundExceptione){
e.printStackTrace();
}catch(IOExceptione){
e.printStackTrace();
}
}else
show.setText("添加失败,用户已经存在");
}
else{
show.setText("添加失败,请重新添加姓名和电话号码!
");
}
}
});
btn2.addActionListener(newActionListener(){
publicvoidactionPerformed(ActionEventarg0){
if(pn.containsKey(name.getText())){
pn.remove(name.getText());
show.setText("成功删除联系人"+name.getText());
try{
if(!
file2.exists())
file2.createNewFile();
out=newFileWriter(file2,true);
out.write(ca.getTime()+"成功删除联系人"+"联系人姓名是"+name.getText()+"联系人号码是"+pn.get(name.getText())+"\r\n");
out.close();
}catch(FileNotFoundExceptione){
//TODOAuto-generatedcatchblock
e.printStackTrace();
}catch(IOExceptione){
//TODOAuto-generatedcatchblock
e.printStackTrace();
}
}
else{
show.setText("联系人不存在,请输入联系人姓名");
}
}
});
btn3.addActionListener(newActionListener(){
publicvoidactionPerformed(ActionEventarg0){
if(pn.containsKey(name.getText())){
show.setText("查询成功,"+"联系人姓名是"+name.getText()+",联系人号码是"+pn.get(name.getText()));
try{
if(!
file2.exists())
file2.createNewFile();
out=newFileWriter(file2,true);
out.write(ca.getTime()+"成功查询联系人"+"联系人姓名是"+name.getText()+"联系人号码是"+pn.get(name.getText())+"\r\n");
out.close();
}catch(FileNotFoundExceptione){
e.printStackTrace();
}catch(IOExceptione){
e.printStackTrace();
}
}
else{
show.setText("查询失败,请输入联系人的姓名");
}
}
});
btn4.addActionListener(newActionListener(){
publicvoidactionPerformed(ActionEventarg0){
if(pn.containsKey(name.getText())&&!
phone.getText().isEmpty()){
pn.remove(name.getText());
pn.put(name.getText(),phone.getText());
show.setText("修改成功");
try{
if(!
file2.exists())
file2.createNewFile();
out=newFileWriter(file2,true);
out.write(ca.getTime()+"成功修改联系人"+"联系人姓名是"+name.getText()+"联系人号码是"+pn.get(name.getText())+"\r\n");
out.close();
}catch(FileNotFoundExceptione){
e.printStackTrace();
}catch(IOExceptione){
e.printStackTrace();
}
}
else{
show.setText("修改失败,请输入联系人姓名和修改的电话号码");
}
}
});
this.addWindowListener(newWindowAdapter(){
publicvoidwindowClosing(WindowEvente){
try{
restore();
}catch(IOExceptione1){
e1.printStackTrace();
}
System.exit(0);
}
});
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
add(lab1);
add(lab2);
add(btn1);
add(btn2);
add(btn3);
add(btn4);
add(show);
add(name);
add(phone);
}
publicvoidload()throwsIOException{
if(file1.exists()){
fin=newFileInputStream(file1);
oin=newObjectInputStream(fin);
Personp=null;
try{
p=(Person)oin.readObject();
oin.close();
name.setText(p.getname());
phone.setText(p.getphone());
}catch(ClassNotFoundExceptione){
e.printStackTrace();
}
if(file3.exists()){
fin=newFileInputStream(file3);
oin=newObjectInputStream(fin);
Stringss=null;
while(true){
try{
p=(Person)oin.readObject();
if(p.getname().equals("结束")&&p.getphone().equals("结束")){
break;
}
pn.put(p.getname(),p.getphone());
}catch(ClassNotFoundExceptione){
e.printStackTrace();
}
}
oin.close();
}
}
}
publicvoidrestore()throwsIOException{
Setset=pn.entrySet();
Iterator
fout=newFileOutputStream(file3);
oout=newObjectOutputStream(fout);
Persons;
Objectm;
if(!
file3.exists())
file3.createNewFile();
while(i.hasNext()){
m=i.next();
s=newPerson();
s.setname((String)((Entry)m).getKey());
s.setphone((String)((Entry)m).getValue());
oout.writeObject(s);
}
s=newPerson();
s.setname("结束");
s.setphone("结束");
oout.writeObject(s);
oout.close();
if(!
file1.exists())
file1.createNewFile();
fout=newFileOutputStream(file1);
oout=newObjectOutputStream(fout);
s=newPerson();
s.setname(name.getText());
s.setphone(phone.getText());
oout.writeObject(s);
oout.close();
}
publicstaticvoidmain(String[]agres){
Phoneph=newPhone();
ph.setVisible(true);
}
}
四、学习心得
在整个编写过程中,总是被一个又一个难题所困扰。
但是在完成时,却是无比欣喜与快乐。
在克服编写过程中的难题后,感觉自己的编写技巧一点一点的提高。
JAVA语言相对来说比其他语言更加容易上手和熟知。
完全可以靠平日的练习和日常积累来提高。
JAVA程序编写是一件很费时间与精力的事情。
但只要全身心投入,所有的困难都能克服。
要编写一个比较理想的JAVA程序是一件很费时间与精力的事情。
因为JAVA语言里包含许多个包,以及不同功能实现的方法不同。
通过这次程序设计使我坚持一种信念“没有做不到,只有想不到”。
只要我充分发挥自己的主观能动性,就一定能编写出一个优秀的JAVA程序。
五、参考文献
《Java程序设计实用教程》
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 面向 对象 程序设计 课程设计 电话号码 管理 系统