Cafe { public static Tuple2<List<Coffee>, Charge> buyCoffees(CreditCard cc, int number) { Stream<Tuple2<Coffee, Charge>> tupledPurchases = Stream.continually(() -> buyCoffee(cc)) .take(number); Tuple2<Stream<Coffee>, Stream<Charge>> purchases = tupledPurchases .unzip(Function.identity()); return purchases.map(Stream::toList, charges -> charges.reduce(Charge::add)); }  static Tuple2<Coffee, Charge> buyCoffee(CreditCard cc); static Tuple2<List<Coffee>, Charge> buyCoffees(CreditCard cc, int number);  }
@Test public void shouldFailToBuyCoffeeWithoutAllowance() throws Exception { Tuple2<List<Coffee>, Charge> purchases = Cafe.buyCoffees(new CreditCard(1), 15); Charge charge = purchases._2; assertThat(charge.apply()).isInstanceOf(Failure.class); }
@Test public void shouldFailToBuyCoffeeWithoutAllowanceToo() throws Exception { Tuple2<List<Coffee>, Charge> purchases = Cafe.buyCoffees(new CreditCard(1), 15); Tuple2<List<Coffee>, Try<CreditCard>> payed = purchases.map2(Charge::apply); assertThat(payed._2).isInstanceOf(Failure.class); }