【7】JAVA---地址App小软件(AddrBusiness.class)(逻辑层)

这个。。。没多少好解释的。。。
表现层的增删改查的具体实现类。

package cn.hncu.addr.business;

import javax.swing.JOptionPane;

import cn.hncu.addr.dao.AddrDaoFile;
import sun.security.util.Length;

public class AddrBusiness {
    //静态方法。访问的是同一个对象--也就是说就算是new这个对象,也只是引用之前的那个静态对象
    private static Object[] objs = new Object[0];

    private boolean save() {
        boolean result = AddrDaoFile.write(objs);
        if(!result){
            JOptionPane.showMessageDialog(null, "数据存储失败!");
            return false;
        }

        return true;
    }


    //判断集合是不是已经有了那个元素
    public boolean contains(Object obj){
        for(Object temp:objs){
            if(temp.equals(obj)){
                return true;
            }
        }
        return false;
    }

    //添加元素,不能添加重复的元素
    public boolean add(Object obj){
        if(contains(obj)){
            return false;
        }
        Object[] temp = new Object[objs.length+1];
        System.arraycopy(objs, 0, temp, 0, objs.length);
        temp[objs.length]=obj;
        objs = temp;
        boolean result = save();
        return result;
    }


    //返回集合的所有元素
    public Object[] getAll(){
        objs = AddrDaoFile.read();
        return objs;
    }

    //返回集合的元素长度
    public int size(){
        return objs.length;
    }

    public boolean update(String oldStrAdd, String newStrAdd) {
        for(int i=0;i<objs.length;i++){
            if(((String)objs[i]).equals(oldStrAdd)){
                objs[i] = (Object)newStrAdd;
                boolean result = save();
                return result;
            }
        }
        return false;
    }

    public boolean delete(String oldStrAdd) {
        Object[] tempObj = new Object[objs.length-1];
        int k=0;
        for(int i=0;i<objs.length;i++){
            if(!(((String)objs[i]).equals(oldStrAdd))){
                tempObj[k++]=objs[i];
            }
        }
        objs = tempObj;
        boolean result = save();
        return result;
    }

    public Object[] query(String[] strsQu) {
        Object[] tempObjs = new Object[objs.length];
        int k=0;
        for(int i=0;i<objs.length;i++){
            String strObj = (String)objs[i];
            String strs[] = strObj.split(",");

            //卫条件
            if(strsQu[0]!=null&&strsQu[0].trim().length()>0){
                if(!strs[0].contains(strsQu[0])){//模糊匹配姓名
                    continue;
                }
            }

            if(strsQu[1]!=null&&strsQu[1].trim().length()>0){
                if(!strs[1].contains(strsQu[1])){//模糊匹配性别
                    continue;
                }
            }

            //年龄大于第一段的
            if(strsQu[2]!=null){
                if(strsQu[2].trim().length()>0){
                    if(Integer.parseInt(strs[2])<Integer.parseInt(strsQu[2])){
                        continue;
                    }
                }
            }

            //年龄小于第二段的
            if(strsQu[5]!=null){
                if(strsQu[5].trim().length()>0){
                    if(Integer.parseInt(strs[2])>Integer.parseInt(strsQu[5])){
                        continue;
                    }
                }
            }

            //电话
            if(strsQu[3]!=null&&strsQu[3].trim().length()>0){
                if(!strs[3].contains(strsQu[3])){//模糊匹配
                    continue;
                }
            }

            //地址
            if(strsQu[4]!=null&&strsQu[4].trim().length()>0){
                if(!strs[4].contains(strsQu[4])){//模糊匹配
                    continue;
                }
            }
            tempObjs[k++]=objs[i];
        }

        Object[] tempObjs2 = new Object[k];
        for(int i=0;i<k;i++){
            tempObjs2[i]=tempObjs[i];
        }
        return tempObjs2;
    }

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值