快递管理训练任务--通过集合存储数据
作者:互联网
![在这里插入图片描述](https://www.icode9.com/i/ll/?i=20210209234531220.png#pic_center
我利用了map同时存储了公司名称和订单号,取件码单独存储在了ArrayList中具体操作看下面代码
package com.java.text;
import java.util.*;
public class Demo1 {
static Scanner input = new Scanner(System.in);
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 class Express {
static Scanner input = new Scanner(System.in);
ArrayList<Integer> number = new ArrayList<>();
HashMap<Integer, String> map = new HashMap<>();
int serve() {
int key;
System.out.println("请输入快递号");
try {
Scanner input = new Scanner(System.in);
key = input.nextInt();
}catch (InputMismatchException e){
System.out.println("输入的不是数字");
return serve();
}
System.out.println("请输入快递公司");
String company = input.next();
String i = map.put(key, company);
int numbers = (int) (Math.random() * (65535 + 1) * 10);
number.add(numbers);
if (i != null) {
System.out.println("该快递号已经有快递了请重新输入");
map.put(key, i);
return serve();
}
return 0;
}
void delete() {
System.out.println("请输入要删除的快递单号(若是修改快递删除已经有的快递)");
int key = input.nextInt();
String i = map.remove(key);
number.remove(key);
if (i == null) {
System.out.println("此快递单号本就没快递sb(帅逼)");
} else
System.out.println("删除成功");
}
void correction() {
System.out.println("请输入要修改的快递单号");
delete();
serve();
}
void check() {
Set<Integer> set = map.keySet();
for (Integer key : set) {
System.out.println("订单号为"+key + ",公司为" + map.get(key) );
System.out.println("取件码为"+number.get(key-1));
}
}
int userget() {
System.out.println("请输入取件码");
int numbers;
try{ Scanner input = new Scanner(System.in);
numbers = input.nextInt();
}
catch (InputMismatchException e){
System.out.println("输入的不是数字");
return userget();
}
for (int i = 0; i < number.size(); i++) {
if (numbers == number.get(i)) {
System.out.println("订单号为" + (i+1 )+ ",公司为" + map.remove(i+1));
return 0;
}
}
System.out.println("输入错误请检查后重新输入");
return userget();
}
}
}
如果有帮助点个赞来激励一下我呗,如果有疑问也可以在下面问看到都会回答的
标签:训练任务,--,System,快递,int,key,println,out 来源: https://blog.csdn.net/chh1ctrl/article/details/113777292