进阶通过面向对象来实现快递e栈

在这里插入图片描述
之前是用静态数组的方式来存储数据而且全部方法都堆砌在main方法的下面感觉不好写也逻辑很容易乱用面向对象的方法(将数组数据都存储在堆内存)后明显无论从代码量或者从编程时间来看都是一个优化而且思路清晰明了下面是我的具体代码
import java.util.Scanner;

public class Demo1{

public static void main(String[] args) {
    Express e=new Express();
    int over=0;
    while(true) {
        System.out.println("请输入您的身份");
        System.out.println("1,快递员");
        System.out.println("2.用户");
        int c = input.nextInt();
        if (c == 1) {
            System.out.println("请选择您的功能");
            System.out.println("1.增加快递2.删除快递3.修改快递4.查看所有快递5.退出");
            int c1 = input.nextInt();
            switch (c1) {
                case 1:
                    e.serve();
                    break;
                case 2:
                    e.delete();
                    break;
                case 3:
                    e.correction();
                    break;
                case 4:
                    e.check();
                    break;
                case 5:
                    over=1;
            }
            if(over==1)
                break;
        }
        else{
            e.userget();
            break;
        }
    }
}
static Scanner input=new Scanner(System.in);
static class Express{
    String []company=new String[100];
    int []number=new int[100];
    int []code=new int[100];
    static int count=0;
    int compare;
    void serve(){
        if(count<=100 ) {
            System.out.println("请输入你要添加的快递单号(0~100)");
            int i = input.nextInt();
            if(code[i]==0) {
                code[i] = 1;
                System.out.println("请输入快递公司");
                String str = input.next();
                company[i] = str;
                number[i] = (int) (Math.random() * (65535 + 1)*10);
                System.out.println("取件码为:" + number[i]);
                count++;
            }
            else
                System.out.println("此快递柜已有快递");
        }
        else
            System.out.println("快递已满");
    }
    void delete(){
        System.out.println("请输入你要删除的快递(0~100)");
        int i= input.nextInt();
        if(code[i]!=0){
            code[i]=0;
            company[i]=null;
            number[i]=0;
            count--;
        }
    }
    void correction(){
        System.out.println("请输入你要修改的快递号(0~100)");
        int i= input.nextInt();
        code[i]=0;
        company[i]=null;
        number[i]=0;
        System.out.println("请输入你新添加的快递单号(0~100)");
        int n = input.nextInt();
        if(code[n]==0) {
            code[n] = 1;
            System.out.println("请输入快递公司");
            String str = input.next();
            company[n] = str;
            number[n] = (int) (Math.random() * (65535 + 1));
            System.out.println("取件码为:" + number[i]);
        }
        else
            System.out.println("此快递柜已有快递");
    }
    void check(){
        System.out.println("单号\t"+"快递公司\t"+"取件码\t");
        for(int i=0;i<code.length;i++){
            if(code[i]==1)
            System.out.println(i+"\t"+company[i]+"\t"+number[i]+"\t");
        }

    }
    int userget(){
        System.out.println("请输入取件码取件");
        compare= input.nextInt();
        for(int i=0;i<code.length;i++){
            if(compare==number[i]){
                System.out.println("单号\t"+"快递公司\t");
                System.out.println(i+"\t"+company[i]+"\t");
                code[i]=0;
                number[i]=0;
                company[i]=null;
               return 0;
            }
        }
        System.out.println("输入错误请检查后重新输入");
        return userget();
    }
}

}

猜你喜欢

转载自blog.csdn.net/chh1ctrl/article/details/113600098