用链表实现快递管理
一 任务概述
1,管理员:
- 快递录入
- 快递位置 (随机,不能重复)
- 快递单号 (输入)
- 快递公司 (输入)
- 6位取件码 (随机,不能重复)
- 快递删除 (根据单号)
- 快递修改 (根据单号)
- 查看所有快递 (遍历)
2,普通用户
- 快递取出
- 输入取件码,显示快递信息和在哪个柜子,从柜子中移除快递
二 复习
1 集合的分类、特点与区别
数组 集合
长度 固定 可变
存储类型 同类型 类型可不一致
存储单元 元素 对象
使用场景 — 对象多时
数组存在的问题:元素的个数固定(不能扩容)、不利于插入。
集合: java 对数据结构(数组、栈、队列、链表和红黑树等)的实现。
类集中最大的几个操作接口:Collection、Map、Iterator,这三个接口为要使用的最重点的接口。
Collection Map
单列集合,单值存储 双列集合,对值存储
Collection Collection Collection Collection
List List Set Set
元素有序,元素可重复 元素有序,元素可重复 元素无序,且不可重复 元素无序,且不可重复
ArrayList LinkedList HashSet TreeSet
数据存储的结构是数组结构 链表结构(可当做栈、队列使用) 链表和哈希表组合的数据存储结构
元素增删慢,查找快 方便元素增删(首尾方法) 散列无序地存储 有序地二叉树存储(自然顺序)
Map:
HashMap与HashTable,结果一样,散列存放;
ConcurrentHashMap,结果一样但与HashMap顺序不同;
TreeMap,会自动排序;
LinkedHashMap,使用HashMap还能保证存储顺序,既在HashMap中又在双向链表中。
鉴于(1)快递的单号、取件码、存货位置不可重复(排除List类),(2)一码一货(key -> value),选择使用HashMap(对值存储)。
三.代码`
package Ema;
import java.util.Objects;
public class Express {
private String number; // 快递单号
private String company; // 快递公司
private int code; // 取件码
private int x; // 快递箱排数
private int y; // 快递箱列数
public Express(String number) {
this.number = number;
}
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 String getNumber() {
return number;
}
public String getCompany() {
return company;
}
public int getCode() {
return code;
}
public int getX() {
return x;
}
public int getY() {
return y;
}
public void setNumber(String number) {
this.number = number;
}
public void setCompany(String company) {
this.company = company;
}
public void setCode(int code) {
this.code = code;
}
public void setX(int x) {
this.x = x;
}
public void setY(int y) {
this.y = y;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Express express = (Express) o;
return code == express.code &&
x == express.x &&
y == express.y &&
Objects.equals(number, express.number) &&
Objects.equals(company, express.company);
}
@Override
public int hashCode() {
return Objects.hash(number, company, code, x, y);
}
@Override
public String toString() {
return "Express{" +
"number='" + number + '\'' +
", company='" + company + '\'' +
", code=" + code +
", x=" + x +
", y=" + y +
'}';
}
}
package Ema;
import java.util.Scanner;
public clas