采用二维数组的方式,存储快递
1. 管理员
- 快递录入
- 柜子位置(系统产生,不能重复)
- 快递单号(输入)
- 快递公司(输入)
- 6位取件码(系统产生,不能重复)
- 删除快递(根据单号)
- 修改快递(根据单号)
- 查看所有快递(遍历)
2. 普通用户
- 快递取出
- 输入取件码:显示快递的信息和在哪个柜子中,从柜子中移除这个快递
Express
package Task_010205.bean;
import java.util.Objects;
public class Express {
private String number;
private String company;
private int code;
private int row;
private int column;
@Override
public String toString() {
return number + "\t\t" + company + "\t\t" + code + "\t" + row + "\t" + column;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Express express = (Express) o;
return Objects.equals(number, express.number);
}
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;
}
public int getRow() {
return row;
}
public void setRow(int row) {
this.row = row;
}
public int getColumn() {
return column;
}
public void setColumn(int column) {
this.column = column;
}
public Express() {
}
public Express(String number, String company, int code, int row, int column) {
this.number = number;
this.company = company;
this.code = code;
this.row = row;
this.column = column;
}
}
View
package Task_010205.view;
import Task_010205.bean.Express;
import java.util.Scanner;
import static java.lang.Integer.parseInt;
public class View {
private Scanner input = new Scanner(System.in);
/**
* 欢迎
*/
public void welcome() {
System.out.println("======欢迎使用新职课快递柜======");
}
/**
* 再见
*/
public void bye() {
System.out.println("欢迎下次使用");
}
/**
* 主菜单
* @return
*/
public int menu() {
System.out.println("---开始菜单---");
System.out.println("请输入您的身份");
System.out.println("1、快递员");
System.out.println("2、用户");
System.out.println("0、退出");
String strNum = input.nextLine();
int num = -1;
try {
num = driver(strNum,0,2);
} catch (NumberFormatException e) {
return menu();
}
return num;
}
/**
* 快递员菜单
* @return
*/
public int cMenu() {
System.out.println("请选择功能序号");
System.out.println("1、录入快递");
System.out.println("2、删除快递");
System.out.println("3、修改快递");
System.out.println("4、查看所有快递");
System.out.println("0、返回上一层");
String strNum = input.nextLine();
int num = 0;
try {
num = driver(strNum,0,4);
} catch (NumberFormatException e) {
return cMenu();
}
return num;
}
/**
* 用户取件
*/
public int uMenu() {
System.out.println("请输入取件码:");
String strNum = input.nextLine();
int code = 0;
try {
code = driver(strNum,100000,999999);
} catch (NumberFormatException e) {
return uMenu();
}
return code;
}
private int driver(String strNum,int begin,int end) throws NumberFormatException {
int num = parseInt(strNum);
if (num<begin || num>end) {
System.out.println("输入错误,请重新输入");
}
return num;
}
/**
* 录入快递
* @return
*/
public Express insert() {
System.out.println("请输入快递单号:");
String number = input.nextLine();
System.out.println("请输入快递公司:");
String company = input.nextLine();
Express e = new Express();
e.setNumber(number);
e.setCompany(company);
return e;
}
/**
* 删除快递
* @return
*/
public int delete() {
System.out.println("是否确认删除?");
System.out.println("1、是");
System.out.println("2、否");
String strNum = input.nextLine();
int num = -1;
try {
num = driver(strNum,1,2);
} catch (NumberFormatException e) {
return delete();
}
return num;
}
/**
* 修改快递
* @return
*/
public Express update(Express e) {
System.out.println("请输入新的快递单号:");
String number = input.nextLine();
System.out.println("请输入新的快递公司:");
String company = input.nextLine();
e.setNumber(number);
e.setCompany(company);
return e;
}
/**
* 查看所有快递
* @param ex
*/
public void printAll(Express[][] ex) {
if (ex==null) {
System.out.println("暂无快递");
} else {
System.out.println("快递单号\t快递公司\t取件码\t行\t列");
for (int i=0;i<10;i++) {
for (int j=0;j<10;j++) {
if (ex[i][j] != null) {
System.out.println(ex[i][j].toString());
}
}
}
}
}
/**
* 展示快递信息
* @param e
*/
public void showExpress(Express e) {
System.out.println("快递信息如下:");
System.out.println("第"+e.getRow()+"排,第"+e.getColumn()+"列");
System.out.println("快递单号:"+e.getNumber()+",快递公司:"+e.getCompany()+",取件码"+e.getCode());
}
/**
* 根据单号查询
* @return
*/
public String findByNumber() {
System.out.println("请输入快递单号:");
String number = input.nextLine();
return number;
}
/**
* 存在
*/
public void exist() {
System.out.println("此单号柜中已存在,请勿重新输入");
}
/**
* 成功
*/
public void success() {
System.out.println("操作成功");
}
/**
* 失败
*/
public void failure() {
System.out.println("操作失败");
}
/**
* 空值
*/
public void getNull() {
System.out.println("此快递不存在");
}
}
Dao
package Task_010205.dao;
import Task_010205.bean.Express;
import java.util.Random;
public class Dao {
private Express[][] ex = new Express[10][10];
private Random random = new Random();
private int size = 0;
/**
* 录入快递
* @param e
* @return
*/
public boolean insert(Express e) {
if (size==100) {
System.out.println("快递柜已满");
return false;
}
int row;
int column;
while (true) {
row = random.nextInt(10);
column = random.nextInt(10);
if (ex[row][column]==null) {
break;
}
}
int code = getCode();
e.setCode(code);
e.setRow(row+1);
e.setColumn(column+1);
ex[row][column] = e;
size++;
return true;
}
/**
* 不重复的取件码
* @return
*/
private int getCode() {
while (true) {
int code = random.nextInt(900000)+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 (ex[i][j]!=null) {
if (ex[i][j].getCode()==code) {
return ex[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 (ex[i][j]!=null) {
if (e.equals(ex[i][j])) {
return ex[i][j];
}
}
}
}
return null;
}
/**
* 删除快递
* @param e
* @return
*/
public boolean delete(Express e) {
for (int i=0;i<10;i++) {
for (int j=0;j<10;j++) {
if (e.equals(ex[i][j])) {
ex[i][j] = null;
size--;
return true;
}
}
}
return false;
}
/**
* 修改快递
* @param e
* @param e2
*/
public void update(Express e,Express e2) {
delete(e);
insert(e2);
}
/**
* 查看所有快递
* @return
*/
public Express[][] printAll() {
return size>0?ex:null;
}
}
Main
package Task_010205.main;
import Task_010205.bean.Express;
import Task_010205.dao.Dao;
import Task_010205.view.View;
public class Main {
private static View view = new View();
private static Dao dao = new Dao();
public static void main(String[] args) {
init();
}
private static void init() {
view.welcome();
m:while (true) {
int num = view.menu();
if (num == 1) {
System.out.println("---快递员菜单---");
client();
} else if (num == 2) {
System.out.println("---用户取件---");
user();
} else if (num == 0) {
break m;
}
}
view.bye();
}
public static void client() {
p:while (true) {
int num = view.cMenu();
if (num == 1) {
System.out.println("---录入快递---");
insert();
} else if (num == 2) {
System.out.println("---删除快递---");
delete();
} else if (num == 3) {
System.out.println("---修改快递---");
update();
} else if (num == 4) {
System.out.println("---查看所有快递---");
printAll();
} else if (num == 0) {
break p;
}
}
}
/**
* 录入快递
*/
public static void insert() {
Express e = view.insert();
if (dao.findByNumber(e.getNumber())!=null) {
view.exist();
return;
}
if (dao.insert(e)) {
view.showExpress(e);
view.success();
} else {
view.failure();
}
}
/**
* 删除快递
*/
public static void delete() {
String number = view.findByNumber();
Express e = dao.findByNumber(number);
if (e==null) {
view.getNull();
} else {
int num = view.delete();
if (num==1) {
dao.delete(e);
view.success();
}
}
}
/**
* 修改快递
*/
public static void update() {
String number = view.findByNumber();
Express e = dao.findByNumber(number);
if (e==null) {
view.getNull();
} else {
view.showExpress(e);
Express e2 = view.update(e);
dao.update(e,e2);
view.success();
}
}
/**
* 查看所有快递
*/
public static void printAll() {
view.printAll(dao.printAll());
}
/**
* 用户取件
*/
public static void user() {
int code = view.uMenu();
Express e = dao.findByCode(code);
if (e==null) {
view.getNull();
} else {
view.showExpress(e);
dao.delete(e);
view.success();
}
}
}