Skip to content

Commit

Permalink
[GSCKATA-47] Clarify and improve the exercises.
Browse files Browse the repository at this point in the history
git-svn-id: svn+ssh://gsckata.svn.services.gs.com/svnroot/gsckata/trunk@117 66aded80-052e-4344-91a8-38ab96726eea
  • Loading branch information
motlin committed Feb 4, 2015
1 parent 0d8f88d commit e453c03
Show file tree
Hide file tree
Showing 15 changed files with 406 additions and 544 deletions.
6 changes: 3 additions & 3 deletions src/main/java/com/gs/collections/kata/Company.java
@@ -1,5 +1,5 @@
/*
* Copyright 2011 Goldman Sachs.
* 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.
Expand Down Expand Up @@ -54,7 +54,7 @@ public MutableList<Customer> getCustomers()

public MutableList<Order> getOrders()
{
Assert.fail("Refactor this code to use GS Collections as part of Exercise 4");
Assert.fail("Refactor this code to use GS Collections as part of Exercise 3");
MutableList<Order> orders = FastList.newList();
for (Customer customer : this.customers)
{
Expand Down Expand Up @@ -89,7 +89,7 @@ public Customer getCustomerNamed(String name)
/**
* Use a {@link Predicate} to find a {@link Customer} with the name given.
*/
Assert.fail("Implement this method as part of Exercise 3");
Assert.fail("Implement this method as part of Exercise 2");
return null;
}
}
34 changes: 7 additions & 27 deletions src/main/java/com/gs/collections/kata/Customer.java
@@ -1,5 +1,5 @@
/*
* Copyright 2011 Goldman Sachs.
* 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.
Expand Down Expand Up @@ -30,32 +30,19 @@
*/
public class Customer
{
public static final Function<Customer, String> TO_NAME = new Function<Customer, String>()
{
@Override
public String valueOf(Customer customer)
{
Assert.fail("Replace with the implementation of the Function.");
return null;
}
public static final Function<Customer, String> TO_NAME = customer -> {
Assert.fail("Replace with the implementation of the Function.");
return null;
};

public static final Function<Customer, String> TO_CITY = null;

public static final Function<Customer, Double> TO_TOTAL_ORDER_VALUE =
new Function<Customer, Double>()
{
@Override
public Double valueOf(Customer customer)
{
return customer.getTotalOrderValue();
}
};
public static final Function<Customer, Double> TO_TOTAL_ORDER_VALUE = Customer::getTotalOrderValue;

private final String name;
private final String city;

private final List<Order> orders = new ArrayList<Order>();
private final List<Order> orders = new ArrayList<>();

public Customer(String name, String city)
{
Expand Down Expand Up @@ -85,14 +72,7 @@ public void addOrder(Order anOrder)

public double getTotalOrderValue()
{
MutableList<Double> orderValues = ListIterate.collect(this.orders, new Function<Order, Double>()
{
@Override
public Double valueOf(Order order)
{
return order.getValue();
}
});
MutableList<Double> orderValues = ListIterate.collect(this.orders, Order::getValue);
return orderValues.injectInto(0.0, AddFunction.DOUBLE_TO_DOUBLE);
}
}
11 changes: 2 additions & 9 deletions src/main/java/com/gs/collections/kata/LineItem.java
@@ -1,5 +1,5 @@
/*
* Copyright 2011 Goldman Sachs.
* 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.
Expand All @@ -23,14 +23,7 @@
*/
public class LineItem
{
public static final Function<LineItem, String> TO_NAME = new Function<LineItem, String>()
{
@Override
public String valueOf(LineItem lineItem)
{
return lineItem.name;
}
};
public static final Function<LineItem, String> TO_NAME = LineItem::getName;

private String name;
private final double value;
Expand Down
47 changes: 7 additions & 40 deletions src/main/java/com/gs/collections/kata/Order.java
@@ -1,5 +1,5 @@
/*
* Copyright 2011 Goldman Sachs.
* 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.
Expand All @@ -21,50 +21,24 @@
import java.util.List;

import com.gs.collections.api.block.function.Function;
import com.gs.collections.api.block.predicate.Predicate;
import com.gs.collections.impl.block.function.AddFunction;
import com.gs.collections.impl.collection.mutable.CollectionAdapter;
import com.gs.collections.impl.utility.Iterate;

/**
* Has a number, a {@link Customer}, a {@link List} of {@link LineItem}s, and a boolean that states whether or not the order
* has been delivered. There is a class variable that contains the next order number.
* has been delivered. There is a class variable that contains the next order number.
*/
public class Order
{
public static final Function<Order, Double> TO_VALUE =
new Function<Order, Double>()
{
@Override
public Double valueOf(Order order)
{
return order.getValue();
}
};

public static final Predicate<Order> IS_DELIVERED = new Predicate<Order>()
{
@Override
public boolean accept(Order order)
{
return order.isDelivered;
}
};

public static final Function<Order, Iterable<LineItem>> TO_LINE_ITEMS =
new Function<Order, Iterable<LineItem>>()
{
@Override
public Iterable<LineItem> valueOf(Order order)
{
return order.lineItems;
}
};
public static final Function<Order, Double> TO_VALUE = Order::getValue;

public static final Function<Order, Iterable<LineItem>> TO_LINE_ITEMS = Order::getLineItems;

private static int nextOrderNumber = 1;

private final int orderNumber;
private final List<LineItem> lineItems = new ArrayList<LineItem>();
private final List<LineItem> lineItems = new ArrayList<>();
private boolean isDelivered;

public Order()
Expand Down Expand Up @@ -106,14 +80,7 @@ public String toString()

public double getValue()
{
Collection<Double> itemValues = Iterate.collect(this.lineItems, new Function<LineItem, Double>()
{
@Override
public Double valueOf(LineItem lineItem)
{
return lineItem.getValue();
}
});
Collection<Double> itemValues = Iterate.collect(this.lineItems, LineItem::getValue);

return CollectionAdapter.adapt(itemValues).injectInto(0.0, AddFunction.DOUBLE_TO_DOUBLE);
}
Expand Down
22 changes: 3 additions & 19 deletions src/main/java/com/gs/collections/kata/Supplier.java
@@ -1,5 +1,5 @@
/*
* Copyright 2011 Goldman Sachs.
* 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.
Expand All @@ -23,25 +23,9 @@
*/
public class Supplier
{
public static final Function<Supplier, String> TO_NAME =
new Function<Supplier, String>()
{
@Override
public String valueOf(Supplier supplier)
{
return supplier.name;
}
};
public static final Function<Supplier, String> TO_NAME = Supplier::getName;

public static final Function<Supplier, Integer> TO_NUMBER_OF_ITEMS =
new Function<Supplier, Integer>()
{
@Override
public Integer valueOf(Supplier supplier)
{
return supplier.itemNames.length;
}
};
public static final Function<Supplier, Integer> TO_NUMBER_OF_ITEMS = supplier -> supplier.getItemNames().length;

private final String name;
private final String[] itemNames;
Expand Down
14 changes: 7 additions & 7 deletions src/test/java/com/gs/collections/kata/CompanyDomainForKata.java
@@ -1,5 +1,5 @@
/*
* Copyright 2011 Goldman Sachs.
* 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.
Expand Down Expand Up @@ -47,14 +47,14 @@ private void setUpCustomersAndOrders()
fredOrder.addLineItem(new LineItem("shed", 50.0));

/**
* TODO 8: Refactor Order and its API so this repetition is not necessary.
* TODO 7: Refactor Order and its API so this repetition is not necessary.
*/
// TODO 8: Add 3 cups at 1.5 each to the order
// TODO 7: Add 3 cups at 1.5 each to the order
fredOrder.addLineItem(new LineItem("cup", 1.5));
fredOrder.addLineItem(new LineItem("cup", 1.5));
fredOrder.addLineItem(new LineItem("cup", 1.5));

// TODO 8: Add 3 saucers at 1.0 each to the order
// TODO 7: Add 3 saucers at 1.0 each to the order
fredOrder.addLineItem(new LineItem("saucer", 1.0));
fredOrder.addLineItem(new LineItem("saucer", 1.0));
fredOrder.addLineItem(new LineItem("saucer", 1.0));
Expand All @@ -70,13 +70,13 @@ private void setUpCustomersAndOrders()
maryOrder.addLineItem(new LineItem("cat", 150.0));
maryOrder.addLineItem(new LineItem("big shed", 500.0));

// TODO 8: Add 4 cups at 1.50 each to the order
// TODO 7: Add 4 cups at 1.50 each to the order
maryOrder.addLineItem(new LineItem("cup", 1.5));
maryOrder.addLineItem(new LineItem("cup", 1.5));
maryOrder.addLineItem(new LineItem("cup", 1.5));
maryOrder.addLineItem(new LineItem("cup", 1.5));

// TODO 8: Add 4 saucers at 1.50 each to the order
// TODO 7: Add 4 saucers at 1.50 each to the order
maryOrder.addLineItem(new LineItem("saucer", 1.5));
maryOrder.addLineItem(new LineItem("saucer", 1.5));
maryOrder.addLineItem(new LineItem("saucer", 1.5));
Expand All @@ -92,7 +92,7 @@ private void setUpCustomersAndOrders()
Order billOrder1 = new Order();
billOrder1.addLineItem(new LineItem("shed", 50.0));

// TODO 8: Add 43 gnomes at 7.50 each to the order
// TODO 7: Add 43 gnomes at 7.50 each to the order
for (int i = 0; i < 43; i++)
{
billOrder1.addLineItem(new LineItem("gnome", 7.50));
Expand Down
17 changes: 4 additions & 13 deletions src/test/java/com/gs/collections/kata/Exercise1Test.java
@@ -1,5 +1,5 @@
/*
* Copyright 2011 Goldman Sachs.
* 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.
Expand Down Expand Up @@ -28,14 +28,7 @@ public class Exercise1Test extends CompanyDomainForKata
@Test
public void getCustomerNames()
{
Function<Customer, String> nameFunction = new Function<Customer, String>()
{
@Override
public String valueOf(Customer customer)
{
return customer.getName();
}
};
Function<Customer, String> nameFunction = Customer::getName;

/**
* Get the name of each of the company's customers.
Expand All @@ -51,9 +44,7 @@ public String valueOf(Customer customer)
public void getCustomerCities()
{
/**
* Get the city for each of the company's customers. Use an anonymous inner class. Use the IDE to help you as
* much as possible. Ctrl+space will help you implement an anonymous inner class. Implementing an interface is
* ctrl+i in IntelliJ. Eclipse's ctrl+1 is auto-fix and works to implement interfaces.
* Get the city for each of the company's customers.
*/
MutableList<Customer> customers = this.company.getCustomers();
MutableList<String> customerCities = null;
Expand All @@ -66,7 +57,7 @@ public void getCustomerCities()
public void getLondonCustomers()
{
/**
* Which customers come from London? Get a collection of those which do. Use an anonymous inner class.
* Which customers come from London? Get a collection of those which do.
*/
MutableList<Customer> customers = this.company.getCustomers();
MutableList<Customer> customersFromLondon = null;
Expand Down

0 comments on commit e453c03

Please sign in to comment.