使用纯控制台输入模拟数据的增删改查操作。

  1 import java.util.Random;
  2 import java.util.Scanner;
  3 
  4 public class Express {
  5     public static Scanner input = new Scanner(System.in);
  6     public static String[] numberArr = new String[100];//快递单号数组
  7     public static String[] companyArr = new String[100];//公司名称数组
  8     public static int[] codeArr = new int[100];//取件码
  9     public static Random random = new Random();
 10     public static int index = 0;//快递柜中共有多少个快递
 11 
 12     public static void main(String[] args) {
 13         //展示菜单
 14         while (true){
 15             int id = startMenu();
 16             if (id==0){
 17                 return;
 18             }
 19         }
 20     }
 21 
 22     /**
 23      * 开始菜单
 24      */
 25     public static int startMenu(){
 26         System.out.println("================欢迎使用快递柜=====================");
 27         System.out.println("请输入身份:1-快递员 2-用户 0-退出");
 28         int id = 0;
 29         do{
 30             id = input.nextInt();
 31             if (id==1){
 32                 //快递员--展示快递员的菜单
 33                 deliverymanMenu();
 34                 break;
 35             }else if (id==2){
 36                 //普通用户
 37                 userMenu();
 38                 break;
 39             }else if (id==0){
 40                 System.out.println("谢谢使用!");
 41                 return 0;
 42             }else {
 43                 System.out.println("选择有误!请重新输入:");
 44             }
 45         }while(true);
 46         return id;
 47     }
 48 
 49     /**
 50      * 普通用户菜单
 51      */
 52     public static void userMenu(){
 53         System.out.println("请输入取件码:");
 54         int code = input.nextInt();
 55         int codeIndex = isExist(code);
 56         if (codeIndex==-1){
 57             System.out.println("未找到快递!");
 58         }else {
 59             del(codeIndex);
 60             System.out.println("取件成功!");
 61         }
 62     }
 63 
 64     /**
 65      * 删除快递
 66      * @param delIndex
 67      */
 68     private static void del(int delIndex) {
 69         if (delIndex!=numberArr.length-1){
 70             for (int j = delIndex; j < index; j++) {
 71                 numberArr[j]=numberArr[j+1];
 72                 companyArr[j]=companyArr[j+1];
 73                 codeArr[j]=codeArr[j+1];
 74             }
 75         }
 76         index--;
 77     }
 78 
 79     /**
 80      * 快递员菜单
 81      */
 82     public static void deliverymanMenu(){
 83         System.out.println("请选择操作:1-存快递 2-删除快递 3-修改快递信息 4-查看所有快递");
 84         int id = input.nextInt();
 85         if (id==1){
 86             saveExpress();
 87         }else if (id==2){
 88             delExpress();
 89         }else if (id==3){
 90             System.out.println("请输入要修改的快递单号:");
 91             String number = input.next();
 92             int updateIndex = findByNumber(number);
 93             if (updateIndex==-1){
 94                 System.out.println("未找到快递!");
 95             }else {
 96                 System.out.println("请输入新的的快递单号:");
 97                 number = input.next();
 98                 System.out.println("请输入新的公司名:");
 99                 String company = input.next();
100                 numberArr[updateIndex]=number;
101                 companyArr[updateIndex]=company;
102                 System.out.println("修改成功!");
103             }
104         }else if (id==4){
105             printAll();
106         }else {
107             System.out.println("选择有误!请重新输入:");
108         }
109     }
110 
111     /**
112      * 删除快递
113      */
114     private static void delExpress() {
115         System.out.println("请输入要删除的快递单号");
116         String number = input.next();
117         int delIndex = findByNumber(number);
118         if (delIndex == -1){
119             System.out.println("未找到快递!");
120         }else {
121             for (int i = 0; i < index; i++) {
122                 if (number.equals(number)){
123                     //删除操作
124                     del(delIndex);
125                     System.out.println("删除成功!");
126                 }
127             }
128         }
129     }
130 
131     /**
132      * 查看所有快递
133      */
134     public static void printAll(){
135         System.out.println("=====所有快递信息========");
136         if (index != 0) {
137             System.out.println("快递单号\t公司名称\t取件码");
138             for (int i = 0; i < index; i++) {
139                 System.out.println(numberArr[i] + "\t" + companyArr[i] + "\t" + codeArr[i]);
140             }
141         }else {
142             System.out.println("暂无快递信息!");
143         }
144     }
145 
146     /**
147      * 根据快递单号
148      * @param number 找到的下标 -1表示找到
149      * @return
150      */
151     public static int findByNumber(String number){
152         for (int i = 0; i < index; i++) {
153             if (numberArr[i].equals(number)){
154                 return i;
155             }
156         }
157         return -1;
158     }
159 
160     /**
161      * 存快递功能
162      */
163     private static void saveExpress() {
164         System.out.println("请输入快递单号:");
165         String number = input.next();
166         System.out.println("请输入公司名称:");
167         String company = input.next();
168         int code = 0;
169         numberArr[index] = number;
170         companyArr[index] = company;
171         //保证取件码不重复
172         do {
173             code = random.nextInt(900)+100;//自动生成取件码100-900
174         }while (isExist(code)!=-1);
175         codeArr[index]=code;
176         index++;
177         System.out.println("快递已存入,取件码是:" + code);
178     }
179 
180     /**
181      * 判定取件码是否重复
182      * @param code
183      * @return
184      */
185     public static int isExist(int code){
186         for (int i = 0; i < index; i++) {//循环遍历取件码
187             if(codeArr[i]==code){//判定是否重复
188                 return 1;
189             }
190         }
191         return -1;
192     }
193 }
194 
195 /*
196 Output:
197 ================欢迎使用快递柜=====================
198 请输入身份:1-快递员 2-用户 0-退出
199 1
200 请选择操作:1-存快递 2-删除快递 3-修改快递信息 4-查看所有快递
201 1
202 请输入快递单号:
203 1001
204 请输入公司名称:
205 1
206 快递已存入,取件码是:303
207 ================欢迎使用快递柜=====================
208 请输入身份:1-快递员 2-用户 0-退出
209 1
210 请选择操作:1-存快递 2-删除快递 3-修改快递信息 4-查看所有快递
211 1
212 请输入快递单号:
213 1002
214 请输入公司名称:
215 2
216 快递已存入,取件码是:211
217 ================欢迎使用快递柜=====================
218 请输入身份:1-快递员 2-用户 0-退出
219 1
220 请选择操作:1-存快递 2-删除快递 3-修改快递信息 4-查看所有快递
221 4
222 =====所有快递信息========
223 快递单号    公司名称    取件码
224 1001    1    303
225 1002    2    211
226 ================欢迎使用快递柜=====================
227 请输入身份:1-快递员 2-用户 0-退出
228 1
229 请选择操作:1-存快递 2-删除快递 3-修改快递信息 4-查看所有快递
230 3
231 请输入要修改的快递单号:
232 1002
233 请输入新的的快递单号:
234 1003
235 请输入新的公司名:
236 3
237 修改成功!
238 ================欢迎使用快递柜=====================
239 请输入身份:1-快递员 2-用户 0-退出
240 1
241 请选择操作:1-存快递 2-删除快递 3-修改快递信息 4-查看所有快递
242 4
243 =====所有快递信息========
244 快递单号    公司名称    取件码
245 1001    1    303
246 1003    3    211
247 ================欢迎使用快递柜=====================
248 请输入身份:1-快递员 2-用户 0-退出
249 2
250 请输入取件码:
251 100
252 未找到快递!
253 ================欢迎使用快递柜=====================
254 请输入身份:1-快递员 2-用户 0-退出
255 2
256 请输入取件码:
257 211
258 取件成功!
259 ================欢迎使用快递柜=====================
260 请输入身份:1-快递员 2-用户 0-退出
261 1
262 请选择操作:1-存快递 2-删除快递 3-修改快递信息 4-查看所有快递
263 4
264 =====所有快递信息========
265 快递单号    公司名称    取件码
266 1001    1    303
267 ================欢迎使用快递柜=====================
268 请输入身份:1-快递员 2-用户 0-退出
269 1
270 请选择操作:1-存快递 2-删除快递 3-修改快递信息 4-查看所有快递
271 2
272 请输入要删除的快递单号
273 1001
274 删除成功!
275 ================欢迎使用快递柜=====================
276 请输入身份:1-快递员 2-用户 0-退出
277 1
278 请选择操作:1-存快递 2-删除快递 3-修改快递信息 4-查看所有快递
279 4
280 =====所有快递信息========
281 暂无快递信息!
282 ================欢迎使用快递柜=====================
283 请输入身份:1-快递员 2-用户 0-退出
284 0
285 谢谢使用!
286  */

 

posted on 2021-05-20 23:16  zrm0612  阅读(63)  评论(0)    收藏  举报