保存数据以排列更有效的代码

时间:2018-07-10 12:24:21

标签: java arrays

我是Android Studio的新手,并编写了一个Product Scanner应用程序,该应用程序将数据存储在一个数组中,它确实可以工作,但是我知道这段代码很长,希望就如何提高效率提出建议,我想重写此代码,以免最终没有两个大块,一个要更新,一个要添加,我已经在此方面进行了详尽的研究。

我的数组称为产品,而currentProduct是类项

public void SavetoArray() {

    //Get Time & Date
    GetTodaysDate();
    //check ALL field before saving

    //if (CurrentDate == null) CurrentDate = todayDateTimeString;

    //if indexpoint at end of array then we need to use ADD action to ADD ARRAY RECORD TO EOF
    if(indexpoint == products.size()){

    }
    if (indexpoint == products.size()) {
        products.add(indexpoint + UPC_FIELD_OFFSET, currentProduct.getmUPC());    
        products.add(indexpoint + DESC_FIELD_OFFSET, currentProduct.getmName());    
        products.add(indexpoint + UNITHEIGHT_FIELD_OFFSET, currentProduct.getmUnitHeight());    
        products.add(indexpoint + UNITWIDTH_FIELD_OFFSET, currentProduct.getmUnitWidth());    
        products.add(indexpoint + UNITDEPTH_FIELD_OFFSET, currentProduct.getmUnitDepth());    
        products.add(indexpoint + TRAYHEIGHT_FIELD_OFFSET, currentProduct.getmTrayHeight());    
        products.add(indexpoint + TRAYWIDTH_FIELD_OFFSET, currentProduct.getmTrayWidth());
        products.add(indexpoint + TRAYDEPTH_FIELD_OFFSET, currentProduct.getmTrayDepth());
        products.add(indexpoint + TRAYNUMH_FIELD_OFFSET, currentProduct.getmTrayNumHigh());
        products.add(indexpoint + TRAYNUMW_FIELD_OFFSET, currentProduct.getmTrayNumWide());
        products.add(indexpoint + TRAYNUMD_FIELD_OFFSET, currentProduct.getmTrayNumDeep());
        products.add(indexpoint + TRAYTOTALNUM_FIELD_OFFSET, currentProduct.getmTrayTotalNum());
        products.add(indexpoint + STATUS_FIELD_OFFSET, currentProduct.getmStatus());
        products.add(indexpoint + DATE_FIELD_OFFSET, currentProduct.getmDate());

        // this just checks we are adding a row to arraylist
        Toast.makeText(this, "Products on file" + products.size() / RecordFieldCount, Toast.LENGTH_LONG).show();

    } else {

        //if indexpoint not end of array then we need to use set action = UPDATE ARRAY RECORD
        products.set(indexpoint + UPC_FIELD_OFFSET, currentProduct.getmUPC());
        products.set(indexpoint + DESC_FIELD_OFFSET,currentProduct.getmName());
        products.set(indexpoint + UNITHEIGHT_FIELD_OFFSET, currentProduct.getmUnitHeight());
        products.set(indexpoint + UNITWIDTH_FIELD_OFFSET, currentProduct.getmUnitWidth());
        products.set(indexpoint + UNITDEPTH_FIELD_OFFSET, currentProduct.getmUnitDepth());
        products.set(indexpoint + TRAYHEIGHT_FIELD_OFFSET, currentProduct.getmTrayHeight());
        products.set(indexpoint + TRAYWIDTH_FIELD_OFFSET, currentProduct.getmTrayWidth());
        products.set(indexpoint + TRAYDEPTH_FIELD_OFFSET, currentProduct.getmTrayDepth());
        products.set(indexpoint + TRAYNUMH_FIELD_OFFSET, currentProduct.getmTrayNumHigh());
        products.set(indexpoint + TRAYNUMW_FIELD_OFFSET, currentProduct.getmTrayNumWide());
        products.set(indexpoint + TRAYNUMD_FIELD_OFFSET, currentProduct.getmTrayNumDeep());
        products.set(indexpoint + TRAYTOTALNUM_FIELD_OFFSET, currentProduct.getmTrayTotalNum());
        products.set(indexpoint + STATUS_FIELD_OFFSET, currentProduct.getmStatus());
        products.set(indexpoint + DATE_FIELD_OFFSET, currentProduct.getmDate());

    }
}

2 个答案:

答案 0 :(得分:0)

考虑使用Objects,而不是将产品读入数组并写入csv文件。这是一种有效且直观的信息存储方式,以便将来可以轻松访问。

这是Oracle在类/对象上的文档,可助您一臂之力。 https://docs.oracle.com/javase/tutorial/java/javaOO/index.html

答案 1 :(得分:0)

正如我所看到的,这更多的是代码设计问题。您可以做的是重新设计代码。

创建一个地图以显示偏移量的类型及其值(DATE_FIELD_OFFSET,STATUS_FIELD_OFFSET,...)。

创建一个工厂,而不是使用currentProduct.getXX()。我们称之为 CurrentProductFactory

您创建一个名为 setUpdate(int gap,int index,inttriggedValue)的方法。此方法根据索引点的值从集合或更新中切换。

public class CurrentProductFactory {    
// We use the offset name to return the needed method
    public int get(String offset){
        switch(offset){
            case "DATE_FIELD_OFFSET":getmDate();
            break;
            case "STATUS_FIELD_OFFSET": getmStatus()
            break;
            }
    public int getmDate() {
    }
    public int getmStatus() {
    }
}
    // A map to manage your offsets
    TreeMap<String,Integer> offsets = new TreeMap<>();
    offsets.put("DATE_FIELD_OFFSET",DATE_FIELD_OFFSET);

// The method to embody the set and the update alternatively
    public void setUpdate(int gap, int index, int retrievedValue) {
        if(gap != 0)set(index, retrievedValue);
        else add(index, retrievedValue);
    }

您的代码变为:

//Get Time & Date
    GetTodaysDate();

    int gap = indexpoint-products.size();

    for (Map.Entry<String,Integer> entry : offsets.entrySet()) {
        String offset = entry.getKey();
        Integer offset_value = entry.getValue();
        products.setUpdate(gap,indexpoint+offset_value,CurrentProductFactory.get(offset));
    }