任务要求
采用二维数组方式,存储快递。
1. 管理员
- 快递录入
- 柜子位置 (系统产生,不能重复〉
- 快递单号<输入>
- 快递公司<输入>
- 6位取件码〈系统产生,不能重复)
- 删除快递(根据单号)
- 修改快递(根据单号〉
- 査看所有快递 <遍历>
2. 普通用户
- 快递取出
- 输入取件码:显示快递的信息和在哪个柜子中,从柜子中移除这个快递
文件目录
express
- bean
- Express.java //快递对象
- dao
- ExpressDao.java //操作数据
- main
- Main.java //入口文件
- view
- Views.java //视图文件
实现代码
Express.java
package com.xiaolinwl.express.bean;
import java.util.Objects;
public class Express {
private String number;//单号
private String company;//快递公司
private int code;//取件码
public Express(String number, String company, int code) {
this.number = number;
this.company = company;
this.code = code;
}
public Express() {
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Express express = (Express) o;
return number.equals(express.number);
}
@Override
public int hashCode() {
return Objects.hash(code);
}
@Override
public String toString() {
return "Express{" +
"number='" + number + '\'' +
", company='" + company + '\'' +
", code=" + code +
'}';
}
public String getNumber() {
return number;
}
public void setNumber(String number) {
this.number = number;
}
public String getCompany() {
return company;
}
public void setCompany(String company) {
this.company = company;
}
public int getCode() {
return code;
}
public void setCode(int code) {
this.code = code;
}
}
ExpressDao
package com.xiaolinwl.express.dao;
import com.xiaolinwl.express.bean.Express;
import java.util.Random;
public class ExpressDao {
private Express[][] postList = new Express[10][];
{
for (int i = 0; i < 10; i++) {
postList[i] = new Express[10];
}
}
private int size = 0;//记录当前快递数量
private Random random = new Random();
/**
* 存储快递
*
* @param e
* @return
*/
public boolean add(Express e) {
if (size == 100) {
return false;
}
//1.随机生成下标存快递
int x = -1;
int y = -1;
while (true) {
x = random.nextInt(10);
y = random.nextInt(10);
if (postList[x][y] == null) {
//此位置无快递
break;
}
}
//2.生成取件码
int code = randomCode();
e.setCode(code);
postList[x][y] = e;
return true;
}
private int randomCode() {
while (true) {
int code = (int) ((Math.random() * 9 + 1) * 100000);
Express e = findByCode(code);
if (e == null) {
return code;
}
}
}
/**
* 根据取件码查询快递对象
*
* @param code 取件码
* @return
*/
public Express findByCode(int code) {
for (int i = 0; i < 10; i++) {
for (int j = 0; j < 10; j++) {
if (postList[i][j] != null) {
if (postList[i][j].getCode() == code) {
return postList[i][j];
}
}
}
}
return null;
}
/**
* 根据单号查询快递对象
*
* @param number 单号
* @return
*/
public Express findByNumber(String number) {
Express e = new Express();
e.setNumber(number);
for (int i = 0; i < 10; i++) {
for (int j = 0; j < 10; j++) {
if (e.equals(postList[i][j])) {//用非空的调用equals方法
return postList[i][j];
}
}
}
return null;
}
/**
* 修改快递信息
*
* @param oldExpress
* @param newExpress
*/
public void update(Express oldExpress, Express newExpress) {
delete(oldExpress);
add(newExpress);
}
public void delete(Express e) {
p:
for (int i = 0; i < 10; i++) {
for (int j = 0; j < 10; j++) {
if (e.equals(postList[i][j])) {//用非空的调用equals方法
postList[i][j] = null;
break p;
}
}
}
}
public Express[][] findAll() {
return postList;
}
}
Main
package com.xiaolinwl.express.main;
import com.xiaolinwl.express.bean.Express;
import com.xiaolinwl.express.dao.ExpressDao;
import com.xiaolinwl.express.view.Views;
public class Main {
//初始化视图对象
private static Views v = new Views();
//初始化Dao对象
private static ExpressDao dao = new ExpressDao();
public static void main(String[] args) {
//1. 欢迎
v.welcome();
//2. 弹出身份选择菜单
m:
while (true) {
int type = v.menu();
switch (type) {
case 0:
break m;
case 1:
cClient();
break;
case 2:
uClient();
break;
}
}
//3. 弹出拜拜
v.bye();
}
private static void uClient() {
//1. 获取取件码
int code = v.uMenu();
//2. 根据取件码查询
Express e = dao.findByCode(code);
if (e == null) {
v.printNull();
} else {
dao.delete(e);
v.success();
v.printExpress(e);
}
}
private static void cClient() {
int type = v.cMenu();
switch (type) {
case 0:
return;
case 1: {
//1. 提示输入快递信息
Express e = v.insert();
//2. 查询是否存储过
Express e2 = dao.findByNumber(e.getNumber());
//3. 存储快递
if (e2 == null) {
dao.add(e);
v.printExpress(e);
} else {
//单号已存在
v.expressExist();
}
}
break;
case 2: {
//1. 提示输入快递信息
String number = v.findByNumber();
//2. 查询数据
Express e = dao.findByNumber(number);
Express e2 = e;
//3. 打印快递信息
if (e == null) {
v.printNull();
} else {
v.printExpress(e);
//4. 提示修改
v.update(e);
dao.update(e, e2);
v.printExpress(e);
}
}
break;
case 3: {
//删除
//1. 提示输入快递信息
String number = v.findByNumber();
//2. 查询数据
Express e = dao.findByNumber(number);
if (e == null) {
v.printNull();
} else {
v.printExpress(e);
int opt = v.delete();
if (opt == 1) {
dao.delete(e);
v.success();
}
}
}
break;
case 4: {
//查看所有
Express[][] data = dao.findAll();
v.printAll(data);
}
break;
}
}
}
Views
package com.xiaolinwl.express.view;
import com.xiaolinwl.express.bean.Express;
import java.util.Scanner;
public class Views {
private Scanner scanner = new Scanner(System.in);
public void welcome() {
System.out.println("====欢迎使用快递柜====");
}
public void bye() {
System.out.println("====欢迎下次使用====");
}
/**
* 主菜单
*
* @return
*/
public int menu() {
System.out.println("请输入您的身份:1-快递员 2-用户 0-退出");
String text = scanner.nextLine();
int type = -1;
try {
type = Integer.parseInt(text);
} catch (NumberFormatException e) {
System.out.println("输入有误,请重新输入!");
return menu();
}
if (type < 0 || type > 2) {
System.out.println("输入有误,请重新输入!");
return menu();
}
return type;
}
/**
* 管理员菜单
*
* @return
*/
public int cMenu() {
System.out.println("请选择操作:1-存快递 2-修改快递信息 3-删除快递 4-查看所有快递 0-返回上一级");
String text = scanner.nextLine();
int type = -1;
try {
type = Integer.parseInt(text);
} catch (NumberFormatException e) {
System.out.println("输入有误,请重新输入!");
return cMenu();
}
if (type < 0 || type > 4) {
System.out.println("输入有误,请重新输入!");
return cMenu();
}
return type;
}
public Express insert() {
System.out.println("请根据提示输入快递信息:");
System.out.println("请输入快递单号:");
String number = scanner.nextLine();
System.out.println("请输入快递公司:");
String company = scanner.nextLine();
Express e = new Express();
e.setNumber(number);
e.setCompany(company);
return e;
}
public String findByNumber() {
System.out.println("请根据提示输入快递信息:");
System.out.println("请输入快递单号:");
String number = scanner.nextLine();
return number;
}
public void printExpress(Express e) {
System.out.println("快递信息如下:");
System.out.println("快递公司:" + e.getCompany() + ",快递单号:" + e.getNumber() + ",取件码:" + e.getCode());
}
public void printNull() {
System.out.println("快递不存在,请检查您的输入!");
}
public void printAll(Express[][] es) {
int count = 0;
for (int i = 0; i < 10; i++) {
for (int j = 0; j < 10; j++) {
if (es[i][j] != null) {
count++;
System.out.println("第" + (i + 1) + "排,第" + (j + 1) + "列快递:");
printExpress(es[i][j]);
}
}
}
if (count == 0) {
System.out.println("当暂无快递信息!");
}
}
public Express update(Express e) {
System.out.println("请输入新的快递单号:");
String newNumber = scanner.nextLine();
System.out.println("请输入新的快递公司:");
String newCompany = scanner.nextLine();
e.setNumber(newNumber);
e.setCompany(newCompany);
return e;
}
public int delete() {
System.out.println("是否确认删除?");
System.out.println("1-确认删除 2-取消操作 0-返回上一级");
String text = scanner.nextLine();
int type = -1;
try {
type = Integer.parseInt(text);
} catch (NumberFormatException e) {
System.out.println("输入有误,请重新输入!");
return delete();
}
if (type < 0 || type > 2) {
System.out.println("输入有误,请重新输入!");
return delete();
}
return type;
}
/**
* 用户菜单
*
* @return
*/
public int uMenu() {
System.out.println("请输入您的取件码:");
String text = scanner.nextLine();
int code = -1;
try {
code = Integer.parseInt(text);
} catch (NumberFormatException e) {
System.out.println("输入有误,请重新输入!");
return uMenu();
}
if (code < 100000 || code > 999999) {
System.out.println("输入有误,请重新输入!");
return uMenu();
}
return code;
}
public void expressExist() {
System.out.println("快递已存在,请勿重复添加");
}
public void success() {
System.out.println("操作成功!");
}
}