任务描述:
疫情期间为了减少人员接触,很多小区在快递的配送流程中,加入了快递柜。快递柜中内置了一套快递管理系统,包含的功能:
- 快递录入(快递员)
- 柜子位置(系统产生)
- 快递单号
- 快递公司
- 取件码(系统产生)
- 快递取出(普通用户)
- 输入取件码:显示快递信息 和 在哪个柜子中。
- 快递管理
- 删除快递
- 修改快递
- 查看所有快递
主要类:
实现快递员的一系列操作(PackageIn类)
private static int[][] nums = new int[10][10];//快递位置
private static String number;//快递单号
private static String company;//快递公司
private static String code;//取件码
private boolean flag = true;
private static Object[][] ob = new Object[101][4];//二维对象数组用来存储快递的所有信息
private static int count = 0;//快递个数
private static int i = 0;
private static int j = 0;
实现普通用户的操作(PackageOut类)
public class PackageOut extends PackageIn{
public PackageOut() {
// TODO Auto-generated constructor stub
super();
}
}
用户操作开始菜单(View类)
public class View {
public static int menu() {
Scanner input = new Scanner(System.in);
System.out.println("请先输入您的身份序号:\n"
+ "1,快递员\n"
+ "2,普通用户\n"
+ "0,退出");
String courier = input.nextLine();
int menu = -1;
try {
menu = Integer.parseInt(courier);
} catch (Exception e) {
// TODO: handle exception
}
if (menu<0 || menu>2) {
System.out.println("输入错误,请重新输入~");
return menu();
}
return menu;
}
public static void exit() {
System.out.println("感谢您使用本系统,再见~~~");
}
}
用户选择处理(Choose类)
public class Choose {
public static void choice() {
int menu = View.menu();
PackageOut p1 = new PackageOut();
if (menu == 1) {
//执行快递员操作选项
p1.courier();
}else if (menu == 2) {
//执行普通用户操作选项
p1.incode();
}else {
//程序退出
View.exit();
}
}
}
整个程序的入口
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println("欢迎您使用本系统~~~");
Choose.choice();
}
主要功能的实现:
系统随机产生位置和取件码
public static void setst() {
Random random = new Random();
boolean fg = true;
while (fg) {
i = random.nextInt(10);
j = random.nextInt(10);
if (nums[i][j]==0) {
nums[i][j] = 1;
fg = false;
}
}
//将随机产生的整型转化为字符串
String a = String.valueOf(random.nextInt(10));
String b = String.valueOf(random.nextInt(10));
String c = String.valueOf(random.nextInt(10));
String d = String.valueOf(random.nextInt(10));
String e = String.valueOf(random.nextInt(10));
String f = String.valueOf(random.nextInt(10));
code = a+b+c+d+e+f;
ob[count][0] = nums[i][j];
ob[count][1] = code;
ob[count][2] = number;
ob[count][3] = company;
System.out.println("该快递的位置是第"+(i+1)+"排,第"+(j+1)+"列"+",取件码为:"+code+",快递单号为:"+number+",快递公司是:"+company);
count++;
}
获取快递员录入的快递单号
public String numb() {
Scanner input = new Scanner(System.in);
String number = null;
try {
System.out.println("快递单号(11位):");
number = input.nextLine();
} catch (Exception e) {
// TODO: handle exception
}
if (number.length()!=11) {
System.out.println("快递单号错误,请重新输入!");
return numb();
}
for (int i = 0; i < count; i++) {
if (number .equals(ob[i][2])) {
System.out.println("快递单号重复,请确认后重新输入!");
return numb();
}
}
return number;
}
获取快递员录入的快递公司
public String comp() {
Scanner input = new Scanner(System.in);
try {
System.out.println("快递公司:");
company = input.nextLine();
} catch (Exception e) {
// TODO: handle exception
System.out.println("输入错误,请重新输入!");
return comp();
}
return company;
}
快递员操作主界面
public void courier () {
Scanner input = new Scanner(System.in);
System.out.println("请选择您要进行的操作:\n"
+ "1,存放一个快递\n"
+ "2,查询所有快递\n"
+ "3,修改快递信息\n"
+ "0,退出");
int menu = 0;
try {
menu = input.nextInt();
} catch (Exception e) {
// TODO: handle exception
}
if (menu == 3) {
//修改
modify();
}else if (menu == 2 ) {
//查询
queryall();
}else if (menu == 1) {
//录入
insert();
}else if (menu == 0) {
System.out.println("程序退出!\n");
Choose.choice();
}else {
System.out.println("输入错误,请重新输入~~~");
courier();
}
}
快递员存放快递
public void insert() {
System.out.println("请输入快递单号和快递公司:");
//获取快递单号和快递公司
PackageIn.number = numb();
PackageIn.company = comp();
//系统产生位置和取件码
setst();
//是否继续存放
couriercontinue();
}
选择是否继续存放
public void couriercontinue() {
//判断是否继续存放
Scanner input = new Scanner(System.in);
System.out.println("是否继续存放?输入1继续,输入2返回上一级菜单,输入0将退出~~~");
int inner = -1;
try {
inner = input.nextInt();
} catch (Exception e) {
// TODO: handle exception
}
if (inner == 1) {
//继续存放
insert();
}else if (inner == 2) {
//返回选择的菜单
courier();
}else if (inner == 0) {
//程序正常退出
System.out.println("程序退出!"
+ "\n");
Choose.choice();
}else {
System.out.println("输入错误,请重新输入~~~");
couriercontinue();
}
}
快递员进行修改操作
public void modify() {
Scanner input = new Scanner(System.in);
if (count == 0) {
System.out.println("柜中没有快递,无法修改!将返回上一级菜单~~~");
courier();
}else {
System.out.println("请输入需要修改的序号\n"
+ "1,快递单号\n"
+ "2,快递公司\n"
+ "0,返回上一级");
int menu = 0;
try {
menu = input.nextInt();
} catch (Exception e) {
// TODO: handle exception
}
if (menu == 1) {
//修改订单号
newNumb();
modifycontinue();
}else if (menu == 2) {
//修改快递公司
newComp();
modifycontinue();
}else if (menu == 0) {
//返回
courier();
}else {
System.out.println("输入错误,请重新输入");
modify();
}
}
}
修改订单号
public void newNumb() {
Scanner input = new Scanner(System.in);
flag = true;
nN:while (flag) {
System.out.println("请输入修改前的订单号:");
String number1 = input.nextLine();
for (int i = 0; i < count; i++) {
if (number1.equals(ob[i][2])) {
System.out.println("请输入新的订单号:");
ob[i][2] = input.nextLine();
break nN;
}
}
System.out.println("订单号不存在!请重新输入~~~");
}
}
修改公司
public void newComp() {
Scanner input = new Scanner(System.in);
flag = true;
nC:while (flag) {
System.out.println("请输入修改前的快递公司:");
String number1 = input.nextLine();
for (int i = 0; i < count; i++) {
if (number1.equals(ob[i][3])) {
System.out.println("请输入新的快递公司:");
ob[i][3] = input.nextLine();
break nC;
}
}
System.out.println("快递公司不存在!请重新输入~~~");
}
}
选择是否继续修改
public void modifycontinue() {
Scanner input = new Scanner(System.in);
System.out.println("是否继续修改?输入1继续,输入2返回上一级菜单,输入0将退出~~~");
int inner = -1;
try {
inner = input.nextInt();
} catch (Exception e) {
// TODO: handle exception
}
if (inner == 1) {
//继续修改
modify();
}else if (inner == 2) {
//返回选择的菜单
courier();
}else if (inner == 0) {
//程序正常退出
System.out.println("程序退出!"
+ "\n");
Choose.choice();
}else {
System.out.println("输入错误,请重新输入~~~");
modifycontinue();
}
}
快递员查询所有快递
public void queryall() {
if (count == 0) {
System.out.println("柜中没有快递!将返回上一级菜单\n");
}else {
System.out.println("柜中一共有"+count+"个快递,快递信息分别是:");
for (int i = 0; i < count; i++) {
System.out.println("快递单号为:"+getOb()[i][2]+",其快递公司是:"+getOb()[i][3]);
}
System.out.println();
}
courier();
}
普通用户取快递
public void incode() {
Scanner input = new Scanner(System.in);
if (getCount() == 0) {
System.out.println("没有快递待取,程序将退出!");
Choose.Choice();
}else {
System.out.println("请输入取件码:");
String code2;
try {
code2 = input.nextLine();
out(code2);
} catch (Exception e) {
// TODO: handle exception
System.out.println("输入有误!请重新输入~~~"
+ "\n");
incode();
}
Choose.choice();
}
}
//取出快递
public void out(String code) {
String code1;
//将对象数组中的对象转化为字符串
code1 = (String) getOb()[0][1];
hh:for (int i = 0; i < getCount(); i++) {
code1 = (String) getOb()[i][1];
if (code.equals(code1)) {
System.out.println("您的快递在第"+(getI()+1)+"排,第"+(getJ()+1)+"列,快递单号为:"+getOb()[i][2]+",快递公司是"+getOb()[i][3]+"的快递~~~");
//快递取出后,顺序前推,若是最后则直接置空
delete(i);
System.out.println("程序正常退出!\n"
+ "\n");
break hh;
}
}
}