5.集合+mvc快递增删改查
bean包中快递实体类
package bean;
public class Express {
private String number;
private String company;
private int code;
private int target;//位置的序号
public Express(String number, String company) {
this.number = number;
this.company = company;
}
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 getTarget() {
return target;
}
public void setTarget(int target) {
this.target = target;
}
@Override
public String toString() {
return
"快递单号:" + number +"\t公司名称:" + company +"\t取件码:" + code +
"\t快递位置:" + target / 10 + "排" + target % 10 + "列";
}
}
view包中视图类views
package view;
import bean.Express;
import java.util.ArrayList;
import java.util.Scanner;
public class Views {
private Scanner sc = new Scanner(System.in);
public void welcome() { print("欢迎使用快递驿站!"); }
private void print(String msg) { System.out.println(msg); }
private String inputStr(String msg) {
System.out.println(msg);
return sc.nextLine();
}
public void printMainMenu() { print("请选择用户:1.快递员,2.用户 3.退出程序!"); }
/**
* 输入选项
* @param max
* @return
*/
public int inputChoose(int max) {
String str = inputStr("请输入一个大于0而且小于" + max + "的数字:");
int num = -1;
try {
num = Integer.parseInt(str);
} catch (NumberFormatException e) {
}
if (num < 0 || num > max) {
num = inputChoose(max);
}
return num;
}
public void bye() { print("Bye bye!"); }
public void printAdminMenu() {
print("1.增加快递,2.删除快递,3.修改快递,4.查看所有,5.返回主菜单!");
}
public void back() { print("返回主菜单!");}
public void printUserMenu() { print("1.取件,2.返回主菜单!"); }
public String inputCompany() {return inputStr("请输入公司名称"); }
public String inputNumber() { return inputStr("请输入快递单号:"); }
public void printAddResult(Express add) {
print(add == null ? "快递柜已满或单号重复,无法添加!" : "添加成功,位置:" +
showTarget(add) + ",取件码:" + add.getCode());
}
/**
* 用字符串的形式显示快递的位置
*
* @param add
* @return
*/
private String showTarget(Express add) {
return add.getTarget() / 10 + "排" + add.getTarget() % 10 + "列";
}
/**
* 取件码
*
* @return
*/
public int inputCode() { return inputChoose(1000000); }
public void printPickResult(boolean pick) { print(pick ? "取件成功!" : "取件失败!");}
public void printRemoveResult(boolean remove) {
print(remove ? "删除成功!" : "删除失败!");
}
public String inputOldNumber() { return inputNumber(); }
public String inputNewNumber() { return inputStr("请输入新的快递单号:"); }
public String inputNewCompany() {return inputStr("请输入新的公司名称:"); }
public void printModifyResult(Express modify) {
print(modify==null?"单号不存在,请检查" : "修改成功,新的快递信息为:" + modify);
}
public void showAll(ArrayList<Express> list) {
if (list.size() == 0) {
print("快递柜为空!无任何快递信息!");
} else {
for (Express express : list) {
System.out.println(express);
}
}
}
}
dao包中操作数据的类ExpressDao
package dao;
import bean.Express;
import java.util.ArrayList;
public class ExpressDao {
private static final int MAX_SIZE = 100;
private ArrayList<Express> list = new ArrayList<>();
//方法:alt+回车,
public Express addExpress(String number, String company) {
if (list.size() > MAX_SIZE) return null;//满了就不放了
//单号的查重,一个是取件码的查重
Express exist = findExpressByNumber(number);
if (exist == null) {
//未存在相同的单号:可以添加
Express newExpress = new Express(number, company);
//位置
int target = initTarget();
newExpress.setTarget(target);
//取件码:
int code = initCode();
newExpress.setCode(code);
list.add(newExpress);
return newExpress;
}
return null;
}
private int initCode() {
int code = 0;
do {
code = (int) (Math.random() * 900000 + 100000);
} while (findExpressByCode(code) != null);
return code;
}
/**
* 通过单号找快递
*
* @param code
* @return
*/
private Express findExpressByCode(int code) {
for (Express express : list) {
if (express.getCode() == code) {
return express;
}
}
return null;
}
//boolean,是否;查找位置同时知道存不存在,返回下标,默认-1可以知道不存在; 返回对象,可以拿到所有信息,null代表不存在
private int initTarget() {
int target = 0;
do {
target = (int) (Math.random() * MAX_SIZE);
} while (findExpressByTarget(target));
return target;
}
/**
* 按位置找快递
*
* @param target
* @return
*/
private boolean findExpressByTarget(int target) {
for (Express express : list) {
if (express.getTarget() == target) {
return true;
}
}
return false;
}
private Express findExpressByNumber(String number) {
for (Express express : list) {
if (express.getNumber().equals(number)) {
return express;
}
}
return null;
}
public boolean pickExpress(int code) {
Express express = findExpressByCode(code);
return list.remove(express);
}
public boolean removeExpress(String number) {
Express express = findExpressByNumber(number);
return list.remove(express);
}
public Express modify(String oldNumber, String newNumber, String newCompany) {
Express old = findExpressByNumber(oldNumber);
if (old == null) return null;
Express newExpress = new Express(newNumber, newCompany);
//想改 重新init,不想改你就old的信息拿过来
newExpress.setCode(old.getCode());
newExpress.setTarget(old.getTarget());
int index = list.indexOf(old);//集合中对象的索引
list.set(index, newExpress);//将指定位置的对象替换为新对象
return newExpress;
}
public ArrayList<Express> query() {
return list;
}
}
Main方法负责流程控制
import bean.Express;
import dao.ExpressDao;
import view.Views;
import java.util.ArrayList;
//只负责流程操作
public class Main {
//视图对象
//数据操作类的对象
static Views v = new Views();
static ExpressDao dao = new ExpressDao();
public static void main(String[] args) {
v.welcome();
boolean flag = true;
while (flag) {
v.printMainMenu();
int key = v.inputChoose(3);
switch (key) {
case 1:
adminLoop();
break;
case 2:
userLoop();
break;
case 3:
flag = false;
v.bye();
break;
}
}
}
private static void adminLoop() {
boolean flag = true;
while (flag) {
v.printAdminMenu();
int key = v.inputChoose(5);
switch (key) {
case 1:
add();
break;
case 2:
remove();
break;
case 3:
modify();
break;
case 4:
query();
break;
case 5:
flag = false;
v.back();
break;
}
}
}
private static void query() {
ArrayList<Express> list=dao.query();
v.showAll(list);
}
private static void modify() {
String oldNumber = v.inputOldNumber();
String newNumber = v.inputNewNumber();
String newCompany = v.inputNewCompany();
Express modify = dao.modify(oldNumber, newNumber, newCompany);
v.printModifyResult(modify);
}
private static void remove() {
String number = v.inputNumber();
boolean remove = dao.removeExpress(number);
v.printRemoveResult(remove);
}
private static void add() {
String number = v.inputNumber();
String company = v.inputCompany();
Express add = dao.addExpress(number, company);
v.printAddResult(add);
}
private static void pick() {
int code = v.inputCode();
boolean pick = dao.pickExpress(code);
v.printPickResult(pick);
}
private static void userLoop() {
boolean flag = true;
while (flag) {
v.printUserMenu();
int key = v.inputChoose(2);
switch (key) {
case 1:
pick();
break;
case 2:
flag = false;
v.back();
break;
}
}
}
}