java實(shí)現(xiàn)簡(jiǎn)單快遞系統(tǒng)
本文實(shí)例為大家分享了java實(shí)現(xiàn)簡(jiǎn)單快遞系統(tǒng)的具體代碼,供大家參考,具體內(nèi)容如下

創(chuàng)建四個(gè)類(lèi)Express,Locker, User, Administrator分別模擬快遞,快遞柜,用戶(hù)和管理員。
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) {
? ? ?? ?// 生成快遞柜, 但在后續(xù)修改快遞位置時(shí)沒(méi)考慮越界問(wèn)題
? ? ? ? Locker locker = new Locker(10, 10);
? ? ? ? // 創(chuàng)建管理員
? ? ? ? Administrator administrator = new Administrator(locker);
? ? ? ? //程序主邏輯
? ? ? ? outer: while (true){
? ? ? ? ? ? switch (login("請(qǐng)選擇身份:(-1退出 0管理員身份 1用戶(hù)身份)", 1)){
? ? ? ? ? ? ? ? case -1:
? ? ? ? ? ? ? ? ? ? break outer;
? ? ? ? ? ? ? ? case 0:
? ? ? ? ? ? ? ? ? ? switch (login("請(qǐng)選擇操作:(-1退出 0快遞錄入 1刪除快遞 2修改快遞 3查看快遞)", 3)){
? ? ? ? ? ? ? ? ? ? ? ? case -1:
? ? ? ? ? ? ? ? ? ? ? ? ? ? break;
? ? ? ? ? ? ? ? ? ? ? ? case 0:
? ? ? ? ? ? ? ? ? ? ? ? ? ? in.nextLine();
? ? ? ? ? ? ? ? ? ? ? ? ? ? System.out.println("請(qǐng)輸入公司名稱(chēng):");
? ? ? ? ? ? ? ? ? ? ? ? ? ? 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("請(qǐng)選擇操作:(-1退出 0取快遞)", 0)){
? ? ? ? ? ? ? ? ? ? ? ? case -1:
? ? ? ? ? ? ? ? ? ? ? ? ? ? break;
? ? ? ? ? ? ? ? ? ? ? ? case 0:
? ? ? ? ? ? ? ? ? ? ? ? ? ? in.nextLine();
? ? ? ? ? ? ? ? ? ? ? ? ? ? fetch(locker);
? ? ? ? ? ? ? ? ? ? ? ? ? ? break;
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? break;
? ? ? ? ? ? }
? ? ? ? }
? ? }
?? ?
?? ?// 生成有效選擇,處理無(wú)效選擇產(chǎn)生的異常
? ? 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("請(qǐng)輸入有效選擇!");
? ? ? ? ? ? ? ? in.nextLine();
? ? ? ? ? ? ? ? return login(string, n);
? ? ? ? ? ? }
? ? ? ? ? ? return select;
? ? ? ? }catch (Exception e){
? ? ? ? ? ? System.out.println("請(qǐng)輸入有效選擇!");
? ? ? ? ? ? in.nextLine();
? ? ? ? ? ? return login(string, n);
? ? ? ? }
? ? }
?? ?
?? ?// 用戶(hù)取快遞,確保輸入有效驗(yàn)證碼
? ? public static void fetch(Locker locker){
? ? ? ? System.out.println("請(qǐng)輸入六位驗(yàn)證碼:");
? ? ? ? String code = in.nextLine();
? ? ? ? User user = new User(code, locker);
? ? ? ? if (user.fetch()){
? ? ? ? ? ? System.out.println("取件成功");
? ? ? ? }else {
? ? ? ? ? ? System.out.println("驗(yàn)證碼有誤,請(qǐng)重新輸入!");
? ? ? ? ? ? fetch(locker);
? ? ? ? }
? ? }
}
class Express {
? ? /**
? ? ?* @param code: ? ? 6位驗(yàn)證碼,數(shù)字字符串
? ? ?* @param company: ?公司名稱(chēng)
? ? ?* @param number: ? 14位快遞單號(hào), 數(shù)字字符串
? ? ?* @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("快遞信息簡(jiǎn)介{");
? ? ? ? stringBuilder.append("快遞公司:");
? ? ? ? stringBuilder.append(company);
? ? ? ? stringBuilder.append(", 快遞單號(hào):");
? ? ? ? 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: ? ? ? ? ? 存儲(chǔ)Express
? ? ?* @param ? random: ? ? ? ? 隨機(jī)數(shù)生成器
? ? ?* @param ? position: ? ? ? 存放臨時(shí)快遞位置
? ? ?* @param ? LENGTH_OF_CODE: 驗(yàn)證碼長(zhǎng)度
? ? ?* @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];
? ? }
?? ?
?? ?// 生成驗(yàn)證碼
? ? 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();
? ? }
? ??
?? ?// 生成快遞單號(hào)
? ? 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;
? ? ? ? }
? ? }
?? ?
?? ?// 檢查有無(wú)重復(fù)驗(yàn)證碼,有則將position改成相應(yīng)位置,無(wú)則position第一個(gè)元素為-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;
? ? ? ? ? ? ? ? }
? ? ? ? ? ? }
? ? ? ? }
? ? }
?? ?
?? ?// 檢查有無(wú)重復(fù)快遞單號(hào)
? ? 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("無(wú)快遞");
? ? ? ? }
? ? }
}
class User {
?? ?/**
? ? *@Param: code: ? ? ?驗(yàn)證碼?
? ? *@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("請(qǐng)輸入14位快遞單號(hào):");
? ? ? ? String number = in.nextLine();
? ? ? ? locker.checkNumber(number);
? ? ? ? if (locker.getPosition()[0] != -1){
? ? ? ? ? ? System.out.println("單號(hào)為"+locker.fetch().getNumber()+"的快遞已經(jīng)刪除");
? ? ? ? }else{
? ? ? ? ? ? System.out.println("快遞單號(hào)不存在,請(qǐng)重試");
? ? ? ? ? ? remove();
? ? ? ? }
? ? }
?? ?
?? ?// 修改快遞
? ? public void change(){
? ? ? ? System.out.println("請(qǐng)輸入14位快遞單號(hào):");
? ? ? ? String number = in.nextLine();
? ? ? ? locker.checkNumber(number);
? ? ? ? if (locker.getPosition()[0] != -1){
? ? ? ? ? ? Express express = locker.fetch();
? ? ? ? ? ? System.out.println("請(qǐng)輸入新的快遞公司:");
? ? ? ? ? ? express.setCompany(in.nextLine());
? ? ? ? ? ? System.out.println("請(qǐng)輸入新的位置:(先行后列,空格隔開(kāi))");
? ? ? ? ? ? int row = in.nextInt();
? ? ? ? ? ? int column = in.nextInt();
? ? ? ? ? ? express.setPosition(row, column);
? ? ? ? ? ? locker.store(express, row, column);
? ? ? ? ? ? System.out.println("修改成功!");
? ? ? ? }else{
? ? ? ? ? ? System.out.println("快遞單號(hào)不存在,請(qǐng)重試");
? ? ? ? ? ? change();
? ? ? ? }
? ? }
?? ?
?? ?// 顯示所有快遞信息
? ? public void show(){
? ? ? ? locker.check();
? ? }
}
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Java多線(xiàn)程正確使用倒計(jì)時(shí)協(xié)調(diào)器CountDownLatch方法詳解
這篇文章主要為大家介紹了Java多線(xiàn)程倒計(jì)時(shí)協(xié)調(diào)器CountDownLatch的正確使用方法詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-09-09
前置++和后置++ 運(yùn)算的詳解及實(shí)例代碼
這篇文章主要介紹了前置++和后置++ 的相關(guān)資料,并附示例代碼,幫助大家學(xué)習(xí)參考,需要的朋友可以參考下2016-09-09
使用java文件過(guò)濾器輸出制定格式文件路徑的實(shí)例代碼
這篇文章主要介紹了使用java文件過(guò)濾器輸出制定格式文件路徑的方法,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-11-11
在eclipse導(dǎo)入Java的jar包的方法JDBC(圖文說(shuō)明)
這篇文章主要介紹了在eclipse導(dǎo)入Java 的jar包的方法 JDBC 圖文說(shuō)明 ,需要的朋友可以參考下2015-09-09
利用Maven入手Spring Boot第一個(gè)程序詳解
這篇文章主要給大家介紹了關(guān)于如何利用Maven入手Spring Boot第一個(gè)程序的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧。2018-02-02
SpringBoot自動(dòng)配置與啟動(dòng)流程詳細(xì)分析
這篇文章主要介紹了SpringBoot自動(dòng)配置原理分析,SpringBoot是我們經(jīng)常使用的框架,那么你能不能針對(duì)SpringBoot實(shí)現(xiàn)自動(dòng)配置做一個(gè)詳細(xì)的介紹。如果可以的話(huà),能不能畫(huà)一下實(shí)現(xiàn)自動(dòng)配置的流程圖。牽扯到哪些關(guān)鍵類(lèi),以及哪些關(guān)鍵點(diǎn)2022-11-11
SpringIOC refresh()初始化代碼實(shí)例
這篇文章主要介紹了SpringIOC refresh()初始化代碼實(shí)例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-03-03
java去除重復(fù)對(duì)象的簡(jiǎn)單實(shí)例
下面小編就為大家?guī)?lái)一篇java去除重復(fù)對(duì)象的簡(jiǎn)單實(shí)例。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-01-01

