Revision: 51688
Updated Code
at October 1, 2011 11:59 by Cur3n4
Updated Code
public LinkedList findOrdersForProduct(Product p) { LinkedList l = new LinkedList(); ArrayList list = getAllOrders(); if (list != null) { int size = list.size(); for (int i=0; i<size; i++) { Order order = (Order) list.get(i); if (order != null && order.getProducts() != null) { if (order.getProducts().contains(p)) l.add(order); } } } return l; } }
Revision: 51687
Updated Code
at October 1, 2011 11:50 by Cur3n4
Updated Code
public LinkedList findOrdersForProduct(Product p) { LinkedList l = new LinkedList(); ArrayList list = getAllOrders(); if (list != null) { Order order = (Order) list.get(i); if (order != null) { int size = list.size(); for (int i=0; i<size; i++) { if (order.getProducts() != null) { if (order.getProducts().contains(p)) l.add(order); } } } } return l; } }
Revision: 51686
Updated Code
at October 1, 2011 11:43 by Cur3n4
Updated Code
public LinkedList findOrdersForProduct(Product p) { LinkedList l = new LinkedList(); ArrayList list = getAllOrders(); if (list != null) { int size = list.size(); for (int i=0; i<size; i++) { if (order.getProducts() != null) { }
Revision: 51685
Updated Code
at October 1, 2011 11:38 by Cur3n4
Updated Code
public LinkedList findOrdersForProduct(Product p) { }
Revision: 51684
Initial Code
Initial URL
Initial Description
Initial Title
Initial Tags
Initial Language
at October 1, 2011 11:20 by Cur3n4
Initial Code
public LinkedList findOrdersForProduct(Product p, boolean debug) { }
Initial URL
Initial Description
The debug parameter doesn't seem like a good idea and it is not used, so it should be removed. list.size() will be evaluated in every loop iteration, so it should be assigned to a variable and the variable should be used in the for loop expression. The first element is always retrieved from the order list. ArrayList allow null elements but the null check should be done before using the order variable, otherwise a NullPointerException will be thrown. A null check should also be added for order.getProducts(). The found elements can be added directly within the inner loop statement, the found variable is not required. The check of product was using reference equality (==), it should be using equals(), the equals implementation of product should also be checked. Also the check won't work unless the product p is the last product in the list. The whole inner loop should be replaced by a contains check. There is no reason to define l as an ArrayList and then create a LinkedList, it should be defined as a LinkedList and be returned. I would suggest having a look to Guava to improve the code below and also writing unit tests as the original code would not work as expected. I would create a public boolean doesOrderContainProduct method in the Order class to simplify this method and improve encapsulation. Also logging something could be useful. Note: the code below if the list of products of an order contains null elements, and p is null, those orders will be returned. This is probably ok but it will be good to double check.
Initial Title
Answer question 4
Initial Tags
Initial Language
Java