id stringlengths 36 36 | text stringlengths 1 1.25M |
|---|---|
4428bd21-0c63-4903-ba77-f9df8995ad52 | private void require(Lexer.Token.Type type) throws ParserException {
if (!is(type)) {
throw new ParserException();
}
try {
lexer.readtok();
}
catch (IOException e) {
throw new ParserException();
}
} |
657fc2ff-180e-47b2-96fd-f73fda6cc73b | private boolean is(Lexer.Token.Type t) {
return lexer.curr().t == t;
} |
a5ad74e7-8257-47b4-bf29-52eb3af35d25 | public Token(String s, Type t) {
this.s = s;
this.t = t;
} |
2b572615-dfe0-43f6-857f-5a50ef09ec5b | public String toString() {
return t + "('" + s + "')";
} |
8be6d8a4-34de-4e58-b055-20e3bc110142 | public Lexer(Reader input) throws IOException {
this.input = input;
readch();
readtok();
} |
ad2ebeab-eae8-47f2-84ab-7fdee380f95c | public Token curr() {
return curr;
} |
bc19c40f-fec2-4c52-b048-cf4a58dadef3 | public Token readtok() throws IOException {
curr = _readtok();
return curr;
} |
a940b3bd-ac44-453c-a9a4-6fd95f6213fa | private Token _readtok() throws IOException {
while (Character.isWhitespace(currCh) && !eof) {
readch();
}
if (eof) return EOF;
if (currCh == '(' || currCh == ')') {
char c = currCh;
readch();
return new Token(c + "", Token.Type.SYNCON);
}
else {
String s = "";
do {
s += currCh;
} while (readch() && !Character.isWhitespace(currCh) && currCh != '(' && currCh != ')');
return new Token(s, Token.Type.IDENTIFIER);
}
} |
2c1478fb-547d-4bf3-8f88-1ea34eb5b2f5 | public boolean readch() throws IOException {
int ch = input.read();
if (ch == -1) {
eof = true;
return false;
}
currCh = (char) ch;
return true;
} |
af37fbe4-a412-4f78-86f2-2d5c599ad133 | protected Interpreter getInterpreter() {
return Interpreter.this;
} |
69da5d69-c72c-4a88-ab2e-f7b859192784 | public BPLValue lookupDefault() {
throw new RuntimeException();
} |
0c3dc3a2-bfaf-4a3d-a1b8-5505ca430a76 | public BPLValue apply(List<SExpression> params) {
return this;
} |
64217df0-5f7c-4d30-b594-8686e2823d1e | public BPLString(String val) {
this.val = val;
} |
9eabb652-bd3b-4d69-bbfc-da9e407baec5 | @Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + getInterpreter().hashCode();
result = prime * result + ((val == null) ? 0 : val.hashCode());
return result;
} |
f30631ec-28d2-4ab1-a50c-51418947969b | @Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
BPLString other = (BPLString) obj;
if (!getInterpreter().equals(other.getInterpreter()))
return false;
if (val == null) {
if (other.val != null)
return false;
} else if (!val.equals(other.val))
return false;
return true;
} |
673657db-ec24-4b1c-a927-0d764a271386 | @Override
public String toString() {
return val;
} |
45b6c044-059f-405c-bff5-bf222747726d | @Override
public BPLValue lookupDefault() {
return this;
} |
eed278fb-261a-48e2-aaa8-5271e030549c | public abstract BPLValue apply(List<BPLValue> params); |
93cf5d37-adaa-48c7-aadf-00ea4f364197 | public BPLOperator(Op me) {
this.me = me;
} |
e9cf23e3-e53f-49d6-8adf-a5714de246ba | @Override
public BPLValue apply(List<SExpression> params) {
List<BPLValue> values = new ArrayList<>();
for (SExpression sexpr: params) {
values.add(interpret(sexpr));
}
return me.apply(values);
} |
0dc7fe9d-20d1-46f4-8765-dccba151badf | public BPLValue string(String val) {
return new BPLString(val);
} |
569c7f37-6538-4684-bb9c-f4850548bd0f | public BPLValue real(double val) {
return new BPLString((Math.ulp(val) * 10 * Math.random() + val) + "");
} |
1d5839d8-42a9-4607-81b5-a60b65a1b485 | public BPLValue interpret(SExpression expr) {
return lookup(string(expr.op)).apply(expr.subexprs);
} |
9c594143-9e73-4c95-b890-dd85403852e8 | public BPLValue lookup(BPLValue value) {
if (!variables.containsKey(value)) {
return value.lookupDefault();
}
return variables.get(value);
} |
981f516a-85e2-4caf-85ef-880ab670218e | public void put(BPLValue var, BPLOperator value) {
variables.put(var, value);
} |
575f3931-6384-4576-ab3e-ece03bebc986 | public DefaultInterpreter() {
put(string("+"), new BPLOperator((List<BPLValue> params) -> {
float sum = 0;
for (BPLValue v: params) {
sum += Double.parseDouble(v.toString());
}
return real(sum);
}));
put(string("*"), new BPLOperator((List<BPLValue> params) -> {
float product = 1;
for (BPLValue v: params) {
product *= Double.parseDouble(v.toString());
}
return real(product);
}));
put(string("print"), new BPLOperator((List<BPLValue> params) -> {
for (BPLValue v: params) {
System.out.print(v);
}
System.out.println();
return null;
}));
} |
0b46e44b-d97d-41bf-980b-93546e9aee36 | public PlayKnop(Graphics graph, int x, int y)
{
g = graph;
posX = x;
posY = y;
createImages();
} |
27ab0d5a-5671-48d4-ae01-64d9219383a2 | public void draw()
{
if(playing == true)
{
g.drawImage(stop, posX, posY, 0);
}
else
{
g.drawImage(play, posX, posY, 0);
}
} |
f233a4f3-7df9-4275-a189-9811e90c96c5 | public void createImages()
{
try
{
stop = Image.createImage("/images/buttons/pauze.png");
play = Image.createImage("/images/buttons/play.png");
}
catch(Exception e)
{
e.printStackTrace();
}
} |
d8dcf04a-dcb9-43af-9cf1-8cb11f70c1e8 | public boolean clicked(int x, int y)
{
if(x > posX && x < posX + 70)
{
if(y > posY && y < posY + 70)
{
if(playing == false)
{
playing = true;
}
else
{
playing = false;
}
return true;
}
else
{
return false;
}
}
else
{
return false;
}
} |
7bc1b184-69bb-4bae-aadd-a8f45d30ace9 | public Pad(int xPos, int yPos, Graphics graphics)
{
posX = xPos;
posY = yPos;
color = 0xff00ff;
activated = false;
g = graphics;
createImages();
} |
b198e7d5-c182-4b88-8e3b-6eb14b634a63 | public void draw()
{
//Als deze knop actief is maar de positie van de beatmaker is NIET deze pad
//dan deze afbeelding tekenen
if(activated == true && currentBeat == false)
{
g.drawImage(active, posX, posY, 0);
}
else if(currentBeat == true)
{
//Als positie wel bij deze pad is dan gaat hij de afbeelding met glow tekenen
//2 verschillende afbeeldingen, ligt eraan of de pad op actief staat of niet
if(activated == true)
{
System.out.println("sdfsdf");
g.drawImage(overin, posX, posY, 0);
}
else
{
g.drawImage(overout, posX, posY, 0);
}
}
else if(currentBeat == false)
{
//Deze afbeelding wordt getekend als huidige positie NIET deze pad is, en deze pad niet actief is.
g.drawImage(inactive, posX, posY, 0);
}
} |
a55588cb-90c4-4387-8908-ac2ec824b044 | public void clicked()
{
if(activated == false)
{
activated = true;
}
else
{
activated = false;
}
draw();
} |
c3e7693a-92d0-4793-bcfd-ef1a00a58206 | public void createImages()
{
try
{
active = Image.createImage("/images/button_in(red).png");
inactive = Image.createImage("/images/button(red).png");
overin = Image.createImage("/images/button_in_over(red).png");
overout = Image.createImage("/images/button_over(red).png");
}
catch (IOException e) {
System.out.println("Images not found");
}
} |
5433d2ea-c385-484d-9328-c19b28d703ae | public PlaySound(String fileName)
{
//Player object maken en alles vast pre-fetchen
file = fileName;
try
{
is = getClass().getResourceAsStream(file);
p = Manager.createPlayer(is, "audio/x-wav");
p.realize();
p.prefetch();
} catch (Exception e)
{
e.printStackTrace();
}
} |
7e170a20-f4f0-40ac-aa9b-a9d9720f9bf6 | public void play()
{
try
{
p.start();
}
catch (Exception e)
{
e.printStackTrace();
}
} |
7949d1ce-9a4a-44a2-9bc4-c3d7687b6ab2 | public void stop()
{
try
{
p.stop();
}
catch (Exception e)
{
e.printStackTrace();
}
} |
d9cd2f25-b978-43af-b82d-4be45bb0e3d2 | public mainCanvas()
{
super(true);
g = getGraphics();
} |
ec537c7d-f02b-4afb-bfd0-882390b916cc | public synchronized void start()
{
//Afbeeldingen maken voor de achtergrond en het pijltje wat aangeeft op welke positie
//de beatmaker is
try
{
positie = Image.createImage("/images/pijl.png");
bgimage = Image.createImage("/background.jpg");
}
catch (IOException e) {
System.out.println("Images not found");
}
//Knoppen om van instrument te wissen aanmaken
piano = new InstrumentButton(30,4, g, "piano");
gitaar = new InstrumentButton(30,130, g, "gitaar");
drums = new InstrumentButton(30, 260, g, "overig");
play = new PlayKnop(g, 30, 570);
//standaard krijg je de piano voor je neus dus deze zetten we aan
piano.turnOn();
//DIt is om te controlleren op welke pad we geklikt hebben
action = new TouchActions();
createInstruments();
draw();
playLoop();
} |
2d8573dc-7845-4b4f-9cb0-8be5579f02bb | public void stop() {} |
c22e6b72-c5da-4747-a642-9024f92b492d | public void run() {} |
d9975f99-541c-4275-b51d-057ac53e20b0 | public void playLoop()
{
task = new SoundTimerTask();
gitaarThread = new Thread(task);
gitaarThread.start();
} |
73e82be8-420c-452c-8979-a44b87d56362 | private void createInstruments()
{
String[] samples = {
"/piano/piano1.wav",
"/piano/piano2.wav",
"/piano/piano3.wav",
"/piano/piano4.wav",
"/piano/piano5.wav",
"/gitaar/gitaar1.wav",
"/gitaar/gitaar2.wav",
"/gitaar/gitaar3.wav",
"/gitaar/gitaar4.wav",
"/gitaar/gitaar5.wav",
"/drums/conga.wav",
"/drums/conga2.wav",
"/drums/conga3.wav",
"/drums/bel.wav",
"/drums/bel2.wav"
};
int j = 0;
instruments = new Instrument[15];
for(int i = 0; i < 15; i++)
{
if(j == 5)
{
j = 0;
}
instruments[i] = new Instrument(samples[i], (j+2) * 67, g);
j++;
}
} |
cda31f88-551a-4c3a-ad3a-ddea38c0f8b9 | private void draw()
{
//De achtergrond tekenen
g.drawImage(bgimage, 0, 0, 0);
//De pijl die positie aangeeft tekenen
g.drawImage(positie, 105, count * 87, 0);
//Als piano open staat tekenen we de pads voor de piano
if(piano.active)
{
for(int i = 0; i < 5; i++)
{
instruments[i].draw();
}
}
//Als gitaar open staat de pads voor gitaar tekenen
if(gitaar.active)
{
for(int i = 5; i < 10; i++)
{
instruments[i].draw();
}
}
//Als overig open staat tekenen we daar de pads voor
if(drums.active)
{
for(int i = 10; i < 15; i++)
{
instruments[i].draw();
}
}
piano.drawButton();
gitaar.drawButton();
drums.drawButton();
play.draw();
//Send all graphics from Buffer to Screen
flushGraphics();
} |
016d25ff-c0e8-4f1f-afc6-f887cfcd0fba | public void pointerPressed (int x, int y)
{
//VOlgende if/else statement is voor de knoppen onderaan. De gene die
//aangeklikt wordt moet groen worden, de rest blauw (dat is uit)
if(piano.clicked(x, y))
{
gitaar.turnOff();
drums.turnOff();
}
else if(gitaar.clicked(x, y))
{
piano.turnOff();
drums.turnOff();
}
else if(drums.clicked(x, y))
{
gitaar.turnOff();
piano.turnOff();
}
//Als er op play of pauze gedrukt wordt zorgt deze functie dat playing op true of false komt te staan.
play.clicked(x, y);
//adhv het actieve instrument gaan controlleren op welke pad is gedrukt
if(piano.active)
{
for(int i = 0; i < 5; i++)
{
action.setPads(instruments[i].pads);
if(action.touchCheck(x, y))
{
instruments[i].pads[action.padClicked].clicked();
}
}
}
else if(gitaar.active)
{
for(int i = 5; i < 10; i++)
{
action.setPads(instruments[i].pads);
if(action.touchCheck(x, y))
{
instruments[i].pads[action.padClicked].clicked();
}
}
}
else if(drums.active)
{
for(int i = 10; i < 15; i++)
{
action.setPads(instruments[i].pads);
if(action.touchCheck(x, y))
{
instruments[i].pads[action.padClicked].clicked();
}
}
}
draw();
} |
8ebd3c2d-deae-4939-8df1-7305aef64533 | public final void run()
{
while(true)
{
//Alleen als playing op true staat gaan we hier iets doen
if(play.playing == true)
{
animate();
try
{
Thread.sleep(230);
}
catch(Exception e)
{
System.out.println(e.toString());
}
}
}
} |
7015bfe3-aa36-4564-ae44-925fcac2447a | public void animate()
{
//15 verschillende toonsoorten (van alle 3 de instrumenten) doorlopen
for(int i = 0; i < 15; i++)
{
//De huidige colom op true zetten zodat deze een glow krijgt en het pijltje weet 7
//wat zijn nieuwe positie moet zijn
instruments[i].pads[counter].currentBeat = true;
//Vorige kolom weer op false zetten
if(counter == 0)
{
instruments[i].pads[7].currentBeat = false;
}
else
{
instruments[i].pads[counter - 1].currentBeat = false;
}
//Als pad in huidige kolom aan staat spelen we het geluidje af
if(instruments[i].pads[counter].activated == true)
{
instruments[i].playSample();
}
}
//En alles naar het scherm tekenen
draw();
counter++;
if(counter >= 8)
{
counter = 0;
}
//Deze count var wordt in de hoofd thread gebruikt voor de positie van het pijltje
count = counter;
} |
5fcf6728-6811-4b4b-b393-f2f6001ec2d8 | public InstrumentButton(int xPos, int yPos, Graphics graphics, String btn)
{
posX = xPos;
posY = yPos;
g = graphics;
button = btn;
createImages();
} |
78aae1f6-3803-4613-928f-0d202be867d9 | public void drawButton()
{
if(active == false)
{
g.drawImage(inactivebtn, posX, posY, 0);
}
if(active == true)
{
g.drawImage(activebtn, posX, posY, 0);
}
} |
56d35b84-193e-4a84-9a2d-92834ec03512 | public void turnOff()
{
active = false;
} |
7ec5a47d-9ef7-49a8-9c29-f6afb917ab27 | public void turnOn()
{
active = true;
} |
66fef4d0-00eb-4602-b4e5-5f29f884896f | public boolean clicked(int x, int y)
{
if(x > posX && x < posX + 60)
{
if(y > posY && y < posY + 100)
{
if(active == false)
{
active = true;
}
return true;
}
else
{
return false;
}
}
else
{
return false;
}
} |
83f4c2f8-7d3e-443e-9f5e-207328721999 | public void createImages()
{
try
{
activebtn = Image.createImage("/images/buttons/" + button + "_active.png");
inactivebtn = Image.createImage("/images/buttons/" + button + ".png");
}
catch (IOException e) {
System.out.println("Images not found");
}
} |
fce6d5b8-f0a4-49f3-84e4-880507cd7639 | public Instrument(String sample, int x, Graphics graphics)
{
sampleFile = sample;
g = graphics;
playr = new PlaySound(sampleFile);
posX = x;
createPads();
} |
3b72ce21-2e01-4701-80ca-eed962802809 | public void createPads()
{
pads = new Pad[8];
for(int i = 0; i < 8; i++)
{
pads[i] = new Pad(posX , i * 87 , g);
}
} |
0faddfd3-ef04-4c9a-a628-109d0b17c065 | public void draw()
{
for(int i = 0; i < pads.length; i++)
{
pads[i].draw();
}
} |
fa0d451e-1c83-4531-87a2-5e086acbfd60 | public void playSample()
{
playr.play();
} |
a8944fc7-fef2-4e9b-a57e-ff9e3229360d | public void stopSample()
{
playr.stop();
} |
b7b90753-864b-4d34-9525-17c0b98ade1b | public App()
{
System.out.println("App called...");
beatmaker = new mainCanvas();
display = Display.getDisplay(this);
display.setCurrent(beatmaker);
beatmaker.start();
} |
cf7b4b63-6609-4fa3-a43d-5ff0da1587d4 | protected void startApp()
{
} |
2d59d695-be4d-4175-8c3b-54e79f35481e | protected void pauseApp()
{
System.out.println("pauseApp called...");
} |
4870d78d-5df4-4f25-a529-bb9a527cb4a3 | protected void destroyApp(boolean unconditional)
{
System.out.println("destroyApp called...");
} |
b3b77e50-5402-4621-b98c-4d8eb83d1f83 | public ZapposModifyInputData(){
zapposGetAPIData = new ZapposGetAPIData();
finalOutputList = new ArrayList<ArrayList<Product>>();
productListToWork = new ArrayList<Product>();
getUniqueProductIdMap = new HashMap<Integer, Product>();
getPriceProductCounterMap = new HashMap<Double,Integer>();
jParser = new JSONParser();
} |
3a254dd3-28b8-444f-9545-0875580593d1 | public HashMap<Integer,Product> getUniqueProductIdMap(){
return getUniqueProductIdMap;
} |
eb32e70c-1767-4b34-9317-c9a9993cb509 | public boolean computeData(String response, double startOfRange, double endOfRange, int numberOfGifts){
boolean isEndOfRangeReached = false;
try{
if(!response.equals(null)){
Object resultObject = jParser.parse(response);
JSONObject resultJObject = (JSONObject) resultObject;
JSONArray resultArray = (JSONArray) resultJObject.get("results");
double lastPriceOfProduct = zapposGetAPIData.getPriceOfLastProductOnPage(resultArray);
double productPrice = 0.0;
int productId, styleId;
String productName;
//checks if the price of last product on the page is greater than startRange
if(lastPriceOfProduct > startOfRange){
for(Object resultObj: resultArray){
JSONObject resultJObj = (JSONObject)resultObj;
productPrice = Double.parseDouble(resultJObj.get("price").toString().substring(1));
//the map below helps to store unique products in the input list
int currentPriceCount = getPriceProductCounterMap.containsKey(productPrice)?getPriceProductCounterMap.get(productPrice):0;
getPriceProductCounterMap.put(productPrice,currentPriceCount+1);
// unique products can occur as the number of gifts in the input list
if(getPriceProductCounterMap.get(productPrice)<= numberOfGifts){
// if price of product is greater than start of range
if(productPrice >= startOfRange){
productId = Integer.parseInt(resultJObj.get("productId").toString());
styleId = Integer.parseInt(resultJObj.get("styleId").toString());
productName = resultJObj.get("productName").toString();
Product product = new Product();
product.setPrice(productPrice);
product.setProductId(productId);
product.setStyleId(styleId);
product.setProductName(productName);
//update the map of productId and product data
getUniqueProductIdMap.put(productId, product);
}
}
}
}
//if product price is greater than the range end then stop product collection process.
if(productPrice > endOfRange){
isEndOfRangeReached = true;
}
}else{
isEndOfRangeReached = false;
}
}catch(Exception e){
}
return isEndOfRangeReached;
} |
e0f4d40e-229d-4e57-b808-7c2649dd0df6 | public List<ArrayList<Product>> getPossibleCombinations(HashMap<Integer,Product> productList,int numberOfGifts,double totalTargetPrice){
for(Entry<Integer, Product> entry:getUniqueProductIdMap.entrySet()){
Product currentProduct = entry.getValue();
productListToWork.add(currentProduct);
}
getOutput(new ArrayList<Product>(),numberOfGifts, totalTargetPrice,0);
return finalOutputList;
} |
8a64e939-1f8d-4e14-b1d1-e594e6ac2d9a | public void getOutput(ArrayList<Product> possibleProductList,int numberOfGifts, double totalTargetPrice,int currentCounter){
if(totalTargetPrice < 0 || numberOfGifts <= 0){
return;
}
if(possibleProductList.size() == numberOfGifts){
finalOutputList.add(possibleProductList);
return;
}
for(int i=currentCounter; i<productListToWork.size();i++){
Product currentProduct = productListToWork.get(i);
ArrayList<Product> updatedPossibleProductList = new ArrayList<Product>(possibleProductList);
if(possibleProductList.size() < numberOfGifts){
updatedPossibleProductList.add(currentProduct);
double currentPrice = currentProduct.getPrice();
getOutput(updatedPossibleProductList,numberOfGifts,(totalTargetPrice-currentPrice), ++currentCounter);
}
}
} |
873bbfaa-4e8d-4b12-bae9-3d06c63f02fb | public ZapposGetAPIData(){
jParser = new JSONParser();
} |
b7962386-03b1-49b9-aaa2-2dd2799ab29b | public int getNumOfItemsInRange(){
return numOfItemsinRange;
} |
05c31d83-57fd-46a0-aa66-b12830663010 | public String getHTTPResponseFromZappos(String url){
StringBuffer response = new StringBuffer();
try {
URL obj = new URL(url);
HttpURLConnection connection = (HttpURLConnection) obj.openConnection();
connection.setRequestMethod("GET");
int responseCode = connection.getResponseCode();
if(responseCode == 200){
BufferedReader in = new BufferedReader(
new InputStreamReader(connection.getInputStream()));
String inputLine;
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();
if (connection != null) {
connection.disconnect();
}
}else{
response.append(PROBLEM);
}
}catch(Exception e){
System.out.println("Got an exception at fetching Price range: "+ e);
}
return response.toString();
} |
bb157454-0cb2-448c-8663-4e0f045e3d42 | public String getActualRangeBasedOnInputCost(String response,double cost){
String actualRange = null;
try{
JSONObject jObject = (JSONObject) jParser.parse(response);
JSONArray priceFacetArray = (JSONArray) jObject.get("facets");
JSONArray facetValuesArray = null;
for(Object priceFacet: priceFacetArray){
JSONObject priceObject = (JSONObject) priceFacet;
facetValuesArray = (JSONArray)priceObject.get("values");
}
if(cost <= 50.0){
actualRange = "$50.00 and Under";
}else if(cost > 50.0 && cost <= 100.0){
actualRange = "$100.00 and Under";
}else if(cost > 100.0 && cost <= 200.0){
actualRange = "$200.00 and Under";
}else if(cost >= 200.0){
actualRange = "$200.00 and Over";
}
for(Object v : facetValuesArray){
JSONObject value_obj = (JSONObject) v;
if (value_obj.get("name").toString().equals(actualRange)){
numOfItemsinRange = Integer.parseInt(value_obj.get("count").toString());
}
}
}catch(Exception e){
System.out.println("Error in getting actual range based on cost"+ e);
actualRange = PROBLEM;
}
return actualRange;
} |
d3a67afa-ed74-4a7c-9319-a426a5ec136c | public JSONArray getResultJSONArray(String response){
JSONArray resultArray = null;
try{
if(response!=null){
Object responseObject = jParser.parse(response);
JSONObject jResponseObject = (JSONObject) responseObject;
resultArray = (JSONArray) jResponseObject.get("results");
}
}catch(Exception e){
System.out.println("Error in getting parsing the response"+ e);
}
return resultArray;
} |
143a78f4-f9b4-4ffc-9811-bbfeee920edc | public double getPriceOfFirstProductOnPage(JSONArray resultArray){
double price = 0;
Object firstResult = resultArray.get(0);
JSONObject firstResultJSON = (JSONObject) firstResult;
price = Double.parseDouble(firstResultJSON.get("price").toString().substring(1));
return price;
} |
32d3321b-b464-4434-8aac-da3a72a1a8be | public double getPriceOfLastProductOnPage(JSONArray resultArray){
double price = 0;
int numberOfProductsOnPage = resultArray.size();
Object lastProductResult = resultArray.get(numberOfProductsOnPage-1);
JSONObject firstResultJSON = (JSONObject) lastProductResult;
price = Double.parseDouble(firstResultJSON.get("price").toString().substring(1));
return price;
} |
fc9a3892-9df3-4f76-a279-fc2b1ada2558 | public static void main(String[] args) {
int numberOfGifts;
double totalCostOfGifts;
// flag to check if any result is found in first iteration else increase range.
boolean isResultFound = false;
ZapposGetAPIData zapposGetAPIData = new ZapposGetAPIData();
ZapposModifyInputData zapposModifyInputData = new ZapposModifyInputData();
// Take user inputs from the user
Scanner inputParams = new Scanner(System.in);
System.out.println("Enter the number of gifts...");
numberOfGifts = Integer.parseInt(inputParams.next());
System.out.println("Enter the total cost of gifts...");
totalCostOfGifts = Integer.parseInt(inputParams.next());
double average = totalCostOfGifts/numberOfGifts;
double limit = 0.0;
do{
//calculate start and end of range to search
double upperLimit = average + limit;
double lowerLimit = average - limit;
double startOfRange = lowerLimit > 0 ? lowerLimit: 0;
double endOfRange = lowerLimit <= totalCostOfGifts? lowerLimit: totalCostOfGifts;
// get different price ranges from Zappos API
String priceRangeURL = "http://api.zappos.com/Search?includes=[%22facets%22]&excludes=[%22results%22]&facets=[%22priceFacet%22]&key="+zapposAPIKey;
String priceRangeURLResponse = zapposGetAPIData.getHTTPResponseFromZappos(priceRangeURL);
if(priceRangeURLResponse.equals(PROBLEM)){
System.out.println("Problem in connecting to Internet");
}else{
// Get the actual range of a data based on total target Cost
String ActualRangeBasedOnTotalCost = zapposGetAPIData.getActualRangeBasedOnInputCost(priceRangeURLResponse,totalCostOfGifts);
if(ActualRangeBasedOnTotalCost.equals(PROBLEM)){
System.out.println("Check internet connection...");
}else{
// get total number of pages based on total number of products
int totalNumberOfProducts = zapposGetAPIData.getNumOfItemsInRange();
int totalNumberOfPages = (int) Math.ceil(totalNumberOfProducts/100);
/*
* Get page number to start looking for relative products.
* This is to minimize the search from page 0 and start searching from closest pages to value.
*/
ZapposUtilityFunction zapposUtilityFunction = new ZapposUtilityFunction();
int pageNum = zapposUtilityFunction.getStartPageFromRange(ActualRangeBasedOnTotalCost, startOfRange);
for(int i=0; i<= totalNumberOfPages ; i++){
//loop through pages and compute the necessary product data
String productURLPage = "http://api.zappos.com/Search?term=&filters={\"priceFacet\":[\""+ ActualRangeBasedOnTotalCost + "\"]}&key="+zapposAPIKey+"&limit=100&page=" + i +"&sort={%22price%22:%22asc%22}";
String response = zapposGetAPIData.getHTTPResponseFromZappos(productURLPage);
if(zapposModifyInputData.computeData(response,startOfRange,endOfRange,numberOfGifts)){
break;
}
}
//get map of unique product ids and product data necessary for computation
HashMap<Integer,Product> productList = zapposModifyInputData.getUniqueProductIdMap();
//gets all possible combinations based on data and inputs
List<ArrayList<Product>> resultProductList = zapposModifyInputData.getPossibleCombinations(productList,numberOfGifts,totalCostOfGifts);
if(!resultProductList.isEmpty()){
//display list of products found so far
zapposUtilityFunction.displayProducts(resultProductList);
isResultFound = true;
}else{
//increase limit of search by 0.5 cents and continue the search.
limit += 0.5;
}
}
}
// repeat if the result list calculated is empty
}while(!isResultFound);
} |
74ab66bb-cc25-49d5-821e-bf3c2132d313 | public ZapposUtilityFunction(){
zapposGetAPIData = new ZapposGetAPIData();
RangePageNumbersMap = new HashMap<String,Integer>();
} |
cfd19ab2-cefb-4522-85c9-1f65541d52ce | public void updateRangePageNumbersMap(){
RangePageNumbersMap.put("$50.00 and Under", 458);
RangePageNumbersMap.put("$100.00 and Under", 936);
RangePageNumbersMap.put("$200.00 and Under", 1230);
RangePageNumbersMap.put("$200.00 and Over", 160);
} |
34cc8387-e375-441d-9c3a-6ac3b8c68798 | public int getStartPageFromRange(String rangeBasedOnCost,double startRange){
int page = 0;
updateRangePageNumbersMap();
int totalNumOfPages = RangePageNumbersMap.get(rangeBasedOnCost);
for(int i=0; i<=totalNumOfPages; i+=25){
String urlString = "http://api.zappos.com/Search?term=&filters={\"priceFacet\":[\""+ rangeBasedOnCost + "\"]}&key="+zapposAPIKey+"&limit=100&page=" + i +"&sort={%22price%22:%22asc%22}";
String response = zapposGetAPIData.getHTTPResponseFromZappos(urlString);
if(!response.equals(null)){
double priceOfFirstItem = zapposGetAPIData.getPriceOfFirstProductOnPage(zapposGetAPIData.getResultJSONArray(response));
if(startRange<=priceOfFirstItem){
page = i-25;
break;
}
}
}
return page;
} |
ae8be597-3a6d-42b5-944d-2738b1746e30 | public void displayProducts(List<ArrayList<Product>> list){
for(int i=0; i<list.size();i++){
System.out.println("Unique product list");
ArrayList<Product> combList = list.get(i);
for(int j=0;j<combList.size(); j++){
Product p = combList.get(j);
p.displayProductInformation();
}
}
} |
7155ff24-8343-4e57-9edd-cee3d2b92a23 | public Product(int productId, int styleId, String productName, double price){
this.productId = productId;
this.styleId = styleId;
this.productName = productName;
this.price = price;
} |
7c98bc17-c1cc-48e4-a384-304ae15bb56e | public Product(){
this.productId = 0;
this.styleId = 0;
this.productName = "null";
this.price = 0.0;
} |
aca0d0b6-6901-44a0-9278-299f51e89f3a | public int getProductId() {
return productId;
} |
14713d96-3338-403c-89db-5da7f1774fc6 | public void setProductId(int productId) {
this.productId = productId;
} |
48f10140-ac27-4b0d-a633-5155a3eddd29 | public int getStyleId() {
return styleId;
} |
334d50a4-56d6-4382-a571-cd25f57f0fed | public void setStyleId(int styleId) {
this.styleId = styleId;
} |
c531bdd8-5b73-4df6-af4a-6ebf7c0239ab | public String getProductName() {
return productName;
} |
532c268c-645c-40f9-9883-f89960d1939a | public void setProductName(String productName) {
this.productName = productName;
} |
7e4c1f7a-b0d1-4982-80cd-e4aae1fd173a | public double getPrice() {
return price;
} |
f4bef985-16b0-40d1-a4f3-3ce6ff29e9f4 | public void setPrice(double price) {
this.price = price;
} |
0500d0cb-41b9-4282-b2df-52066229b7f6 | public void displayProductInformation(){
System.out.println(this.productName);
System.out.println(this.productId);
System.out.println(this.styleId);
System.out.println(this.price);
} |
1479f2b4-da3c-4b01-9d70-3baa6cc5f75f | private static String getFilenameGuid()
{
// Value to use a Windows or Unix like filesystem.
// Set to false if you use a Mac or linux variant.
boolean WindowsOS = false;
String path = System.getProperty("user.home");
if(WindowsOS){
return path + "\\" + UUID.randomUUID().toString() + ".txt";
}else{
return path + "/" + UUID.randomUUID().toString() + ".txt";
}
} |
58042515-0f66-4f08-a897-55f5eda043d0 | @Before
public void setUp()
{
//REPLACE WordPairDemoV1 WITH YOUR OWN CLASS THAT IMPLEMENTS THE INTERFACE
wordPairDemo = new SolidControl(); // !!!!! REPLACE HERE !!!!!
assertTrue(wordPairDemo.size() == 0);
wordPairDemo.add("hest", "horse"); //We trust the add() method here!
wordPairDemo.add("hus", "house");
wordPairDemo.add("bord", "table");
} |
2b0e6c78-b1e5-412c-9c07-8a8f6f645a51 | @After
public void tearDown()
{
} |
1ee688bc-481b-4f0d-860e-ec75cad26055 | @Test
/**
* This test requires that both save and load are implemented, since the
* test strategy is: Save the file, clear the content of the list, and read
* the file back in and check the result.
*/
public void testLoad()
{
assertTrue(wordPairDemo.save(fileName));
wordPairDemo.clear();
assertTrue(wordPairDemo.load(fileName));
assertTrue(wordPairDemo.size() == 3);
assertEquals(wordPairDemo.lookup("hest"), "horse");
assertEquals(wordPairDemo.lookup("hus"), "house");
assertEquals(wordPairDemo.lookup("bord"), "table");
} |
8dd8d5c5-3614-4351-b02c-02168b82f929 | @Test
/**
* This test is redundant (covered by testLoad).
*/
public void testSave()
{
} |
de215021-b25d-459e-8a43-769b7bc98921 | @Test
public void testLookup_1()
{
assertEquals("Looked up hest. Expected: horse. Got: " + wordPairDemo.lookup("hest"),
wordPairDemo.lookup("hest"), "horse");
} |
d0c99dc5-0329-4fbe-b779-acec067d05a9 | @Test
public void testLookup_SecondWord()
{
assertNotSame("Expected: something different from Cow. Got: " + wordPairDemo.lookup("hest"),
wordPairDemo.lookup("hest"), "Cow");
} |
d117a9d1-c103-45b6-9750-cfd173f42d52 | @Test
public void testClear()
{
wordPairDemo.clear();
int collectionSize = wordPairDemo.size();
assertTrue("Cleared the collection of word pairs. Expected size: 0. Got size: " + collectionSize, collectionSize == 0);
} |
810e5912-ffc5-450c-95af-72741a4e5a7b | @Test
public void testAdd_4()
{
wordPairDemo.add("ko", "cow");
int resultSize = wordPairDemo.size();
assertTrue("Added a 4th wordpair. Expected size: 4. Got size: " + resultSize, resultSize == 4);
assertEquals(wordPairDemo.lookup("ko"), "cow");
} |
7446b451-5c2a-4d9a-9f6e-88555d0d7621 | @Test
public void testAdd_GotItRight()
{
wordPairDemo.add("ko", "cow");
String lookUpResult = wordPairDemo.lookup("ko");
assertEquals("Added wordpair ko-cow and called loookup(). Expected cow. Got: " + lookUpResult, lookUpResult, "cow");
} |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.