通讯录管理系统终章
来源:互联网 发布:ubuntu mate 15.04 编辑:程序博客网 时间:2024/06/01 10:15
LinkMan
package AddressBook;//创建一个通讯录的联系人类class LinkMan{ //联系人姓名电话 String name; String telephone; //定义带有一个参数的构造方法 public LinkMan(String name) { this.name = name; this.telephone = null; } //定义无参的构造方法 public LinkMan() { } //定义带参的构造方法 public LinkMan(String name,String telephone) { this.name = name; this.telephone = telephone; } //获得联系人姓名 public String getName() { return name; } //获得联系人电话 public String getTelephone() { return telephone; } //设置联系人姓名 public void setName(String name) { name = this.name; } //设置联系人电话 public void setTelephone(String telephone) { telephone = this.telephone; } //输出联系人姓名和电话 public String toString() { return "用户的姓名:"+this.name+","+"用户的电话:"+this.telephone; }}
AddressBook:
package AddressBook;import AddressBook.LinkMan;import java.util.Scanner;import java.io.Serializable;public class AddressBook implements Serializable{ LinkMan [] array;//保存联系人的数组 public int number;//联系人数量 //创建无参的构造方法,空的数组,人数为0 public AddressBook() { array = new LinkMan[1000]; number = 0; } //带参的构造方法 ,创建number个人的通讯录 public AddressBook(int number) { String name; String telephone; Scanner sc = new Scanner(System.in); this.array = new LinkMan[1000]; this.number = number; for(int i = 0;i<number;i++) { System.out.println("请输入第"+(i+1)+"联系人:"); System.out.println("姓名:"); name = sc.next(); System.out.println("电话:"); telephone = sc.next(); this.array[i] = new LinkMan(name,telephone); } } /*获取人数*/ public boolean linkman(int number) { if(this.number == this.array.length) { System.out.println("空间存满,不能添加"); return false; } else { number ++; return true; } } /*查找方法*/ public LinkMan search(String name) { for(int j = 0;j < this.number;j++) { if(name.compareTo(this.array[j].getName())==0) return this.array[j]; } return null; } /*浏览方法*/ public void display() { System.out.println("--通讯录--"); System.out.println("姓名 电话"); for(int i=0;i < number;i++) { System.out.println(this.array[i].toString()); } System.out.println("结束"); } /*添加联系人*/ public String append(String name,String telephone) { array[this.number]=new LinkMan(); array[this.number].name = name; array[this.number].telephone = telephone; this.number++; return "添加成功"; } /*删除方法*/ public boolean delete(LinkMan im) { int i; if(this.number==1) { this.number = 0; return true; } for(i = 0;i<this.number;i++) { System.out.println("this.number="+this.number+",i="+i); if(im.getName().compareTo(this.array[i].getName()) == 0) break; } if(i < this.number) { this.array[i] = this.array[this.number-1]; this.number--; return true; } else { return false; } } /*获取LinkMan类型的数组内容*/ public LinkMan getLinkMan(int i) { if(i>=0&&i<this.number) return this.array[i]; return null; }}
AddressBookService:
package AddressBook;import AddressBook.LinkMan;import AddressBook.AddressBook;import java.util.Scanner;import java.io.*;public class AddressBookService { static Scanner sc = new Scanner(System.in); private AddressBook myAB; private static String ObjectFileName = "source.txt"; private static String TxtFileName = "test.txt"; //构造方法 public AddressBookService(int num) { myAB = new AddressBook(num); } /*查找联系人姓名*/ public void search() { print("--开始查找--"); print("输入搜索姓名:"); String name = sc.next(); LinkMan searchResult = myAB.search(name); if(searchResult != null) { print(searchResult.name+","+searchResult.telephone); } else { print("没找到!"); } print("--搜索--"); } public void display() { myAB.display(); } /*添加方法*/ public void append() { print("--添加开始--"); print("输入添加的人名字:"); String appName = sc.next(); print("输入添加的电话号码:"); String appTel = sc.next(); myAB.append(appName,appTel); myAB.display(); print("--添加结束--"); } /*删除*/ public void delete() { print("--删除开始--"); print("输入删除的名字:"); String deName = sc.next(); LinkMan deLM = new LinkMan(deName); if(myAB.delete(deLM) == true) myAB.display(); else print("删除失败!"); print("--删除结束--"); } /*从指定文件读取数据*/ public void read() throws Exception { print("开始读取:"); //创建对象输入流,并从指定的输入流中读取数据 FileInputStream fis = new FileInputStream(ObjectFileName); ObjectInputStream ois = new ObjectInputStream(fis); AddressBook myAB = (AddressBook)ois.readObject(); ois.close(); fis.close(); print("读取成功!"); } /*从指定文件写入数据*/ public void write() throws Exception { //创建对象输出流 FileOutputStream fos = new FileOutputStream(ObjectFileName); ObjectOutputStream oos = new ObjectOutputStream(fos); oos.writeObject(myAB); oos.close(); fos.close(); print("保存成功"); } /*保存通讯录写到文本文件*/ public void writetxt() throws Exception { FileWriter fw = new FileWriter(TxtFileName); BufferedWriter bw = new BufferedWriter(fw); for(int i = 0 ;i < myAB.number;i++) { String line = myAB.getLinkMan(i).toString(); bw.write(line); bw.newLine();//换行 } bw.close(); fw.close(); print("写入成功!"); print("文件名:"+TxtFileName); } public static void print(String message) { System.out.println(message); }}
AddressBookManage
package AddressBook;import AddressBook.LinkMan;import AddressBook.AddressBook;import AddressBook.AddressBookService;import java.util.Scanner;import java.io.*;public class AddressBookManage{ static Scanner sc = new Scanner(System.in); private AddressBookService myABS;//第二次对AddressBookService进行包装 public AddressBookManage() { } public void run()throws Exception { showMenuBar();//显示主菜单 } private void showMenuBar() throws Exception { print("注意:请先创建通讯录!"); print("------通讯录----------------"); print("------1 创建通讯录----------"); print("------2 导入通讯录----------"); print("------0 退出系统-------------"); print("----------------------------------------------"); print("请选择:"); int choice = sc.nextInt(); switch(choice) { case 1: print("请输入人数:"); int number = sc.nextInt(); myABS = new AddressBookService(number); print("创建成功,可以操作通讯录!"); break; case 2: print("调用读取功能:"); myABS = new AddressBookService(0); myABS.read(); print("读入成功,可以操作通讯录!"); break; case 0: System.exit(0); } showMenu(); } private void showMenu() throws Exception { do { print("------通讯录功能菜单-----"); print("------1 浏览-------------"); print("------2 查找-------------"); print("------3 添加-------------"); print("------4 删除-------------"); print("------5 保存-------------"); print("------6 读取-------------"); print("------7 导出到文本文件---"); print("------8 退出-------------"); print("---------------------------------------"); print("请选择:"); int choice = sc.nextInt(); switch(choice) { case 1:myABS.display();break; case 2:myABS.search();break; case 3:myABS.append();break; case 4:myABS.delete();break; case 5:myABS.write();break; case 6:myABS.read();break; case 7:myABS.writetxt();break; default:print("谢谢使用,再见"); System.exit(0); } }while(true); } public static void print(String message) { System.out.println(message); }}
text:
package AddressBook;import AddressBook.AddressBookManage;public class text{ public static void main(String []args) throws Exception { new AddressBookManage().run(); }}
阅读全文
0 0
- 通讯录管理系统终章
- 通讯录管理系统(原创)
- 通讯录管理系统
- 学生通讯录管理系统
- 通讯录管理系统
- 通讯录管理系统
- 通讯录管理系统
- 通讯录管理系统
- 《通讯录管理系统》
- 通讯录管理系统
- 通讯录管理系统设计
- 个人通讯录管理系统
- 【课程设计-通讯录管理系统】
- 通讯录个人管理系统
- python通讯录管理系统
- 通讯录管理系统(C语言版)
- 通讯录管理系统的下载
- 通讯录管理系统需求分析
- 557. Reverse Words in a String III
- 对象审计+查看审计内容+查看对哪些用户对象进行对象审计+取消对象审计
- lintcode(182)删除数字
- linux下安装Nginx
- JAVA-JDBC: (1) java连接数据库的粗略步骤
- 通讯录管理系统终章
- System.getProperty
- 【C/C++】解决“不是有效的win32应用程序”思路
- Android构造函数有没有返回值?
- C#.NET:高级编程之匿名类、匿名方法与扩展方法
- Linux搭建Kafka+Spark实时处理系统
- springMvc实现处理多个input的表单对象封装name到对象
- python2.7 安装numpy no module name zlib
- Spring ModelAttribute注解