创建一个方法来将数据设置到对象

时间:2014-08-15 07:42:32

标签: java methods arraylist

编写用于检查不同条件的数据的代码。 我有一个CRM对象的ArrayList“actionsList”(在另一个类中制作)。 在这里,我检查这些对象的不同条件。 满足条件的对象我必须添加到ArrayList“remarksList”。 问题是如何创建一个方法setRemarkObject()来立即将所有数据设置为remarkObject? 不要每次都写:

  remarkObject.setRemark(checkString);
  remarkObject.setNumber(crm.getNumber());
  remarkObject.setDealer(crm.getDealer());
  remarkObject.setName(crm.getName());

...

现在是否正确?

import java.util.ArrayList;

public class Conditions {

    static ArrayList<CRM> remarksList = new ArrayList<CRM>();

    public ArrayList<CRM> conditionsChecking() {
        for (CRM crm : App.actionsList) {
            CRM remarkObject = new CRM();

            remarkObject.setNumber(crm.getNumber());
            remarkObject.setDealer(crm.getDealer());
            remarkObject.setName(crm.getName());
            remarkObject.setGroup(crm.getGroup());
            remarkObject.setClientStatus(crm.getClientStatus());
            remarkObject.setEntity(crm.getEntity());
            remarkObject.setTypeOfContact(crm.getTypeOfContact());
            remarkObject.setTypeOfFirstContact(crm.getTypeOfFirstContact());
            remarkObject.setSourceOfFirstContact(crm
                    .getSourceOfFirstContact());
            remarkObject.setOfferType(crm.getOfferType());
            remarkObject.setEventDate(crm.getEventDate());
            remarkObject.setBrand(crm.getBrand());
            remarkObject.setCarClass(crm.getCarClass());
            remarkObject.setModel(crm.getModel());
            remarkObject.setCarCode(crm.getCarCode());
            remarkObject.setWeek(crm.getWeek());
            remarkObject.setMonth(crm.getMonth());
            remarkObject.setYear(crm.getYear());
            remarkObject.setAmmount(crm.getAmmount());
            remarkObject.setSalesman(crm.getSalesman());
            remarkObject.setPhone(crm.getPhone());
            remarkObject.setEmail(crm.getEmail());
            remarkObject.setAddress(crm.getAdress());
            remarkObject.setCreationDate(crm.getCreationDate());
            remarkObject.setCreationTime(crm.getCreationTime());
            remarkObject.setModificationDate(crm.getModificationDate());
            remarkObject.setModificationTime(crm.getModificationTime());
            remarkObject.setBackdating(crm.getBackdating());

            if ((crm.getClientStatus().equals("Yes")) && ((crm.getAdress().isEmpty()))){
                crm.setRemark("Client's address is empty");
            remarksList.add(remarkObject);
            }
            else if ((crm.getClientStatus().equals("Yes")) && (crm.getPhone().isEmpty())){          
                crm.setRemark( "Phone field is empty");
            remarksList.add(remarkObject);
            }
            ///....
            else
                crm.setRemark("Nothing wrong");
                /// not adding to remarksLis

        }
        return remarksList;
    }

}

2 个答案:

答案 0 :(得分:0)

你似乎一遍又一遍地做同样的事情,添加了一个字符串,表明字段是否为空。那么为什么不在for循环之外将setter移动一个块呢?

static ArrayList<CRM> remarksList = new ArrayList<CRM>();

public ArrayList<CRM> conditionsChecking() {
    for (CRM crm : App.actionsList) {
        CRM remarkObject = new CRM();

        remarkObject.setNumber(crm.getNumber());
        remarkObject.setDealer(crm.getDealer());
        remarkObject.setName(crm.getName());
        remarkObject.setGroup(crm.getGroup());
        //etcetera, etcetera, all setters, except remark

        if ((crm.getClientStatus().equals("Yes")) && ((crm.getAdress().isEmpty())))
            crm.setRemark("Client's address is empty");
        else if ((crm.getClientStatus().equals("Yes")) && (crm.getPhone().isEmpty()))            
            crm.setRemark( "Phone field is empty");
        // etc, etc
        else
            remarksList.add(remarkObject);
    }
    return remarksList;
}

对于一般的setter和getter,你也可以使用构造函数:

public CRM(String number, String dealer, String name, ...) { 
    this.number = number;
    //...etc
}

或建设者

public CRM() {}

public CRM setNumber(String number) { 
    this.number = number;
    return this;
}

public CRM setDealer(String dealer) { 
    this.dealer = dealer;
    return this;
}

//and use it like this
CRM crm = new CRM().setNumber("123").setDealer("dealer"); //et cetera

答案 1 :(得分:0)

据我所知,您正在将每个if-else语句中的crm对象复制到remarkObject中。为什么不在所有if语句之前复制它,只在if-else语句中设置remark(checkString)?然后,您可以在所有这些语句之后将其添加到remarkList。 您可以使用CRM构造函数复制对象,即。 CRM(CRM crm)或通过克隆对象: How do I copy an object in Java?

你也在你的每个if语句中检查crm.getClientStatus()。equals(&#34; Yes&#34;) - 为什么不在开始时检查一次?