Express 类(实体类)
用于表示快递的相关信息,是对快递对象的抽象。 存储内容:包含快递单号(id)、快递公司(company)和取件码(code)
package com.nj.bean;
// 实体类
public class Express {
// 快递单号
private String id;
// 快递公司
private String company;
// 取件码
private int code;
public Express() {
}
public Express(String id, String company, int code) {
this.id = id;
this.company = company;
this.code = 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;
}
//重写toString方法,用于打印对象的字符串表示
@Override
public String toString() {
return "Express{" +
"id='" + id + '\'' +
", company='" + company + '\'' +
", code=" + code +
'}';
}
}
Dao类
数据访问对象类,负责处理与数据存储和操作相关的逻辑。 存储内容:包含一个二维数组 expresses用于模拟存储快递信息,以及一些与快递数量和操作相关的变量,如 size表示已存储的快递数量。
package com.nj.dao;
import com.nj.bean.Express;
import java.util.Random;
public class Dao {
//二维数组存放快递id和company
private Express[][] expresses = new Express[10][10];
private int x;
private int y;
private int size=0;
//根据快递单号查找快递,如果查不到该快递,则返回null
public Express findByNumber(String id){
Express express = new Express();
express.setId(id);
for (int i = 0; i < 10; i++) {
for (int j = 0; j < 10; j++) {
if(expresses.equals(expresses[i][j])){
return expresses[i][j];
}
}
}return null;
}
//检验插入数据是否成功,成功则返回true否则返回false
public boolean insert(Express express) {
if(size<100){
while(true){
x=new Random().nextInt(10);
y=new Random().nextInt(10);
if(expresses[x][y] == null){
expresses[x][y] = express;
size++;//存储快递的数量加1
return true;
}
}
}return false;
}
//根据快递单号删除快递
public boolean delete(String id) {
for (int i = 0; i < 10; i++) {
for (int j = 0; j < 10; j++) {
if (expresses[i][j]!= null && expresses[i][j].getId().equals(id)) {
expresses[i][j] = null;
size--;
return true;
}
}
}
return false;
}
//修改快递单号
public void updateId(String oldId, String newId) {
for (int i = 0; i < 10; i++) {
for (int j = 0; j < 10; j++) {
if (expresses[i][j]!= null && expresses[i][j].getId().equals(oldId)) {
expresses[i][j].setId(newId);
}
}
}
}
//修改快递公司
public void updateCompany(String oldId,String newCompany) {
for (int i = 0; i < 10; i++) {
for (int j = 0; j < 10; j++) {
if (expresses[i][j]!= null && expresses[i][j].getId().equals(oldId)) {
expresses[i][j].setCompany(newCompany);
}
}
}
}
//根据id检查是否存在该快递,存在返回true,不存在返回false
public boolean update(String id) {
for (int i = 0; i < 10; i++) {
for (int j = 0; j < 10; j++) {
if (expresses[i][j]!= null && expresses[i][j].getId().equals(id)) {
return true;
}
}
}
return false;
}
//根据取件码删除快递
public boolean quJianyanVerify(int code){
for (int i = 0; i < 10; i++) {
for (int j = 0; j < 10; j++) {
if (expresses[i][j]!= null && expresses[i][j].getCode()==code) {
expresses[i][j] = null;
size--;
return true;
}
}
}return false;
}
public Express[][] getALL() {
return expresses;
}
//判断是否有存储的快递,如果有返回true
public boolean size() {
if(size == 0){
return false;
}return true;
}
}
View类
负责与用户进行交互,获取用户输入并展示相关信息。存储内容:包含一个 Scanner对象 sc 用于读取用户输入,以及一个 List对象 list用于可能的其他存储需求(在当前代码中未明确其具体用途)
package com.nj.view;
import com.nj.bean.Express;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.Scanner;
public class View {
private Scanner sc = new Scanner(System.in);
private List<Integer> list = new ArrayList<>();
public int start() {
System.out.println("欢迎来到主界面,请选择:");
System.out.println("1.快递员\t\t2.用户\t\t3.退出");
return input();
}
private int input() {
int num = sc.nextInt();
return num;
}
//用户录入的快递信息传递给对象
public Express addMethod() {
System.out.println("请输入快递单号:");
String number = sc.next();
System.out.println("请输入快递公司:");
String company = sc.next();
int code = code();
Express express = new Express();
express.setId(number);
express.setCompany(company);
express.setCode(code);
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) == code) {
chongFu++;
}
}
if (chongFu == 0) {
list.add((int) code);
return (int) code;
}
}
}
public int courierFace() {
System.out.println("欢迎来到快递员界面,请选择:");
System.out.println("1.add\t\t2.Delete\t\t3.Update\t\t4.Select\t\t5.Quit");
return input();
}
public void haveAlready() {
System.out.println("此快递已存在!");
}
public void operatorSuccessful() {
System.out.println("Operator Successful!");
}
public void operatorDefeat() {
System.out.println("Operator Defeat!");
}
public void printAll(Express[][] expresses) {
for (int i = 0; i < 10; i++) {
for (int j = 0; j < 10; j++) {
if (expresses[i][j] != null) {
printExpress(expresses[i][j]);
}
}
}
}
private void printExpress(Express express) {
System.out.println("Id:" + express.getId() + "\t\tCompanys:" + express.getCompany() + "\t\tCode:" + express.getCode());
}
public String deleteSentens() {
System.out.println("请输入你要删除的快递单号:");
String id = sc.next();
return id;
}
public void noFind() {
System.out.println("没有找到此快递");
}
public String update() {
System.out.println("请输入你要修改的快递单号:");
String id = sc.next();
return id;
}
public int updateOption() {
System.out.println("请选择你要修改的信息选项:\t\t1.Id\t\t2.CompanyName");
return input();
}
public String updateId() {
System.out.println("请输入新的快递单号:");
return sc.next();
}
public String updateCompany() {
System.out.println("请输入新的快递公司名称");
return sc.next();
}
public void inputRightOption() {
System.out.println("请输入正确的选项!");
}
//根据取件码返回取件码的索引i
public int qiJiqnVerify() {
System.out.println("请输入取件码:");
return sc.nextInt();
}
}
ExpressMain类(业务类/测试类)
作为项目的主要业务逻辑类,协调 Dao类和 View类进行各种操作,如登录、快递员操作等。 - 存储内容:主要是一些静态的Dao对象dao 和 View对象 view 用于在方法中调用相关的操作
package com.nj.service;
import com.jn.Views;
import com.nj.dao.Dao;
import com.nj.view.View;
import com.nj.bean.Express;
import static sun.security.jgss.GSSUtil.login;
//业务类(测试类)
public class ExpressMain {
private static Dao dao;
private static View view;
public static void main(String[] args) {
init();
login();
}
public 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);
}
}
}
private static void courier() {
while(true){
int option = view.courierFace();
if(option==1){
addMethod();
} else if (option==2) {
deleteMethod();
} else if (option==3) {
updateMethod();
} else if (option==4) {
printAll();
} else if (option==5) {
login();
}
}
}
public static void addMethod(){
Express express = view.addMethod();
if(dao.findByNumber(express.getId()) != null){
view.haveAlready();
}else{
if(dao.insert(express)){
view.operatorSuccessful();
}
else{
view.operatorDefeat();
}
}
}
//打印操作
private static void printAll() {
Express[][] expresses = dao.getALL();
if(dao.size()){
view.printAll(expresses);
}
}
//删除操作
private static void deleteMethod() {
if(dao.delete(view.deleteSentens())){
view.operatorSuccessful();
}else {
view.noFind();
}
}
//修改快递信息
private static void updateMethod() {
String id = view.update();//输入快递单号
if(dao.update(id)){//判断是否存在此快递
switch (view.updateOption()){//修改信息选项1.修改单号2.修改公司名称
case 1:
String newId = view.updateId();
dao.updateId(id,newId);
view.operatorSuccessful();
break;
case 2:
String newCompany = view.updateCompany();
dao.updateCompany(id,newCompany);
view.operatorSuccessful();
break;
default:
view.inputRightOption();
}
}
else{
view.noFind();
}
}
private static void user() {
if(dao.quJianyanVerify(view.qiJiqnVerify())){
view.operatorSuccessful();
}else {
view.noFind();
}
}
private static void init() {
dao = new Dao();
view = new View();
}
}