java_二维数组简单快递系统
利用二维数组存储快递信息
通过二维数组存储快递信息,利用一个临时数组操作,取件码通过Random函数自动生成
快递员存快递
快递员查看所有快递
快递员修改快递
快递员删除快递
用户取快递
代码如下:
public class Demo1 {
public static void main(String[] args) {
String express[][] = new String[0][3]; //定义快递二维数组
String[][] arr; // 定义临时数组
while (true) {
System.out.println("==欢迎使用XXX快递柜===");
System.out.println("请输入你的身份:1-快递员,2-用户");
Scanner s = new Scanner(System.in);
int i = s.nextInt();
if (i == 1) {
System.out.println("请选择操作:1-存快递 2-删除快递 3-修改快递信息 4-查看所有快递");
int j = s.nextInt();
switch (j) {
// 1-存快递
case 1:
System.out.println("请输入快递单号:");
String num = s.next();
System.out.println("请输入公司名称:");
String name = s.next();
Random ran = new Random();
String r = Integer.toString(ran.nextInt(1000));
System.out.println("快递已存入,取件码是:" + r);
arr = new String[express.length + 1][3];
for (int n = 0; n < express.length; n++) {
System.arraycopy(express, 0, arr, 0, express.length);
}
arr[arr.length - 1] = new String[] { num, name, r };
express = arr;
break;
// 2-删除快递
case 2:
arr = new String[express.length - 1][3];
System.out.println("请输入要删除的快递单号:");
String l = s.next();
for (int n = 0; n < express.length; n++) {
if (express[n][0].equals(l)) {
System.arraycopy(express, 0, arr, 0, n);
System.arraycopy(express, n + 1, arr, n, arr.length - n);
express = arr;
System.out.println("删除成功");
break;
} else if (n == express.length - 1) {
System.out.println("未找到快递");
break;
}
}
break;
//3-修改快递信息
case 3:
System.out.println("请输入要修改的快递单号:");
String o = s.next();
for (int n = 0; n < express.length; n++) {
if (express[n][0].equals(o)) {
System.out.println("请输入新的快递单号:");
String newnum = s.next();
express[n][0] = newnum;
System.out.println("请输入新的快递公司:");
String newname = s.next();
express[n][1] = newname;
System.out.println("修改成功");
} else if (i == express.length - 1) {
System.out.println("未找到快递");
break;
}
}
break;
// 4-查看所有快递
case 4:
System.out.println("----------------全部快递---------------");
System.out.println("快递号\t\t快递公司\t\t取件码");
for (int n = 0; n < express.length; n++) {
for (int m = 0; m < express[n].length; m++) {
System.out.print(express[n][m] + "\t\t");
}
System.out.println();
}
break;
default:
System.out.println("输入有误!");
}
// 用户取件部分
} else if (i == 2) {
arr = new String[express.length - 1][3];
System.out.println("请输入取件码:");
String p = s.next();
for (int n = 0; n < express.length; n++) {
if (express[n][2].equals(p)) {
System.arraycopy(express, 0, arr, 0, n);
System.arraycopy(express, n + 1, arr, n, arr.length - n);
express = arr;
System.out.println("取件成功");
break;
} else if (n == express.length - 1) {
System.out.println("未找到快递");
break;
}
}
} else {
System.out.println("输入有误!");
}
}
}
}