Java基础-05数组综合任务-快递E栈

1. 任务描述

1.1. 使用对象

  1. 用户
  2. 快递员

1.2. 功能

  1. 实现<快递单号>,<快递公司>,<取件码>等信息的存储/修改/删除/查询
  2. 提供交互提示页面给<用户>/<快递员>
  3. <用户>操作
    > 通过输入<取件码>查询快递信息,并且取件
  4. <快递员>操作
    • 存件

      通过输入<快递单号>,<快递公司>存储快递(隐藏信息:需要随机生成取件码)

    • 查询

      通过输入<快递单号>查询快递信息

    • 修改

      通过<快递单号>查询快递,并且修改<快递单号>,<快递公司>等信息;

    • 删除

      通过<快递单号>查询快递,并且删除

  5. more: 同一家快递公司的快递单号不能重复

2. 分解任务

2.1. 对象

  1. 快递员
  2. 用户
  3. 快递E栈系统

2.2. 操作

  1. <快递员>向<快递E栈系统>提交存储信息实现存件
  2. <用户>向<快递E栈系统>输入<取件码>取件
  3. <快递E栈系统>查询/修改/删除存储信息

2.3. 细节

  1. 用户

    1. 方法
      * 取快递
    2. 数据
      * NA
  2. 快递员

    1. 方法
      * 存快递
    2. 数据
      * 快递单号
      * 快递公司
  3. 快递E栈系统

    1. 方法
      * 交互界面信息
      * 查询
      * 修改
      * 删除
    2. 数据
      * 快递E栈容量
      * 已存快递总数
      * 快递单号
      * 快递公司
      * 取件码

3. 测试框架搭建

  • 详情请见 4. 方法模块编写 ==>> 4.4. ScenarioTest

3.1. 交互界面进入

3.2. 快递员存储快递

3.3. 用户取快递

3.4. 快递E栈系统查询快递

3.5. 交互界面退出


4. 方法模块编写

4.1. User

public class User {
	private Scanner input = new Scanner(System.in);

	public String input_code() {
		System.out.println("请输入您的取件码");
		return this.input.next();
	}
}

4.2. Deliveryman

public class Deliveryman {
	private Scanner input = new Scanner(System.in);
	
	public String input_number() {
		System.out.println("请输入快递单号");
		return this.input.next();
	}
	
	public String input_company() {
		System.out.println("请输入公司名称");
		return this.input.next();
	}
}

4.3. ExpressStation


public class ExpressStation {
	private Scanner input = new Scanner(System.in);
	private int capacity;			// 快递E栈容量
	private int stored;				// 已经存储快递数
	private String[] arr_number;	// 快递单号数组
	private String[] arr_company;	// 快递公司数组
	private String[] arr_code;		// 取件码数组

	/**
	 * @param capacity
	 */
	public ExpressStation(int capacity) {
		this.capacity = capacity;
		this.stored = 0;
		this.arr_number = new String[capacity];
		this.arr_company = new String[capacity];
		this.arr_code = new String[capacity];
	}
	
	public int startMenu() {
		System.out.println("请问您是要存快递还是取快递?1-存快递,2-取快递,3-查询所有快递,0-退出");
		int selection = this.input.nextInt();
		switch (selection) {
		case 0:
			System.out.println("正在准备退出");
			break;
			
		case 1:
			System.out.println("正在准备存快递");
			break;

		case 2:
			System.out.println("正在准备取快递");
			break;

		case 3:
			System.out.println("正在准备查询快递");
			break;
			
		default:
			selection = -1;
			System.out.println("请确认您的输入是否正确");
			break;
		}
		
		return selection;
	}
	
	public void endMenu() {
		System.out.println("=====感谢您的使用=====");
	}
	
	public int storeExpress(String number, String company) {
		System.out.println("=====正在准备创建快递表单信息=====");
		int box = -1;
		if (this.stored >= this.capacity) {
			System.out.println("快递E栈已满,请移步其他快递E栈存储快递");
			return box;
		}
		
		// 先判断快递单号是否已经存在
		int index = getStoredExpressByNumber(number);
		if (index != -1) {
			System.out.println("快递已存在,请确认后重试");
			return box;
		}
		
		// 检查完成,存储快递信息
		int code = setExpressCode();
		box = setExpressInfo(number, company, "" + code, this.stored);
		
		this.stored++;
		
		return box;
	}
	
	public int retriveExpress(String code) {
		System.out.println("=====正在准备取快递=====");
		int index = getStoredIndexByCode(code);
		if (index == -1) {
			System.out.println("没有找到快递,请确认后重试");
			return index;
		}		

		String number = arr_number[index];
		String company = arr_company[index];
		String codeIn = arr_code[index];
		System.out.println("取到快递:" + number + "\t\t" + company + "\t\t" + codeIn);
		
		for (int i = index; i < this.stored - 1; i++) {
			setExpressInfo(arr_number[i + 1], arr_company[i + 1], arr_code[i + 1], i);
		}
		
		this.stored--;
		return index;
	}
	
	public void printExpress() {
		System.out.println("快递单号\t\t快递公司\t\t取件码");
		for (int i = 0; i < this.stored; i++) {
			System.out.println(this.arr_number[i] + "\t\t"
				+ this.arr_company[i] + "\t\t" + this.arr_code[i]);
		}
	}

	/**
	 * @param number
	 * @param company
	 * @return  -1:failed; > 0: set express infomation OK;
	 * 			need check parameters before using this method!
	 */
	public int setExpressInfo(String number, String company, String code, int index) {
		this.arr_number[index] = number;
		this.arr_company[index] = company;
		this.arr_code[index] = code;
		return index;
	}

	/**
	 * @param NA
	 */
	public int setExpressCode() {
		// 生成取件码, 范围100-999, 注意随机数也有可能会重复
		// execute silently, no log needed
		Random random = new Random();
		int code = 0;
		while (true) {
			code = random.nextInt(900) + 100;
			int storedIndex = getStoredIndexByCode("" + code);
			if (storedIndex == -1) {
				break;
			}
		}

		return code;
	}

	/**
	 * @param code 取件码
	 * @return
	 */
	public int getStoredIndexByCode(String code) {
		int indexStored = -1;
		for (int i = 0; i < this.stored; i++) {
			if (arr_code[i].equals("" + code)) {
				indexStored = i;
				break;
			}
		}
		return indexStored;
	}

	/**
	 * @param number
	 * @return
	 */
	public int getStoredExpressByNumber(String number) {
		int index = -1;
		for (int i = 0; i < this.stored; i++) {
			if (arr_number[i].equals(number)) {
				index = i;
				break;
			}
		}
		return index;
	}
}

4.4. ScenarioTest

public class ScenarioTest {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner input = new Scanner(System.in);
		User user = new User();
		Deliveryman dman = new Deliveryman();
		ExpressStation eStat = new ExpressStation(3);
		
		out:
		do {
			int selection = eStat.startMenu();

			switch (selection) {
			case 0:
				eStat.endMenu();
				break out;
				
			case 1:
				String number = dman.input_number();
				String company = dman.input_company();
				eStat.storeExpress(number, company);				
				break;

			case 2:
				String code = user.input_code();
				eStat.retriveExpress(code);
				break;

			case 3:
				eStat.printExpress();
				break;
				
			default:
				break;
			}
		} while (true);
		
		input.close();
	}
}

5. 调试

  • 需要自己打log追踪问题,或者打断点
  • 注意数组的上下限
  • index获取/判断已存在
  • 统一使用String作为存储信息
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值