JavaIO流作业——将数组电话本改为IO流输出,保存在txt文件中

电话本记录的属性


public class TelephoneBook {
	private String name;
	private String age;
	private String telephone;
	

	public String getName() {
		return name;
	}


	public void setName(String name) {
		this.name = name;
	}


	public String getAge() {
		return age;
	}


	public void setAge(String age) {
		this.age = age;
	}


	public String getTelephone() {
		return telephone;
	}


	public void setTelephone(String telephone) {
		this.telephone = telephone;
	}
	


	public TelephoneBook(String name, String age, String telephone) {
		super();
		this.name = name;
		this.age = age;
		this.telephone = telephone;
	}
   

	public TelephoneBook() {
		super();
	}


	@Override
	public String toString() {
		return "TelephoneBook [name=" + name + ", age=" + age + ", telephone=" + telephone + "]";
	}
    public  String  getMassage(){
    	
		return name+","+age+","+telephone;
    }
    public void display() {
		System.out.println( "姓名:" + getName() +
				 ",年龄:" + getAge() + ",电话:"
				+ getTelephone());

    }
}

实现层

public class TelService {
	static Scanner sc = new Scanner(System.in);
	// 存放电话本信息
	static List<TelephoneBook> tels = new ArrayList<>();


	/**
	 * 
	 * 方法描述:初始化电话本信息
	 * 
	 * @param path
	 *            电话本文件的路径
	 */
	public void init(String path) {
		// 读取文件中的数据,将每一行转化为一个Telephone对象,将此对象添加到tels集合中
		BufferedReader br = null;
		try {
			File file = new File(path);
			// 判断电话本是否存在
			if(!file.exists()){
				file.createNewFile();
			}			
			br = new BufferedReader(new FileReader(new File("telephone.txt")));
			// 每一行文本
			String line = null;
			while ((line = br.readLine()) != null) {
				//zhangsan,20,44878777
				String[] info = line.split(",");
				TelephoneBook tpb = new TelephoneBook(info[0], info[1], info[2]);
				// 将telephone对象添加到集合中
				tels.add(tpb);				
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (br != null) {
				try {
					br.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}

		}

	}

	public static TelephoneBook createTelephoneBook() {
		System.out.print("姓名:");
		String name = sc.nextLine();
		System.out.print("年龄:");
		String age = sc.nextLine();
		System.out.print("电话:");
		String telephone = sc.nextLine();
		TelephoneBook tel = new TelephoneBook(name, age, telephone);		
		return tel;
	}
/**
 * 
 * 方法描述: 根据姓名查询索引
 * @param name 姓名
 * @return  返回对应名称的索引
 */
	public static int getIndexByName(String name) {
		for (int i = 0; i < tels.size(); i++) {
			//判断查询的姓名是否存在
			if (tels.get(i).getName().equals(name)) {
				// 找到了
				return i;
			}
		}
		//此人不存在
		return -1;
	}
   /**
    * 
    * 方法描述:添加电话本业务
    * @param telephoneBook 电话本信息
    * @return 1:添加成功 0:添加失败 -1:此人已存在
    */
	public static void  addTel() {
		System.out.println("------添加电话本------");
		TelephoneBook tel = createTelephoneBook();
		//查看是否存在此名称的电话本信息,如果已存在,直接返回-1
		/*int index = getIndexByName(tel.getName());
		if(index!=-1){
			return false;
		}*/
		tels.add(tel);
		tel.display();
  		System.out.println("添加成功!");
        
	}
/**
 * 
 * 方法描述:根据姓名删除
 * @param name
 * @return  boolean
 */
	public static boolean delTel() {
		System.out.println("------删除电话本------");
		System.out.println("请输入姓名:");
		String name = sc.nextLine();
		// 查询所删除的用户是否存在,如果存在就删除(删除元素的下标)
		// 根据姓名得到电话本索引
		int index = getIndexByName(name);
		if (index != -1) {
          //信息存在
			System.out.println("确定吗?1(是) 0(否):");
			int a = sc.nextInt();
			if (a == 1) {
				// 删除

				tels.remove(index);
				System.out.println("删除成功");
				return true;// 结束该方法
			}
			if (a == 0) {
				System.out.println("取消删除成功");
				return false;
			}

		} else {
			System.out.println("此人不存在");
			
		}
		return false;
	}
/**
 * 
 * 方法描述:根据姓名查询索引
 * @param tBook 电话本信息
 * @return true 更新成功 false: 不存在此姓名
 */
	public static void updateTel() {
		System.out.println("------修改电话本------");
		System.out.println("请输入姓名:");
		String name = sc.nextLine();
		int index = getIndexByName(name);
		if (index == -1) {
			System.out.println("此人不存在");
			return;
		}
		TelephoneBook tel = createTelephoneBook();
		tels.set(index, tel);
		System.out.println("修改成功");
		tel.display();
	}
/**
 * 
 * 方法描述:打印所有电话本
 */
	public static void queryAll() {
		System.out.println("------打印所有电话本------");
		// 得到数组[]中有值的元素
		for (int i = 0; i < tels.size(); i++) {
			// 得到

			TelephoneBook tel = tels.get(i);
			tel.display();
		}

	}
	/**
	 * 
	 * 方法描述: 根据姓名查询电话本信息
	 * @param name 姓名
	 * @return  电话本信息
	 */  

	public static void queryTel() {
		System.out.println("------查找电话本------");	
		System.out.println("请输入姓名:");
		String name = sc.nextLine();
		int index = getIndexByName(name);
		//如果是-1 说明没有找到此人
		if (index == -1) {
			System.out.println("此人不存在");
			return;
		}
		TelephoneBook tb = tels.get(index);
		tb.display();
		return;
	}

	/**
	 * 
	 * 方法描述:退出应用的时候,将集合中的数据写入到path文件中
	 * @param path
	 */

	public void saveTel(String path) {
		BufferedWriter bw = null;
		try {
			bw = new BufferedWriter(new FileWriter(new File(path)));
			//遍历tels集合
			for (TelephoneBook tel : tels) {
				//得到每行信息
				String line =tel.getMassage();
				//写入每一行
				bw.write(line);
				//换行
				bw.newLine();
				bw.flush();
			}

		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (bw != null) {
				try {
					bw.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
	}

}

测试类

public class TelTest {
	
	static TelephoneBook  tBook = new TelephoneBook();
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		// 开始界面
        TelService ts =new TelService();
        ts.init("telephone.txt");
		// 业务选择
		while (true) {
			System.out.println("|-------------------电话本管理系统-------------|");
			System.out.println("1.添加   2.删除   3.修改   4.查询所有   5.根据姓名查询   0.退出并保存");
			System.out.println("|-------------------电话本管理系统-------------|");
			System.out.println("请选择业务:");
			String choice = sc.nextLine();
			switch (choice) {
			case "1": // 添加电话本
				TelService.addTel();
				break;
			case "2": // 删除雇员
				TelService.delTel();
				break;
			case "3": // 修改雇员信息
				TelService.updateTel();
				break;
			case "4": // 根据姓名查找雇员信息
				TelService.queryAll();
				break;
			case "5": // 根据姓名查找雇员信息
				TelService.queryTel();
				break;
			case "0": // 退出
				ts.saveTel("telephone.txt");
				System.out.println("退出成功!");
				System.exit(0);
				break;

			default:
				System.out.println("您输入的数字不正确!");
				break;
			}
		}
	}

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值