增删改查之企业管理系统

package com.welcomtoMorning.p1.p6;


import java.util.Arrays;
import java.util.Scanner;

public class CompanyApplication {

    public static String[] companies = {"阿里巴巴-2000", "华为-2005", "中兴-1998", "腾讯-2003", "京东-2005"};
    static Scanner scanner = new Scanner(System.in);

    public static void main(String[] args) {
        while (true) {
            menu();
            int option = scanner.nextInt();
            switch (option) {
                case 1:
                    //查询所有的企业
                    findAllCompanies();
                    break;
                case 2:
                    //根据关键字查询
                    System.out.println("请输入你要查的企业的关键字:");
                    String keyword = scanner.next();
                    findCompaniesByKeyword(keyword);
                    break;
                case 3:
                    //根据关键字统计符合关键字的企业有多少家
                    System.out.println("请输入你统计的企业的关键字:");
                    String keyword_Count = scanner.next();

                    int count = countCompaniesByKeyword(keyword_Count);
                    System.out.println("符合关键字的企业有" + count + "家");
                    break;
                case 4:
                    //根据企业成立时间区间查询
                    System.out.println("请输入你要查的企业的成立时间年份的区间:");

                    System.out.println("左界:");
                    int startTime = scanner.nextInt();

                    System.out.println("右界:");
                    int capTime = scanner.nextInt();

                    findCompanyByTimeRange(startTime, capTime);

                    break;
                case 5:
                    //新增一家企业
                    addNewCompany();
                    break;
                case 6:
                    //根据下标修改企业
                    updateCompany();
                    break;
                case 7:
                    //根据下标删除企业
                    System.out.println("请输入你要删除的企业的下标:");
                    int index = scanner.nextInt();
                    removeCompany(index);
                    break;


            }

        }
    }

    //    public static void removeCompany(int index) {
//        //索引值必须正确才能进行移除元素操作
//        if (index >= 0 && index < companies.length) {
//
//            //1.创建一个新的数组,长度要-1
//            String[] newCompanyList = new String[companies.length - 1];
//            // 新数组的索引总数
//
//            int newIndex = 0;
//            //2.把除了要删的那个元素以外的所有元素复制到新的数组中
//            for (int i = 0; i < companies.length; i++) {
//                //如果不是要删的那个元素的索引,就逐一复制到新的数组中
//                if (i != index) {
//                    newCompanyList[newIndex] = companies[i];
//                    //新的索引号加1
//                    newIndex++;
//                }
//            }
//            //将新数组赋值给上面的全局变量
//            companies = newCompanyList;
//        } else {
//            System.out.println("下标超出范围,删除失败!");
//        }
//    }
    public static void removeCompany(int index) {
        if (index >= 0 && index <= companies.length) {
            //1.新建一个比原数组小的新数组
            //int [] a = new int[];
            //int[] arr = {1,2,3};**
            //new 数据类型[] {1,2,3}

//            String[] newCompany =
            int newIndex = 0;
            String[] newCompany = new String[companies.length - 1];

            //把除了要删的那个元素以外所有的元素添加到新数组中
            for (int i = 0; i < companies.length; i++) {
                if (i != index) {
                    newCompany[newIndex] = companies[i];

                    //索引号自增
                    newIndex++;
                }
            }
            companies=newCompany;

        } else {
            System.out.println("下标超出范围,删除失败!");
        }
    }


    //修改企业
    public static void updateCompany() {
        //因为字符数组是引用类型,传递的是数组的地址,所以不需要返回值,即返回值类型为void
        //直接覆盖指定索引处的元素内容
        System.out.println("请输入要修改的公司下标:");
        int index = scanner.nextInt();

        //判断下标是否有效
        if (index > 0 && index < companies.length) {
            System.out.println("请输入公司名:");
            String companyName = scanner.next();

            System.out.println("请输入成立时间:");
            int companyStartTime = scanner.nextInt();
            //直接修改元素
            //写入指定索引元素的两个信息量
            companies[index] = companyName + "-" + companyStartTime;

        }


    }

    //加新公司
//    public static void addNewCompany() {
//        System.out.println("请输入公司名:");
//        String companyName = scanner.next();
//
//        System.out.println("请输入成立时间:");
//        int companyStartTime = scanner.nextInt();
//
//        //公司列表这个字符串需要扩容 ,使用.copyOf()方法在原先数组内容基础上加一个单位长度
//        String[] newCompanyList = Arrays.copyOf(companies, companies.length + 1);
//
//        //赋值到公司的数组最后一个位置:
//        newCompanyList[newCompanyList.length - 1] = companyName + "-" + companyStartTime;
//
//        //覆盖旧数组
//        companies = newCompanyList;
//        System.out.println("添加公司成功");
//
//    }
    public static void addNewCompany() {
        System.out.println("请输入公司名:");
        String companyName = scanner.next();

        System.out.println("请输入成立时间:");
        int companyStartTime = scanner.nextInt();

        String[] newCompanyArrays = Arrays.copyOf(companies, companies.length + 1);
        newCompanyArrays[newCompanyArrays.length - 1] = companyName + "-" + companyStartTime;

        companies = newCompanyArrays;
        System.out.println("添加公司成功");

    }


    public static void findAllCompanies() {
        for (int i = 0; i < companies.length; i++) {
            String company = companies[i];
            //没有换行
            System.out.print("下标:" + i + "\t");
            //调用下面的方法打印输出
            printCompany(company);
        }


    }

    public static void printCompany(String company) {
        //将字符串拆分成两个元素
        String[] info = company.split("-");
        System.out.println("企业名字:" + info[0] + "\t成立时间:" + info[1]);
    }

    //查询所有含有关键字的企业
    public static void findCompaniesByKeyword(String str) {

        boolean found = false;
        for (String company : companies) {
            String[] info = company.split("-");
            if (info[0].contains(str)) {
                printCompany(company);
                found = true;
            }
        }
        if (!found)
            System.out.println("你输入的字符没有在企业名录中找到!");
    }
    //

    public static void findCompanyByTimeRange(int startTime, int capTime) {  //价格上下界
        int count = 0;
        for (String company : companies) {
            String[] info = company.split("-");

            //得到成立时间
            int companyTime = Integer.parseInt(info[1]);
            //
            if (companyTime >= startTime && companyTime <= capTime) {
                printCompany(company);
//                count++;

            }
        }
//        System.out.println(count);
//        return count;


    }


    //按关键字统计企业个数
    public static int countCompaniesByKeyword(String keyword) {
        int count = 0;
        //遍历所有企业
        for (String company : companies) {
            String[] info = company.split("-");
            //如果关键字与企业名字相同,则计数加1

            if (info[0].contains(keyword)) {
                count++;

                printCompany(company);
            }
        }
        return count;
    }


    public static void menu() {
        System.out.println("--------欢迎进入企业管理系统--------");
        System.out.println("按1.查询所有企业");
        System.out.println("按2.根据企业中的关键字查询企业");
        System.out.println("按3.根据关键字统计符合关键字的企业有多少家");
        System.out.println("按4.根据企业创办时间区间查询企业");
        System.out.println("按5.新增一家企业");
        System.out.println("按6.根据下标修改一家企业");
        System.out.println("按7.根据下标删除一家企业");
        System.out.println("---------------------------------");
    }


}


写的作业,有菜单上那么多功能,共七个;

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

小面包人

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

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

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

打赏作者

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

抵扣说明:

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

余额充值