Browse Source

Add the code I wrote for my coding challenge when I interviewed with

Zappos.
master
Solomon Peachy 9 years ago
commit
22e762673a
  1. 187
      zappos/ZapposCodingChallenge.java
  2. BIN
      zappos/images/14078-p-DETAILED.jpg
  3. BIN
      zappos/images/15643-p-DETAILED.jpg
  4. BIN
      zappos/images/1632312-p-DETAILED.jpg
  5. BIN
      zappos/images/944325-p-DETAILED.jpg
  6. BIN
      zappos/json-simple-1.1.1.jar
  7. 8
      zappos/skus.txt

187
zappos/ZapposCodingChallenge.java

@ -0,0 +1,187 @@
//package org.shaftnet.zappos;
import java.io.*;
import java.util.*;
import java.net.URL;
import java.net.MalformedURLException;
import org.json.simple.*;
import org.json.simple.parser.*;
/**
* Sample Zappos Coding Challenge
*
* (c) 2013 Solomon Peachy <slp@shaftnet.org>, All Rights Reserved
*
* Some comments:
*
* The nature of this assignment doesn't lend itself well towards
* OO programming as it's inherently procedural. I could set this up to
* be more OO, but it would feel forced. This is actually something that
* would more naturally be implemented using something like perl.
*
* You will need the json-simple jar, available here:
*
* http://json-simple.googlecode.com/files/json-simple-1.1.1.jar
*
* To compile:
* java -cp *jar *java
*
* To run:
* java -cp *jar:. ZapposCodingChallenge filename
*
* Other comments:
*
* This doesn't have the degree of robustness and error reporting I'd like,
* but my Java skills are a little rusty. There are probably better ways of handling
* some of these things, but this is the first Java code I've written in more than
* ten years.
*/
public class ZapposCodingChallenge {
/* Note that these should be configurable, ideally via a config file
or something like that. However, due to time constraints I'm leaving
this as a WIBNI. */
private static final String apiKey = "52ddafbe3ee659bad97fcce7c53592916a6bfd73";
private static final String skuPattern = "\\s+(\\d*).*";
private static final String baseURL = "http://api.zappos.com/Product/";
/**
* This is a dummy instantiation; this class is static.
*/
public ZapposCodingChallenge() {
}
/**
* Let's do this thing!
*/
public static void main(String[] args) {
if (args.length == 0) {
System.out.println("Usage: ZapposCodingChallenge filename");
System.exit(0);
}
String filename = args[0];
/* Initialize a new JSON parser */
JSONParser parser = new JSONParser();
/* Create the images directory */
File f = new File("images");
if (!f.exists()) {
if (!f.mkdir()) {
System.out.println("Can't create images directory!");
System.exit(4);
}
}
/* Open filename */
try {
BufferedReader br = new BufferedReader(new FileReader(filename));
try {
/* Read file in, assuming one line per SKU.. */
while (true) {
String sku = br.readLine();
if (sku == null) {
break;
}
/* Sanitize the SKUs.
This is inefficient; is faster to predefine the regex
and compile it
*/
sku = sku.replaceAll(skuPattern, "$1");
if (sku.length() == 0) {
continue;
}
System.out.println("Attempting to fetch: " + sku);
/* Fetch URL */
StringBuffer buffer = new StringBuffer();
try {
URL url = new URL(baseURL + sku + "?key=" + apiKey);
InputStream is = url.openStream();
int ptr = 0;
while ((ptr = is.read()) != -1) {
buffer.append((char)ptr);
}
is.close();
} catch (MalformedURLException e) {
System.err.println(e);
System.exit(2);
} catch (IOException e) {
System.out.println("Failed to fetch: " + sku);
System.err.println(e);
continue;
}
/* Parse the JSON response */
try {
/* Stupid json library. Wish I had time to find something better. */
ContainerFactory containerFactory = new ContainerFactory(){
public List creatArrayContainer() {
return new LinkedList();
}
public Map createObjectContainer() {
return new LinkedHashMap();
}
};
Map top = (Map)parser.parse(buffer.toString(), containerFactory);
if (top.get("statusCode").toString().equals("200")) {
LinkedList ll = (LinkedList) top.get("product");
Map jo = (Map) ll.get(0);
String defaultImageUrl = (String) jo.get("defaultImageUrl");
/* And now fetch the Image, and drop in the right place.
This doesn't cleanly handle the situation where the file exists already. */
try {
URL url = new URL(defaultImageUrl);
String path = url.getPath();
File fo = new File("images/" + path.substring(path.lastIndexOf("/") + 1));
FileOutputStream fw = new FileOutputStream(fo);
InputStream is = url.openStream();
/* This seems pretty inefficient, but it works. */
byte b[] = new byte[1024];
int c;
while ((c = is.read(b)) > 0) {
fw.write(b, 0, c);
}
fw.close();
is.close();
} catch (MalformedURLException e) {
System.err.println(e);
System.exit(2);
} catch (IOException e) {
System.out.println("Failed to fetch: " + sku);
System.err.println(e);
continue;
}
}
} catch (ParseException e) {
System.out.println("Failed to parse response!");
System.err.println(e);
continue;
}
}
} catch (EOFException e) {
/* We're done, ignore the exception. */
}
br.close();
} catch (IOException e) {
System.err.println(e);
System.exit(1);
}
System.exit(0);
}
}

BIN
zappos/images/14078-p-DETAILED.jpg

Binary file not shown.

After

Width:  |  Height:  |  Size: 17 KiB

BIN
zappos/images/15643-p-DETAILED.jpg

Binary file not shown.

After

Width:  |  Height:  |  Size: 16 KiB

BIN
zappos/images/1632312-p-DETAILED.jpg

Binary file not shown.

After

Width:  |  Height:  |  Size: 36 KiB

BIN
zappos/images/944325-p-DETAILED.jpg

Binary file not shown.

After

Width:  |  Height:  |  Size: 21 KiB

BIN
zappos/json-simple-1.1.1.jar

Binary file not shown.

8
zappos/skus.txt

@ -0,0 +1,8 @@
107999
ZCN24365
7860495436
7860495
7554973
7324204
Loading…
Cancel
Save