有没有办法修复这个问题,而不需要重复编写列的代码?

huangapple go评论84阅读模式
英文:

is there any way to fix this without repeating of the columns code?

问题

请任何帮助将不胜感激。我有一个名为private void update database的方法,其中的代码如下。我想知道是否有任何方法可以在不重复的情况下缩短代码?

firstNameColumn.setOnEditCommit(event -> {
    Customer customer = event.getRowValue();
    customer.setFirstName(event.getNewValue());
    try {
        Utils.getInstance().update("firstName", event.getNewValue(), customer.getId());
    } catch (SQLException e) {
        throw new RuntimeException(e);
    }
});
middleNameColumn.setOnEditCommit(event -> {
    Customer customer = event.getRowValue();
    customer.setFirstName(event.getNewValue());
    try {
        Utils.getInstance().update("middleName", event.getNewValue(), customer.getId());
    } catch (SQLException e) {
        throw a RuntimeException(e);
    }
});
lastNameColumn.setOnEditCommit(event -> {
    Customer customer = event.getRowValue();
    customer.setFirstName(event.getNewValue());
    try {
        Utils.getInstance().update("lastName", event.getNewValue(), customer.getId());
    } catch (SQLException e) {
        throw new RuntimeException(e);
    }
});

我有三个字段:姓氏列、中间名列和姓氏列,但它们在一个方法内重复出现。

英文:

Please any help would be appreciated. I have an method called private void update database and the its code inside is as below. I wonder is there any way to shorten the code without repeating ?

	firstNameColumn.setOnEditCommit(event -> {
	    Customer customer = event.getRowValue();
	    customer.setFirstName(event.getNewValue());
	    try {
			Utils.getInstance().update("firstName", event.getNewValue(), customer.getId());
		} catch (SQLException e) {
			throw new RuntimeException(e);
		}
	});
	middleNameColumn.setOnEditCommit(event -> {
	    Customer customer = event.getRowValue();
	    customer.setFirstName(event.getNewValue());
	    try {
			Utils.getInstance().update("middleName", event.getNewValue(), customer.getId());
		} catch (SQLException e) {
			throw new RuntimeException(e);
		}
	});
	lastNameColumn.setOnEditCommit(event -> {
	    Customer customer = event.getRowValue();
	    customer.setFirstName(event.getNewValue());
	    try {
			Utils.getInstance().update("lastName", event.getNewValue(), customer.getId());
		} catch (SQLException e) {
			throw new RuntimeException(e);
		}
	});

I have the fields: first name column, middle name column, and last name column, but they are repeating inside a method.

答案1

得分: 1

是的,你可以。

据我所见,下面的代码在这三个更新中都被使用:

Customer customer = event.getRowValue();
customer.setFirstName(event.getNewValue());
try {
    Utils.getInstance().update("xxxx", event.getNewValue(), customer.getId());
} catch (SQLException e) {
    throw new RuntimeException(e);
}

它只是将 "xxxx" 更改为 firstName、middleName 或 lastName。

你可以创建一个函数,这个函数会被这三个更新调用。

firstNameColumn.setOnEditCommit(event -> {
    updateCustomerBy("firstName", event);
});
middleNameColumn.setOnEditCommit(event -> {
    updateCustomerBy("middleName", event);
});
lastNameColumn.setOnEditCommit(event -> {
    updateCustomerBy("lastName", event);
});

private void updateCustomerBy(String name, Event event) {
    // 我不知道事件的类型,所以我只是用 Event 代替了。你应该使用正确的类型。
    Customer customer = event.getRowValue();
    customer.setFirstName(event.getNewValue());
    try {
        Utils.getInstance().update(name, event.getNewValue(), customer.getId());
    } catch (SQLException e) {
        throw new RuntimeException(e);
    }
}
英文:

Yes you can.

As far i can see the next code is used in the three updates:

    Customer customer = event.getRowValue();
    customer.setFirstName(event.getNewValue());
    try {
        Utils.getInstance().update("xxxx", event.getNewValue(), customer.getId());
    } catch (SQLException e) {
        throw new RuntimeException(e);
    }

It just changes the xxx by firstName, middleName or lastName.

You can create a function that is gonna be called by the three updates.

    firstNameColumn.setOnEditCommit(event -> {
        updateCustomerBy("firstName", event);
    });
    middleNameColumn.setOnEditCommit(event -> {
        updateCustomerBy("middleName", event);
    });
    lastNameColumn.setOnEditCommit(event -> {
        updateCustomerBy("lastName", event);
    });

private void updateCustomerBy(String name, Event event){
   //I dont know the type of event, so i just put Event. You shall use the correct type
    Customer customer = event.getRowValue();
    customer.setFirstName(event.getNewValue());
    try {
        Utils.getInstance().update(name, event.getNewValue(), customer.getId());
    } catch (SQLException e) {
        throw new RuntimeException(e);
    }
}

答案2

得分: 0

firstNameColumn.setOnEditCommit(event -> {
   update("firstName", event);
});

middleNameColumn.setOnEditCommit(event -> {
   update("middleName", event);
});

lastNameColumn.setOnEditCommit(event -> {
   update("lastName", event);
});

public void update(String column, Event event) {
   Customer customer = event.getRowValue();
   customer.setFirstName(event.getNewValue());
   try {
       Utils.getInstance().update(column, event.getNewValue(), customer.getId());
   } catch (SQLException e) {
       throw new RuntimeException(e);
   }
}
英文:
       firstNameColumn.setOnEditCommit(event -> {
       update("firstName",event)
        });
       middleNameColumn.setOnEditCommit(event -> {
 update("middleName",event) });
      lastNameColumn.setOnEditCommit(event -> {
     update("lastName",event)});

       public void update(String column,Event event){
      
            Customer customer = event.getRowValue();
            customer.setFirstName(event.getNewValue());
            try {
                Utils.getInstance().update(column, event.getNewValue(),         customer.getId());
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
    }

huangapple
  • 本文由 发表于 2020年7月30日 02:49:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/63160495.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定