Java基础控制台快递E栈
1.快递E栈(控制台简易版)
package com. kkb. task ;
import java. util. Random ;
import java. util. Scanner ;
public class Task_1010105 {
public static Scanner input = new Scanner ( System . in) ;
public static String [ ] numberArr = new String [ 100 ] ;
public static String [ ] companyArr = new String [ 100 ] ;
public static int [ ] codeArr = new int [ 100 ] ;
public static int index = 0 ;
public static Random random = new Random ( ) ;
public static void main ( String [ ] args) {
while ( true ) {
int id = startMenu ( ) ;
if ( id == 0 ) {
return ;
}
}
}
public static int startMenu ( ) {
System . out. println ( "======欢迎使用新职课快递柜=========" ) ;
System . out. println ( "请输入您的身份:1-快递员 2-用户 0-退出" ) ;
int id = 0 ;
do {
id = input. nextInt ( ) ;
if ( id == 1 ) {
System . out. println ( "展示快递员的菜单:" ) ;
deliverymanMenu ( ) ;
break ;
} else if ( id == 2 ) {
System . out. println ( "展示普通用户的菜单:" ) ;
userMenu ( ) ;
break ;
} else if ( id == 0 ) {
System . out. println ( "谢谢使用,欢迎下次再来!" ) ;
return 0 ;
} else {
System . out. println ( "选择有误,请重新输入!" ) ;
}
} while ( true ) ;
return id;
}
public static void deliverymanMenu ( ) {
System . out. println ( "请选择操作:1-存快递 2-删除快递 3-修改快递信息 4-查看所有快递" ) ;
int id = input. nextInt ( ) ;
if ( id == 1 ) {
saveExpress ( ) ;
} else if ( id == 2 ) {
deleteExpress ( ) ;
} else if ( id == 3 ) {
modifyExpress ( ) ;
} else if ( id == 4 ) {
printAll ( ) ;
}
}
public static int isExist ( int code) {
for ( int i = 0 ; i < index; i++ ) {
if ( codeArr[ i] == code) {
return i;
}
}
return - 1 ;
}
public static void saveExpress ( ) {
System . out. println ( "请输入快递单号:" ) ;
String number = input. next ( ) ;
System . out. println ( "请输入快递公司:" ) ;
String company = input. next ( ) ;
numberArr[ index] = number;
companyArr[ index] = company;
int code = 0 ;
do {
code = random. nextInt ( 900 ) + 100 ;
} while ( isExist ( code) != - 1 ) ;
codeArr[ index] = code;
index++ ;
System . out. println ( "快递已录入,取件码是:" + code) ;
}
public static void deleteExpress ( ) {
System . out. println ( "请输入要删除的快递单号:" ) ;
String number = input. next ( ) ;
int delIndex = findByNumber ( number) ;
if ( delIndex == - 1 ) {
System . out. println ( "该快递单号不存在,请重新操作!" ) ;
} else {
del ( delIndex) ;
System . out. println ( "删除成功!" ) ;
}
}
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++ ] ;
codeArr[ i] = codeArr[ i+ 1 ] ;
}
}
index-- ;
}
public static int findByNumber ( String number) {
for ( int i = 0 ; i < index; i++ ) {
if ( numberArr[ i] . equals ( number) ) {
return i;
}
}
return - 1 ;
}
public static void modifyExpress ( ) {
System . out. println ( "请输入需要修改的快递单号:" ) ;
String number = input. next ( ) ;
int updateIndex = findByNumber ( number) ;
if ( updateIndex == - 1 ) {
System . out. println ( "未找到该快递,请重新操作!" ) ;
} else {
System . out. println ( "请输入新的快递单号:" ) ;
number = input. next ( ) ;
System . out. println ( "请输入新的公司名称:" ) ;
String company = input. next ( ) ;
numberArr[ updateIndex] = number;
companyArr[ updateIndex] = company;
System . out. println ( "修改成功!" ) ;
}
}
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\t" + companyArr[ i] + "\t\t" + codeArr[ i] ) ;
}
}
public static void userMenu ( ) {
System . out. println ( "请输入取件码:" ) ;
int code = input. nextInt ( ) ;
int codeIndex = isExist ( code) ;
if ( codeIndex == - 1 ) {
System . out. println ( "未找到该快递,请重新操作!" ) ;
} else {
del ( codeIndex) ;
System . out. println ( "取出成功!" ) ;
}
}
}
运行结果:
== == == 欢迎使用新职课快递柜== == == == =
请输入您的身份:1 - 快递员 2 - 用户 0 - 退出
1
展示快递员的菜单:
请选择操作:1 - 存快递 2 - 删除快递 3 - 修改快递信息 4 - 查看所有快递
1
请输入快递单号:
1001
请输入快递公司:
sf
快递已录入,取件码是:386
== == == 欢迎使用新职课快递柜== == == == =
请输入您的身份:1 - 快递员 2 - 用户 0 - 退出
1
展示快递员的菜单:
请选择操作:1 - 存快递 2 - 删除快递 3 - 修改快递信息 4 - 查看所有快递
1
请输入快递单号:
2002
请输入快递公司:
sto
快递已录入,取件码是:158
== == == 欢迎使用新职课快递柜== == == == =
请输入您的身份:1 - 快递员 2 - 用户 0 - 退出
1
展示快递员的菜单:
请选择操作:1 - 存快递 2 - 删除快递 3 - 修改快递信息 4 - 查看所有快递
2
请输入要删除的快递单号:
1001
删除成功!
== == == 欢迎使用新职课快递柜== == == == =
请输入您的身份:1 - 快递员 2 - 用户 0 - 退出
1
展示快递员的菜单:
请选择操作:1 - 存快递 2 - 删除快递 3 - 修改快递信息 4 - 查看所有快递
3
请输入需要修改的快递单号:
2002
请输入新的快递单号:
3003
请输入新的公司名称:
zto
修改成功!
== == == 欢迎使用新职课快递柜== == == == =
请输入您的身份:1 - 快递员 2 - 用户 0 - 退出
1
展示快递员的菜单:
请选择操作:1 - 存快递 2 - 删除快递 3 - 修改快递信息 4 - 查看所有快递
4
-- -- -- -- - 所有快递信息 -- -- -- -- -
快递单号 公司名称 取件码
3003 zto 386
== == == 欢迎使用新职课快递柜== == == == =
请输入您的身份:1 - 快递员 2 - 用户 0 - 退出
2
展示普通用户的菜单:
请输入取件码:
386
取出成功!
== == == 欢迎使用新职课快递柜== == == == =
请输入您的身份:1 - 快递员 2 - 用户 0 - 退出
1
展示快递员的菜单:
请选择操作:1 - 存快递 2 - 删除快递 3 - 修改快递信息 4 - 查看所有快递
4
-- -- -- -- - 所有快递信息 -- -- -- -- -
快递单号 公司名称 取件码
== == == 欢迎使用新职课快递柜== == == == =
请输入您的身份:1 - 快递员 2 - 用户 0 - 退出
0
谢谢使用,欢迎下次再来!