Java集合实现快递E栈管理系统(控制台版)
一 、功能介绍
管理员(快递员)
- 快递录入
- 快递的位置(系统生成)
- 快递单号(输入)
- 快递公司(输入)
- 六位取件码(系统随机生成)
- 删除快递(根据单号)
- 修改快递(根据单号)
- 查看所有快递
普通用户
- 取快递
- 输入取件码,显示快递信息和快递柜的位置
二 、解决思路
- 基于面向对象思想,我们可以将快递的相关信息封装为一个类(Express),提供相应的get、set方法来操作属性。
- 对于快递的位置,为了操作方便,我们在Express类中设置两个属性x和y,分别对应快递柜的所在行和所在列。
- 由于涉及到根据指定单号快速定位到快递信息,结合每个集合的特点,我们使用 Map 集合进行数据存储,key为快递单号,value 为快递对象。
三、实现代码
1 快递类的实现
public class Express {
String number; //快递单号
String company; //快递公司名称
int code; //取件码
int x; //快递的行数
int y; //快递的列数
public Express(String number, String company, int code, int x, int y) {
this.number = number;
this.company = company;
this.code = code;
this.x = x;
this.y = y;
}
public Express() {
}
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 getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
}
2 菜单和逻辑代码的实现
import java.util.HashMap;
import java.util.Random;
import java.util.Scanner;
/**
* @author :lxc
* @date :Created in 15:21
* @description:主类,实现身份选择、菜单显示和业务逻辑
*/
public class Main {
static Scanner scanner = new Scanner(System.in);
static HashMap<String, Express> hashMap = new HashMap<>(); //存储快递的数据结构
public static void main(String[] args) {
while (true) {
System.out.println("欢迎使用智慧快递柜!");
System.out.println("请选择您的身份码:1-快递员;2-普通用户");
String s = scanner.nextLine();
int code = Integer.parseInt(s);
if (code == 1) {
cClient();
} else if (code == 2) {
uClient();
} else {
System.out.println("身份码有误,请重新输入!");
continue;
}
}
}
private static void cClient() {
System.out.println("请输入操作码:0-返回上级菜单;1-录入快递;2-删除快递;3-查看所有快递;4-修改快递;");
String s = scanner.nextLine();
int i = -1;
try {
i = Integer.parseInt(s);
}catch (NumberFormatException numberFormatException){
}
if (i == 0) {
main(new String[]{"1", "2"});
} else if (i == 1) {
System.out.println("请输入快递单号:");
String number = scanner.nextLine();
System.out.println("请输入快递公司名称:");
String company = scanner.nextLine();
addExpress(number, company);
} else if (i == 2) {
System.out.println("请输入需要删除的快递的单号:");
String number = scanner.nextLine();
deleteExpress(number);
} else if (i == 3) {
showExpress();
} else if(i==4){
System.out.println("输入待修改的快递单号:");
String s1 = scanner.nextLine();
System.out.println("输入修改后的快递单号:");
String number = scanner.nextLine();
System.out.println("输入修改后的快递公司名称:");
String company= scanner.nextLine();
updateExpress(s1,number,company);
}else {
System.out.println("操作码输入错误,请重新输入!");
cClient();
}
}
/**
* 修改快递信息
* @param s1 原来的快递单号
* @param number 新的单号
* @param company 新的公司名称
*/
private static void updateExpress(String s1, String number, String company) {
//先遍历hashmap找到对应的快递,再更新
Express express = hashMap.get(s1);
if (express==null){
System.out.println("该快递不存在,请检查输入!");
cClient();
}else{
express.setNumber(number);
express.setCompany(company);
hashMap.put(number,express);
hashMap.remove(s1); //删除原来的快递
System.out.println("修改成功!");
}
}
/**
* 查询出所有快递并显示相关信息
*/
private static void showExpress() {
//遍历hashmap的键集输出
if (hashMap.keySet().isEmpty()){
System.out.println("当前没有快递!");
cClient();
}else {
System.out.println("快递信息如下:");
for (String number : hashMap.keySet()) {
Express e = hashMap.get(number);
System.out.println("第" + e.getX() + "行, 第" + e.getY() + "列, 快递单号为:" + number + ", 快递公司为:" + e.getCompany() + ", 取件码为:" + e.getCode());
}
}
}
/**
* 根据快递单号删除快递
*
* @param number
*/
private static void deleteExpress(String number) {
if (hashMap.containsKey(number) == false) {
System.out.println("不存在该快递,请检查单号!");
cClient();
} else {
hashMap.remove(number); //删除映射关系
// hashMap.remove()
System.out.println("删除成功!");
}
}
/**
* 添加快递,并生成快递位置和随机的六位取件码
*
* @param number 单号
* @param company 快递公司名称
*/
private static void addExpress(String number, String company) {
Express e = new Express();
Random random = new Random();
if (hashMap.containsKey(number)) { //已经存在对应单号的快递
System.out.println("该单号已存在,请勿重复录入!");
cClient();
} else { //设置信息
e.setCompany(company);
e.setNumber(number);
//生成位置
int x = -1, y = -1;
do {
x = random.nextInt(10);
y = random.nextInt(10);
} while (hashMap.get(x) != null && hashMap.get(y) != null);
e.setX(x);
e.setY(y);
//生成随机的取件码
int code = randomCode();
e.setCode(code);
hashMap.put(number, e);
System.out.println("该快递位置为第" + x + "行,第" + y + "列!取件码为:" + code);
}
}
/**
* 生成六位随机取件码
*
* @return
*/
private static int randomCode() {
Random random = new Random();
int code = 0;
do {
code = random.nextInt(899999) + 100000;
} while (hashMap.get(code) != null); //保证取件码不重复
return code;
}
private static void uClient() {
System.out.println("请输入取件码:");
String s = scanner.nextLine();
int code = Integer.parseInt(s);
getExpress(code);
}
/**
* 根据取件码取件
* @param code 取件码
*/
private static void getExpress(int code) {
for(String number:hashMap.keySet()){
Express e = hashMap.get(number);
if (e.getCode()==code) { //找到对应的快递,则输出位置信息并删除
System.out.println("该快递位置为第" + e.getX() + "行,第" + e.getY() + "列!");
hashMap.remove(number);
System.out.println("取件成功!");
return;
}
}
System.out.println("取件码不存在,请检查输入!");
}
}
随手点赞,手有余香!
有任何问题,欢迎评论区留言讨论!