MVC设计模式
model view controller
模型 视图 控制器
三者分离
视图文字 数据处理 调度逻辑分开
程序员出bug的时候只需要修改期中的一种就可以了
package kauidi;
public class Main {
//初始化视图对象
private static View2 v = new View2();
//初始化dao对象
private static dao dao1= new dao();
public static void main(String[] args) {
//1欢迎
v.welcome();
m:while(true) {
//弹出身份选择菜单
int menu = v.menu();
switch(menu) {
case 1:
cClient();
break;
case 2:
uClient();
break;
case 0:
v.bye();
break m;
}
}
}
private static void uClient() {
int code=v.uMenu();
Express e=dao1.findByCode(code);
if(e==null) {
v.printNull();
}else {
v.printExpress(e);
dao1.delete(e);
}
}
private static void cClient() {
while (true) {
int menu = v.cMenu();
switch(menu) {
case 1:{
//提示输入快递
Express e=v.insert();
//此快递是否已经存储过
Express e2=dao1.findByNumber(e.getId());
//村快递
if(e2==null) {
dao1.add(e);
}else{
v.exit();
}
}
break;
case 2:{
//提示用户输入信息
String number =v.findById();
//查找数据
Express e=dao1.findByNumber(number);
if(e==null) {
v.printNull();
}else {
v.printExpress(e);
}
//提示修改
v.update(e);
dao1.update(e, e);
v.printExpress(e);
}
break;
case 3:{
String number=v.findById();
Express e =dao1.findByNumber(number);
if(e== null) {
v.printNull();
}else {
v.printExpress(e);
int type=v.delete();
if(type ==1) {
dao1.delete(e);
v.sucessDelete();
}else {
}
}
}
break;
case 4:{
Express[][]date=dao1.findAll();
v.printAll(date);
}
break;
case 0:
return ;
}
}
}
}
package kauidi;
import java.util.Scanner;
public class View2 {
private Scanner input = new Scanner(System.in);
public void welcome() {
System.out.println("欢迎使用快递管理系统");
}
public void bye() {
System.out.println("欢迎下次再来");
}//身份菜单
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);
}catch(NumberFormatException e) {
}
if(num<0||num>2) {
System.out.println("输入有误");
return menu();
}
return num;
}
//快递员菜单
public int cMenu (){
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(NumberFormatException e) {
}
if(num<0||num>4) {
System.out.println("输入有误");
return cMenu();
}
return num;
}
//快递员录入返回包含了快递单号和快递公司的快递对象
public Express insert() {
System.out.println("请根据提示输入快递信息");
System.out.println("请输入快递单号");
String id = input.nextLine();
System.out.println("请输入快递公司");
String company = input.nextLine();
Express e=new Express();
e.setId(id);
e.setCompany(company);
return e;
}
//提示用户输入
public String findById() {
System.out.println("请根据提示输入快递信息");
System.out.println("请输入要操作的快递单号");
String number = input.nextLine();
return number;
}//显示快递信息
public void printExpress(Express e) {
System.out.println("快递视图如下");
System.out.println("快递公司"+e.getCompany()+"快递单号"+e.getId()+"快递取件码:"+e.getCode());
}
//快递员修改
public void update(Express e) {
System.out.println("请输入要修改的快递单号");
String number= input.nextLine();
System.out.println("请输入要修改的快递公司");
String company = input.nextLine();
e.setId(number);
e.setCompany(company);
}
//快递员删除
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(NumberFormatException e) {
}
if(num<1||num>2) {
System.out.println("输入有误");
return delete();
}
return num;
}
//查看所有快递
public void printAll(Express[][]es) {
int count =0;
for(int i=0;i<10;i++) {
for(int j=0;j<10;j++) {
if(es[i][j]!=null) {
count++;
System.out.print("第"+(i+1)+"排 "+(j+1)+"列");
printExpress(es[i][j]);
}
}
}
if(count ==0) {
System.out.println("暂无快递信息");
}
}
//用户菜单
public int uMenu (){
System.out.println("请根据提示进行取件");
System.out.println("请输入你的取件码");
String code =input.nextLine();
int num = -1;
try {
num=Integer.parseInt(code);
}catch(NumberFormatException e) {
}
if(num<100000||num>999999) {
System.out.println("输入有误");
return uMenu();
}
return num;
}
public void exit() {
System.out.println("此单号已存在,请勿重复存储");
}
public void printNull() {
System.out.println("快递不存在请检查您的输入");
}
public void printCode(Express e) {
System.out.println("取件码为"+e.getCode());
}
public void sucessDelete() {
System.out.println("删除成功");
}
}
package kauidi;
import java.util.Objects;
public class Express {
private String id;//快递单号
private String company;//公司
private int code;//验证码
public Express(String id,String company ,int code) {
this.id=id;
this.company=company;
this.code=code;
}
public Express(){
}
public String toString() {
return"Express{"+
"id="+id+'\''+
",company='"+company+'\''+
",code="+code+
"}";
}
public boolean equals(Object o) {
if (this == o)return true;
if(o==null||getClass()!=o.getClass())return false;
Express express = (Express)o;
return Objects.equals(id, express.id);
}
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;
}
}
package kauidi;
import java.util.Random;
public class dao {
//数据处理
private Express[] []date = new Express[100][];
//当前存储的快递数量
private int size;
{
for(int i=0;i<10;i++) {
date[i]= new Express[10];
}
}
private Random random = new Random();
public boolean add(Express e) {
if(size == 100) {
//说明存满了
return false;
}
//随机生成2个0-9的下标
int x = -1;
int y = -1;
while(true) {
x=random.nextInt(10);
y=random.nextInt(10);
if(date[x][y]==null) {
//说明此位置无快递
break;
}
}
//取件码
int code = randomCode();
e.setCode(code);
date[x][y] = e;
return true;
}
//随机数的生成
private int randomCode() {
while(true) {
int code = random.nextInt(900000)+100000;
Express e = findByCode(code);
if(e==null) {
//查询失败
return code;
}
}
}
public Express findByNumber(String number ) {
Express e = new Express();
e.setId(number);
for(int i=0;i<10;i++) {
for(int j=0;j<10;j++) {
if(e.equals(date[i][j])) {
return date[i][j];
}
}
}
return null;
}
public Express findByCode(int code) {
for(int i=0;i<10;i++) {
for(int j=0;j<10;j++) {
if(date[i][j]!=null)
if(date[i][j].getCode() == code) {
return date[i][j];
}
}
}
return null;
}
public Express[][] findAll() {
return date;
}
}
代码还有很多不完善的地方。。。请多见谅