Skip to content

Instantly share code, notes, and snippets.

@iamtrk
Last active December 25, 2015 01:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save iamtrk/6896505 to your computer and use it in GitHub Desktop.
Save iamtrk/6896505 to your computer and use it in GitHub Desktop.
bloomreach Wine selection problem Solution
import java.io.BufferedReader;
import java.io.FileReader;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Hashtable;
public class mmy{
public static void main(String[] args){ int total = 0;
try{
Class.forName("com.mysql.jdbc.Driver");
Connection connect = DriverManager.getConnection("jdbc:mysql://localhost:3306/bloom","root","ravi");
Statement statement = connect.createStatement();
BufferedReader wbr = new BufferedReader(new FileReader("large-wine-req.txt"));
Hashtable<String,Integer> winelist = new Hashtable<String,Integer>(320000000);
String[] index = new String[32000000];
ArrayList<String> dWinelist = new ArrayList<String>();
String x = wbr.readLine();
String query;
int n = 0; int wl=0;
while (x!=null){
winelist.put(x.split(",")[0],wl);
index[wl]= x.split(",")[0];
wl++;
x=wbr.readLine();
}
String q;
ArrayList<Integer> count = new ArrayList<Integer>();
wbr.close();
BufferedReader pbr = new BufferedReader(new FileReader("large-peop-req.txt"));
String y = pbr.readLine();
while (y!=null){
q= y.split(",")[0];
query = "select wine_id from inmem_people_wine WHERE person_id = " +"'" + q + "'" ;
ResultSet resultSet = statement.executeQuery(query);
while (resultSet.next()){
if(winelist.get(resultSet.getString(1))!=null) {
count.add(winelist.get(resultSet.getString(1))); }
}
Collections.sort(count);
if(count.size()>3){
for (int i=0;i<3;i++){
winelist.remove(index[count.get(i)]);
total++;
}
}
else {
for (int j:count){
winelist.remove(index[j]);
total++;
}
}
y=pbr.readLine();
count.clear();
}
pbr.close();
connect.close();
}catch (Exception e){e.printStackTrace();}
System.out.println("Total possible are "+total);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment