Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a Java code that allows Customers SEARCH for a PlayStation game with title

ID: 3602200 • Letter: W

Question

Write a Java code that allows Customers SEARCH for a PlayStation game with title "FIFA 18 Ronaldo Edition" and RETRIEVE web pages related to the game title from Amazon.com, Bestbuy.com, and Newegg.com, respectively, so as to COMPARE the prices from these three websites, in order for Customers to find the less expensive price related to the game and ultimately purchase the game. The web pages should be viewable like normal web pages from websites, containing all the image(s) and/or text(s) contained on the web page.

Explanation / Answer

import java.util.*;

class ArrayLst

{

    public static void main(String args[])

    {

       List <String> list = new ArrayList<String>();

       list.add("xxxx");

       list.add("FIFA 18 Ronaldo Edition");

       list.add("yyyy");

//Load the list into a hashSet

Set<String> set = new HashSet<String>(list);

if (set.contains("FIFA 18 Ronaldo Edition"))

{

    String searchQuery = "FIFA 18 Ronaldo Edition" ;

     WebClient client = new WebClient(); //HtmlUnit needs a WebClient to make a request. There are //many options (Proxy settings, browser, redirect enabled ...)We are going to disable Javascript since it's //not required for our example, and disabling Javascript makes the page load faster

   client.getOptions().setCssEnabled(false);

     client.getOptions().setJavaScriptEnabled(false);

     try {

       String searchUrl1= "http://amazon.com";

       HtmlPage page = client.getPage(searchUrl1);

       String searchUrl2= "http://BestBuy.com";

       HtmlPage page = client.getPage(searchUrl2);

       String searchUrl3= "http://Newegg.com";

       HtmlPage page = client.getPage(searchUrl3);

       List<HtmlElement> items = (List<HtmlElement>) page.getByXPath("//p[@class='result-info']" );

      if(items.isEmpty()){

        System.out.println("No items found !");

      }else{

      for(HtmlElement item : items){

      HtmlAnchor itemAnchor = ((HtmlAnchor) htmlItem.getFirstByXPath(".//a"));

       String itemName = itemAnchor.asText();

       String itemUrl = itemAnchor.getHrefAttribute() ;

          HtmlElement spanPrice =((HtmlElement) htmlItem.getFirstByXPath(".//span[@class='result-price']")) ;

      // It is possible that an item doesn't have any price

       String itemPrice = spanPrice == null ? "no price" : spanPrice.asText() ;

       System.out.println( String.format("Name : %s Url : %s Price : %s", itemName, itemPrice, itemUrl));

      }

      }catch(Exception e){

         e.printStackTrace();

        }

    }

   }

}

Then instead of just printing the results, we are going to put it in JSON, using Jackson library, to map items in JSON format.

First we need a plain old java object to represent Items

Item.java

public class Item {

    private String title ;

    private Decimal price ;

    private String url ;

//getters and setters

}

Then add this to your pom.xml :

<dependency>

<groupId>com.fasterxml.jackson.core</groupId>

<artifactId>jackson-databind</artifactId>

<version>2.7.0</version>

</dependency>

//we are going to fetch items from website since they don't seem to offer an API, to collect //names, prices and images, and export it to JSON.

//Now all we have to do is create an Item, set its attributes, and convert it to JSON string (or a //file ...), and adapt the previous code a little bit :

for(HtmlElement htmlItem : items){

HtmlAnchor itemAnchor = ((HtmlAnchor) htmlItem.getFirstByXPath(".//span[@class='txt']/span[@class='pl']/a"));

HtmlElement spanPrice = ((HtmlElement) htmlItem.getFirstByXPath(".//span[@class='txt']/span[@class='l2']/span[@class='price']")) ;

// It is possible that an item doesn't have any price, we set the price to 0.0 in this case

String itemPrice = spanPrice == null ? "0.0" : spanPrice.asText() ;

Item item = new Item();

item.setTitle(itemAnchor.asText());

item.setUrl( baseUrl +

itemAnchor.getHrefAttribute());

item.setPrice(new BigDecimal(itemPrice.replace("$", "")));

ObjectMapper mapper = new ObjectMapper();

String jsonString = mapper.writeValueAsString(item) ;

System.out.println(jsonString);

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote