练手小程序——Java电话簿小程序

版权声明:以上所有博客均为冷漠的小猿所有,引用请注明出处 https://blog.csdn.net/lyf1997115/article/details/81569496

电话簿

功能:

  1. 添加联系人
  2. 查找指定的联系人
  3. 查找所有的联系人
  4. 退出系统

界面类

显示程序主界面
public class UI {
    public void print() {
        System.out.println("============================");
        System.out.println("============================");
        System.out.println("======1.添加一个新的联系人---====");
        System.out.println("======2.查找指定的联系人----====");
        System.out.println("======3.查找全部的联系人----====");
        System.out.println("======4.删除一个联系人------====");
        System.out.println("======0.退出-------------====");
        System.out.println("============================");
        System.out.println("============================");
    }
}

联系人信息类

定义并存放联系人的所有信息
import java.io.Serializable;

/**
 * 联系人 类 记录联系人的姓名,性别,年龄,手机号,身份证号
 * 
 * @author Administrator
 *
 */
public class Contacts implements Serializable{
    private String name;
    private String sex;
    private int age;
    private long phoneNo;
    private String id;

    public Contacts() {
    }

    public Contacts(String name, String sex, int age, long phoneNo, String id) {
        this.name = name;
        this.sex = sex;
        this.age = age;
        this.phoneNo = phoneNo;
        this.id = id;
    }

    public String getName() {
        return name;
    }

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

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }

    public int getAge() {
        return age;
    }

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

    public long getPhoneNo() {
        return phoneNo;
    }

    public void setPhoneNo(long phoneNo) {
        this.phoneNo = phoneNo;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    @Override
    public String toString() {
        return "[姓名=" + name + ", 性别=" + sex + ", 年龄=" + age + ", 电话号码=" + phoneNo + ", 身份证号=" + id + "]";
    }



    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + age;
        result = prime * result + ((id == null) ? 0 : id.hashCode());
        result = prime * result + ((name == null) ? 0 : name.hashCode());
        result = prime * result + (int) (phoneNo ^ (phoneNo >>> 32));
        result = prime * result + ((sex == null) ? 0 : sex.hashCode());
        return result;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        Contacts other = (Contacts) obj;
        if (age != other.age)
            return false;
        if (id == null) {
            if (other.id != null)
                return false;
        } else if (!id.equals(other.id))
            return false;
        if (name == null) {
            if (other.name != null)
                return false;
        } else if (!name.equals(other.name))
            return false;
        if (phoneNo != other.phoneNo)
            return false;
        if (sex == null) {
            if (other.sex != null)
                return false;
        } else if (!sex.equals(other.sex))
            return false;
        return true;
    }

}

电话本类

实现所有功能,并将数据保存到文件当中,以便下次运行程序数据不会丢失
/**
 * 功能:
 *  1. 添加联系人
 *  2. 查找指定的联系人
    3. 查找所有的联系人
    4. 退出系统

 * @author Administrator
 *
 */

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Scanner;
import java.util.Set;

public class PhoneBook {
    private Set<Contacts> set = null;
    private final String PATH = "D:\\Java\\workspace\\MyTest\\src\\com\\lyf\\小程序\\电话本\\save.txt" ;
    /**
     * 无参构造 初始化set集合
     */
    public PhoneBook() {
        File f = new File(PATH) ;
        if (f.exists()) {
            try {
                readSet(f) ;
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }else {
            set = new HashSet<Contacts>();
        }

    }

    /**
     * 添加一个新用户
     * 
     * @param con
     * @return
     */
    public boolean add() {
        Scanner input = new Scanner(System.in) ;
        System.out.print("请输入姓名:");
        String name = input.next() ;
        System.out.print("请输入性别:");
        String sex = input.next() ;
        System.out.print("请输入年龄:");
        int age = input.nextInt() ;
        System.out.print("请输入电话号码:");
        long phoneNo = input.nextLong() ;
        System.out.print("请输入身份证号码:");
        String id = input.next() ;
        Contacts con = new Contacts(name, sex, age, phoneNo, id) ;
        if (set.add(con)) {
            System.out.println("新用户保存成功。");
            return true;
        }
        return false;
    }

    /**
     * 查找指定的联系人
     * 
     * @param name
     */
    public void select(String name) {
        Iterator<Contacts> it = set.iterator();
        int count = 0 ;
        while (it.hasNext()) {
            Contacts con = (Contacts) it.next();
            if (con.getName().equals(name)) {
                System.out.println(con);
                count ++ ;
            }
        }
        if (count == 0) {
            System.out.println("无此联系人");
        }
    }

    /**
     * 删除指定的联系人
     */
    public void delete(String name) {
        Scanner input = new Scanner(System.in);
        Iterator<Contacts> it = set.iterator();
        Contacts[] arr = new Contacts[set.size()];
        int i = 0;
        while (it.hasNext()) {
            Contacts con = (Contacts) it.next();
            if (con.getName().equals(name)) {
                arr[i] = con;
                System.out.println((i + 1) + "." + arr[i]);
                i++;
            }
        }
        if (i == 0) {
            System.out.println("查无此人");
            return ;

        }else {
            System.out.println("请输入删除编号。");
            i = input.nextInt() ;
        }
        if (set.remove(arr[i - 1])) {
            System.out.println("删除成功。");
        } else {
            System.out.println("删除失败。");
        }

    }

    /**
     * 查询所有的联系人
     * 
     * @param name
     */
    public void selectAll() {
        Iterator<Contacts> it = set.iterator();
        while (it.hasNext()) {
            Contacts con = (Contacts) it.next();
            System.out.println(con);
        }
    }
    /**
     * 将电话本数据保存到文本
     */
    public void save() throws Exception{
        File f = new File(PATH) ;
        if (!f.exists()) {
            f.createNewFile() ;
        }
        FileOutputStream fos = new FileOutputStream(f) ;
        ObjectOutputStream oos = new ObjectOutputStream(fos) ;
        oos.writeObject(set);
        oos.close();
    }
    /**
     * 从文件中读取数据
     * @throws Exception 
     */
    public void readSet(File f) throws Exception{
        FileInputStream fis = new FileInputStream(f) ;
        ObjectInputStream ois = new ObjectInputStream(fis) ;
        set = (Set<Contacts>) ois.readObject() ;
        ois.close();
    }
}

客户端类(main方法)

负责整个程序的运行,和与用户的交互
import java.util.Scanner;

public class Run {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        PhoneBook pb = new PhoneBook();
        UI ui = new UI() ;
        Scanner input = new Scanner(System.in);
        int ch = 0;
        do {
            ui.print();
            System.out.println("请输入所需功能:");
            ch = input.nextInt();
            String name = "" ;
            switch (ch) {
            case 1:
                pb.add();
                break;
            case 2:
                System.out.println("请输入姓名:");
                name = input.next() ;
                pb.select(name);
                break;
            case 3:
                pb.selectAll();
                break;
            case 4:
                System.out.println("请输入姓名:");
                name = input.next() ;
                pb.delete(name);
                break ;
            default:
                break;
            }
        } while (ch != 0);
        try {
            pb.save();
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }


}

运行结果
这里写图片描述

这里写图片描述

这里写图片描述

这里写图片描述

这里写图片描述

猜你喜欢

转载自blog.csdn.net/lyf1997115/article/details/81569496