一、主要涉及知识
1、数组
2、分支结构
二、源码解析
1、先创建三个数组
public static String[] numberArr = new String[100];// 快递单号数组
public static String[] companyArr = new String[100];// 公司名称数组
public static int[] codeArr = new int[100];// 取件码数组
2、定义展示菜单并应用方法
public static void main(String[] args) {
/*
* 展示菜单
*/
while (true) {
int id = startMenu();
if (id == 0)
return;
}
}
3、创建展示菜单方法
public static int startMenu() {
System.out.println("--------欢迎使用河北唐山快递柜--------");
System.out.print("请输入身份信息: 1-快递员 2-用户 0-退出");
int id = 0;
do {
id = input.nextInt();
if (id == 1) {// 快递员 展示快递员菜单信息
deliverymanMenu();
break;
} else if (id == 2) {// 用户 展示用户菜单信息
userMenu();
break;
} else if (id == 0) {
System.out.println("谢谢使用!");
return id;
} else {// 输入错误
System.out.println("输入错误!请检查并重新输入");
}
} while (true);
return id;
}
4、定义快递员菜单
/*
* 快递员菜单
*/
public static void deliverymanMenu() {
System.out.print("请选择操作:1-存快递 2-删除快递 3-修改快递信息 4-查看所有快递");
int id = input.nextInt();
if (id == 1) {
System.out.println(saveExpress());
} else if (id == 2) {
delExpress();
System.out.println("删除成功!");
} else if (id == 3) {
revExpress();
} else if (id == 4) {
printAll();
}
}
5、定义快递员详细操作的方法
查找快递
/*
* 根据快递单号查找快递 找到的下标 是-1 表示没有找到
*/
public static int findByNumber(String number) {
for (int i = 0; i < index; i++) {
if (numberArr[i].equals(number)) {
return i;
}
}
return -1;
}
存入快递
private static String saveExpress() {
System.out.print("请输入快递单号:");
String number = input.next();
System.out.print("请输入公司名称:");
String company = input.next();
numberArr[index] = number;
companyArr[index] = company;
判断快递单号重复
if (findByNumber(number) != -1) {
return "当前单号已经存在";
}
// 自动生成取件码
int code = 0;
// 保证取件码不重复
do {
code = random.nextInt(900) + 100;
} while (isExist(code) != -1);
codeArr[index] = code;
index++;
String flag = "快递已存入,取件码是:" + code;
return flag;
}
判断取件码重复问题
public static int isExist(int code) {
for (int i = 0; i < index; i++) {// 循环遍历取件码数组
if (codeArr[i] == code) {// 判断是否重复
return i;
}
}
return -1;
}
修改快递
/*
* 修改快递
*/
public static void revExpress() {
System.out.println("请输入要修改的快递单号:");
String number = input.next();
int updateIndex = findByNumber(number);
if (updateIndex == -1) {
System.out.println("未找到快递!");
} else {
System.out.print("请输入新的的快递单号:");
number = input.next();
System.out.print("请输入新的公司名称:");
String company = input.next();
numberArr[updateIndex] = number;
companyArr[updateIndex] = company;
System.out.println("修改成功!");
}
}
删除快递
/*
* 删除快递
*/
public static void delExpress() {
System.out.print("请输入要删除的快递单号:");
String number = input.next();
int delIndex = findByNumber(number);
if (delIndex == -1) {
System.out.println("没有找到快递!");
} else {
// 删除
del(delIndex);
}
}
public static void del(int delIndex) {
if (delIndex != numberArr.length - 1) {
for (int i = delIndex; i < index; i++) {
numberArr[i] = numberArr[i + 1];
companyArr[i] = companyArr[i + 1];
codeArr[i] = codeArr[i + 1];
}
}
index--;
}
查看所有快递
/*
* 查看所有快递
*/
public static void printAll() {
System.out.println("--------所有的快递信息--------");
System.out.println("快递单号\t公司名称\t取件码");
for (int i = 0; i < index; i++) {
System.out.println(numberArr[i] + "\t" + companyArr[i] + "\t" + codeArr[i]);
}
}
6、定义用户菜单
/*
* 用户菜单
*/
public static void userMenu() {
System.out.print("请输入取件码:");
int code = input.nextInt();
int codeIndex = isExist(code);
if (codeIndex == -1) {
System.out.println("未找到该快递!");
} else {
del(codeIndex);
System.out.println("取件成功");
}
}
}