lundi 23 décembre 2019

How to re-use method and add different variables and if statements?

im trying to use this method by doing search by amount, but i dont have any params. All i would like to change in this piece of code is outputs and sysout messages it gives and if statements params. So can i do so without duplicating everything ? Is it ok to duplicate if i cant re-use it ?

 public void searchByName() throws FileNotFoundException {
    x = new Scanner(file);
    Scanner scan = new Scanner(System.in);

    String name = "";
    String code = "";
    String amount = "";
    String date = "";
    x.useDelimiter("[,\n]");
    x.nextLine();

    System.out.print("Input name of product: ");
    String input = scan.next();

    while (x.hasNext()) {
        name = x.next();
        code = x.next();
        amount = x.next();
        date = x.next().substring(0, 10);

        if (input.equals(name.toLowerCase())) {
            ArrayList<String> productList = new ArrayList<>();
            productList.add("Name: " + name + "\n" + "Code: " + code + "\n" + "Amount: " + amount + "\n" + "Date: " + date);
            System.out.println("______________________");
            System.out.println(productList);
        }
    }
}

Aucun commentaire:

Enregistrer un commentaire