之前练习写一个快递e栈,然后用集合来存储数据(快递信息)ArrayList。
1、界面模块(View1 )
public class View1 { //快递菜单
private Scanner sc = new Scanner(System.in);
private ExpressOp expressOp = new ExpressOp();
//首页
//欢迎
public void welcome(){
System.out.println("*******欢迎使用快递系统*******");
}
//结束
public void bye(){
System.out.println("期待下次使用,再见");
}
// 选择用户界面
public int menu(){
System.out.println("********请选择用户类型*******:");
System.out.println("1:快递员 2:用户 0:退出");
System.out.println("******************************");
String text = sc.nextLine();
int num = -1;
try{
num = Integer.parseInt(text);
}catch(NumberFormatException e){
}
if(num < 0 || num > 2){
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:返回上层");
System.out.println("********************");
String text = sc.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 int uMenu(){
System.out.println("请根据提示完成操作:");
System.out.println("请输入取件码:");
String code = sc.nextLine();
int num = -1;
try{
num = Integer.parseInt(code);
}catch(NumberFormatException e){
}
if(num < 100000 || num > 999999){
System.out.println("输入有误");
return uMenu();
}
return num;
}
}
2、快递信息模块(Express)
public class Express {
//快递单号
private String number;
//快递公司
private String company;
//取件码
private int code;
public Express(int code) {
this.code = code;
}
public Express() {//无参构造参数
}
public Express(String number, String company, int code) {//有参构造参数
this.number = number;
this.company = company;
this.code = code;
}
public String getCompany() {
return company;
}
public void setCompany(String company) {
this.company = company;
}
public String getNumber() {
return number;
}
public void setNumber(String number) {
this.number = number;
}
public int getCode() {
return code;
}
public void setCode(int code) {
this.code = code;
}
@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 &&
Objects.equals(number, express.number) &&
Objects.equals(company, express.company);
}
@Override
public int hashCode() {
return Objects.hash(number, company, code);
}
@Override
public String toString() {
return "com.java.Express{" +
"number='" + number + '\'' +
", company='" + company + '\'' +
", code=" + code +
'}';
}
}
3、功能模块界面()
import com.java.Express.bean.Express;
import java.util.ArrayList;
import java.util.Random;
import java.util.Scanner;
public class ExpressOp {
private Scanner sc = new Scanner(System.in);
//存快递
public Express create(){
System.out.println("请输入快递单号:");
String number = sc.nextLine();
System.out.println("请输入快递公司:");
String company = sc.nextLine();
Express e = new Express();
e.setNumber(number);
e.setCompany(company);
e.setCode(randomCode());
return e;
}
//修改 存新快递
public Express newCreat(){
System.out.println("请输入新的快递单号");
String number = sc.nextLine();
System.out.println("请输入新的快递公司");
String company = sc.nextLine();
Express e = new Express();
e.setNumber(number);
e.setCompany(company);
e.setCode(randomCode());
return e;
}
public ArrayList<Express> insertOp(ArrayList<Express> expressArrayList, Express express){
int result = findByNumber(expressArrayList, express.getNumber());
if (result == -1){
expressArrayList.add(express);
}else {
System.out.println("单号已存在,请重新输入");
}
return expressArrayList;
}
//判断是否存在并打印快递信息
public void printExpressInfo(Express express){
if (express == null){
System.out.println("快递不存在,请检查输入是否正确!");
}else{
System.out.println("快递单号:" + express.getNumber());
System.out.println("快递公司:" + express.getCompany());
System.out.println(" 取件码: " + express.getCode());
}
}
/**
*判断快递单号
* */
public int findByNumber(ArrayList<Express> expressList, String number){
for (int i = 0; i < expressList.size(); i++){
if (expressList.get(i) != null){
if (expressList.get(i).getNumber() == number || expressList.get(i).getNumber().equals(number)){
return i;
}
}
}
return -1;
}
/**
*判断取件码
* */
public int findByCode(ArrayList<Express> expressList,int code){
for (int i = 0;i < expressList.size();i++){
if(expressList.get(i) != null){
if(expressList.get(i).getCode() == code || expressList.get(i).equals(code)){
return i;
}
}
}
return -1;
}
//输入快递单号
public String inputNumber(){
System.out.println("请根据提示完成操作:");
System.out.println("请输入快递单号:");
String number = sc.nextLine();
return number;
}
private int randomCode(){
//0-899999+100000
Random random = new Random();
while(true){
int code = random.nextInt(900000)+100000;
return code;
}
}
}
4、逻辑实现界面(main)
import com.java.Express.bean.Express;
import java.util.ArrayList;
public class Main {
//初始化视图对象
private View1 view = new View1();
private ExpressOp op = new ExpressOp();
private ArrayList<Express> expressList = new ArrayList<>();
public void op(){
//欢迎
view.welcome();
//身份菜单
m:while(true){
int menu = view.menu();
switch (menu){
case 1:
cClient();
break;
case 2:
uClient();
break;
case 0:
break m;
}
}
view.bye();
}
private void uClient() {
//获取取件码
int code = view.uMenu();
//取快递
int result = op.findByCode(expressList,code);
if(result == -1){
System.out.println("取件码错误!");;
}else{
System.out.println("取件成功");;
}
}
private void cClient() {
int menu = view.cMenu();
switch (menu){
case 0:
return;
/**
* 存快递
*/
case 1: {
//生成快递信息
Express e = op.create();
//添加快递
expressList = op.insertOp(expressList, e);
//打印快递信息
op.printExpressInfo(e);
}
break;
/**
* 删快递
*/
case 2:{
//输入快递单号
String number = op.inputNumber();
//查找
int result = op.findByNumber(expressList, number);
if (result == -1){
System.out.println("快递不存在,请检查输入是否正确!");
}else{
System.out.println("所要删除的快递信息");
op.printExpressInfo(expressList.get(result));
}
//删除
expressList.remove(result);
System.out.println("快递已删除");
}
/**
* 修改
*/
case 3:{
//修改
String number = op.inputNumber();
//查找
int result = op.findByNumber(expressList, number);
if(result == -1){
System.out.println("快递不存在,请检查输入是否正确!");
}else{
System.out.println("所要修改的快递信息");
op.printExpressInfo(expressList.get(result));
Express e = op.newCreat();
expressList.set(result, e);
System.out.println("快递已更改");
System.out.println("更改之后的快递信息");
op.printExpressInfo(e);
}
}
break;
/**
* 查看所有
*/
case 4:{
//查看所有
System.out.println("快递单号\t" + "快递公司\t" + "取件码") ;
for (Express express: expressList) {
if (express != null){
System.out.println(express.getNumber() + "\t\t" + express.getCompany() + "\t\t" + express.getCode());
}
}
break;
}
}
}
}
5、main函数
public class MainEnter {
public static void main(String[] args) {
Main main = new Main();
main.op();
}
}