ss:如果单车列表里面的单车删完了,值就为空,需要判断
@Override public void putBike(BikeCompany[] bikeCompanies, Scanner scanner) { System.out.println("迷你共享单车管理系统>>>管理员操作>>>投放单车"); //显示公司列表 for (BikeCompany bikeCompany : bikeCompanies) { System.out.println(bikeCompany.getCompanyId() + "-->" + bikeCompany.getCompanyName()); } //选择单车品牌 System.out.println("请输入要操作的单车品牌:"); int inputCompanyId = scanner.nextInt(); //根据品牌id查找品牌信息 BikeCompany bikeCompany = getBikeCompanyById(inputCompanyId, bikeCompanies); //先判断 if (bikeCompany == null) { System.out.println("[投放失败,该单车品牌不存在!]"); return; } //允许投放 System.out.println("请输入要投放的单车数量:"); int count = scanner.nextInt(); //获取单车品牌下的单车列表 Bike[] putBikes = bikeCompany.getBikes(); //001
//如果单车列表里面的单车删完了,值就为空,需要判断 //扩容之前的数组的长度 int oldLength = (putBikes == null) ? 0 : putBikes.length; //扩容之前最后一个单车的编号 int lastId = (putBikes == null) ? getStartId(bikeCompany.getCompanyName()) : putBikes[oldLength - 1].getBikeId(); //扩容 putBikes = (putBikes == null) ? new Bike[count] : Arrays.copyOf(putBikes, putBikes.length + count); //002 //循环来存入单车对象 for (int i = oldLength; i < putBikes.length; i++) { lastId = ++lastId; putBikes[i] = new Bike(lastId, bikeCompany.getCompanyName() + lastId, BikeConstants.UN_LEND_STATE, null, null); } //将扩容之后的数组反填充 bikeCompany.setBikes(putBikes); //总数 bikeCompany.setSum(putBikes.length); System.out.println("投放<" + count + ">辆<" + bikeCompany.getCompanyName() + ">车成功!"); } /** * 根据品牌编号查找品牌信息 * * @param id * @param bikeCompanies * @return */ private BikeCompany getBikeCompanyById(int id, BikeCompany[] bikeCompanies) { for (BikeCompany bikeCompany : bikeCompanies) { if (id == bikeCompany.getCompanyId()) { return bikeCompany; } } return null; } /** * 根据公司名称获取起始ID * * @param name * @return */ private int getStartId(String name) { switch (name) { case BikeConstants.OFO: return BikeConstants.OFO_START_ID; case BikeConstants.HALO: return BikeConstants.HALO_START_ID; case BikeConstants.MO_BAI: return BikeConstants.MO_BAI_START_ID; } return 0; }
package com.javasm.constants; import javax.swing.plaf.synth.SynthTextAreaUI; /** * @Author:liulei * @Version:1.0 * @Date:2022/11/24-16:13 * @Since:jdk1.8 * @Description: */ public class BikeConstants { //公司名称 public static final String OFO = "ofo小黄车"; public static final String MO_BAI = "摩拜"; public static final String HALO = "哈罗"; //单车借出状态(0、未借出 1、已借出) public static final int LEND_STATE = 1; public static final int UN_LEND_STATE = 0; //单车的起始ID public static final int OFO_START_ID = 99; public static final int MO_BAI_START_ID = 199; public static final int HALO_START_ID = 299; }
标签:删空后,return,putBikes,BikeManage,bikeCompany,int,单车,public From: https://www.cnblogs.com/19981206-zxb/p/16927088.html