实现快递管理系统(序列化,网络编程)

任务要求:

加入网络编程+io处理。要求:快递数据永久存储在文件中 , 每次程序打
开加载数据. 客户端不存储任何数据, 全部传输到服务器来存储;

1.创建Express类 里面包括四个成员属性,省略了get/set/tostring等方法
public class Express implements Serializable {
    private String company;//快递公司
    private String number;//快递单号
    private int code;//取件码
    private String phone;//手机号码:使用URL知识可发送短信,本次未加入

    public Express(String company, String number, int code, String phone) {
        this.company = company;
        this.number = number;
        this.code = code;
        this.phone = phone;
     }
    }
2.创建Express的子类,管理快递信息

包括随机取件码、录入新快递、将快递信息保存到本地、取出快递、查看所有快递等

public class Manage extends Express implements Serializable {

    //设置取件码
    public int addcode() throws IOException, ClassNotFoundException {
        int code = 0;
        Random random = new Random();
        File file = new File("g:\\Express.txt");
        file.createNewFile();
        //当有快递信息时
        if (file.length() != 0) {
            //遍历文件中的快递信息 ,查询是否有相同的取件码;这里没有考虑所有取件码都被占用的情况
            ObjectInputStream ois = new ObjectInputStream(new FileInputStream(file));
            ArrayList<Express> list = (ArrayList<Express>) ois.readObject();
            ois.close();
            wh1:
            while (true) {
                //随机六位取件码
                code = random.nextInt(900000) + 100000;
                for (Express e : list
                ) {
                    if (e.getCode() == code) {
                        continue wh1;
                    }
                }
                return code;
            }
        }
        //没有快递信息时
        code = random.nextInt(900000) + 100000;
        return code;

    }

    //录入快递
    public ArrayList<Express> addExpress() throws IOException, ClassNotFoundException {
        ArrayList<Express> list = new ArrayList<>();
        Scanner input = new Scanner(System.in);
        System.out.println("输入快递公司");
        String company = input.next();
        System.out.println("输入快递单号");
        String number = input.next();
        System.out.println("输入手机号");
        String phone = input.next();
        int code = addcode();//随机取件码
        System.out.println(code);
        Express express = new Express(company, number, code, phone);
        list.add(express);//将快递添加到集合
        return list;//返回此集合;
    }

