快递管理训练

学习完集合后,写了关于快递存储的集合练习,代码如下:
在这里插入图片描述
在这里插入图片描述

package ExpressProjectDemo2.bean;

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;
    }

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

    public String getNumber() {
        return number;
    }

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

    public String getCompany() {
        return company;
    }

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

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

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

    public int getCode() {
        return code;
    }

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



import ExpressProjectDemo2.bean.Express;

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

public class ExpressDaoJi implements Serializable{
    private ArrayList<Express> arr = new ArrayList<>();
    static String PATH = "MyExpress.txt";
    ObjectOutputStream oos;
    /**
     * 读取文件中的集合
     */
    public ExpressDaoJi() throws IOException, ClassNotFoundException {
        File file = new File(PATH);
        ObjectInputStream ois = null;
        if (file.exists()) {
            //如果文件存在,在文件中读取集合
            try {
                ois = new ObjectInputStream(new FileInputStream(PATH));
                Object o = ois.readObject();
                arr = (ArrayList<Express>) o;
            } catch (IOException | ClassNotFoundException e) {
                e.printStackTrace();
                arr = new ArrayList<Express>();
                throw new IOException("无法从数据文件中读取集合");
            }finally {
                ois.close();
            }
        }else{
            //文件不存在,新建集合存储
            ArrayList<Express> arr = new ArrayList<>();
        }

    }

    /**
     * 写快递
     */
    public void write() throws IOException {
        if(oos == null){
            oos = new ObjectOutputStream(new FileOutputStream(PATH));
        }
        //将集合写入文件
        oos.writeObject(arr);
        //oos.close();
    }
    //当前存储的快递数量
    //private int size = getSize();

    public void close() throws IOException {
        oos.close();
    }
    private int index = -1;
    private Random random = new Random();
    /**
     * 用于存储快递
     * @param e
     * @return
     */
    public boolean add(Express e) throws IOException {
        //判断能否存储(满了?)
        if(arr != null &&arr.size() ==100){
            return false;
        }else{
            final boolean a = arr.add(e);//按顺序添加
            //产生取件码并设置
            int code = Randomcode();
            e.setCode(code);
            if(a){
                this.write();
            }
            return true;
        }
    }

    /**
     * 随机生成取件码
     * @return
     */
    public int Randomcode(){
        //0-99999
        //100000-999999
        while(true){
            int code = random.nextInt(899999)+100000;

            //查询取件码是否重复
            Express e = findByCode(code);
            if(e == null){
                return code;//e为空,可以存
            }
        }
    }


    /**
     * 根据单号查询快递
     * @param number
     * @return
     */
    public Express findByNumber(String number){
        if(arr != null)
        for(Express e:arr){
            if(e.getNumber().equals(number)){
                return e;
            }
        }    return null;
    }
    /**
     * 根据取件码查询快递
     * @param code,查询失败时返回null
     */
    public Express findByCode(int code){
        for (Express e:arr){
            if(e.getCode() == code){
                return e;
            }
        }   return null;
    }

    /**
     * 修改快递,多余的操作,为MVC更圆润
     * @param oldExpress
     * @param newExpress
     */
    public void update(Express oldExpress,Express newExpress) throws IOException {
        delete(oldExpress);
        add(newExpress);
    }
    public void delete(Express e) throws IOException {
        p:for (Express e1:arr){
            if(e1.getNumber().equals(e.getNumber())){
                final boolean b = arr.remove(e1);
                if(b){
                    this.write();
                }
                break p;
            }
        }
    }
    public ArrayList<Express> findAll(){
        return arr;
    }
    public int getSize(){
        return arr.size();
    }


}

package ExpressProjectDemo2.views;



import ExpressProjectDemo2.bean.Express;

import java.util.ArrayList;
import java.util.Scanner;

public class ViewsJi {
    private Scanner input = new Scanner(System.in);

    /**
     * 欢迎
     */
    public void welcome(){
        System.out.println("欢迎使用快递e栈!");
    }

    /**
     * 再见
     */
    public void bye(){
        System.out.println("欢迎下次使用!");
    }

    /**
     * 选择身份的菜单
     * @return
     */
    public int menu(){
        System.out.println("请根据提示,选择身份:");
        System.out.println("1-快递员");
        System.out.println("2-用户");
        System.out.println("0-退出");
        //这里的代码逻辑 相较于. nextInt优点在哪?
        //单思考这个方法内的逻辑,没有什么优点.
        //但是思考全局代码,是有优点:所有方法均使用nextLine ,不会因为输入产生冲突,还可以更好的接收各种类型的数据
        String text = input.nextLine();
        int num = -1;
        try{
            num = Integer.parseInt(text);
        }catch(NumberFormatException e){

        }if(num<0||num>4){
            System.out.println("请输入正确的信息 !");
            return menu();
        }
        return num;
    }

    /**
     * 快递员菜单
     * @return
     */
    public int cMenu(){
        System.out.println("请根据提示,选择操作信息:");
        System.out.println("1-录入快递");
        System.out.println("2-修改快递");
        System.out.println("3-删除快递");
        System.out.println("4-查看所有快递");
        System.out.println("0-返回上级目录");
        String text = input.nextLine();
        int num = -1;
        try{
            num = Integer.parseInt(text);
        }catch(NumberFormatException e){

        }if(num<0||num>4){
            System.out.println("请输入正确的信息!");
            return cMenu();
        }
        return num;
    }

    /**
     * 用户菜单
     * @return
     */
    public int uMenu(){
        System.out.println("请根据提示,选择操作信息:");
        System.out.println("请输入取件码:");
        String code = input.nextLine();
        int num = -1;
        try{
            num = Integer.parseInt(code);
        }catch(NumberFormatException e){

        }if(num<100000||num>999999){
            System.out.println("请输入正确的信息!");
            return uMenu();
        }
        return num;
    }

