java实现简单快递系统
作者:helloworld370629 时间:2021-11-28 07:01:57
本文实例为大家分享了java实现简单快递系统的具体代码,供大家参考,具体内容如下
创建四个类Express,Locker, User, Administrator分别模拟快递,快递柜,用户和管理员。
import java.util.Random;
import java.util.Scanner;
/**
* @author hxf
* * @date 2020/12/12
*/
public class Test {
static Scanner in = new Scanner(System.in);
public static void main(String[] args) {
// 生成快递柜, 但在后续修改快递位置时没考虑越界问题
Locker locker = new Locker(10, 10);
// 创建管理员
Administrator administrator = new Administrator(locker);
//程序主逻辑
outer: while (true){
switch (login("请选择身份:(-1退出 0管理员身份 1用户身份)", 1)){
case -1:
break outer;
case 0:
switch (login("请选择操作:(-1退出 0快递录入 1删除快递 2修改快递 3查看快递)", 3)){
case -1:
break;
case 0:
in.nextLine();
System.out.println("请输入公司名称:");
System.out.println("取件码:"+administrator.store(in.nextLine()));
break;
case 1:
administrator.remove();
break;
case 2:
administrator.change();
break;
case 3:
administrator.show();
break;
}
break;
case 1:
switch (login("请选择操作:(-1退出 0取快递)", 0)){
case -1:
break;
case 0:
in.nextLine();
fetch(locker);
break;
}
break;
}
}
}
// 生成有效选择,处理无效选择产生的异常
public static int login(String string, int n){
try{
System.out.println(string);
int select = in.nextInt();
if (select > n || select < -1){
System.out.println("请输入有效选择!");
in.nextLine();
return login(string, n);
}
return select;
}catch (Exception e){
System.out.println("请输入有效选择!");
in.nextLine();
return login(string, n);
}
}
// 用户取快递,确保输入有效验证码
public static void fetch(Locker locker){
System.out.println("请输入六位验证码:");
String code = in.nextLine();
User user = new User(code, locker);
if (user.fetch()){
System.out.println("取件成功");
}else {
System.out.println("验证码有误,请重新输入!");
fetch(locker);
}
}
}
class Express {
/**
* @param code: 6位验证码,数字字符串
* @param company: 公司名称
* @param number: 14位快递单号, 数字字符串
* @param position: 快递存放位置
* @author hxf
* @describe: 快递
* @date 2020/12/11 1:17
*/
private final String code;
private String company;
private final String number;
private int[] position;
public Express(String code, String company, String number, int[] position) {
this.code = code;
this.company = company;
this.number = number;
this.position = position;
}
public String getCode() {
return code;
}
public String getCompany() {
return company;
}
public String getNumber() {
return number;
}
public int[] getPosition() {
return position;
}
public void setCompany(String company) {
this.company = company;
}
public void setPosition(int row, int column) {
this.position[0] = row;
this.position[1] = column;
}
@Override
public String toString() {
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append("快递信息简介{");
stringBuilder.append("快递公司:");
stringBuilder.append(company);
stringBuilder.append(", 快递单号:");
stringBuilder.append(number);
stringBuilder.append(", 存放位置:");
stringBuilder.append("第");
stringBuilder.append(position[0]);
stringBuilder.append("排,第");
stringBuilder.append(position[1]);
stringBuilder.append("列");
return stringBuilder.toString();
}
}
class Locker {
/**
* @param code: 存储Express
* @param random: 随机数生成器
* @param position: 存放临时快递位置
* @param LENGTH_OF_CODE: 验证码长度
* @author hxf
* @describe: 快递柜
* @date 2020/12/11 1:25
*/
Express[][] code;
Random random;
int[] position;
private final int LENGTH_OF_CODE = 6;
private final int LENGTH_OF_NUMBER = 14;
public Locker(int row, int column){
code = new Express[row][column];
random = new Random();
position = new int[2];
}
// 生成验证码
public String generateCode(){
StringBuilder stringBuilder = new StringBuilder();
for (int i = 0; i < LENGTH_OF_CODE; i++){
stringBuilder.append(random.nextInt(10));
}
String string = stringBuilder.toString();
checkCode(string);
return position[0] == -1 ? string : generateCode();
}
// 生成快递单号
public String generateNumber(){
StringBuilder stringBuilder = new StringBuilder();
for (int i = 0; i < LENGTH_OF_NUMBER; i++){
stringBuilder.append(random.nextInt(10));
}
String string = stringBuilder.toString();
checkNumber(string);
return position[0] == -1 ? string : generateNumber();
}
//生成存放位置
public int[] generatePosition(){
int row = random.nextInt(code.length);
int column = random.nextInt(code[0].length);
if (code[row][column] != null){
return generatePosition();
}else{
position[0] = row;
position[1] = column;
return position;
}
}
// 检查有无重复验证码,有则将position改成相应位置,无则position第一个元素为-1
public void checkCode(String string){
position[0] = -1;
outer: for (int i = 0; i < code.length; i++){
for (int j = 0; j < code[0].length; j++) {
if (code[i][j] != null && code[i][j].getCode().equals(string)) {
position[0] = i;
position[1] = j;
break outer;
}
}
}
}
// 检查有无重复快递单号
public void checkNumber(String string){
position[0] = -1;
outer: for (int i = 0; i < code.length; i++){
for (int j = 0; j < code[0].length; j++) {
if (code[i][j] != null && code[i][j].getNumber().equals(string)) {
position[0] = i;
position[1] = j;
break outer;
}
}
}
}
// 将快递存入快递柜
public String store(Express express){
code[position[0]][position[1]] = express;
return express.getCode();
}
public void store(Express express, int row, int column){
code[row][column] = express;
}
// 取出快递
public Express fetch(){
Express express = code[position[0]][position[1]];
code[position[0]][position[1]] = null;
return express;
}
public int[] getPosition() {
return position;
}
// 打印所有快递信息
public void check(){
int count = 0;
for (Express[] expresses: code){
for (Express express : expresses){
if (express != null){
System.out.println(express.toString());
count++;
}
}
}
if (count == 0){
System.out.println("无快递");
}
}
}
class User {
/**
*@Param: code: 验证码
*@Param: locker: 快递柜
*@Author: hxf
*@date: 2020/12/12
*/
private final String code;
private Locker locker;
public User(String code, Locker locker) {
this.code = code;
this.locker = locker;
}
// 取快递
public boolean fetch(){
locker.checkCode(code);
if (locker.getPosition()[0] == -1){
return false;
}else {
System.out.println(locker.fetch().toString());
return true;
}
}
}
class Administrator {
/**
*@Param: locker: 快递柜
*@Author: hxf
*@date: 2020/12/12
*/
Locker locker;
Scanner in = new Scanner(System.in);
public Administrator(Locker locker) {
this.locker = locker;
}
// 录入快递
public String store(String company){
String number = locker.generateNumber();
String code = locker.generateCode();
int[] position = locker.generatePosition();
return locker.store(new Express(code, company, number, position));
}
// 删除快递
public void remove(){
System.out.println("请输入14位快递单号:");
String number = in.nextLine();
locker.checkNumber(number);
if (locker.getPosition()[0] != -1){
System.out.println("单号为"+locker.fetch().getNumber()+"的快递已经删除");
}else{
System.out.println("快递单号不存在,请重试");
remove();
}
}
// 修改快递
public void change(){
System.out.println("请输入14位快递单号:");
String number = in.nextLine();
locker.checkNumber(number);
if (locker.getPosition()[0] != -1){
Express express = locker.fetch();
System.out.println("请输入新的快递公司:");
express.setCompany(in.nextLine());
System.out.println("请输入新的位置:(先行后列,空格隔开)");
int row = in.nextInt();
int column = in.nextInt();
express.setPosition(row, column);
locker.store(express, row, column);
System.out.println("修改成功!");
}else{
System.out.println("快递单号不存在,请重试");
change();
}
}
// 显示所有快递信息
public void show(){
locker.check();
}
}
来源:https://blog.csdn.net/helloworld370629/article/details/111062436
标签:java,快递系统
0
投稿
猜你喜欢
maven报错:Failed to execute goal on project问题及解决
2021-09-30 09:58:36
C#语法之泛型的多种应用
2022-03-15 02:02:43
详解Java对象结构与对象锁的升级
2021-12-05 16:18:38
Linux下JDK安装教程
2022-04-08 18:04:46
SpringBoot集成easy-rules规则引擎流程详解
2022-04-20 11:39:35
JAVA Map架构和API介绍
2023-01-25 14:01:38
javaweb实现app扫码登录功能
2022-03-25 03:48:19
java中的基础运算符
2022-06-23 04:19:38
SpringBoot中使用Servlet三大组件的方法(Servlet、Filter、Listener)
2021-07-19 18:41:43
C#特性-迭代器(上)及一些研究过程中的副产品
2023-12-05 18:26:49
Maven+Tomcat8 实现自动化部署的方法
2023-01-03 06:44:20
C# WinForm开发中使用XML配置文件实例
2022-09-07 23:04:03
java 创建线程的方法总结
2023-02-25 20:20:30
Flutter实现矩形取色器的封装
2023-06-19 04:08:47
C# JSON格式化转换辅助类 ConvertJson
2023-09-27 19:40:20
Android系列---JSON数据解析的实例
2022-07-04 19:52:50
Spring数据访问模板化方法
2022-03-15 06:23:02
dubbo整合springboot新手入门教程详解
2022-07-29 21:19:00
Android开发软键盘遮挡登陆按钮的完美解决方案
2022-05-09 07:44:47
Android实现动态添加数据与堆叠折线图详解流程
2021-09-27 17:50:49