id
stringlengths 36
36
| text
stringlengths 1
1.25M
|
---|---|
cbc06297-b7eb-4aae-8d8b-8b187baddf56 | public int[] sort(int[] hand){
int tmp;
for(int i=0;i<hand.length;i++){
for(int j=hand.length-1;j>i;j--){
if(hand[j]>hand[j-1]){
tmp = hand[j];
hand[j] = hand[j-1];
hand[j-1] = tmp;
}
}
}
return hand;
} |
4eea6ce5-defc-474c-9806-253eebe27c56 | public int minBigger(int [] hand, int card){
int index = -1;
for(int i=0;i<hand.length;i++){
if(hand[i]>card){
index = i;
}
}
if(index>=0){
return index;
}
return hand.length-1;
} |
f1cf1d28-9b79-401e-b0af-6ccaf27e1bbf | public int secondBigger(int [] hand){
if(hand.length==0){
return -1;
}
if(hand.length<=2){
return 0;
}
else{
return 1;
}
} |
3b455519-0de6-476b-bf26-57514b041bb5 | public boolean activeChallenge(int[] hand, int win, int lose, int theirPoint, int ourPoint){
// System.out.println("Active: Win:" + winTime + " Lose:" + loseTime + " Tied:" + tiedTime);
double p = 0.0;
double base = 0.4;
if ( theirPoint >= 9 ) {
return true;
}
if ( ourPoint >= 9 ) {
return false;
}
sort(hand);
if ( tiedTime == 0 ) {
if(win==0 && lose==0){
int sum = hand[0] + hand[1] + hand[2];
int boulder = 33;
if ( hand[0] >= 10 && hand[1] >= 10 && hand[2] >= 10
&& sum >= boulder) {
base = 0.4;
p = base + (sum - boulder) * 0.20;
// if ( p >= 1.0 ) strategy = 1;
}
// return false;
}
else if ( win == 1 && lose == 0 ) {
int sum = hand[0] + hand[1];
int boulder = 20;
int decrement = 0;
if ( theirLastCard > 0 && theirLastCard < 6 ) {
decrement = 6 - theirLastCard;
}
if ( sum - decrement >= boulder ) {
base = 0.5;
p = base + (sum - boulder) * 0.1;
}
}
else if ( win == 0 && lose == 1 ) {
// if ( hand.length != 4) {
// System.err.println("W:" + winTime + " L:" + loseTime + " T:" + tiedTime + "\n");
// System.err.println("Hand: ");
// for ( int x : hand ) {
// System.err.print(x + " ");
// }
// }
if ( hand.length != 4 ) {
System.err.println("W:" + winTime + " L:" + loseTime + " T:" + tiedTime + "\n");
System.err.println("Hand: ");
for ( int x : hand ) {
System.err.print(x + " ");
}
System.err.println("\n");
return false;
}
int sum = hand[0] + hand[1] + hand[2];
int boulder = 30;
if ( sum >= boulder ) {
base = 0.3;
p = base + (sum - boulder) * 0.1;
}
}
else if ( win == 2 && lose == 0 ) {
int sum = hand[0];
if ( sum == 13 ) { // must win
return true;
}
int boulder = 10;
if ( sum >= boulder ) {
base = 0.5;
p = base + (sum - boulder) * 0.2;
}
}
else if ( win == 0 && lose == 2 ) {
if ( hand.length != 3 ) {
System.err.println("W:" + winTime + " L:" + loseTime + " T:" + tiedTime + "\n");
System.err.println("Hand: ");
for ( int x : hand ) {
System.err.print(x + " ");
}
System.err.println("\n");
return false;
}
int sum = hand[0] + hand[1] + hand[2];
int boulder = 33;
if ( sum >= boulder ) {
base = 0.8;
p = base + (sum - boulder) * 0.2;
}
}
else if ( win == 1 && lose == 1) {
int sum = hand[0] + hand[1];
int boulder = 22;
if ( sum >= boulder ) {
base = 0.4;
p = base + (sum - boulder) * 0.1;
}
}
else if ( win == 2 && lose == 1) {
int sum = hand[0];
if ( sum == 13 ) { // must win
return true;
}
else {
return false;
}
}
else if ( win == 1 && lose == 2) {
// if ( hand.length != 2) {
// System.err.println("W:" + winTime + " L:" + loseTime + " T:" + tiedTime + "\n");
// System.err.println("Hand: ");
// for ( int x : hand ) {
// System.err.print(x + " ");
// }
// }
if ( hand.length != 2) {
System.err.println("W:" + winTime + " L:" + loseTime + " T:" + tiedTime + "\n");
System.err.println("Hand: ");
for ( int x : hand ) {
System.err.print(x + " ");
}
System.err.println("\n");
return false;
}
int sum = hand[0] + hand[1];
int boulder = 24;
if ( sum >= boulder ) {
base = 0.8;
p = base + (sum - boulder) * 0.2;
}
}
else if ( win == 2 && lose == 2) {
int sum = hand[0];
if ( sum == 13 ) { // must win
return true;
}
int boulder = 11;
if ( sum >= boulder ) {
base = 0.6;
p = base + (sum - boulder) * 0.4;
}
}
}
else if ( tiedTime == 1 ) {
if(win==0 && lose==0){
int sum = hand[0] + hand[1] + hand[2];
int boulder = 32;
if ( sum >= boulder) {
base = 0.4;
p = base + (sum - boulder) * 0.20;
// if ( p >= 1.0 ) strategy = 1;
}
}
else if ( win == 1 && lose == 0 ) {
int sum = hand[0];
if ( sum == 13 ) { // must win
return true;
}
int boulder = 11;
int decrement = 0;
if ( theirLastCard > 0 && theirLastCard < 6 ) {
decrement = 6 - theirLastCard;
}
if ( sum - decrement >= boulder ) {
base = 0.5;
p = base + (sum - boulder) * 0.1;
}
}
else if ( win == 0 && lose == 1 ) {
int sum = hand[0] + hand[1] + hand[2];
int boulder = 30;
if ( sum >= boulder ) {
base = 0.5;
p = base + (sum - boulder) * 0.1;
}
}
else if ( win == 1 && lose == 1) {
int sum = hand[0] + hand[1];
int boulder = 23;
if ( sum >= boulder ) {
base = 0.8;
p = base + (sum - boulder) * 0.1;
}
}
else if ( win == 2) {
return true;
}
else if ( lose == 2 ) {
return false;
}
}
else if ( tiedTime == 2 ) {
if(win==0 && lose==0){
int sum = hand[0] + hand[1];
int boulder = 23;
if ( sum >= boulder) {
base = 0.8;
p = base + (sum - boulder) * 0.10;
}
}
else if ( win == 1 && lose == 0 ) {
int sum = hand[0];
if ( sum == 13 ) { // must win
return true;
}
int boulder = 11;
int decrement = 0;
if ( theirLastCard > 0 && theirLastCard < 6 ) {
decrement = 6 - theirLastCard;
}
if ( sum - decrement >= boulder ) {
base = 0.5;
p = base + (sum - boulder) * 0.1;
}
}
else if ( win == 0 && lose == 1 ) {
int sum = hand[0] + hand[1];
int boulder = 34;
if ( sum >= boulder ) {
return true;
}
}
else if ( win == 1 && lose == 1) {
return false;
}
}
else if ( tiedTime == 3 ) {
if(win==0 && lose==0){
int sum = hand[0] + hand[1];
int boulder = 23;
if ( sum >= boulder) {
base = 0.8;
p = base + (sum - boulder) * 0.10;
}
}
else if ( win == 1 && lose == 0 ) {
return true;
}
else if ( win == 0 && lose == 1 ) {
return false;
}
}
else if ( tiedTime == 4 ) {
if( win == 0 && lose == 0 ) {
int sum = hand[0];
if ( sum == 13 ) return true;
else return false;
}
}
return (p > Math.random()) ? true : false;
} |
cf0a5925-d02d-40fa-abb0-1a12820cb7a0 | public boolean passiveChallenge(int[] hand, int win, int lose, int theirPoint, int ourPoint){
// System.out.println("Passive: Win:" + winTime + " Lose:" + loseTime + " Tied:" + tiedTime);
double p = 0.0;
double base = 0.4;
// return true;
if ( theirPoint >= 9 ) {
return true;
}
sort(hand);
if ( tiedTime == 0 ) {
if(win==0 && lose==0){
int sum = hand[0] + hand[1] + hand[2];
if ( ourPoint < 9 ) {
int boulder = 33;
if ( hand[0] >= 10 && hand[1] >= 10 && hand[2] >= 10
&& sum >= boulder) {
base = 0.4;
p = base + (sum - boulder) * 0.1;
p = p * 0.8;
}
}
else if ( ourPoint == 9 ) {
int boulder = 30;
if ( hand[0] >= 10 && hand[1] >= 10 && hand[2] >= 10 ) {
base = 0.4;
p = base + (sum - boulder) * 0.1;
}
if ( hand[0] >= 13 && hand[1] >= 13 && hand[3] >= 7 ) {
boulder = 33;
base = 0.4;
p = base + (sum - boulder) * 0.2;
}
}
}
else if ( win == 1 && lose == 0 ) {
int sum = hand[0] + hand[1];
int boulder = 23;
if ( sum >= boulder ) {
base = 0.5;
p = base + (sum - boulder) * 0.1;
p = p*1.1;
}
}
else if ( win == 0 && lose == 1 ) {
int sum = hand[0] + hand[1] + hand[2];
int boulder = 35;
if ( sum >= boulder ) {
base = 0.8;
p = base + (sum - boulder) * 0.1;
p = p*0.9;
}
}
else if ( win == 1 && lose == 1) {
int sum = hand[0] + hand[1];
int boulder = 23;
if ( sum >= boulder ) {
base = 0.7;
p = base + (sum - boulder) * 0.1;
}
}
else if ( win == 2 && lose == 0 ) {
int sum = hand[0];
if ( sum == 13 ) { // must win
return true;
}
int boulder = 5;
if ( sum >= boulder ) {
base = 0.5;
p = base + (sum - boulder) * 0.2;
}
}
else if ( win == 0 && lose == 2 ) {
int sum = hand[0] + hand[1] + hand[2];
int boulder = 36;
if ( sum >= boulder ) {
base = 0.8;
p = base + (sum - boulder) * 0.1;
}
}
else if ( win == 2 && lose == 1) {
int sum = hand[0];
if ( sum == 13 ) { // must win
return true;
}
int boulder = 11;
if ( sum >= boulder ) {
base = 0.5;
p = base + (sum - boulder) * 0.25;
}
}
else if ( win == 1 && lose == 2) {
int sum = hand[0] + hand[1];
int boulder = 23;
if ( sum >= boulder ) {
base = 0.5;
p = base + (sum - boulder) * 0.2;
p = p*0.9;
}
}
else if ( win == 2 && lose == 2) {
int sum = hand[0];
if ( sum == 13 ) { // must win
return true;
}
else {
return false;
}
}
}
else if ( tiedTime == 1 ) {
if(win==0 && lose==0){
int sum = hand[0] + hand[1] + hand[2];
if ( ourPoint < 9 ) {
int boulder = 30;
if ( sum >= boulder) {
base = 0.4;
p = base + (sum - boulder) * 0.1;
p = p * 0.8;
}
}
else if ( ourPoint == 9 ) {
int boulder = 30;
if ( hand[0] >= 10 && hand[1] >= 10 && hand[2] >= 10 ) {
base = 0.4;
p = base + (sum - boulder) * 0.1;
}
if ( hand[0] >= 13 && hand[1] >= 13 && hand[3] >= 7 ) {
boulder = 33;
base = 0.4;
p = base + (sum - boulder) * 0.2;
}
}
}
else if ( win == 1 && lose == 0 ) {
int sum = hand[0] + hand[1];
int boulder = 22;
if ( sum >= boulder ) {
base = 0.5;
p = base + (sum - boulder) * 0.1;
p = p*1.1;
}
}
else if ( win == 0 && lose == 1 ) {
int sum = hand[0] + hand[1] + hand[2];
int boulder = 33;
if ( sum >= boulder ) {
base = 0.7;
p = base + (sum - boulder) * 0.1;
p = p*0.9;
}
}
else if ( win == 1 && lose == 1) {
int sum = hand[0] + hand[1];
int boulder = 24;
if ( sum >= boulder ) {
base = 0.8;
p = base + (sum - boulder) * 0.1;
}
}
else if ( win == 2 ) {
return true;
}
else if ( lose == 2 ) {
return false;
}
}
else if ( tiedTime == 2 ) {
if(win==0 && lose==0){
int sum = hand[0] + hand[1];
int boulder = 23;
if (ourPoint >= 9) {
if ( hand[0] >= 10 && hand[1] >= 10 ) {
base = 0.5;
p = base + (sum - boulder) * 0.1;
}
}
else {
if ( sum >= boulder) {
base = 0.7;
p = base + (sum - boulder) * 0.1;
p = p * 0.8;
}
}
if ( ourPoint < 9 ) {
if ( sum >= boulder) {
base = 0.7;
p = base + (sum - boulder) * 0.1;
p = p * 0.8;
}
}
else if ( ourPoint == 9 ) {
if ( hand[0] >= 13 && hand[1] >= 10 ) {
boulder = 33;
base = 0.4;
p = base + (sum - boulder) * 0.2;
}
}
}
else if ( win == 1 && lose == 0 ) {
int sum = hand[0];
if ( sum == 13 ) {
return true;
}
else {
return false;
}
}
else if ( win == 0 && lose == 1 ) {
int sum = hand[0] + hand[1];
int boulder = 36;
if ( sum >= boulder ) {
base = 0.8;
p = base + (sum - boulder) * 0.1;
p = p*0.9;
}
}
else if ( win == 1 && lose == 1) {
int sum = hand[0];
if ( sum == 13 ) {
return true;
}
else {
return false;
}
}
}
else if ( tiedTime == 3 ) {
int sum = hand[0];
if ( sum == 13 ) return true;
else return false;
}
return (p > Math.random()) ? true : false;
} |
6a4506b1-780c-4547-8455-df9b21cd8603 | public JsonSocket(String host, int port) {
this.host = host;
this.port = port;
} |
26c80513-ba50-4506-896a-7a218266cae9 | public void connect() throws IOException {
sock = new Socket(host, port);
// disable nagle's algorithm to prevent latency on small packets
sock.setTcpNoDelay(true);
reader = new DataInputStream(sock.getInputStream());
writer = new DataOutputStream(sock.getOutputStream());
} |
e8cc11cf-5bc9-4eab-9184-e812092817c0 | public Message getMessage() throws Exception {
String jsonMessage = "";
try {
int payloadLen = reader.readInt();
byte[] jsonArr = new byte[payloadLen];
if (reader.read(jsonArr) < 1) {
throw new Exception("server response was truncated");
}
jsonMessage = new String(jsonArr);
}
catch (Exception e) {
System.err.println("Error reading response from server: " + e.toString());
throw e;
}
Message message = MessageFactory.getServerMessage(jsonMessage);
return message;
} |
f09e04aa-f663-4b7a-a133-17558a88358a | public void sendMessage(PlayerMessage response) throws IOException {
String json = MessageFactory.getPlayerMessage(response);
try {
byte[] jsonArr = json.getBytes("US-ASCII");
writer.writeInt(jsonArr.length);
writer.write(jsonArr);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (IOException e) {
throw e;
}
} |
5fee685e-1c71-44ca-872c-b9ac34576d16 | public static Message getServerMessage(String jsonMessage) {
JsonParser parser = new JsonParser();
JsonObject obj = parser.parse(jsonMessage).getAsJsonObject();
String type = gson.fromJson(obj.get("type"), String.class);
if (type.equals("greetings_program")) {
return gson.fromJson(jsonMessage, GreetingMessage.class);
}
else if (type.equals("request")) {
return gson.fromJson(jsonMessage, MoveMessage.class);
}
else if (type.equals("result")) {
return gson.fromJson(jsonMessage, ResultMessage.class);
}
else if (type.equals("error")) {
return gson.fromJson(jsonMessage, ErrorMessage.class);
}
else {
ErrorMessage err = new ErrorMessage();
err.message = "Unknown response type [" + type + "] from server";
return err;
}
} |
7224d1eb-907d-4739-9c59-532e2b419bc8 | public static String getPlayerMessage(PlayerMessage response) {
return gson.toJson(response);
} |
cd50352c-38a4-46dc-96f4-9a5445d5aba4 | @Override
public String toString() {
StringBuilder sb = new StringBuilder("Move Message:\n");
sb.append("\trequest: " + request + "\n");
sb.append("\trequest_id: " + request_id + "\n");
sb.append("\tremaining: " + remaining + "\n");
sb.append(state);
return sb.toString();
} |
0627cbf2-b450-403b-bb80-3e4fdabb2f2d | @Override
public String toString() {
return "Error: " + message + "\n";
} |
d0ae0d8e-a102-4de9-b473-b264c01312d0 | public static int resultType(String type) {
return resType.get(type);
} |
b381f796-0f18-45d5-a185-d5ca98c87e85 | @Override
public String toString() {
StringBuilder sb = new StringBuilder("\ttype: " + type + "\n");
if (by != null) sb.append("\tby: " + by + "\n");
if (reason != null) sb.append("\treason: " + reason + "\n");
if (card != null) sb.append("\tcard: " + card + " (last card played)\n");
return sb.toString();
} |
eb1904e4-4517-4730-ac0b-fd48e25e333e | public PercentageDiscount(final double percent) {
this.setPercentRate(percent);
} |
4bc0311d-00f0-42f1-b595-1667eee41dfc | public void setPercentRate(final double percent) {
percentRate = percent;
} |
186957bc-cc32-48e6-b861-1fd35a29aa37 | public double getPercentRate() {
return percentRate;
} |
1d045ce7-f9c2-493b-917b-c456bf90f232 | @Override
public double getDiscount(double qty, double price) {
return (qty * price * percentRate);
} |
6d227cc3-82cc-450e-b909-fb6090c66121 | @Override
public double getAmountAfterDiscount(double qty, double price) {
return (qty * price) - getDiscount(qty, price);
} |
3b668062-2a06-44c5-8d2f-e8b709611328 | public abstract Product findProduct(String itemId); |
77fe2557-4bda-406a-8bcd-75df283ecba2 | public abstract void retrieveProduct(double qty); |
1b6fe182-2cd8-4e70-a3a8-983776626671 | @Override
public double getDiscount(double qty, double price) {
double discount = 0;
if(qty >= minQty){
discount = (int)(qty / minQty) * price;
}
return discount;
} |
19092c04-90d4-49fb-9de4-49ad064a4c26 | @Override
public double getAmountAfterDiscount(double qty, double price) {
return (qty * price) - getDiscount(qty, price);
} |
cefd6ee9-229c-43a8-a0d2-4676d574f065 | public Receipt() {
lineItems = new LineItem[1];
} |
d5771fd0-a193-4210-90d8-f4ed1646d04f | public LineItem createLineItem(String itemId, double qty) {
lineItems[0].findLineItemProduct(itemId);
lineItems[0].generateLineItem(qty);
return lineItems[0];
} |
3605f5c6-6a29-457a-b592-793b006e28d4 | private void addToArray(final LineItem item) {
LineItem[] tempItems = new LineItem[lineItems.length + 1];
System.arraycopy(lineItems, 0, tempItems, 0, lineItems.length);
tempItems[lineItems.length] = item;
lineItems = tempItems;
} |
136378e2-c7c3-4bdf-802a-59d5a87a0fdb | public static void main(String[] args) {
Receipt receipt1 = new Receipt();
receipt1.addToArray(receipt1.createLineItem("135792", 1));
// Receipt receipt1 = new Receipt();
// receipt1.createLineItem("135792", 1);
// receipt1.createLineItem("234567", 3);
} |
d84999e4-f15d-4478-8f3d-94ef85e7c085 | public LineItem[] getLineItems() {
return lineItems;
} |
8a232db7-4773-42c4-bc5e-c52b0d7102cc | public void setLineItems(LineItem[] lineItems) {
this.lineItems = lineItems;
} |
9203fccd-4d4c-4b13-9556-15a4beea1ebe | public Product(final String id, final String description,
final double price, final DiscountStrategy discountStrategy) {
this.setItemId(id);
this.setDescription(description);
this.setPrice(price);
this.setDiscountStrategy(discountStrategy);
} |
538c1c28-3eb5-46e0-aaac-9ba9285c74b0 | public void setItemId(final String id) {
this.itemId = id;
} |
099a9832-db1d-4fbc-a2e7-0056cea26e2c | public void setDescription(final String description) {
this.description = description;
} |
e30a6264-dc6a-48f5-8c4f-4a83d0814b53 | public void setPrice(final double price) {
this.price = price;
} |
d3a1f222-8421-4bc5-8c77-c85a0b51f50a | public void setDiscountStrategy(final DiscountStrategy discountStrategy) {
this.discountStrategy = discountStrategy;
} |
958d39f1-a0ab-4a39-8f8b-3b391c337211 | public String getItemId() {
return itemId;
} |
afbccac2-dbb4-439a-935f-a504627fd80f | public String getDescription() {
return description;
} |
c2ae290b-3a82-4695-a039-e76d9f7077a8 | public double getPrice() {
return price;
} |
560c5062-2ff6-4516-9f57-bf32952dab19 | public DiscountStrategy discountStrategy() {
return discountStrategy;
} |
a494da5a-a0ff-478c-af91-9c29883cd93d | public void generateDiscount(final double qty) {
double discount = discountStrategy.getDiscount(qty, price);
System.out.print(" " + formatter.format(discount));
} |
d6205ada-92be-42d4-9fee-a28fdd5d1881 | public void generateAmountAfterDiscount(final double qty) {
double amount = discountStrategy.getAmountAfterDiscount(qty, price);
System.out.print(" " + formatter.format(amount) + "\n");
} |
c4feb2e4-4101-4cbb-bd8d-4fcd2249c37e | @Override
public Product findProduct(String itemId) {
for(Product p: products){
if(itemId.equals(p.getItemId())) {
product = p;
break;
}
}
return product;
} |
4987d236-276d-425f-83a3-bc1460e63f86 | @Override
public final void retrieveProduct(double qty) {
System.out.print(product.getItemId());
System.out.print(" " + product.getDescription());
System.out.print(" " + product.getPrice());
product.generateDiscount(qty);
product.generateAmountAfterDiscount(qty);
} |
c92613dd-4577-45a3-b584-304e15a4c251 | public FixedAmountDiscount(double discount) {
this.setAmountDiscount(discount);
} |
fb8dc11d-a4c1-42f2-bb60-4ebe0fdce407 | public void setAmountDiscount(double discount) {
if(discount == 0.0) {
throw new IllegalArgumentException("error: amountDiscount must be greater than zero");
}
this.amountDiscount = discount;
} |
2b6eb4dd-1a34-4580-9c6a-ca23cba4a84e | public double getAmountDiscount() {
return amountDiscount;
} |
c501cd5b-af69-48bb-bd42-8d9faafc14e4 | @Override
public double getDiscount(double qty, double price) {
return (qty * price) - (qty * price - (qty * amountDiscount));
} |
c7dd6435-4711-4cb1-b38e-53c6aebe21ca | @Override
public double getAmountAfterDiscount(double qty, double price) {
return (qty * price) - (qty * amountDiscount);
} |
0a146613-9498-4a16-8cf0-f2172980aab9 | public abstract double getDiscount(double qty, double price); |
64727d88-9bec-4756-8fad-b67a61f5e6a6 | public abstract double getAmountAfterDiscount(double qty, double price); |
46ee7ab5-3fe7-4da6-b373-bef5f742418b | public LineItem(DataStorageStrategy dataStorage) {
this.setDataStorage(dataStorage);
} |
be9447bd-d599-4504-9756-e36bca0e306f | public void setDataStorage(DataStorageStrategy dataStorage) {
this.dataStorage = dataStorage;
} |
bce8dda6-e78d-4981-bfc9-71f6a20e5a25 | public DataStorageStrategy getDataStorage() {
return dataStorage;
} |
5a293f4a-d9d7-49ad-b760-dc0bc5ef95ee | public void findLineItemProduct(String itemId) {
dataStorage.findProduct(itemId);
} |
217ccab7-7122-49fd-8a77-9f04b402c291 | public void generateLineItem(double qty) {
dataStorage.retrieveProduct(qty);
} |
1b51de59-919b-4c67-8145-9b9a6025bd61 | @Override
public double getDiscount(double qty, double price) {
return 0;
} |
e016fe90-b9d7-4493-b2c9-76f58db1181d | @Override
public double getAmountAfterDiscount(double qty, double price) {
return qty * price;
} |
48602c1c-8e47-4601-9064-f3fec2a14c73 | public static void main(String[] args) {
IImageDownload imageDownloader = new ImageDownloader();
// IImageStore imageStore = new StoreFileDbFS();
//
// byte[] image = imageDownloader
// .getImageFromSite("http://liberaldead.com/blog/wp-content/uploads/marveluniverse.jpg");
List<String> images = imageDownloader.getImagesList("http://showusyourass.tumblr.com/");
BufferedOutputStream bufferedWriter = null;
int i = 0;
try {
for (String image : images) {
System.out.println("Downloading "+image);
bufferedWriter = new BufferedOutputStream(new FileOutputStream(
"/Users/admin/Downloads/zjava/" + i + "_" + image.substring(image.length() - 4, image.length())));
bufferedWriter.write(imageDownloader.getImageFromSite(image));
bufferedWriter.close();
i++;
System.out.println("Downloading "+image+" complete");
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("That's it");
} |
d921e20f-70c2-461a-898d-8f616d7eda5b | public static void main(String[] args) {
// ([a-z\-_0-9\/\:\.]*\.(jpg|jpeg|png|gif))/i
// Pattern MY_PATTERN = Pattern.compile("(.*.[jpg|gif])");
Pattern MY_PATTERN = Pattern.compile("(http:[A-Za-z0-9_\\./]*\\.[jpg|jpeg|png|gif]{3,4})");
// Matcher m =
// MY_PATTERN.matcher("<a src=\"http:va_sya/pequacomasdasdwwqskaasdhdppp.jpg\"></a><b>http://vasyapetu.com/asdasdwwqska/asdhd/tttt.gif</b>");
// while (m.find()) {
// String s = m.group(1);
// System.out.println(s);
// }
try {
String pageStr = readPage(new URL(
"http://showusyourass.tumblr.com/"));
Matcher m = MY_PATTERN.matcher(pageStr);
while (m.find()) {
String s = m.group(1);
System.out.println(s);
}
System.out.println("______________");
System.out.println(pageStr);
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
} |
69537813-87dd-447f-acce-019fdd4166e2 | private static String readPage(URL url) throws Exception {
DefaultHttpClient client = new DefaultHttpClient();
HttpGet request = new HttpGet(url.toURI());
HttpResponse response = client.execute(request);
Reader reader = null;
try {
reader = new InputStreamReader(response.getEntity().getContent());
StringBuffer sb = new StringBuffer();
int read;
char[] cbuf = new char[1024];
while ((read = reader.read(cbuf)) != -1) {
sb.append(cbuf, 0, read);
}
return sb.toString();
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
} |
35b05177-ad7b-4eb2-9bf7-51e1b3771a7e | @Override
public void putImageFile(String dir, String fileName, Date date) {
DAOFactory factory = DAOFactory.getDAOFactory(1);
SiteDAO sitedao = factory.getSiteDAO();
Site site = new Site();
site.setSiteName("google.com");
sitedao.insertCustomer(site);
} |
9c5c2d81-458d-4d66-ae08-dc7581a278a5 | private PostgreSQLPooledConnection() {
} |
31d17f58-7de4-4b6d-8abe-1adea065423f | public static PGPoolingDataSource getInstance() {
if (source == null) {
synchronized (PostgreSQLPooledConnection.class) {
if (source == null) {
source = new PGPoolingDataSource();
source.setDataSourceName("PGSQLDataSource");
source.setServerName("localhost");
source.setDatabaseName("postgres");
source.setUser("postgres");
source.setPassword("123456");
source.setMaxConnections(20);
}
}
}
return source;
} |
fecc839d-99e9-43cc-808f-6c8b28099ea9 | public Connection getConnection(Boolean commit) {
try {
conn = PostgreSQLPooledConnection.getInstance().getConnection();
if (commit)
conn.setAutoCommit(true);
else
conn.setAutoCommit(false);
} catch (SQLException e) {
e.printStackTrace();
}
return conn;
} |
93b8ac22-ac72-4204-baef-1de98a364a30 | public void closeConnection() {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
} |
a66612ec-c92d-44d5-9820-f39545b62012 | public ImageProperties() {
} |
c3ae1267-d7df-459d-a1e8-26eccb2f32fe | public ImageProperties(Integer id, Date date, String fileName, String type,
String path, Site linkTo) {
super();
this.id = id;
this.date = date;
this.fileName = fileName;
this.type = type;
this.path = path;
this.linkTo = linkTo;
} |
ef6f02c5-60a3-4e1d-be3d-c09f1860794f | public Integer getId() {
return id;
} |
00bc6a11-aa4c-47d0-ae42-a7a66090c64c | public void setId(Integer id) {
this.id = id;
} |
b48581c2-a327-41ad-99c4-6fed13f1b988 | public Date getDate() {
return date;
} |
f868ecfb-d97c-4a8e-be8f-aa6e0caf0b67 | public void setDate(Date date) {
this.date = date;
} |
c613e19e-c4d9-4b64-bf5c-3546850f845a | public String getFileName() {
return fileName;
} |
d1767c53-a6e4-46a4-843f-d0fbc1bd05a2 | public void setFileName(String fileName) {
this.fileName = fileName;
} |
badc6b47-c7e0-4bcc-b351-59e9ced925cd | public String getType() {
return type;
} |
38f6ef82-8608-48d6-8d33-5e4b983e313f | public void setType(String type) {
this.type = type;
} |
8e1c8352-6b11-4299-8d74-cd87be2f794a | public String getPath() {
return path;
} |
63e93dc2-9a38-4ffb-821e-63b62f3d84bd | public void setPath(String path) {
this.path = path;
} |
96c2b59a-f5d4-44c5-aa1b-6d999ff2a62a | public Site getLinkTo() {
return linkTo;
} |
55e34670-0fb7-445d-8cae-f912717aa881 | public void setLinkTo(Site linkTo) {
this.linkTo = linkTo;
} |
794928af-ba24-4c40-992e-8365bc42cf99 | public Site() {
images = new ArrayList<ImageProperties>();
} |
86d20c8e-b8bd-4bf3-b61f-0097ac6a5355 | public Site(Integer id, String siteName) {
super();
this.id = id;
this.siteName = siteName;
images = new ArrayList<ImageProperties>();
} |
8031c7c2-3ea6-4e5d-ab18-3826f25f1076 | public Integer getId() {
return id;
} |
0ea54a55-6f8e-441a-b276-faf77ec3a818 | public void setId(Integer id) {
this.id = id;
} |
54c1055f-5112-418e-b349-894e92ccf6cf | public String getSiteName() {
return siteName;
} |
25a055b2-1e90-436e-84f6-e93d90c6d927 | public void setSiteName(String siteName) {
this.siteName = siteName;
} |
a6c313ea-988f-46c8-baf7-3048ea9b4cb4 | public List<ImageProperties> getImages() {
return images;
} |
bab9e67d-dd05-417c-88af-9a059ae2da05 | public void setImages(List<ImageProperties> images) {
this.images = images;
} |
fdc65df9-c2a9-4d5f-bdd7-b2173745c32f | public int insertCustomer(T site); |
c8cbd82a-86e8-47ae-9c6b-a79b63d64ea8 | @Override
public int insertCustomer(Site site) {
Connection con = conn.getConnection(true);
try {
PreparedStatement ps = con.prepareStatement("insert into public.site values (?,?)");
ps.setInt(1, site.getId());
ps.setString(2, site.getSiteName());
ps.execute();
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
con.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
return 0;
} |
4844bee6-a8f5-4f15-9bb5-76eed4915f57 | public int insertCustomer(Site site); |
79447a1a-1115-46f5-9155-06c58c716fac | @Override
public SiteDAO getSiteDAO() {
return new PostgreSiteDAO();
} |
72bdd90a-89f8-42d7-b2fc-b579dbda7907 | @Override
public ImagePropertyDAO getImagePropertyDAO() {
// TODO Auto-generated method stub
return null;
} |
d1bbc2c0-fec9-4cf7-836d-1ef55fdc6e7e | public abstract SiteDAO getSiteDAO(); |
814656cf-4fe6-4848-a5a8-9b24caa87b8b | public abstract ImagePropertyDAO getImagePropertyDAO(); |
0b4dedb5-5b4a-4af7-abd3-daadd0b211ee | public static DAOFactory getDAOFactory(int whichFactory) {
switch (whichFactory) {
case POSTGRESQL:
return new PostgreDAOFactory();
default:
return null;
}
} |
d4894b62-f8b7-48af-a2fc-37dee5e390ad | @Override
public byte[] getImageFromSite(String url) {
URL siteUrl = null;
ByteArrayOutputStream out = null;
InputStream in = null;
byte[] response = null;
try {
siteUrl = new URL(url);
in = new BufferedInputStream(siteUrl.openStream());
out = new ByteArrayOutputStream();
byte[] buf = new byte[1024];
int n = 0;
while (-1 != (n = in.read(buf))) {
out.write(buf, 0, n);
}
response = out.toByteArray();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (out != null)
out.close();
if (in != null)
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return response;
} |
77767f50-75aa-4e58-bf3d-3f63d16bd02e | @Override
public List<String> getImagesList(String url) {
ArrayList<String> imgList = new ArrayList<String>();
try {
String pageStr = readPage(new URL(url));
Matcher m = imagePattern.matcher(pageStr);
while (m.find()) {
imgList.add(m.group(1));
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
return imgList;
} |
ea026f19-6787-45f6-b3ff-721c23ed581c | private String readPage(URL url) throws Exception {
DefaultHttpClient client = new DefaultHttpClient();
HttpGet request = new HttpGet(url.toURI());
HttpResponse response = client.execute(request);
Reader reader = null;
try {
reader = new InputStreamReader(response.getEntity().getContent());
StringBuffer sb = new StringBuffer();
int read;
char[] cbuf = new char[1024];
while ((read = reader.read(cbuf)) != -1) {
sb.append(cbuf, 0, read);
}
return sb.toString();
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
} |
7f8e6ed9-40dc-4ac2-93e0-012ccf26fc78 | public byte[] getImageFromSite(String url); |
b863257a-ff44-4358-a682-d8e88a189f99 | public List<String> getImagesList(String url); |