J Hubbard
J Hubbard

Reputation: 107

In Scala, how do I return a list of all object instances with a property set to a certain value

I am using Scala, and would like to be able to select all instances of an object with a property set to a given value. Say I have an Order class and a Order Item class, and I want to select or pull all of the order items with an order id of say 1, and return all of these in a list, how do I do this?

I would like to be able to return a list of order line items that have an order id of say 1

Here is my Line Item class definition: Code:

case class LineItem(val itemId : Int, val orderId:Int, val productId:Int, val quantity:Int)

I then have some order items defined in an object:

Code:

object LineItem {

  val li1 = new LineItem(1, 1, 1, 10)
  val li2 = new LineItem(2, 1, 4, 1)
  val li3 = new LineItem(3, 1, 2, 1)
  val li4 = new LineItem(4, 1, 3, 1)
  val li5 = new LineItem(5, 2, 1, 1)
  val li6 = new LineItem(6, 1, 7, 1)

  var lineItems = Set(li1, li2, li3, li4, li5, li6)

  def findItemsInOrder(ordId:Int) = lineItems.find(_.orderId == ordId)

  }

As you can see 5 of the line items belong to the order with an id of 1. So first a list of orders are printed out for the user to see, then I want the user to be able to select an order to see all the line items within that order, so I would like these line items to be printed out in the shell.

So the order id is inputted by the user: Code:

val orderNo = readLine("Which order would you like to view?").toInt

And then this line: Code:

println("OrderID: " + LineItem.findItemsInOrder(orderNo).get.orderId + ", ProductID: " + LineItem.findItemsInOrder(orderNo).get.productId + ", Quantity: " + LineItem.findItemsInOrder(orderNo).get.quantity)

Only prints out the first line item with an order id of 1.

I have then tried to loop through all line items, like this:

Code:

var currentLineItems = new ListBuffer[LineItem]()

for (item <- LineItem.lineItems){
  val item1: LineItem = LineItem(LineItem.findItemsInOrder(orderNo).get.lineId, LineItem.findItemsInOrder(orderNo).get.orderId, LineItem.findItemsInOrder(orderNo).get.productId, LineItem.findItemsInOrder(orderNo).get.quantity)
  currentLineItems += item1
}

for (item <- currentLineItems ){
  println("OrderID: " + LineItem.findItemsInOrder(orderNo).get.orderId + ", ProductID: " + LineItem.findItemsInOrder(orderNo).get.productId + ", Quantity: " + LineItem.findItemsInOrder(orderNo).get.quantity)
}

But this code just prints out the same line item 6 times.

I would be very grateful for any help received to help solve my problem

Thanks Jackie

Upvotes: 1

Views: 2604

Answers (1)

Yuval Itzchakov
Yuval Itzchakov

Reputation: 149548

Define findItemsInOrder to filter out all elements in the list that match the order id:

def findItemsInOrder(ordId: Int): List[LineItem] = lineItems.filter(_.orderId == ordId)

find will locate the first element matching the id, if found will return Some[T] where T is the element type, else None:

Finds the first element of the sequence satisfying a predicate, if any.

If you have multiple elements which match against the id, you need filter:

Selects all elements of this traversable collection which satisfy a predicate.

Upvotes: 1

Related Questions