    //保存快递信息到本地
    public void save(ArrayList<Express> list) throws IOException, ClassNotFoundException {
        File file = new File("g:\\Express.txt");
        file.createNewFile();
        //如果文件中有快递信息
        if (file.length() != 0) {
            //反序列化
            ObjectInputStream ois = new ObjectInputStream(new FileInputStream(file));
            ArrayList<Express> list1 = (ArrayList<Express>) ois.readObject();
            ois.close();
            list1.addAll(list);//将新快递信息加入
            //序列化
            ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(file));
            oos.writeObject(list1);
            oos.close();
        } else {
            ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(file));
            oos.writeObject(list);
            oos.close();
        }
    }

    //取出快递
    public StringBuffer take(int code) throws IOException, ClassNotFoundException {
        StringBuffer text = new StringBuffer();
        File file = new File("g:\\Express.txt");
        file.createNewFile();
        //反序列化
        ObjectInputStream ois = new ObjectInputStream(new FileInputStream(file));
        ArrayList<Express> list = (ArrayList<Express>) ois.readObject();
        ois.close();
        //根据取件码定位快递信息
        for (Express e : list
        ) {
            if (e.getCode() == code) {
                text.append("位置: " + list.indexOf(e) + " ," + "快递信息:" + e.toString());
                list.remove(e);//将此快递删除
                //序列化
                ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(file));
                oos.writeObject(list);
                oos.close();
                return text;//返回此快递信息
            }
        }
        return text.append("未找到");//如果没有则返回未找到
    }

    //查看所有快递
    public ArrayList<Express> show() throws IOException, ClassNotFoundException {
        File file = new File("g:\\Express.txt");
        file.createNewFile();
        //反序列化
        ObjectInputStream ois = new ObjectInputStream(new FileInputStream(file));
        ArrayList<Express> list = (ArrayList<Express>) ois.readObject();
        ois.close();
        return list;//返回快递信息的集合
    }

}
3.服务器:与客户端交流并存储信息到本地
public class Server  {
    public static void main(String[] args) throws IOException, ClassNotFoundException {
        ServerSocket serverSocket = new ServerSocket(65534);
        Manage m = new Manage();
        w1:
        while (true) {
            //等待连接
            Socket socket = serverSocket.accept();
            InputStream is = socket.getInputStream();
            int x = is.read();//接收客户端传过来的序号
            //功能选择
            switch (x) {
                case 0: {
                    BufferedReader br = new BufferedReader(new InputStreamReader(is));
                    //服务器关闭
                    if (br.readLine().equals("程序结束")) {
                        br.close();
                        break w1;
                    }
                }
                case 1: {
                    ObjectInputStream ois = new ObjectInputStream(is);
                    //接收到的一个集合形式的快递信息
                    ArrayList<Express> list = (ArrayList<Express>) ois.readObject();
                    ois.close();
                    m.save(list);//保存到本地文件
                    break;
                }
                case 2: {
                    BufferedReader br = new BufferedReader(new InputStreamReader(is));
                    int code = Integer.valueOf(br.readLine());//读取返回的是String类型的取件码,读取其中的数字;
                    StringBuffer text = m.take(code);//取出快递信息;
                    br.close();
                    socket = serverSocket.accept();
                    OutputStream os = socket.getOutputStream();
                    PrintStream ps = new PrintStream(os);
                    ps.println(text);//将快递信息发给客户端
                    ps.close();
                    break;
                }
                case 3: {
                    is.close();
                    socket = serverSocket.accept();
                    OutputStream os = socket.getOutputStream();
                    ObjectOutputStream oos = new ObjectOutputStream(os);
                    oos.writeObject(m.show());//将快递信息以集合形式发给客户端
                    oos.close();
                    break;
                }
            }
        }
    }
}

4.客户端:用户操作界面
public class Client {
    public static void main(String[] args) throws IOException, ClassNotFoundException {
        w1:
        while (true) {
            Socket socket = new Socket("127.0.0.1", 65534);
            OutputStream os = socket.getOutputStream();
            try {
                System.out.println("输入0结束,输入1录入快递,输入2取快递,输入3查看所有快递");
                Scanner input = new Scanner(System.in);
                int x = input.nextInt();
                //将序号发给服务器,功能一一对应;
                os.write(x);
                switch (x) {
                    //结束程序
                    case 0: {
                        PrintStream ps = new PrintStream(os);
                        ps.println("程序结束");
                        ps.close();
                        break w1;//跳出while循环
                    }
                    case 1: {
                        ObjectOutputStream oos = new ObjectOutputStream(os);
                        oos.writeObject(new Manage().addExpress());//创建快递信息并将此信息发给服务器
                        oos.close();
                        break;
                    }
                    case 2: {
                        System.out.println("请输入取件码");
                        PrintStream ps = new PrintStream(os);
                        ps.println(input.nextInt());//将取件码传给服务器
                        ps.close();
                        socket = new Socket("127.0.0.1", 65534);
                        InputStream is = socket.getInputStream();
                        BufferedReader br = new BufferedReader(new InputStreamReader(is));
                        System.out.println(br.readLine());//读取由服务器传过来的快递信息
                        br.close();
                        break;
                    }
                    case 3: {
                        os.close();
                        socket = new Socket("127.0.0.1", 65534);
                        InputStream is = socket.getInputStream();
                        ObjectInputStream ois = new ObjectInputStream(is);
                        ArrayList<Express> list = (ArrayList<Express>) ois.readObject();//服务器将快递信息以集合的形式传过来;
                        ois.close();
                        //遍历
                        for (Express e :
                                list) {
                            System.out.println(e.toString());
                        }
                        break;
                    }
                }
            } catch (Exception e) {
                System.out.println("操作有误");
                continue w1;
            }
        }
    }
}
5.运行效果

实现快递管理系统(序列化,网络编程)_第1张图片

你可能感兴趣的:(java,java)