快递管理训练任务
作者:互联网
Dao类:
package dao;
import bean.Express;
import utils.FileUtils;
import java.io.*;
import java.util.ArrayList;
import java.util.List;
public class Dao {
FileUtils file;
//创建集合对象
private List<Express> list = new ArrayList<>();
public void setList(List<Express> list) {
this.list = list;
}
//数据录入
public boolean insert(Express e) throws IOException {
//判断
try(ObjectInputStream ois = new ObjectInputStream(new FileInputStream("D:\\IdeaProjects\\basic-code\\day21-code-Express\\b.txt"))){
list =(ArrayList<Express>) ois.readObject();
for (int i = 0; i < list.size(); i++) {
if (list.get(i).getId() == e.getId() ){
System.out.println("快递已存在请重新输入");
}else {
list.add(e);
file.ObjectOutputMethod((ArrayList<Express>) list);
return true;
}
}
ois.close();
} catch (IOException | ClassNotFoundException e1) {
//添加到集合中
list.add(e);
ObjectOutputStream bw = new ObjectOutputStream(new FileOutputStream("D:\\IdeaProjects\\basic-code\\day21-code-Express\\a.txt"));
bw.writeObject(list);
bw.close();
System.out.println("写入成功");
return true;
}
return false;
}
//删除数据
public boolean delete(Express e) throws IOException, ClassNotFoundException {
ObjectInputStream ois = new ObjectInputStream(new FileInputStream("D:\\IdeaProjects\\basic-code\\day21-code-Express\\a.txt"));
list = (ArrayList<Express>)ois.readObject();
boolean remove = list.remove(e);
ObjectOutputStream bw = new ObjectOutputStream(new FileOutputStream("D:\\IdeaProjects\\basic-code\\day21-code-Express\\a.txt"));
bw.writeObject(list);
bw.close();
ois.close();
return remove;
}
//修改
public boolean update(Express e1,Express e2) throws IOException, ClassNotFoundException {
ObjectInputStream ois = new ObjectInputStream(new FileInputStream("D:\\IdeaProjects\\basic-code\\day21-code-Express\\a.txt"));
//读取文件
list =(ArrayList<Express>) ois.readObject();
//删除旧的
list.remove(e1);
//添加新的
list.add(e2);
ObjectOutputStream bw = new ObjectOutputStream(new FileOutputStream("D:\\IdeaProjects\\basic-code\\day21-code-Express\\a.txt"));
bw.writeObject(list);
bw.close();
ois.close();
System.out.println("修改成功");
return true;
}
//查询所有数据
public List<Express> getList() throws IOException, ClassNotFoundException {
ObjectInputStream ois = new ObjectInputStream(new FileInputStream("D:\\IdeaProjects\\basic-code\\day21-code-Express\\a.txt"));
list = (ArrayList<Express>)ois.readObject();
if (list == null)
return null;
ois.close();
return list;
}
//数据查询,根据快递单号查询
public Express getByNumber(String number){
//增强for循环
for (Express e:list){
if (number.equals(e.getId())){
return e;
}
}
return null;
}
//数据查询,根据取件码查询
public Express getByCode(int code){
//遍历获取集合中所有元素,拿集合中的取件码和输入的取件对比
for (Express e:list){
if (e.getCode() == code){
return e;
}
}
return null;
}
//获取位置
public int getIndex(Express e){
return list.indexOf(e)+1;
}
}
Express类:
package bean;
import java.io.Serializable;
import java.util.Objects;
public class Express implements Serializable {
//快递单号
private String id;
//快递公司
private String company;
//取件码
private int code;
@Override
public String toString() {
return "Express{" +
"id='" + id + '\'' +
", company='" + company + '\'' +
", code=" + code +
'}';
}
@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 &&
Objects.equals(id, express.id) &&
Objects.equals(company, express.company);
}
@Override
public int hashCode() {
return Objects.hash(id, company, code);
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
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 Express(String id, String company, int code) {
this.id = id;
this.company = company;
this.code = code;
}
public Express() {
}
}
View类:
package view;
import bean.Express;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.Scanner;
public class View {
private Scanner input = new Scanner(System.in);
//随机码
private Random random = new Random();
//取件码是否重复的集合
private List<Integer> list = new ArrayList<>();
//欢迎
public void welcom(){
System.out.println("---欢迎使用---");
}
//结束
public void end(){
System.out.println("欢迎再次使用!");
}
//接收用户输入
public int input(){
while (true){
int num = -1;
try {
num = input.nextInt();
return num;
}catch (Exception e){
System.out.println("请输入正确序号");
input.nextLine();
}
}
}
//打印
public void printExpress(Express e){
System.out.println("快递单号:"+e.getId()+" 快递公司:"+e.getCompany()+" 取件码:"+e.getCode());
}
//输入有误
public void error(){
System.out.println("你输入的信息有误,快递不存在,请重新输入");
}
//输入选项有误
public void choiceError(){
System.out.println("你输入的信息有误,快递不存在,请重新输入");
}
//快递为空
public void nullExpress(){
System.out.println("当前快递为空");
}
//随机码生产取件码
public int code(){
while (true){
int isRepeat = 0;
int code = (int) (random.nextInt(900000)+100000);
//拿集合中的取件码和生产的对比是否重复
for (int i = 0;i < list.size();i++){
if (list.get(i) == code){
isRepeat++;
}
}
if (isRepeat==0){
list.add(code);
return code;
}
}
}
//登录
public int login(){
System.out.println("请选择登录用户");
System.out.println("---1.管理员登录-----");
System.out.println("---2.用户登录-------");
System.out.println("---0.退出-----------");
return input();
}
//管理员界面
public int adminLogin(){
System.out.println("---请选择要进行的操作---");
System.out.println("---1.快递录入----------");
System.out.println("---2.删除快递----------");
System.out.println("---3.修改快递----------");
System.out.println("---4.查看所有快递------");
System.out.println("---0.返回上级目录------");
return input();
}
//快递录入
public Express insert(){
System.out.println("请输入快递单号:");
String number = input.next();
System.out.println("请输入快递公司:");
String company = input.next();
//自定义取件码变量,并把随机生产的取件码赋给它
int code = code();
Express e = new Express(number,company,code());
return e;
}
//重复录入
public void repeat(){
System.out.println("快递已存在!");
}
//录入成功
public void success(){
System.out.println("快递录入成功");
}
//录入失败
public void fail(){
System.out.println("快递录入失败");
}
//快递删除,根据快递单删除
public String delete(){
System.out.println("请输入要删除的快递单号");
String delNumber = input.next();
return delNumber;
}
//确认删除
public int deleteConfirm(){
System.out.println("---是否确认删除该快递---");
System.out.println("---1.是---------------");
System.out.println("---2.否---------------");
return input();
}
//删除成功
public void deleteSuccess(){
System.out.println("快递删除成功");
}
//删除失败
public void deleteFail(){
System.out.println("快递删除失败");
}
//修改快递
public String update(){
System.out.println("---请输入需要修改的快递单号---");
String number = input.next();
return number;
}
//新的数据
public Express newUpdate(){
System.out.println("---请输入新的快递单号---");
String number = input.next();
System.out.println("---请输入新的快递公司---");
String company = input.next();
int code = code();
Express e = new Express(number,company,code);
return e;
}
//修改成功
public void updateSuccess(){
System.out.println("修改成功");
}
//修改失败
public void updateFail(){
System.out.println("修改失败");
}
//查询所有快递
public void printAllExpress(List<Express> list){
//遍历集合输出所有元素
for (Express e :list){
System.out.println(e);
}
}
//用户界面
public int userLogin(){
System.out.println("---请选择要进行的操作---");
System.out.println("---1.快递取出----------");
System.out.println("---0.返回上级目录------");
return input();
}
//取快递
public int getExpress(){
System.out.println("请输入取件码:");
return input();
}
//快递柜位置
public void address(int index){
System.out.println("快递在"+index+"号快递柜");
}
//删除取件码
public void deleteCode(int code){
list.remove((Integer) code);
}
//取件提醒
public void printGet(){
System.out.println("请尽快取出快递!");
}
//取件失败
public void getFail(){
System.out.println("取件失败!");
}
//取件成功
public void getSuccess(){
System.out.println("取件成功!");
}
}
Main类:
package main;
import bean.Express;
import dao.Dao;
import view.View;
import java.io.*;
import java.util.ArrayList;
import java.util.List;
public class Main {
private static Dao dao; //增删改查方法
private static View view;//操作界面
public static void main(String[] args) throws IOException, ClassNotFoundException {
init();
login();
}
//初始化
public static void init(){
dao = new Dao();
view = new View();
}
//权限选择
public static void login() throws IOException,ClassNotFoundException {
/*ArrayList<Express> list = new ArrayList<>();
ObjectInputStream ois = new ObjectInputStream(new FileInputStream("D:\\IdeaProjects\\basic-code\\day21-code\\a.txt"));
list = (ArrayList<Express>) ois.readObject();
ois.close();*/
o: while (true){
int num = -1;
num = view.login();
switch (num){
case 0://退出
break o;
case 1://快递员
adminLogin();
break ;
case 2://用户
userLogin();
break ;
default:
view.choiceError();
break ;
}
}
/*ObjectOutputStream fos = new ObjectOutputStream(new FileOutputStream("D:\\IdeaProjects\\basic-code\\day21-code\\a.txt"));
fos.writeObject(list);
fos.close();*/
}
private static void userLogin() throws IOException, ClassNotFoundException {
q:while (true){
int num = -1;
num = view.userLogin();
switch (num){
case 0:
break q;
case 1:
int code = view.getExpress();
Express e = dao.getByCode(code);
if (e!=null){
view.address(dao.getIndex(e));//输出位置
view.printExpress(e);//输出快递信息
view.printGet();//取件提醒
//删除对应快递
if (dao.delete(e)){
//单独 删除取件码
view.deleteCode(e.getCode());
view.getSuccess();
}else {
view.getFail();
}
}else {
view.error();
}
break ;
default:
view.choiceError();
break ;
}
}
}
private static void adminLogin() throws IOException, ClassNotFoundException {
p:while (true){
int num = -1;
num = view.adminLogin();
switch (num){
case 0://结束
break p;
case 1://添加
insert();
break ;
case 2://删除
delete();
break;
case 3://修改
update();
break ;
case 4://打印
printAll();
break;
default:
view.choiceError();
break ;
}
}
}
private static void printAll() throws IOException, ClassNotFoundException {
List<Express> list = dao.getList();
if (list.size()>0){
view.printAllExpress(list);
}else {
view.nullExpress();
}
}
private static void update() throws IOException, ClassNotFoundException {
String update = view.update();
//从快递单号修改
Express e = dao.getByNumber(update);
if (e!=null){
view.printExpress(e);
Express e2 = view.newUpdate();
if (dao.update(e,e2)){
view.updateSuccess();
//同理code单独存放在集合中,需要删除
view.deleteCode(e.getCode());
}else {
view.updateFail();
}
}else {
view.error();
}
}
private static void delete() throws IOException, ClassNotFoundException {
String delete = view.delete();
Express byNumber = dao.getByNumber(delete);
if (byNumber != null){//判断快递是否存在
int confirm = view.deleteConfirm();
switch (confirm){
case 1:
if (dao.delete(byNumber)){
view.deleteSuccess();
//取件码单独存放,把取件码从集合中删除
view.deleteCode(byNumber.getCode());
}else {
view.deleteFail();
}
break;
case 2:
break;
default:
view.choiceError();
break;
}
}else {
view.error();
}
}
private static void insert() throws IOException {
Express e = view.insert();
//判断是否存在
if (dao.getByNumber(e.getId())!=null){
view.repeat();
}else {
//没有快递,开始存
if (dao.insert(e)){
view.success();
view.printExpress(e);
}else {
//添加失败
view.fail();
}
}
}
}
标签:训练任务,code,管理,System,快递,println,view,public,out 来源: https://blog.csdn.net/qq_42295268/article/details/112713737