Express类
public class Express {
public Express(){
}
public Express(String id, String company, int verifyNumber) {
this.id = id;
this.company = company;
this.verifyNumber = verifyNumber;
}
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 getVerifyNumber() {
return verifyNumber;
}
public void setVerifyNumber(int verifyNumber) {
this.verifyNumber = verifyNumber;
}
@Override
public String toString() {
return "Express{" +
"id='" + id + '\'' +
", company='" + company + '\'' +
", verifyNumber=" + verifyNumber +
'}';
}
private String id;
private String company;
private int verifyNumber;
}
Dao类
import com.jn.listexpress.bean.Express;
import com.jn.listexpress.view.View;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Random;
public class Dao {
ArrayList<Express> expresslist = new ArrayList<>();
public Express findBynumber(String id) {
for (Express express : expresslist) {
if (express.getId().equals(id)) {
return express;
}
}
return null;
}
public boolean insert(Express express) {
if(expresslist.size()<100){
expresslist.add(express);
return true;
}
return false;
}
public List<Express> getAll() {
return expresslist;
}
public boolean size() {
return !expresslist.isEmpty();
}
//根据快递单号查找快递,存在快递返回true
public boolean idVerify(String id) {
for (Express express : expresslist) {
if(express.getId().equals(id)){
return true;
}
}return false;
}
public void deleteVerify(String id) {
Iterator<Express> iterator = expresslist.iterator();
while (iterator.hasNext()) {
Express express = iterator.next();
if (express.getId().equals(id)) {
iterator.remove();
}
}
}
public void updateCompany(String id,String company) {
for (Express express : expresslist) {
if (express.getId().equals(id)) {
express.setCompany(company);
}
}
}
public void updateId(String oldid,String newid) {
for (Express express : expresslist) {
if (express.getId().equals(oldid)){
express.setId(newid);
}
}
}
public boolean outVerify(int code) {
for (Express express : expresslist) {
if(express.getVerifyNumber()==code){
expresslist.remove(express);
return true;
}
}return false;
}
}
View类
import java.util.*;
import static jdk.nashorn.tools.ShellFunctions.input;
public class View {
Scanner sc = new Scanner(System.in);
//存放验证码的列表
ArrayList<Integer> list = new ArrayList<>();
public int start() {
System.out.println("请选择:");
System.out.println("1.快递员\t\t2.用户\t\t3.退出");
return input();
}
private int input() {
return sc.nextInt();
}
public void errorInput() {
System.out.println("你输入的选项有误,请重新输入:");
}
public int expressFace() {
System.out.println("欢迎来到快递员界面,请选择:");
System.out.println("1.添加快递\t\t2.修改快递\t\t3.删除快递\t\t4.查看快递信息\t\t5.退出");
return input();
}
public Express insert() {
System.out.println("请输入快递单号:");
String id = sc.next();
System.out.println("请输入快递公司:");
String companyName = sc.next();
int verifyNumber =code();
Express express = new Express();
express.setId(id);
express.setCompany(companyName);
express.setVerifyNumber(verifyNumber);
return express;
}
//去除重复代码
private int code() {
while (true) {
int chongFu = 0;
long code = new Random().nextInt(900000) + 100000;
//检验生成的验证码是否存在于列表中
for (int i = 0; i < list.size(); i++) {
if (list.get(i).equals(code)) {
chongFu++;
}
}
if (chongFu == 0) {
return (int) code;
}
}
}
public void printAll(List<Express> expresses) {
for (Express express : expresses) {
printExpress(express);
}
}
private void printExpress(Express express) {
System.out.println("Id:" + express.getId() + "\t\tCompanys:" + express.getCompany() + "\t\tCode:" + express.getVerifyNumber());
}
public void haveAlready() {
System.out.println("此快递已存在");
}
public void operaterSuccessful() {
System.out.println("OperaterSuccessful!");
}
public void operaterDelete() {
System.out.println("OperaterDefeat");
}
public String delete() {
System.out.println("请输入你要删除的快递单号:");
return sc.next();
}
public void noFind() {
System.out.println("没有发现此快递,请重新输入快递单号");
}
public String update() {
System.out.println("请输入你要修改的快递单号:");
return sc.next();
}
public int updateFace() {
System.out.println("请选择你要修改的内容:");
System.out.println("1.快递单号\t\t2.快递公司");
return input();
}
public String updateId() {
System.out.println("请输入新的快递单号:");
return sc.next();
}
public String updateCompany() {
System.out.println("请输入新的快递公司:");
return sc.next();
}
public int outMethod() {
System.out.println("请输入取件码:");
return sc.nextInt();
}
public void outSuccessful() {
System.out.println("取件成功");
}
}
ExpressMain类
import java.util.List;
import static sun.security.jgss.GSSUtil.login;
public class ExpressMain {
private static View view;
private static Dao dao;
public static void main(String[] args) {
init();
login();
}
private static void login() {
while(true){
int option = view.start();
if (option == 1){
courier();
} else if (option == 2) {
user();
} else if (option == 3) {
System.exit(0);
}else {
view.errorInput();
}
}
}
private static void user() {
if(dao.outVerify(view.outMethod())){
view.outSuccessful();
}
else {
view.noFind();
}
}
private static void courier() {
while (true){
switch (view.expressFace()){
case 1:
addMethod();
break;
case 2:
updateMethod();
break;
case 3:
deleteMethod();
break;
case 4:
printAll();
case 5:
login();
}
}
}
private static void updateMethod() {
String id = view.update();
if(dao.idVerify(id)){
switch (view.updateFace()){
case 1:
dao.updateId(id,view.updateId());
view.operaterSuccessful();
break;
case 2:
dao.updateCompany(id,view.updateCompany());
view.operaterSuccessful();
break;
default:
view.errorInput();
break;
}
}else {
view.noFind();
}
}
private static void deleteMethod() {
String id = view.delete();
if(dao.idVerify(id)){
dao.deleteVerify(id);
view.operaterSuccessful();
}else {
view.noFind();
}
}
private static void printAll() {
List<Express> expresses = dao.getAll();
if(dao.size()){
view.printAll(expresses);
}
courier();
}
private static void addMethod() {
Express express = view.insert();
//先判断,再插入数据
if(dao.findBynumber(express.getId())!=null){
view.haveAlready();
}else{
if(dao.insert(express))
view.operaterSuccessful();
else {
view.operaterDelete();
}
}
}
private static void init(){
dao = new Dao();
view = new View();
}
}