package com.jn.expressage;
import java.util.Random;
import java.util.Scanner;
public class expressDelivery {
static Scanner sc=new Scanner(System.in);
static String[] numbers=new String[10];
static String[] companys = new String[10];
static int[] verificationCode=new int[10];
static int index=0;
public static void main(String[] args) {
mainInterface();
}
//主界面
private static void mainInterface(){
while (true){
System.out.println("--------------------------------------------");
System.out.println("|\t请输入你的选项:\t\t\t\t\t\t\t|");
System.out.println("|\t1.快递员\t\t\t" + "2.用户\t\t" + "3.退出\t\t|");
System.out.println("--------------------------------------------");
int option = sc.nextInt();
if (option==1){
courier();
} else if (option==2) {
userInterface();
} else if (option==3) {
System.exit(0);
}
else {
break;
}
}
}
//快递员界面
private static void courier(){
System.out.println("欢迎来到快递员界面,请选择:");
System.out.println("1.存快递\t\t2.取出快递\t\t3.修改快递信息\t\t4.查看快递信息\t\t5.返回上级目录");
int n= sc.nextInt();
if (n==1){
//存快递
saveMethod();
} else if (n==2) {
//取出快递
deleteMethod();
} else if (n==3) {
//修改快递信息
updateMethod();
} else if (n==4) {
selectMethod();
} else if (n==5) {
mainInterface();
}
}
//添加快递
private static void saveMethod(){
System.out.println("请输入快递单号");
String num = sc.next();
System.out.println("请输入快递公司");
String com = sc.next();
numbers[index] = num;
companys[index] = com;
verificationCode[index] = new Random().nextInt(999);
index++;
System.out.println("SaveSuccessfully!");
System.out.println(index);
courier();
}
//查看信息
private static void selectMethod(){
System.out.println("快递单号\t快递公司\t验证码");
for (int i = 0; i <index; i++) {
System.out.println(numbers[i] + "\t\t" + companys[i] + "\t\t" + verificationCode[i]);
}
courier();
}
//取件
private static void deleteMethod(){
System.out.println("请输入快递单号:");
String num= sc.next();
delMethod(num);
}
//快递单号得到索引
private static int getIndexByNumber(String num) {
for (int i = 0; i < index; i++) {
if (numbers[i].equals(num)) {
return i;
}
}
return -1; // 如果未找到,返回 -1 表示不存在
}
//修改信息
private static void delMethod(String num){
int deleteIndex = getIndexByNumber(num);
if (deleteIndex == -1) {
System.out.println("快递单号不存在。");
} else {
for (int i = deleteIndex; i < index - 1; i++) {
numbers[i] = numbers[i + 1];
companys[i] = companys[i + 1];
verificationCode[i] = verificationCode[i + 1];
}
// 更新索引计数
index--;
System.out.println("Delete Successful!");
}
courier();
}
//修改界面
private static void updateMethod(){
System.out.println("请输入你要修改的快递单号");
String num= sc.next();
int updateIndex=getIndexByNumber(num);
System.out.println("请选择你要修改的信息:\t1.快递单号\t2.公司名称");
int n= sc.nextInt();
if (n==1) {
System.out.println("请输入新的快递单号");
String newNumber= sc.next();
numbers[updateIndex]=newNumber;
System.out.println("Update Successful!");
}
else if (n==2) {
System.out.println("请输入新的快递公司名称");
String newCompony= sc.next();
numbers[updateIndex]=newCompony;
System.out.println("Update Successful!");
}
else{
System.out.println("输入信息有误");
}
courier();
}
//用户界面
private static void userInterface(){
System.out.println("请输入取件码:");
int userIndex=getIndexVerificationCode(sc.nextInt());
if(userIndex==-1){
System.out.println("取件码错误");
}
else{
for (int i = userIndex; i <index ; i++) {
numbers[i]=numbers[i+1];
companys[i]=companys[i+1];
verificationCode[i]=verificationCode[i+1];
}
System.out.println("取件成功,欢迎下次光临");
index--;
}
}
//取件码得到下标
private static int getIndexVerificationCode(int num){
for (int i = 0; i < index; i++) {
if(verificationCode[i]==num){
return i;
}
}
return -1;
}
}
JAVA项目之快递柜
于 2024-08-06 20:05:03 首次发布