代码均由IntelliJ IDEA 2020.1.4 x64运行
任务概述:
要求:运用面向对象,集合,IO流,实现快递管理系统
包视图:
- Express1:快递信息存储类
- MyList:调度逻辑类
- View1:视图类
- util:序列化与反序列化
- main:调用实现类
注:导包可能出现问题,这里就不用我的包了,运用时看情况导包。
1.快递信息储存类:
public class Express1 implements Serializable {
private String number;
private String company;
private int code;
public Express1() {
}
public Express1(String number, String company, int code) {
this.number = number;
this.company = company;
this.code = code;
}
@Override
public String toString() {
return "Express1{" +
"number='" + number + '\'' +
", company='" + company + '\'' +
", code=" + code +
'}';
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Express1 express1 = (Express1) o;
return Objects.equals(number, express1.number);
}
@Override
public int hashCode() {
return Objects.hash(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 class View1 {
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("1. 快递员");
System.out.println("2. 用户");
System.out.println("0. 退出");
String text = input.nextLine();//输入功能序号
int num = -1;
try {
num = Integer.parseInt(text);//将输入的值转换为int类型
}catch (Exception e){
}
if (num<0 || num>2){
System.out.println("输入有误,请重新输入:");
return menu();
}
return num;
}
/**
* 快递员菜单
* @return
*/
public int Courier(){
System.out.println("请根据提示,输入功能序号:");
System.out.println("1. 快递录入");
System.out.println("2. 快递删除");
System.out.println("3. 快递修改");
System.out.println("4. 查看所有快递");
System.out.println("0. 返回上级目录");
String text = input.nextLine();
int num = -1;
try {
num = Integer.parseInt(text);
}catch (Exception e){
}
if (num<0 || num>4){
System.out.println("输入有误,请重新输入:");
return Courier();
}
return num;
}
/**
* 快递录入
* @return
*/
public Express1 Insert(){
System.out.println("请根据提示,输入快递信息:");
System.out.println("请输入快递单号:");
String number = input.nextLine();
System.out.println("请输入快递公司:");
String company = input.nextLine();
Express1 e = new Express1();
e.setNumber(number);//将快递单号设置为新输入的值
e.setCompany(company);//将快递公司设置为新输入的值
return e;
}
/**
* 快递删除
* @return
*/
public int delete() {
System.out.println("是否确认删除?");
System.out.println("1. 确认删除");
System.out.println("2. 取消操作");
System.out.println("0. 退出");
String text = input.nextLine();
int num = -1;
try {
num = Integer.parseInt(text);
} catch (Exception e) {
}
if (num < 0 || num > 2) {
System.out.println("输入错误,请重新输入:");
return delete();
}
return num;
}
/**
* 查询快递单号
* @return
*/
public String findByNumber(){
System.out.println("请根据提示,输入快递信息:");
System.out.println("请输入要操作的快递单号:");
String number = input.nextLine();
return number;
}
/**
* 没有找到快递
*/
public void printNull(){
System.out.println("没有找到该快递");
}
/**
* 修改快递
* @param e
*/
public void update(Express1 e){
System.out.println("请输入新的快递单号:");
String number = input.nextLine();
System.out.println("请输入新的快递公司:");
String company = input.nextLine();
e.setNumber(number);
e.setCompany(company);
}
/**
* 打印所有快递信息
* @param e
*/
public void printExpress(Express1 e){
System.out.println("快递信息如下:");
System.out.println("快递公司;"+e.getCompany()+",快递单号:"+e.getNumber()+",取件码:"+e.getCode());
}
/**
* 查看所有快递
* @param list
*/
public void printAll(ArrayList<Express1> list){
if (list.isEmpty()){
System.out.println("没有信息");
return;
}else {
System.out.println(list.toArray().toString());
}
}
/**
* 用户界面
* @return
*/
public int User(){
System.out.println("请根据提示,输入快递信息:");
System.out.println("请输入取件码:");
String code = input.nextLine();
int num = -1;
try {
num = Integer.parseInt(code);
}catch (Exception e){
}
if (num<100000 || num>999999){
System.out.println("输入有误,请重新输入:");
return User();
}
return num;
}
/**
* 重复存储
*/
public void expressExits(){
System.out.println("快递已存在,请勿重复存储:");
}
/**
* 操作成功
*/
public void success(){
System.out.println("操作成功");
}
}
调度逻辑类:
public class MyList {
private ArrayList<Express1> list = new ArrayList<>();//创建集合
private Random random = new Random();//随机数
private static final String FILE_NAME = "express.txt";//定义一个不可改变的文件名称
/**
* 判断文件是否存在
*/
static {
System.out.println("静态代码块执行---------");
File file = new File(FILE_NAME);//创建文件对象
//如果文件不存在,则创建一个文件
if (!file.exists()){
try {
if (file.createNewFile()){
System.out.println("第一次启动项目,创建储存文件成功");
}
} catch (IOException e) {
System.out.println(e.getMessage());
}
}
}
public MyList() {
//初始化数据,从文件中读取数据
System.out.println("构造方法执行-------------");
try {
Object obj = util.readFile(FILE_NAME);//读取文件
//如果文件不为空且是集合类型,则放入集合中
if (obj!=null && obj instanceof ArrayList){
list = (ArrayList<Express1>) obj;
}
} catch (IOException e) {
System.out.println(e.getMessage());
} catch (ClassNotFoundException e) {
System.out.println(e.getMessage());
}
}
/**
* 快递录入
* @param e
* @return
* @throws IOException
*/
public boolean add(Express1 e) throws IOException {
//设置集合长度为100
if (list.size() == 100){
return false;
}
//判断集合中该位置是否有快递
int x = -1;
int y = -1;
while (true){
x = random.nextInt(10);
y = random.nextInt(10);
if (!(list.contains(x) && list.contains(y))){
break;
}
}
int code = randomCode();//生成取件码
e.setCode(code);//设置取件码为新生成的值
list.add(e);//添加到集合中
util.writeFile(list,FILE_NAME);//将集合写入文件当中
return true;
}
/**
* 生成随机取件吗
* @return
*/
public int randomCode(){
while (true){
int code = random.nextInt(900000)+100000;//随机生成六位数取件码
Express1 e = findByCode(code);//判断取件码是否已经存在
//如果为空,则不存在,返回给快递录入阶段
if (e == null){
return code;
}
}
}
/**
* 判断取件码是否重复
* @param code
* @return
*/
public Express1 findByCode(int code){
for (int i = 0; i < list.size(); i++) {
//如果集合中有快递,且取件码相同,则重复,否则不重复,返回null
if (list.size()!=0 && list.get(i).getCode() == code){
return list.get(i);
}
}
return null;
}
/**
* 快递删除
* @param e
* @throws IOException
*/
public void delete(Express1 e) throws IOException {
p:for (int i = 0; i < list.size(); i++) {
if (e.equals(list.get(i))){
list.remove(i);//快递删除
util.writeFile(list,FILE_NAME);//将删除后的集合写入文件中
break p;
}
}
}
/**
* 查询快递单号
* @param number
* @return
*/
public Express1 findByNumber(String number){
Express1 e = new Express1();
e.setNumber(number);//设置快递单号为新输入的值
for (int i = 0; i < list.size(); i++) {
if (e.getNumber().equals(list.get(i).getNumber())){
return list.get(i);
}
}
return null;
}
/**
* 修改快递-删旧添新
* @param oldExpress
* @param newExpress
* @throws IOException
*/
public void update(Express1 oldExpress,Express1 newExpress) throws IOException {
delete(oldExpress);
add(newExpress);
}
/**
* 查看所有快递信息
*/
public void findAll(){
for (int i = 0; i < list.size(); i++) {
System.out.println(list.get(i).toString());//将集合中的信息循环按照字符串类型打印
}
}
}
IO序列化与反序列化:
public class util {
/**
* 反序列化,从指定文件中读取数据
* @param fileName
* @return
* @throws IOException
* @throws ClassNotFoundException
*/
public static Object readFile(String fileName) throws IOException, ClassNotFoundException {
FileInputStream fileInputStream = new FileInputStream(fileName);//字节流
ObjectInputStream objectInputStream= new ObjectInputStream(fileInputStream);//转换为字符流
return objectInputStream.readObject();
}
/**
* 序列化,写入数据到指定文件
* @param obj
* @param fileName
* @throws IOException
*/
public static void writeFile(Object obj,String fileName) throws IOException {
FileOutputStream fileOutputStream = new FileOutputStream(fileName);//字节流
ObjectOutputStream objectOutputStream = new ObjectOutputStream(fileOutputStream);//转换为字符流
objectOutputStream.writeObject(obj);
}
}
调用实现类:
public class main {
private static View1 v = new View1();//初始化视图对象
private static MyList list = new MyList();//初始化dao对象
public static void main(String[] args) throws IOException {
/**
* 欢迎界面
*/
v.welcome();
/**
* 主菜单
*/
p:while (true){
int menu = v.menu();//输入功能序号
switch (menu){
case 1:
cClient();
break;
case 2:
uClient();
break;
case 0:
break p;
}
}
/**
* 退出界面
*/
v.bye();
}
/**
* 快递员菜单
* @throws IOException
*/
public static void cClient() throws IOException {
p:while (true){
int menu = v.Courier();//输入功能序号
switch (menu){
case 1:
Insert();
break;
case 2:
delete();
break;
case 3:
upadte();
break;
case 4:
All();
break;
case 0:
break p;
}
}
}
/**
* 快递录入
* @throws IOException
*/
public static void Insert() throws IOException {
Express1 e = v.Insert();//输入快递信息
list.add(e);//添加到集合中
}
/**
* 快递删除
* @throws IOException
*/
public static void delete() throws IOException {
String number = v.findByNumber();//输入要删除的快递单号
Express1 e = list.findByNumber(number);//判断此快递单号是否存在
//如果为空,不存在,反之删除
if (e == null){
v.printNull();
}else {
v.printExpress(e);
int type= v.delete();//输入功能序号
if (type == 1){
list.delete(e);
v.success();
}
}
}
/**
* 修改快递
* @throws IOException
*/
public static void upadte() throws IOException {
String number = v.findByNumber();//输入要修改的快递单号
Express1 e = list.findByNumber(number);//判断此快递单号是否存在
Express1 e2 = e;//新
if (e == null){
v.printNull();
}else {
v.printExpress(e);
v.update(e2);//输入新的快递信息
list.update(e,e2);//删旧添新
v.printExpress(e2);
}
}
/**
* 查看所有快递信息
*/
public static void All(){
list.findAll();//打印所有快递信息
}
/**
* 用户操作
* @throws IOException
*/
public static void uClient() throws IOException {
int code = v.User();//输入取件码
Express1 e = list.findByCode(code);//判断此取件码是否存在
if (e == null){
v.printNull();
}else {
v.success();//取出成功
list.delete(e);//删除快递信息
v.printExpress(e);
}
}
}
注:仅供参考,可以根据代码自己修改。