快递e栈 动态数组版
使用动态数组实现快递的存储。
整体思路
1.主函数
2.View类-完成输入
3.MyList类-完成数据库处理
4.Express-完成单个快递信息对象的构建
1.主函数
package com.java.main;
import com.java.bean.Express;
import com.java.dao.MyList;
import com.java.view.Views;
public class Main {
private static MyList myList = new MyList();
private static Views menu = new Views();
public static void main(String args[]){
menu.welcome();
//菜单1
STOP1: while(true){
int menu1 = menu.menu();
switch(menu1){
case 1:
cClient();
break;
case 2:
uClient();
break;
case 0:
break STOP1;
default:
}
}
menu.bye();
}
/**
* 0.退出,返回上层菜单
* 1.快递录入
* 2.删除快递
* 3.修改快递
* 4.查看所有快递信息
*/
private static void cClient(){
int menuC = menu.cmenu();
switch(menuC){
case 0:
return; //返回上层目录
case 1:
Express e1 = menu.expressInput();
if(myList.addExpress(e1)){
menu.success();
}else{
menu.fail();
}
break;
case 2:
String text2 = menu.findByNumber();
Express e2 = myList.findExpress(text2);
if(e2 == null){
menu.noResult();
}else{
int deleteConfirm = menu.deleteInput();
if(deleteConfirm == 1){
myList.delExpress(e2);
menu.success();
}
}
break;
case 3:
String text3 = menu.findByNumber();
Express e3 = myList.findExpress(text3);
if(e3 == null){
menu.noResult();
}else{
menu.inputNewNode(e3);
menu.success();
}
break;
case 4:
myList.showExpress();
break;
default:
}
}
private static void uClient(){
int menuU = menu.umenu();
Express e = myList.getExpress(menuU);
if(e == null) {
menu.noResult();
}else{
myList.showToUserExpress(e);
myList.delExpress(e);
menu.success();
}
}
}
2.View类-完成输入
package com.java.view;
import com.java.bean.Express;
import java.util.Scanner;
public class Views {
private Scanner input = new Scanner(System.in);
public void welcome(){
System.out.println("欢迎光临快递e栈~~");
}
public void bye(){
System.out.println("欢迎下次光临");
}
public int menu(){
System.out.println("请选择功能,输入对应序号:");
System.out.println("1.快递员");
System.out.println("2.普通用户");
System.out.println("0.退出");
//从全局角度,方便接收各类信息,符号包括空格,并且nextLine()与nextInt()互斥
// 故统一选择nextLine()
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;
}
/**
* 快递员的菜单
* @return
*/
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.退出");
//从全局角度,方便接收各类信息,符号包括空格,并且nextLine()与nextInt()互斥
// 故统一选择nextLine()
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;
}
/**
*
* 普通用户界面
*
* @return
*/
public int umenu(){
System.out.println("欢迎取件,请输入6位取件码:");
//从全局角度,方便接收各类信息,符号包括空格,并且nextLine()与nextInt()互斥
// 故统一选择nextLine()
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;
}
/**
* 快递员子界面
* @return
*/
//快递录入
public Express expressInput(){
Express e = new Express();
System.out.println("请输入快递单号:");
String num = input.nextLine();
e.setNumber(num);
System.out.println("请输入快递公司:");
String company = input.nextLine();
e.setCompany(company);
return e;
}
//删除快递
public int deleteInput(){
System.out.println("是否确定删除?1.是 2.否");
String text = input.nextLine();
int num = -1;
try{
num = Integer.parseInt(text);
}catch(NumberFormatException ee){
}
if(num<1||num>2){
System.out.println("输入有误,请重新输入!");
return deleteInput();
}
return num;
}
//修改和删除快递复用模块,通过单号来查找快递。输入单号
public String findByNumber(){
System.out.println("请输入快递单号:");
String num = input.nextLine();
return num;
}
public void inputNewNode(Express e){
System.out.println("输入新的快递单号:");
String num = input.nextLine();
e.setNumber(num);
System.out.println("请输入新的快递公司:");
String company = input.nextLine();
e.setCompany(company);
}
public void success(){
System.out.println("成功!");
}
public void fail(){
System.out.println("失败,请重试!");
}
public void noResult(){
System.out.println("查无此快递,请重试!");
}
}
3.MyList类-完成数据库处理
package com.java.dao;
import java.util.ArrayList;
import java.util.Random;
import com.java.bean.Express;
/**
* 本类主要有如下功能-从数据层面:
* 1.添加快递 到链表中
* 2.根据取件码取件
* 3.删除快递 从链表中
* 4.遍历所有快递
* 5.修改快递。
*/
public class MyList {
private ArrayList<Express> list = new ArrayList<Express>();
private boolean[] tag = new boolean[25]; //判断对应柜子里面有没有快递,boolean默认为false
/**
添加快递
*/
public boolean addExpress(Express e){
if(list.size() == 25){
System.out.println("快递柜已满,无法存入快递。");
return false;
}
for(int i = 0;i < list.size();i++){
if(e.getNumber().equals(list.get(i).getNumber() )){
System.out.println("已存在该快递,请重试。");
return false;
}
}
int code = randomCode();
e.setCode(code);
System.out.println("该快递取件码:"+code);
int boxNum = randomBoxNum();
e.setBoxNum(boxNum);
System.out.println("该快递柜子号:"+boxNum);
list.add(e);
return true;
}
//生成取件码
public int randomCode(){
Random r = new Random();
while(true){
int code = r.nextInt(900000)+100000;
Express e = getExpress(code);
if(e == null){
return code;
}
}
}
//生成随机的柜子,柜子范围为0-24,若柜子原本为false,则可用。故占用并返回柜子号。
public int randomBoxNum(){
Random r = new Random();
while(true){
int boxNum = r.nextInt(25);
if(!tag[boxNum]){
tag[boxNum] = true;
return boxNum;
}
}
}
//根据取件码查找快递,取件和添加、修改快递节点的复用模块。
public Express getExpress(int code){
for(int i = 0;i < list.size();i++){
if(list.get(i).getCode() == code){
return list.get(i);
}
}
return null;
}
//根据单号查找快递,取件和删除快递节点的复用模块。
public Express findExpress(String num){
for(int i = 0;i < list.size();i++){
if(num.equals(list.get(i).getNumber())){
return list.get(i);
}
}
return null;
}
//删除快递节点 释放柜子
public void delExpress(Express e){
STOP:for(int i = 0;i < list.size();i++) {
if(e.equals(list.get(i))){
System.out.println(e.toString());
tag[e.getBoxNum()] = false; //释放柜子
list.remove(i);
break STOP;
}
}
}
/*//更新快递节点
public void updateExpress(Express oldNode, Express newNode){
delExpress(oldNode);
addExpress(newNode);
}
*/
//显示所有快递
public void showExpress(){
for(int i = 0;i < list.size();i++){
System.out.println(list.get(i).toString());
}
}
//成功
public void showToUserExpress(Express e){
System.out.println("欢迎取出,快递柜为" + e.getBoxNum());
}
}
4.Express-完成单个快递信息对象的构建
package com.java.bean;
import java.util.Objects;
public class Express {
private String number ; //快递单号
private String company; //快递公司
private int code; //取件号
private int boxNum; //柜子号
public Express(){}
public Express(String number, String company, int code, int boxNum) {
this.number = number;
this.company = company;
this.code = code;
this.boxNum = boxNum;
}
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 getBoxNum() {
return boxNum;
}
public void setBoxNum(int boxNum) {
this.boxNum = boxNum;
}
@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; //只要取件码相同,就是同一个快递
}
@Override
public int hashCode() {
return Objects.hash(code);
}
@Override
public String toString() {
return "number='" + number + '\t' +
", company='" + company + '\t' +
", code=" + code +'\t'+
", boxNum=" + boxNum ;
}
}