栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

用集合实现快递e栈,注释详细。

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

用集合实现快递e栈,注释详细。

我们前面已经写过面向过程的,面向对象的快递e栈了。

但其实在存储快递方面还有优化。

我们用面向对象时,不管是存快递,取快递,还是增删快递,用到的最常用的查找方法就是遍历,这样很麻烦。所以我们这次用合适的集合----map集合来存储快递。

因为map是一种依照键值对来存储的容器。键和值有一 一对应关系,所以这很方便查找。

一、视图部分
import java.util.InputMismatchException;
import java.util.Scanner;

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

    public void welcom() {
        System.out.println("-----欢迎使用白日梦想家快递柜-----");
    }
    public int begin(){
        System.out.println("请选择您的身份:1-用户,2-快递员,0-退出");
        int text = -1;  //因为用户输入的可能有问题,所以后面要try
        try{
            Scanner input = new Scanner(System.in);
            
            text = input.nextInt();     //获取到了用户输入的数字
            if(text<0||text>2){    //判断数字是否符合规定
                System.out.println("请输入0-2");
            }
             }catch (InputMismatchException e1){  //用户不输入数字会出的错
            System.out.println("请输入1/2");
        }
        return text;
    }
//用户界面
    public int user() {
        System.out.println("请输入取件码:");
        int num = -1;
        try{          //同上面的道理
            Scanner input = new Scanner(System.in);
            num = input.nextInt();
            }catch (InputMismatchException e1){
            System.out.println("请输入合法的数字!");
            return user();
        }
        return num;  //获取到了用户输入的取件码
    }
//快递员界面
    public int courier() {
        System.out.println("请输入您要选择的操作:1-快递录入 2-删除快递 3-修改快递 4-查看所有快递 0-返回上一级");
        int text = -1;
        try {
            Scanner input = new Scanner(System.in);
            text = input.nextInt();    //获取到了快递员的选择
            if(text<0||text>4){
                System.out.println("请输入1-4的数字。");
            }
        }catch (InputMismatchException e1){
            System.out.println("请输入1-4的数字。");
            return courier();
        }
        return text;
    }
//添加快递
    public int add() {
        System.out.println("请输入要存储的快递单号:");
        int num = input.nextInt();
        return num;
    }
    public String add2(){
        System.out.println("请输入要存储的快递公司:");
        String company = input.next();
        return company;
    }
//删除快递
    public int delete() {
        System.out.println("请输入要删除的快递编号:");
        int num = input.nextInt();      //获取要删除的快递编号
        return num;
    }
//修改快递
    public int modify() {
        System.out.println("请输入要修改的快递的单号:");
        int num = input.nextInt();     //获取要修改的快递单号
        return num;
    }
}

二、快递类

此类除了一个随机生成6位取件码的方法外,就是快递自身的属性与方法了。

import java.util.Objects;
import java.util.Random;

public class Express {
    private int num;    //单号
    private String company;    //公司

    Random r = new Random();   //随机生成6位取件码
    private int code = r.nextInt(999999)+100000;

    public Express() {
    }

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

    @Override
    public String toString() {
        return
                "快递单号:" + num +
                "   快递公司:'" + company + ''' +
                "   取件码:" + code +'n';

    }

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

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

    public int getNum() {
        return num;
    }

    public void setNum(int num) {
        this.num = num;
    }

    public String getCompany() {
        return company;
    }

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

    public Random getR() {
        return r;
    }

    public void setR(Random r) {
        this.r = r;
    }

    public int getCode() {
        return code;
    }

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

三、增删,查找等方法实现类

注意:这里用到HashMap,建议可以参考API帮助理解

import java.util.HashMap;
import java.util.Scanner;

public class Dao {
    
    HashMap expressHashMap1 = new HashMap<>(); //键为快递单号,值为快递
    HashMap expressHashMap2 = new HashMap<>(); //键为取件码,值为单号
    
    public boolean add(Integer num, Express e,int code) {
        for (int n : expressHashMap1.keySet()) {
            if (num.equals(n)) {
                System.out.println("快递已存过,请勿重复存储!");
                return false;
            }
        }
        expressHashMap1.put(num, e);  //存入键值对中
        expressHashMap2.put(code, num);
        System.out.println("存储成功!取件码为:"+e.getCode());
        return true;
    }
//取快递
    public void out(int code){
        if(expressHashMap1.get(expressHashMap2.get(code))==null){
            //注意:这里是通过传入的取件码先找到对应的单号,再将单号作为键传给expressHashMap1,来寻找值-快递
            System.out.println("未找到此快递,请重新输入:");
        }else {
            System.out.println(expressHashMap1.get(expressHashMap2.get(code)).toString());//同上
            expressHashMap1.remove(expressHashMap2.get(code));  //删除
            System.out.println("成功取出!");

        }
    }
    
    public void delete(int num) {
        if (expressHashMap1.get(num) == null) {
            System.out.println("未找到此快递,请重新输入:");
        } else {
            expressHashMap1.remove(num);
            System.out.println("删除成功!");
        }
    }
    
    public void modify(int num){
        if (expressHashMap1.get(num) == null){
            System.out.println("未找到此快递,请重新输入:");
        }else{
            System.out.println(expressHashMap1.get(num).toString());
            expressHashMap1.remove(num);  //删除旧快递
            System.out.println("请输入新的快递单号:");
            Scanner input = new Scanner(System.in);
            int newN = input.nextInt();
            System.out.println("请输入新的快递公司:");
            Scanner input2 = new Scanner(System.in);
            String company = input2.nextLine();
            Express newE = new Express(newN,company); //存入新快递
            expressHashMap1.put(newN,newE);
            expressHashMap2.put(newE.getCode(),newN);
            System.out.println(newE.toString());
            System.out.println("修改成功!");
        }
    }
    public void show(){
        if(expressHashMap1.isEmpty()){
            System.out.println("当前无快递!");
        }else {
            System.out.println(expressHashMap1.values().toString());
        }
    }
}

四、main函数来测试
public class Main {

    public static void main(String[] args) {
        
        Dao dao = new Dao();
        view v = new view();
        v.welcom();  //欢迎用户
       m: while (true) {  //可一直循环出现初始页面供用户选择
            switch (v.begin()) {
                case 1:  //用户
                    dao.out(v.user());
                    break;
                case 2:  //快递员
                    switch (v.courier()){
                        case 1:  //存快递
                            //创建快递对象,并存入单号和公司
                            int num = v.add();
                            Express e = new Express(num,v.add2());
                            dao.add(num,e,e.getCode());
                            break;
                        case 2:  //删除快递
                            int num2 = v.delete();
                            dao.delete(num2);
                            break;
                        case 3:   //修改快递
                            dao.modify(v.modify());
                            break ;
                        case 4:   //展示所有
                            dao.show();
                            break ;
                    }
                    break ;
                case 0:
                    System.out.println("-----感谢使用!-----");
                    break m;
            }
        }
    }
}

main类和视图类详细版可参考:

面向对象快递e栈

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/309522.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号