Advertisement
Guest User

Untitled

a guest
May 24th, 2016
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.96 KB | None | 0 0
  1. /**
  2.  * @(#)shopify.java
  3.  *
  4.  * shopify application
  5.  *
  6.  * @Atilla Saadat
  7.  * @version 1.00 2016/1/19
  8.  *
  9.  * Library imported from JSON jar found here: http://central.maven.org/maven2/org/json/json/20151123/json-20151123.jar
  10.  * Knapsack algorithm based on: https://cs.uwaterloo.ca/~mgrzes/code/mg/Knapsack.java
  11.  */
  12.  
  13. import java.util.*;
  14. import java.io.*;
  15. import java.net.URL;
  16. import java.nio.charset.Charset;
  17.  
  18. import org.json.*;
  19.  
  20. public class bench {
  21.  
  22.   //---------------------------------------------------------------------------------------------------
  23.   // JSON Parse and read funtions
  24.   private static String readAll(Reader rd) throws IOException {
  25.     StringBuilder sb = new StringBuilder();
  26.     int cp;
  27.     while ((cp = rd.read()) != -1) {
  28.       sb.append((char) cp);
  29.     }
  30.     return sb.toString();
  31.   }
  32.  
  33.   public static JSONObject readJsonFromUrl(String url) throws IOException, JSONException {
  34.     InputStream is = new URL(url).openStream();
  35.     try {
  36.       BufferedReader rd = new BufferedReader(new InputStreamReader(is, Charset.forName("UTF-8")));
  37.       String jsonText = readAll(rd);
  38.       JSONObject json = new JSONObject(jsonText);
  39.       return json;
  40.     } finally {
  41.       is.close();
  42.     }
  43.   }
  44.  
  45.   //---------------------------------------------------------------------------------------------------
  46.   // Gets Price and Weights of Keyboard and Computer Variants
  47.   public static double findKeyCompTotalPrice( String jsonLink) throws IOException, JSONException{
  48.     JSONArray products = (readJsonFromUrl(jsonLink)).getJSONArray("products");
  49.  
  50.     for(int index = 0; index < products.length(); index++){
  51.       String currentItem = products.getJSONObject(index).getString("product_type");
  52.       if(currentItem.equals("Keyboard")||currentItem.equals("Computer")){ // Search for Keyboard or Computer
  53.         int numOfVariants = products.getJSONObject(index).getJSONArray("variants").length();
  54.         for(int variant = 0; variant < numOfVariants; variant++){
  55.           priceList.add(Double.parseDouble(products.getJSONObject(index).getJSONArray("variants").getJSONObject(variant).getString("price")));
  56.           weightList.add(products.getJSONObject(index).getJSONArray("variants").getJSONObject(variant).getInt("grams"));
  57.         }
  58.       }
  59.     }
  60.     // Return Price of all Keyboard and Computer Variants based of 1/0 Greedy Algorithm
  61.     double x = 0;
  62.     int y = 0;    
  63.     for(int i = 0; i < weightList.size();i++){
  64.         x+=priceList.get(i);
  65.         y+=weightList.get(i);;
  66.     }
  67.     return finalPrice(maxWeight,priceList,weightList);
  68.   }
  69.   //---------------------------------------------------------------------------------------------------
  70.   public static void main(String[] args) throws IOException {
  71.     String jsonLink = "http://resttest.bench.co/transactions/";
  72.    
  73.     //Based on above link, all items should be taken because: all Keyboard and Computer weights < 100kg
  74.     System.out.println(findKeyCompTotalPrice(maxWeight,jsonLink));
  75.   }
  76. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement