快递系统(文件)

任务概述

在这里插入图片描述

代码实现

1.创建Express对象属性

package ExpressUpdateFile.Express;


import java.io.Serializable;
import java.util.Objects;


public class Express implements Serializable {//快递属性


    private String number;//快递的单号
     private String company;//快递的公司
    private  int  code;//取件码

    public Express() {
    }

    public Express(String number, String company, int code) {
        this.number = number;
        this.company = company;
        this.code = code;
    }

    public String getNumber() {
        return number;
    }

    public void setNumber(String number) {
        this.number = number;
    }

    public String getCompany() {
        return company;
    }

    public void setCompany(String company) {
        this.company = company;
    }

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Express express = (Express) o;
        return Objects.equals(number, express.number);
    }

    @Override
    public int hashCode() {
        return Objects.hash(number);
    }

    @Override
    public String toString() {
        return "Express{" +
                "number='" + number + '\'' +
                ", company='" + company + '\'' +
                ", code='" + code + '\'' +
                '}';
    }



}

2.创建Express实现方法

papackage ExpressUpdateFile.Express;

import Serializable.Student;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Random;

public class ExpressDao {
    public static File file=new File("book.txt");
    public static ArrayList<Express> arrayList=new ArrayList<Express>();
    private Random random = new Random();
    private  SerializableUtil serializableUtil=new SerializableUtil();

    /**
     * 用于存储快递
     *
     * @param e
     * @return
     */
    boolean add(Express e)  {
        //增加
        e.setCode(randomCode());
        arrayList.add(e);
        try {
            SerializableUtil.mySerializable(arrayList,file);
        } catch (IOException ioException) {

        }
        return true;
    }

    /**
     * 模板方法设计模式
     *
     * @return
     */
    public  int randomCode() {
        while (true) {
            int code = (int) random.nextInt(900000) + 100000;
            Express e = findByCode(code);
            if (e == null) {
                return code;
            }

        }
    }

    /**
     * 通过单号查找快递
     *
     * @param
     */
    public Express findByNumber(String number) {
        for (Express e:arrayList) {
            if(number.equals(e.getNumber())){
                return  e;
            }
        }

        return null;
    }

    /**
     * 通过取件码查找快递
     *
     * @param code 要查询的快递取件码
     * @return 查询的结果,查询失败时返回null
     */
    public Express findByCode(int code) {
        for(Express e:arrayList){
            if(e!=null){
                if(e.getCode()==code){
                    return  e;
                }
            }
        }


        return null;
    }

    /**
     * 多余的操作  为了使MVC更圆润
     *
     * @param oldExpress
     * @param newExpress
     */
   public void update(Express oldExpress, Express newExpress) {
        delete(oldExpress);
        add(newExpress);
       try {
           SerializableUtil.mySerializable(arrayList,file);
       } catch (IOException ioException) {
           ioException.printStackTrace();
       }


    }

    public boolean delete(Express e) {

        for (Express e1 : arrayList) {
            if(e1.equals(e)) {
               arrayList.remove(e1);
                try {
                    SerializableUtil.mySerializable(arrayList,file);
                    return arrayList.remove(e1);
                } catch (IOException ioException) {
                    ioException.printStackTrace();
                }

            }
        }
        return false;
    }





    /**
     * 查看所有
     *
     * @param
     */
    public ArrayList<Express> findAll(){
        try {
            Object o = SerializableUtil.myDesSerializable(file);
            if(o instanceof Express){
               Express s= (Express) o;
                System.out.println(s);
            }
        }catch (Exception e){
        }
        return arrayList;
    }

}

3.创建视图

package ExpressUpdateFile.Express;

import TeacherExpressIO.Parcel;
import TeacherExpressIO.ParcelIMPL;

import java.io.*;
import java.util.ArrayList;
import java.util.Scanner;


public class view {//视图
    Scanner scanner = new Scanner(System.in);

    /**
     * 欢迎界面
     */
    void welcome() {
        System.out.println("欢迎来到快递驿站!");
        System.out.println("正在读取数据....");
        readFile(ExpressDao.file);

    }

