快递E栈任务分析:
使用者:快递员,用户
快递员包含功能:存快递,删快递,修改快递,查询全部快递,
用户功能:输入取件码取快递
快递属性:快递号,公司,取件码
public class ExpressEstack {
/**记录长度 默认可以存储100条记录*/
static int length = 100;
/**快递号*/
static int[] expressId = new int[length];
/**快递公司*/
static String[] expressCompany = new String[length];
/**取件码*/
static int[] expressCode = new int[length];
//用于存储快递的index
static int index = 0;
//开始界面
public static void startInterface(){
System.out.println("=====欢迎使用新职课快递柜=====");
System.out.println("请输入您的身份 1-快递员,2-用户");
Scanner scanner = new Scanner(System.in);
int role = scanner.nextInt();
switch (role){
//快递员
case 1:
expressInterface();
break;
//用户
case 2:
userInterface();
break;
default:
System.out.println("请输入正确身份");
startInterface();
}
}
//用户登录界面
private static void userInterface() {
Scanner scanner = new Scanner(System.in);
System.out.println("请选择操作:1-取快递 ");
int play2 = scanner.nextInt();
//取快递操作
switch (play2){
//快递员
case 1:
//去快递
takeExpress();
break;
default:
System.out.println("请输入正确对应数值");
userInterface();
}
}
//用户取件
private static void takeExpress() {
boolean istakeExpress = false;
Scanner scanner = new Scanner(System.in);
System.out.println("请输入取件码:");
int code = scanner.nextInt();
for (int i = 0; i < expressCode.length; i++) {
if (code == expressCode[i]){
expressId[i] = 0;
expressCompany[i] = "";
expressCode[i] = 0;
istakeExpress = true;
}
}
if (istakeExpress){
System.out.println("取件成功");
}else {
System.out.println("未找到该快递");
}
startInterface();
}
//快递员登录界面
private static void expressInterface() {
Scanner scanner = new Scanner(System.in);
System.out.println("请选择操作:1-存快递 2-删除快递 3-修改快递信息 4-查看所有快递");
int play1 = scanner.nextInt();
//选择功能
switch (play1){
//存快递,查询是否有同样快递号快递公司的件
case 1:
depositExpress();
break;
//删除快递
case 2:
deleteExpress();
break;
//修改信息
case 3:
modifyInformation();
break;
//查看所有快递
case 4:
findAll();
break;
default:
System.out.println("请输入正确对应数字");
expressInterface();
}
}
//查看所有快递
private static void findAll() {
System.out.println("所有快递信息如下:");
System.out.printf("%-9s%-9s%-9s\n", "快递单号", "公司名称", "取件码");
//System.out.println("快递单号\t快递公司\t取件码");
//下标计数
int index=0;
while (index < 100){
if (expressId[index]!=0 && expressCode[index]!=0 ){
System.out.printf("%-9d%-9s%-9d\n", expressId[index], expressCompany[index], expressCode[index]);
}
index++;
}
startInterface();
}
//修改信息
private static void modifyInformation() {
boolean isModify = false;
Scanner scanner = new Scanner(System.in);
System.out.println("请输入要修改的快递单号");
int expressid = scanner.nextInt();
for (int i = 0; i < expressId.length; i++) {
if (expressid == expressId[i]){
System.out.println("请输入新的快递单号");
int expressNewId = scanner.nextInt();
expressId[i]=expressNewId;
System.out.println("请输入新的快递公司");
String expressNewCompany = scanner.next();
expressCompany[i] = expressNewCompany;
isModify = true;
}
}
if (isModify){
System.out.println("修改成功");
}else {
System.out.println("未找到对应快递");
}
startInterface();
}
//删除快递
private static void deleteExpress() {
boolean isDelete = false;
System.out.println("请输入要删除的快递单号");
Scanner delete = new Scanner(System.in);
int deleteNumber = delete.nextInt();
for (int i = 0; i < length - 1; i++) {
if (expressId[i] == deleteNumber) {
expressId[i] = 0;
expressCompany[i] = "";
expressCode[i] = 0;
isDelete = true;
break;
}
}
if (isDelete) {
System.out.println("删除成功");
} else {
System.out.println("未找到快递");
}
expressInterface();
}
private static void depositExpress() {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入快递单号");
int expressid = scanner.nextInt();
System.out.println("请输入公司名称");
String company = scanner.next();
//将输入的存入数组中
expressId[index] = expressid;
expressCompany[index] = company;
//随机三位数生成
Random ran = new Random();
int randomNum = ran.nextInt(900)+100;
expressCode[index] = randomNum;
//三个数生成的数组下标一致
if (expressId[index] == expressid & expressCompany[index].equals(company) & expressCode[index] == randomNum){
System.out.println("快递已存入,取件码"+ randomNum);
}
index ++;
startInterface();
}
public static void main(String[] args) {
startInterface();
}
}
总结:
知识点1:取件码使用的随机数生成
int randNumber =rand.nextInt(MAX - MIN + 1) + MIN; // randNumber 将被赋值为一个 MIN 和 MAX 范围内的随机数
rand.nextInt(100);这行代码将生成范围[0,100) 之间的随机数,即[0,99]
获得随机两位整数,代码写成:rand.nextInt(90) + 10;
生成随机三位数的代码为:rand.nextInt(900)+ 100;
知识点2:判断字符是否相等使用equals
使用方法:字符串.equals(比较的字符串)
知识点3:对齐输出
Java输出数据的对齐方法:
你可以把数字转换成字符串,用 String.format("% 4d", number1); 可以补充空格
有个更好的方法用printf();这个给C中的方法差不多,很方便例如这样:System.out.printf("%-10s",“abc”); //输出10列,左对齐(-号表示左对齐)
System.out.printf("%8d",23); //输出8列, 右对刘
这里就不详细讲述printf用法,可以参照以下博客:
http://c.biancheng.net/view/159.html
https://www.runoob.com/cprogramming/c-function-printf.html
快递E栈简易版所有功能都大同小异,通过数组循环下标一个个对比数据,再根据具体要求进行修改数据或删除数据即可