通讯录案例
通讯录:用于保存姓名和电话号
1、将姓名和电话号保存到map集合里,key=姓名;value=电话号
2、map集合中保存5组数据
3、遍历map集合,将map中的记录保存到文件,要求文件名为通讯录的姓名,文件内容是通讯录的电话号
4、实现用户输入姓名查找电话号的功能
package test;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Scanner;
import java.util.Set;
public class test {
public static void main(String[] args) {
Map<String, String> book = new HashMap<String, String>();
//初始化数据
book.put("张三", "13113313443");
book.put("李四", "13113315555");
book.put("王五", "13113312343");
book.put("赵六", "13113312112");
book.put("二哈", "13113314567");
//通过entrySet方法进行遍历集合
Set<Entry<String, String>> ens = book.entrySet();
for(Entry<String, String> en : ens){
String name = en.getKey();//用户名
String phone = en.getValue();//电话号
try {
//创建名为用户名的文件
FileOutputStream fos = new FileOutputStream("C:\\Users\\yinshuang\\Desktop\\IOTest"+name+".txt");
BufferedOutputStream bos = new BufferedOutputStream(fos, 1024);
try {
//将电话号码写入文件
bos.write(phone.getBytes());
} catch (IOException e) {
e.printStackTrace();
}finally{
try {
bos.close();
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
while(true){
System.out.println("请输入姓名:");
String name = new Scanner(System.in).next();
File fi = new File("C:\\Users\\yinshuang\\Desktop\\IOTest"+name+".txt");
if(fi.exists()) { //判断该文件是否存在
try {
FileInputStream fis = new FileInputStream(fi);
BufferedInputStream bis = new BufferedInputStream(fis, 1024);
int b = 0;
String msg = "";
try {
while((b = bis.read()) != -1){
msg += (char) b;
}
System.out.println("电话号:"+msg);
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
bis.close();
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
} else {
System.out.println("电话本中不存在该姓名!!!");
}
}
}
}
人才管理系统案例
“人才管理系统”
类描述:
人才信息,属性:姓名,性别,年龄,专业,毕业学校,掌握技能(list集合)
个人简历,继承人才信息,属性:个人简介
猎头公司,属性:公司名称,地址
接口描述:
1、增加人才信息
2、查询人才信息
3、根据年龄向控制台查询人才信息
4、根据专业向控制台查询人才信息
5、查询出技能中掌握java的人才信息
6、猎头公司可以查看人才系统所有人才
7、猎头公司根据选择的人才查看个人的简介
controller 存放接口
package controller;
import java.util.List;
import entity.Person;
/**
* 增加人才信息接口
* @author yinshuang
*
*/
public interface Add {//增加人事信息方法
public void addPerson(List<Person> list);
}
package controller;
import java.util.List;
import entity.Person;
/**
* 查询人才信息接口
* @author yinshuang
*
*/
public interface Select {
public void selectInfor();//查询人事的所有信息方法
public void selectAge(List<Person> list);;//根据年龄向控制台查询人才信息
public void selectWork(List<Person> list);//根据专业向控制台查询人才信息
public void selectJava(List<Person> list);//查询出技能中掌握java的人才信息
public void selectPerson();//查看人才系统所有人才
public void selectChoose();//根据选择的人才查看个人的简介
}
entity 存放类
package entity;
/**
* 人才信息类
* @author yinshuang
*
*/
public class Person {
private String strName;//姓名
private String strSex;//性别
private int intAge;//年龄
private String strWork;//专业
private String strSchool;//毕业院校
private String strSkill;//掌握技能
public Person(String strName, String strSex, int intAge, String strWork, String strSchool, String strSkill) {
super();
this.strName = strName;
this.strSex = strSex;
this.intAge = intAge;
this.strWork = strWork;
this.strSchool = strSchool;
this.strSkill = strSkill;
}
@Override
public String toString() {
return "姓名:"+this.getStrName()+"; 性别:"+this.getStrSex()+"; 年龄:"+this.getIntAge()+"; 专业:"+this.getStrWork()+"; 学校:"+this.getStrSchool()+"; 掌握技能:"+this.getStrSkill();
}
public String getStrName() {
return strName;
}
public void setStrName(String strName) {
this.strName = strName;
}
public String getStrSex() {
return strSex;
}
public void setStrSex(String strSex) {
this.strSex = strSex;
}
public int getIntAge() {
return intAge;
}
public void setIntAge(int intAge) {
this.intAge = intAge;
}
public String getStrWork() {
return strWork;
}
public void setStrWork(String strWork) {
this.strWork = strWork;
}
public String getStrSchool() {
return strSchool;
}
public void setStrSchool(String strSchool) {
this.strSchool = strSchool;
}
public String getStrSkill() {
return strSkill;
}
public void setStrSkill(String strSkill) {
this.strSkill = strSkill;
}
}
package entity;
/**
* 个人简历类
* @author yinshuang
*
*/
public class Resume extends Person{
private String strResume;//简介
public Resume(String strName, String strSex, int intAge, String strWork, String strSchool, String strSkill) {
super(strName, strSex, intAge, strWork, strSchool, strSkill);
}
@Override
public String toString() {
return "个人简介: "+super.toString();
}
}
package entity;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.List;
import java.util.Scanner;
import controller.Add;
import controller.Select;
/**
* 猎头公司类
* @author yinshuang
*
*/
public class Company implements Add,Select{
private String strName;//公司名称
private String strAddress;//公司地址
@Override
public String toString() {
return "公司名称:"+this.strName+"; 公司地址:"+this.strAddress;
}
@Override
public void addPerson(List<Person> list) {//增加人事信息
System.out.println("请输入姓名、性别、年龄、专业、毕业院校、掌握技能");
Scanner scanner = new Scanner(System.in);
String strName = scanner.next();
String strSex = scanner.next();
int intAge = scanner.nextInt();
String strWork = scanner.next();
String strSchool = scanner.next();
String strSkill = scanner.next();
Person p = new Person(strName, strSex, intAge, strWork, strSchool, strSkill);
Resume r = new Resume(strName, strSex, intAge, strWork, strSchool, strSkill);
list.add(p);
File file1 = new File("C:\\Users\\yinshuang\\Desktop\\IOTest\\"+strName);
file1.mkdirs();
File file2 = new File("C:\\Users\\yinshuang\\Desktop\\IOTest\\"+strName+"\\个人信息.txt");
File file3 = new File("C:\\Users\\yinshuang\\Desktop\\IOTest\\"+strName+"\\个人介绍.txt");
try {
file2.createNewFile();
file3.createNewFile();
FileWriter fw = new FileWriter(file2);
BufferedWriter bw = new BufferedWriter(fw,1024);
FileWriter fw1 = new FileWriter(file3);
BufferedWriter bw1 = new BufferedWriter(fw1,1024);
try {
bw.write(p.toString());
bw.newLine();
bw1.write(r.toString());
bw1.newLine();
} catch (Exception e) {
e.printStackTrace();
}
finally {
bw1.close();
fw1.close();
bw.close();
fw.close();
}
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("人才信息已添加");
}
@Override
public void selectInfor() {//查询人事的所有信息方法
System.out.println("请输入要查询的姓名:");
String strName = new Scanner(System.in).next();
BufferedReader br;
String s;
try {
FileReader fr = new FileReader("C:\\Users\\yinshuang\\Desktop\\IOTest\\"+strName+"\\个人信息.txt");
br = new BufferedReader(fr,1024);
s = "";
try {
while((s = br.readLine()) != null) {
System.out.println(s);
}
} catch (IOException e) {
e.printStackTrace();
}finally {
try {
br.close();
fr.close();
} catch (IOException e) {
e.printStackTrace();
}
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
@Override
public void selectAge(List<Person> list) {//根据年龄向控制台查询人才信息
System.out.println("请输入年龄");
int age = new Scanner(System.in).nextInt();
for(Person p : list) {
if(age == p.getIntAge()) {
System.out.println(p.toString());
}
}
}
@Override
public void selectWork(List<Person> list) {//根据专业向控制台查询人才信息
System.out.println("请输入专业");
String work = new Scanner(System.in).next();
for(Person p : list) {
if(work.equals(p.getStrWork())) {
System.out.println(p.toString());
}
}
}
@Override
public void selectJava(List<Person> list) {//查询出技能中掌握java的人才信息
for(Person p : list) {
if("java".equals(p.getStrSkill())) {
System.out.println(p.toString());
}
}
}
@Override
public void selectPerson() {//查看人才系统所有人才
File f = new File("C:\\Users\\yinshuang\\Desktop\\IOTest");
File[] files = f.listFiles();
for(File file : files) {//遍历文件夹
System.out.println(file.getName());
}
System.out.println("查询完毕");
}
@Override
public void selectChoose() {//根据选择的人才查看个人的简介
System.out.println("请输入要查询的姓名:");
String strName = new Scanner(System.in).next();
BufferedReader br;
String s;
try {
FileReader fr = new FileReader("C:\\Users\\yinshuang\\Desktop\\IOTest\\"+strName+"\\个人介绍.txt");
br = new BufferedReader(fr,1024);
s = "";
try {
while((s = br.readLine()) != null) {
System.out.println(s);
}
} catch (IOException e) {
e.printStackTrace();
}finally {
try {
br.close();
fr.close();
} catch (IOException e) {
e.printStackTrace();
}
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
}
service 测试类
package service;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import entity.Company;
import entity.Person;
import entity.Resume;
public class Test {
public static void main(String[] args) throws IOException {
List<Person> list = Init();//人才信息集合
List<Resume> list2 = Init2();//人才简介集合
Scanner scanner = new Scanner(System.in);
int m = 0;
while(m==0) {
System.out.println("************欢迎来到人事管理系统*************");
System.out.println("1.增加人才信息");
System.out.println("2.查询人才信息");
System.out.println("3.根据年龄向控制台查询人才信息");
System.out.println("4.根据专业向控制台查询人才信息");
System.out.println("5.查询出技能中掌握java的人才信息");
System.out.println("6.查看人才系统所有人才");
System.out.println("7.根据选择的人才查看个人的简介");
System.out.println("8.退出");
int i = scanner.nextInt();
Company c = new Company();
switch (i) {
case 1:
c.addPerson(list);
break;
case 2:
c.selectInfor();
break;
case 3:
c.selectAge(list);;
break;
case 4:
c.selectWork(list);
break;
case 5:
c.selectJava(list);
break;
case 6:
c.selectPerson();
break;
case 7:
c.selectChoose();
break;
case 8:
m = 1;
break;
default:
break;
}
}
}
public static List<Person> Init() throws IOException {
Person p1 = new Person("张三","男", 21, "软件工程", "xx1大学", "java");
Person p2 = new Person("李四","女", 20, "大数据技术", "xx2大学", "数据挖掘");
Person p3 = new Person("王五","男", 22, "计算机科学与技术", "xx3大学", "java");
List<Person> list = new ArrayList<Person>();
list.add(p1);
list.add(p2);
list.add(p3);
for(int i = 0;i < list.size();i++) {
File file1 = new File("C:\\Users\\yinshuang\\Desktop\\IOTest\\"+list.get(i).getStrName());
file1.mkdirs();
File file2 = new File("C:\\Users\\yinshuang\\Desktop\\IOTest\\"+list.get(i).getStrName()+"\\个人信息.txt");
file2.createNewFile();
File file3 = new File("C:\\Users\\yinshuang\\Desktop\\IOTest\\"+list.get(i).getStrName()+"\\个人介绍.txt");
file3.createNewFile();
FileWriter fw = new FileWriter(file2);
BufferedWriter bw = new BufferedWriter(fw,1024);
bw.write(list.get(i).toString());
bw.newLine();
bw.close();
fw.close();
}
return list;
}
private static List<Resume> Init2() throws IOException {
List<Resume> list2 = new ArrayList<Resume>();
list2.add(new Resume("张三","男", 21, "软件工程", "xx1大学", "java"));
list2.add(new Resume("李四","女", 20, "大数据技术", "xx2大学", "数据挖掘"));
list2.add(new Resume("王五","男", 22, "计算机科学与技术", "xx3大学", "java"));
for(Resume r : list2) {
FileWriter fw = new FileWriter("C:\\Users\\yinshuang\\Desktop\\IOTest\\"+r.getStrName()+"\\个人介绍.txt");
BufferedWriter bw = new BufferedWriter(fw,1024);
bw.write(r.toString());
bw.newLine();
bw.close();
fw.close();
}
return list2 ;
}
}