使用几乎完全重复的代码但对其他方法的不同调用来重构函数

j p*_*ton 3 java methods refactoring code-duplication intellij-idea

我正在学习 Java,正在学习在线课程等,我正在进行其中一项编码练习,并意识到我的两种方法之间存在大量重复,如下所示:

private static void addCustomerTransaction() {
    System.out.println("Enter the branch name:");
    String branchName = scanner.nextLine();

    System.out.println("Enter the customer name:");
    String customerName = scanner.nextLine();

    System.out.println("Enter the transaction");
    while (!scanner.hasNextDouble()) {
        scanner.next();
    }
    double transaction = scanner.nextDouble();

    bank.addCustomerTransaction(branchName,customerName,transaction);

}

private static void addCustomer() {
    System.out.println("Enter the branch name:");
    String branchName = scanner.nextLine();

    System.out.println("Enter the customer name:");
    String customerName = scanner.nextLine();

    System.out.println("Enter the transaction");
    while (!scanner.hasNextDouble()) {
        scanner.next();
    }
    double transaction = scanner.nextDouble();

    bank.addCustomer(branchName,customerName,transaction);
}
Run Code Online (Sandbox Code Playgroud)

现在显然这两个函数之间的唯一区别是对银行类对象的方法调用 - 最终执行不同的操作。

我想知道如何重构这些方法以减少重复。我了解到:

private static void addCustomerTransaction() {
    customerInput();

}

private static void addCustomer() {
    customerInput();
}

private static void customerInput() {
    System.out.println("Enter the branch name:");
    String branchName = scanner.nextLine();

    System.out.println("Enter the customer name:");
    String customerName = scanner.nextLine();

    System.out.println("Enter the transaction");
    while (!scanner.hasNextDouble()) {
        scanner.next();
    }
    double transaction = scanner.nextDouble();

    bank.addCustomerTransaction(branchName,customerName,transaction);
}
Run Code Online (Sandbox Code Playgroud)

但我不知道如何使代码能够更改方法调用(当前 bank.addCustomerTransaction(branchName,customerName,transaction);customerInput函数中根据哪个函数调用customerInput.

有人可以建议下一步吗?

ass*_*ias 5

这是一种选择。

为最后一个方法创建一个接口:

@FunctionalInterface public interface CustomerOperation {
  void apply(Bank bank, String branch, String customer, String transaction);
}
Run Code Online (Sandbox Code Playgroud)

那么你的常用方法可以如下所示:

private static void customerInput(CustomerOperation operation) {
  //common code here
  operation.apply(bank, branchName, customerName, transaction);
}
Run Code Online (Sandbox Code Playgroud)

你这样称呼它:

private static void addCustomerTransaction() {
  customerInput((bank, branchName, customerName, transaction) ->
      bank.addCustomerTransaction(branchName, customerName, transaction));
}

private static void addCustomer() {
  customerInput((bank, branchName, customerName, transaction) ->
      bank.addCustomer(branchName, customerName, transaction));
}
Run Code Online (Sandbox Code Playgroud)

或者使用方法参考:

private static void addCustomerTransaction() {
  customerInput(Bank::addCustomerTransaction);
}

private static void addCustomer() {
  customerInput(Bank::addCustomer);
}
Run Code Online (Sandbox Code Playgroud)