sortedTotalValues = null;
// Don't forget the handy utility methods getFirst() and getLast()...
Assert.assertEquals("Highest total order value", Double.valueOf(857.0), sortedTotalValues.getLast());
Assert.assertEquals("Lowest total order value", Double.valueOf(71.0), sortedTotalValues.getFirst());
}
/**
* Find the max total order value across all customers.
*/
@Test
public void maximumTotalOrderValue()
{
Double maximumTotalOrderValue = null;
Assert.assertEquals("max value", Double.valueOf(857.0), maximumTotalOrderValue);
}
/**
* Find the customer with the highest total order value.
*/
@Test
public void customerWithMaxTotalOrderValue()
{
Customer customerWithMaxTotalOrderValue = null;
Assert.assertEquals(this.company.getCustomerNamed("Mary"), customerWithMaxTotalOrderValue);
}
/**
* Create some code to get the company's supplier names as a tilde delimited string.
*/
@Test
public void supplierNamesAsTildeDelimitedString()
{
String tildeSeparatedNames = null;
Assert.assertEquals(
"tilde separated names",
"Shedtastic~Splendid Crocks~Annoying Pets~Gnomes 'R' Us~Furniture Hamlet~SFD~Doxins",
tildeSeparatedNames);
}
/**
* Deliver all orders going to customers from London.
*
* Hint: Use {@link RichIterable#forEach(Procedure)}. To solve the ambiguity error, use {@link Procedures#cast(Procedure)}}.
*
* @see Order#deliver()
*/
@Test
public void deliverOrdersToLondon()
{
Verify.assertAllSatisfy(this.company.getCustomerNamed("Fred").getOrders(), Order::isDelivered);
Verify.assertNoneSatisfy(this.company.getCustomerNamed("Mary").getOrders(), Order::isDelivered);
Verify.assertAllSatisfy(this.company.getCustomerNamed("Bill").getOrders(), Order::isDelivered);
}
}
================================================
FILE: src/test/java/com/gs/collections/kata/Exercise7Test.java
================================================
/*
* Copyright 2015 Goldman Sachs.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.gs.collections.kata;
import java.util.List;
import com.gs.collections.api.map.MutableMap;
import com.gs.collections.api.multimap.list.MutableListMultimap;
import com.gs.collections.impl.list.mutable.FastList;
import com.gs.collections.impl.map.mutable.UnifiedMap;
import com.gs.collections.impl.test.Verify;
import org.junit.Assert;
import org.junit.Test;
public class Exercise7Test extends CompanyDomainForKata
{
/**
* Create a multimap where the keys are the names of cities and the values are the customers from those cities.
*/
@Test
public void customersByCity()
{
// Notice that the second generic type is Customer, not List
MutableListMultimap multimap = null;
Assert.assertEquals(FastList.newListWith(this.company.getCustomerNamed("Mary")), multimap.get("Liphook"));
Assert.assertEquals(
FastList.newListWith(
this.company.getCustomerNamed("Fred"),
this.company.getCustomerNamed("Bill")),
multimap.get("London"));
}
@Test
public void mapOfItemsToSuppliers()
{
Assert.fail("Refactor this as part of Exercise 7");
/**
* Change itemsToSuppliers to a MutableMultimap
*/
final MutableMap> itemsToSuppliers = UnifiedMap.newMap();
for (Supplier supplier : this.company.getSuppliers())
{
for (String itemName : supplier.getItemNames())
{
List suppliersForItem;
if (itemsToSuppliers.containsKey(itemName))
{
suppliersForItem = itemsToSuppliers.get(itemName);
}
else
{
suppliersForItem = FastList.newList();
itemsToSuppliers.put(itemName, suppliersForItem);
}
suppliersForItem.add(supplier);
}
}
Verify.assertIterableSize("should be 2 suppliers for sofa", 2, itemsToSuppliers.get("sofa"));
}
@Test
public void reminder()
{
Assert.fail("Refactor setUpCustomersAndOrders() in the super class to not have so much repetition.");
// Delete this whole method when you're done. It's just a reminder.
}
}
================================================
FILE: src/test/java/com/gs/collections/kata/Exercise8Test.java
================================================
/*
* Copyright 2015 Goldman Sachs.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.gs.collections.kata;
import java.util.Collections;
import com.gs.collections.api.RichIterable;
import com.gs.collections.api.bag.sorted.MutableSortedBag;
import com.gs.collections.api.block.function.Function;
import com.gs.collections.api.block.function.Function0;
import com.gs.collections.api.block.function.Function2;
import com.gs.collections.api.list.MutableList;
import com.gs.collections.api.map.MutableMap;
import com.gs.collections.api.multimap.list.MutableListMultimap;
import com.gs.collections.impl.bag.sorted.mutable.TreeBag;
import com.gs.collections.impl.list.mutable.FastList;
import com.gs.collections.impl.test.Verify;
import org.junit.Assert;
import org.junit.Test;
public class Exercise8Test extends CompanyDomainForKata
{
/**
* Extra credit. Aggregate the total order values by city.
*
* @see RichIterable#aggregateBy(Function, Function0, Function2)
*/
@Test
public void totalOrderValuesByCity()
{
Function0 zeroValueFactory = () -> 0.0;
Function2 aggregator = (result, customer) -> result + customer.getTotalOrderValue();
MutableMap map = null;
Assert.assertEquals(2, map.size());
Assert.assertEquals(446.25, map.get("London"), 0.0);
Assert.assertEquals(857.0, map.get("Liphook"), 0.0);
}
/**
* Extra credit. Aggregate the total order values by item.
* Hint: Look at {@link RichIterable#aggregateBy(Function, Function0, Function2)} and remember
* how to use {@link RichIterable#flatCollect(Function)} to get an iterable of all items.
*/
@Test
public void totalOrderValuesByItem()
{
Function0 zeroValueFactory = () -> 0.0;
Function2 aggregator = (result, lineItem) -> result + lineItem.getValue();
MutableMap map = null;
Verify.assertSize(12, map);
Assert.assertEquals(100.0, map.get("shed"), 0.0);
Assert.assertEquals(10.5, map.get("cup"), 0.0);
}
/**
* Extra credit. Find all customers' line item values greater than 7.5 and sort them by highest to lowest price.
*/
@Test
public void sortedOrders()
{
MutableSortedBag orderedPrices = null;
MutableSortedBag expectedPrices = TreeBag.newBagWith(
Collections.reverseOrder(), 500.0, 150.0, 120.0, 75.0, 50.0, 50.0, 12.5);
Verify.assertSortedBagsEqual(expectedPrices, orderedPrices);
}
/**
* Extra credit. Figure out which customers ordered saucers (in any of their orders).
*/
@Test
public void whoOrderedSaucers()
{
MutableList customersWithSaucers = null;
Verify.assertSize("customers with saucers", 2, customersWithSaucers);
}
/**
* Extra credit. Look into the {@link MutableList#toMap(Function, Function)} method.
*/
@Test
public void ordersByCustomerUsingAsMap()
{
MutableMap> customerNameToOrders =
this.company.getCustomers().toMap(null, null);
Assert.assertNotNull("customer name to orders", customerNameToOrders);
Verify.assertSize("customer names", 3, customerNameToOrders);
MutableList ordersForBill = customerNameToOrders.get("Bill");
Verify.assertSize("Bill orders", 3, ordersForBill);
}
/**
* Extra credit. Create a multimap where the values are customers and the key is the price of
* the most expensive item that the customer ordered.
*/
@Test
public void mostExpensiveItem()
{
MutableListMultimap multimap = null;
Assert.assertEquals(3, multimap.size());
Assert.assertEquals(2, multimap.keysView().size());
Assert.assertEquals(
FastList.newListWith(
this.company.getCustomerNamed("Fred"),
this.company.getCustomerNamed("Bill")),
multimap.get(50.0));
}
}