1

I am using java8 steams for sometime now and I want to use it for the following scenario. I have a bank class. There are two types of account in the bank - checking and savings and there are three types of activities : Withdrawal, Deposit and Transfer.

import java.util.Arrays;
import java.util.List;

public class Chasebank {


    public static void main(String[] args) {
        Customer john_checking_w = new Customer("1234", "John", "ChaseBanking",
                "checkingAccount", "Withdrawal", "500");

        Customer john_checking_d = new Customer("1234", "John", "ChaseBanking",
                "checkingAccount", "Deposit", "1000");

        Customer john_checking_t = new Customer("1234", "John", "ChaseBanking",
                "checkingAccount", "Transfer", "100");


        Customer john_saving_w = new Customer("1234", "John", "ChaseBanking",
                "savingAccount", "Withdrawal", "500");

        Customer john_saving_d = new Customer("1234", "John", "ChaseBanking",
                "savingAccount", "Deposit", "10000");


        Customer john_saving_t = new Customer("1234", "John", "ChaseBanking",
                "savingAccount", "Transfer", "200");



        Customer mary_saving_d = new Customer("2222", "Mary", "ChaseBanking",
                "savingAccount", "Deposit", "100");


        Customer mary_saving_t = new Customer("1234", "Mary", "ChaseBanking",
                "savingAccount", "Transfer", "50");


        Customer joseph_checking_w = new Customer("3333", "Joseph", "ChaseBanking",
                "checkingAccount", "Withdrawal", "760");




        List<Customer> customers = Arrays.asList(john_checking_w,
                john_checking_d, john_checking_t, john_saving_w, john_saving_d,
                john_saving_t, mary_saving_d, mary_saving_t, joseph_checking_w);

    }



    public static class Customer {
        final String Id;
        final String Name;
        final String pCode;
        final String accountType;
        final String activity;
        final String amount;

        public Customer(String id, String name, String pCode, String accountType, String activity, String amount) {
            Id = id;
            Name = name;
            this.pCode = pCode;
            this.accountType = accountType;
            this.activity = activity;
            this.amount = amount;
        }
    }
}

What I am trying to do here is check if there are 6 distinct entries for the customer with 1234 with the following combination in the list:

Id = 1234 accountType = savingAccount activity = Withdrawal value = 500
Id = 1234 accountType = savingAccount activity = Deposit value = 10000
Id = 1234 accountType = savingAccount activity = Transfer value = 200
Id = 1234 accountType = checkingAccount activity = Withdrawal value = 500
Id = 1234 accountType = checkingAccount activity = Deposit value = 1000
Id = 1234 accountType = checkingAccount activity = Transfer value = 100

The value field can be anything.

What I tried so far :

Predicate<KonaFileLineItem> condition = k -> k.getId().equals("1234")
                && (k.getAccountType().equals("savingAccount")
                || k.getAccountType().equals("checkingAccount"))
                && (k.getActivity().equals("Withdrawal")
                || k.getActivity().equals("Deposit")
                || k.getActivity().equals("Transfer")

boolean result = customers.stream()
                .map(k -> k.getId().concat(k.getAccountType).concat(k.getActivity())
                .distinct().count() == 6; 

This is working as expected without the value field. I am not sure how can validate the value field along with this. Need help with it.

1
  • By 6 distinct entries for the customer, did you mean those six possible combinations of accountType * activity? Commented Jul 17, 2020 at 8:40

1 Answer 1

1

You have to pass the predicate to filter, I think you are missing out the filter. Please check the code below :

customers.stream().filter(t->t.getId().equals("1234")).map(k -> k.getId().concat(k.getAccountType()).concat(k.getActivity())).distinct().count()

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.