    /**
     * 把文件里的数据进行反序列化
     */
    public static void readFile(File file) {
        if (file.length() != 0) {
            try {
                Object o = SerializableUtil.myDesSerializable(file);
                ArrayList<Express> e1 =(ArrayList<Express>) o;
                for(Express e:e1){
                    ExpressDao.arrayList.add(e);
                }
            } catch (IOException e) {
//                e.printStackTrace();
            } catch (ClassNotFoundException e) {
//                e.printStackTrace();
            }
        }

  }


    /**
     * 结束界面
     */
    void bye() {
        System.out.println("期待你下次再来!");

    }


    /**
     * 选择身份的菜单
     */
    int menu() {

        System.out.println("请选择你的身份:1.管理员 2.用户 3.退出");
        String s = scanner.nextLine();
        int n = -1;
        try {
            n = Integer.parseInt(s);
        } catch (NumberFormatException e) {

        }
        if (n < 1 || n > 3) {
            System.out.println("输入有误,请重新选择!");
            return menu();
        }
        return n;


    }

    /**
     * 管理员界面
     */
    public int cMenu() {

        System.out.println("请选择功能:1.快递录入 2.修改快递 3.删除快递 4.查看所有快递 5.返回上一层");
        String s = scanner.nextLine();
        int n = -1;
        try {
            n = Integer.parseInt(s);
        } catch (NumberFormatException e) {

        }
        if (n < 1 || n > 5) {
            System.out.println("输入有误,请重新选择!");
            return cMenu();
        }
        return n;

    }

    /**
     * 快递录入
     */
    public Express insert() {
        System.out.println("请根据提示,输入快递信息:");
        System.out.println("请输入快递的单号:");
        String number = scanner.nextLine();
        System.out.println("请输入快递公司:");
        String company = scanner.nextLine();
        Express e = new Express();
        e.setNumber(number);
        e.setCompany(company);
        try {
            SerializableUtil.mySerializable(e,ExpressDao.file);
        } catch (IOException ioException) {
            ioException.printStackTrace();
        }
        return e;
    }

    /**
     * 提示用户输入快递单号
     */
    public String findByNumber() {
        System.out.println("请根据提示,输入快递信息:");
        System.out.println("请输入操作的快递单号:");
        String number = scanner.nextLine();
        return number;
    }

    /**
     * 显示快递信息
     *
     * @param e
     */
    public void printExpress(Express e) {
        System.out.println("快递信息如下:");
        System.out.println("快递公司:" + e.getCompany() + "快递单号:" + e.getNumber() + "取件码:" + e.getCode());

    }

    /**
     * 修改快递信息
     *
     * @return
     */
    public void update(Express e) {
        System.out.println("请输入新的快递单号:");
        String number = scanner.nextLine();
        System.out.println("请输入新的快递公司:");
        String company = scanner.nextLine();
        e.setCompany(company);
        e.setNumber(number);
        try {
            SerializableUtil.mySerializable(e,ExpressDao.file);
        } catch (IOException ioException) {
            ioException.printStackTrace();
        }

    }

    /**
     * 询问是否删除
     */
    public int delete() {
        System.out.println("是否确认删除 1.确认删除 2.取消操作 3.退出");
        String s = scanner.nextLine();
        int n = -1;
        try {
            n = Integer.parseInt(s);
        } catch (NumberFormatException e) {

        }
        if (n < 1 || n > 3) {
            System.out.println("输入有误,请重新选择!");
            return delete();
        }
        return n;
    }

    /**
     * 查看所有快递    将给定数组的快递信息, 遍历显示
     *
     * @param
     */
    public void printAll(ArrayList arrayList) {
        int count = 0;
        for (int i = 0; i < arrayList.size(); i++) {
            if (arrayList.get(i) != null) {
                count++;
                System.out.print("第" + (i + 1) + "个");
                printExpress((Express) arrayList.get(i));
            }
        }
        if (count == 0) {
            System.out.println("暂无快递信息!");
        }

    }

    /**
     * 用户菜单
     *
     * @return
     */
    public int uMenu() {
        System.out.println("请根据提示,进行取件:");
        System.out.println("请输入取件码:");
        String code = scanner.nextLine();
        int n = -1;
        try {
            n = Integer.parseInt(code);
        } catch (NumberFormatException e) {

        }
        if (n < 100000 || n > 999999) {
            System.out.println("输入有误,请重新输入,长度必须为6位数!");
            return uMenu();
        }
        return n;
    }

