I am new to programming and selenium webdriver, i am trying to do the below in website, could you please help

Step1. select a random item from <https://www.o2.co.uk/shop/smartwatches/#sort=content.sorting.featured&page=1>

which when clicked will navigate to <https://www.o2.co.uk/shop/smartwatches/samsung/gear-s2/#contractType=nonconnected>

here i need to check whether the item is instock or out of stock

if its out of stock, i need to comeback to Step1 

and select next item.

Can anyone pls help either with function in java?