簡單快遞系統 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();
}
}
相關文章
- 基於Java+SpringBoot+Mysql實現的快遞櫃寄取快遞系統功能實現十JavaSpring BootMySql
- 基於Java+SpringBoot+Mysql實現的快遞櫃寄取快遞系統功能實現三JavaSpring BootMySql
- 基於Java+SpringBoot+Mysql實現的快遞櫃寄取快遞系統功能實現四JavaSpring BootMySql
- 基於Java+SpringBoot+Mysql實現的快遞櫃寄取快遞系統功能實現八JavaSpring BootMySql
- GFS(谷歌檔案系統)的JAVA簡單實現谷歌Java
- 使用簡單的Java程式碼實現酒店管理系統Java
- C++釋出訂閱者模式:實現簡單訊息傳遞系統C++模式
- LRU cache快取簡單實現快取
- 實現快遞單號物流資訊介面APIAPI
- Java實現多級選單(遞迴)Java遞迴
- ServerSocket實現簡單的廣播系統Server
- java實現簡單的JDBCJavaJDBC
- 全國快遞物流 API 實現快遞單號自動識別的原理解析API
- UITabBarController移除系統自帶UITabBarButton簡單實現UItabBarController
- Python技法:實現簡單的遞迴下降ParserPython遞迴
- 常用快遞單號物流查詢介面通用API(JAVA快遞鳥對接)APIJava
- java實現棧的簡單操作Java
- Java用UDP實現簡單聊天JavaUDP
- C#反射實現簡單的外掛系統C#反射
- SpringBoot整合MongoDB(實現一個簡單快取)Spring BootMongoDB快取
- Lumen/Laravel 整合 GatewayWorker (Workerman),實現簡單的聊天系統.LaravelGateway
- 基於Netty的Android系統IM簡單實現原理NettyAndroid
- Java使用Netty實現簡單的RPCJavaNettyRPC
- 二分法的簡單實現——-遞迴和非遞迴遞迴
- PHP快遞代取系統跑腿系統(畢業設計)PHP
- OCR文字識別系統助力快遞行業提高快遞分揀效率行業
- Go實踐:用Sync.Map實現簡易記憶體快取系統Go記憶體快取
- Java簡單學生資訊管理系統Java
- 快排其實很簡單
- 免費常用快遞單號物流通用API查詢介面(JAVA快遞鳥對接)APIJava
- 網易雲音樂推薦系統簡單實現系列
- Java Web簡單登陸功能的實現JavaWeb
- 使用Java實現簡單的鬥地主案例Java
- Java 簡單實現撲克牌抽象類Java抽象
- mac系統上hdfs java api的簡單使用MacJavaAPI
- 直播系統搭建,簡單實現Android應用的啟動頁Android
- web 端怎麼實現套打發票、快遞單等套打功能?Web
- Java實現簡單的增刪改查操作Java