    public void expressExist() {
        System.out.println("此单号的快递已存在,请勿重复存储!");
    }

    public void printNull() {
        System.out.println("此单号的快递不存在!");
    }

    /**
     * 打印快递单号
     *
     * @param e
     */
    public void printCode(Express e) {
        System.out.println("快递的取件码为: " + e.getCode());
    }

    public void success() {
        System.out.println("操作成功!");
    }

}

4.用Serializable工具进行文件的序列化和反序列化

package ExpressUpdateFile.Express;

import java.io.*;

public class SerializableUtil {
    /**
     * 序列化
     */
    public static void mySerializable(Object obj, File file) throws IOException {
        OutputStream out = new FileOutputStream(file);
        ObjectOutputStream objout = new ObjectOutputStream(out);
        objout.writeObject(obj);
        objout.close();
    }

    /**
     * 反序列化
     *
     * @param file
     * @return
     * @throws IOException
     */
    public static Object myDesSerializable(File file) throws IOException, ClassNotFoundException {
        InputStream into = new FileInputStream(file);
        ObjectInputStream objint = new ObjectInputStream(into);
        Object o = objint.readObject();
        return o;

    }


}

4.主方法Main进行运行

package ExpressUpdateFile.Express;

import java.util.ArrayList;

public class Main {
    //初始化视图对象
    private static ExpressUpdateFile.Express.view view = new view();
    //初始化Dao对象
    private static ExpressDao expressDao = new ExpressDao();
    public static void main(String[] args) {
        //1.欢迎
        view.welcome();


        //2.弹出身份选择菜单
        m:
        while (true) {
            int menu = view.menu();
            switch (menu) {
                case 1:
                    cClient();
                    break;
                case 2:
                    uClient();
                    break;
                case 3:
                    break m;
                default:
                    break ;
            }
        }
        view.bye();

    }

    /**
     * 用户菜单
     */
    private static void uClient() {
        //取件码的获取

        int code= view.uMenu();
        //2. 根据取件码取快递
        Express e=expressDao.findByCode(code);
        if(e==null){
            view.printNull();
        }else {
            view.success();
            view.printExpress(e);
            expressDao.delete(e);
        }
    }

    /**
     * 快递员菜单
     */
    private static void cClient() {
        while (true) {
            int menu = view.cMenu();
            switch (menu) {
                case 1: {
                    //提示输入快递信息
                    Express e = view.insert();
                    //2.此快递是否已经存储过
                    Express e2 = expressDao.findByNumber(e.getNumber());
                    //3.存储快递
                    if (e2 == null) {
                        //未存储过 存
                        expressDao.add(e);
                        view.printExpress(e);

                    } else {
                        //单号已存在
                        view.expressExist();
                    }

                }
                break ;
                //快递修改
                case 2: {
                    //1.  提示输入快递信息
                    String number = view.findByNumber();
                    //2.查找快递
                    Express e = expressDao.findByNumber(number);
                    Express e2 = e;
                    //3.打印快递信息
                    if (e == null) {
                        view.printNull();
                    } else {
                        view.printExpress(e);
                        //提示修改
                        view.update(e2);
                        expressDao.update(e, e2);
                        view.printExpress(e);
                    }

                }
                break ;
                //删除
                case 3: {
                    String number = view.findByNumber();
                    //2.通过快递单号查找快递
                    Express e = expressDao.findByNumber(number);
                    if (e == null) {
                        view.printNull();
                    } else {
                        view.printExpress(e);
                        int type = view.delete();
                        if (type == 1) {
                            expressDao.delete(e);
                            view.success();
                        }
                    }
                }
                break ;
                case 4: {
                   ArrayList arrayList =  expressDao.findAll();
                    view.printAll( arrayList);
                }
                break ;
                case 5:
                    return;
                default:
                    return;
            }
        }
    }

}


运行结果

欢迎来到快递驿站!
正在读取数据…
请选择你的身份:1.管理员 2.用户 3.退出
注意:后续的操作------------略
请选择你的身份:1.管理员 2.用户 3.退出

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小高求学之路

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值