    /**
     * 快递员录入快递
     * @return包含了快递单号和快递公司的名称
     */
    public Express insert(){
        System.out.println("请根据提示,选择操作信息:");
        System.out.println("请输入快递单号:");
        String number = input.nextLine();
        System.out.println("请输入快递公司:");
        String company = input.nextLine();
        Express e = new Express();
        e.setNumber(number);
        e.setCompany(company);
        return e;
    }
    /**
     *提示用户输入信息
     */
    public String findByNumber(){
        System.out.println("请根据提示,选择操作信息:");
        System.out.println("请输入要修改的快递单号:");
        String number = input.nextLine();
        return number;
    }
    /**
     *打印快递信息
     */
    public void print(Express e){
        System.out.println("快递单号:"+e.getNumber()+"\t快递公司:"+e.getCompany()+"\t取件码:"+e.getCode());

    }
    /**
     * 快递员修改快递
     */
    public void update(Express e){
        System.out.println("===请根据提示,选择操作信息:===");
        System.out.println("请输入新的快递单号:");
        String number = input.nextLine();
        System.out.println("请输入新的快递公司:");
        String company = input.nextLine();
        e.setNumber(number);
        e.setCompany(company);
    }
    /**
     *删除快递,0-取消删除,1-确认删除
     */
    public int delete() {
        System.out.println("请输入是否确认删除:");
        System.out.println("1-确认删除:");
        System.out.println("0-取消删除:");
        String text = input.nextLine();
        int num = -1;
        try {
            num = Integer.parseInt(text);
        } catch (NumberFormatException e) {

        }
        if (num < 0 || num > 1) {
            System.out.println("请输入正确的信息!");
            return delete();
        }
        return num;
    }
    /**
     * 查看所有快递:将给定数组的快递信息遍历显示。
     */
    public void printAll(ArrayList<Express> es) {
        int count = 0;
        if(es != null) {
            if (es.size() > 0) {
                count++;
                for (Express s : es) {
                    System.out.println("快递单号:" + s.getNumber() + "   快递公司:" + s.getCompany() + "   取件码:" + s.getCode());
                }
            } else {
                System.out.println("===暂无快递信息!===");
            }
        }
    }
    public void expressExist() {
        System.out.println("===此单号已经存在,请勿重复存储!===");
    }

    public void printNull() {
        System.out.println("===快递不存在,请重新输入!===");
    }

    public void printExpress(Express e) {
        System.out.println("快件的取件码为:"+e.getCode());
    }

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

    /*public void printAll(Express[] es){
        if(es == null || es.length == 0){
            System.out.println("快递暂时为空!");
            return;
        }
        for(int i=0;i<es.length;i++){
            System.out.println(es[i]);

        }
        }*/




}
package ExpressProjectDemo2.main;


import ExpressProjectDemo2.bean.Express;
import ExpressProjectDemo2.dao.ExpressDaoJi;
import ExpressProjectDemo2.views.ViewsJi;


import java.io.IOException;
import java.util.ArrayList;

public class MainJi {
    //初始化视图对象
    private static ViewsJi v = new ViewsJi();
    //初始化dao对象
    static ExpressDaoJi dao;
    static{
        try {
            dao = new ExpressDaoJi();
        } catch (IOException |ClassNotFoundException e) {
            e.printStackTrace();
        }
    }


    public static void main(String[] args) throws IOException {
        //1、欢迎
        v.welcome();
        m:while(true){
            //2、弹出身份菜单
            int menu = v.menu();
            switch(menu){
                case 0:
                    break m;
                case 1:
                    cClient();
                    break;
                case 2:
                    uClient();
                    break;
            }
        }
        v.bye();
    }

    private static void uClient() throws IOException {
        //1、    取件码获取
        int code = v.uMenu();
        //2、    根据取件码获取快递
        Express e = dao.findByCode(code);
        if(e == null){
            v.printNull();
        }else{
            v.success();
            v.printExpress(e);
            dao.delete(e);
        }

    }

    private static void cClient() throws IOException {
        while(true){
            int menu = v.cMenu();
            switch(menu){
                case 0:
                    return;
                case 1:{//快递添加
                    //1、    提示输入快递信息
                    Express e = v.insert();
                    //2、    此快递是否已经存储过
                    Express e2 = dao.findByNumber(e.getNumber());
                    //3、    存储快递
                    if(e2 != null){
                        //单号再快递柜中已经存在
                        v.expressExist();
                    }else{
                        //未存储过,存储
                        dao.add(e);
                        v.printExpress(e);
                    }
                }
                break;
                case 2:{//快递修改
                    //1、    提示输入快递信息
                    String number = v.findByNumber();
                    //2、    查找数据
                    Express e = dao.findByNumber(number);
                    Express e2 = e;
                    //3、    打印快递信息
                    if(e2 == null){
                        v.printNull();
                    }else{
                        v.printExpress(e);
                        //4、提示修改
                        v.update(e2);
                        dao.update(e,e2);
                        v.printExpress(e2);
                    }
                }
                break;
                case 3:{
                    //删除快递
                    //1、   输入快递单号
                    String number = v.findByNumber();
                    //2、   查找快递对象
                    Express e = dao.findByNumber(number);
                    if(e==null){
                        v.printNull();
                    }else{
                        v.printExpress(e);
                        int type = v.delete();
                        if(type == 1){
                            dao.delete(e);
                            v.success();
                        }
                    }
                }
                break;
                case 4:{
                    ArrayList<Express> data = dao.findAll();
                    v.printAll(data);
                }
                break;
            }
        }
    }
}

  • 4
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值