Skip to content

Commit

Permalink
Update InsultGenerator.java
Browse files Browse the repository at this point in the history
  • Loading branch information
tzvatot authored Sep 16, 2018
1 parent 7ef3dcf commit 729c6f9
Showing 1 changed file with 32 additions and 9 deletions.
41 changes: 32 additions & 9 deletions src/main/java/org/openshift/InsultGenerator.java
Original file line number Diff line number Diff line change
@@ -1,19 +1,42 @@
package org.openshift;

import java.util.Random;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

public class InsultGenerator {
public String generateInsult() {
String words[][] = {{"Artless", "Bawdy", "Beslubbering"}, {"Base-court", "Bat-fowling", "Beef-witted"}, {"Apple-john", "Baggage", "Barnacle"}};
String vowels = "AEIOU";
String article = "an";
String firstAdjective = words[0][new Random().nextInt(words[0].length)];
String secondAdjective = words[1][new Random().nextInt(words[1].length)];
String noun = words[2][new Random().nextInt(words[2].length)];
if (vowels.indexOf(firstAdjective.charAt(0)) == -1) {
article = "a";
String theInsult = "";

try {
String databaseURL = "jdbc:postgresql://";
databaseURL += System.getenv("POSTGRESQL_SERVICE_HOST");
databaseURL += "/" + System.getenv("POSTGRESQL_DATABASE");

String username = System.getenv("POSTGRESQL_USER");
String password = System.getenv("PGPASSWORD");
Connection connection = DriverManager.getConnection(databaseURL, username, password);

if (connection != null) {
String SQL = "select a.string AS first, b.string AS second, c.string AS noun from short_adjective a , long_adjective b, noun c ORDER BY random() limit 1";
Statement stmt = connection.createStatement();
ResultSet rs = stmt.executeQuery(SQL);
while (rs.next()) {
if (vowels.indexOf(rs.getString("first").charAt(0)) == -1) {
article = "a";
}
theInsult = String.format("Thou art %s %s %s %s!", article, rs.getString("first"),
rs.getString("second"), rs.getString("noun"));
}
rs.close();
connection.close();
}
} catch (Exception e) {
return "Database connection problem!";
}
return String.format("Thou art %s %s %s %s!", article, firstAdjective, secondAdjective, noun);
return theInsult;
}

}

0 comments on commit 729c6f9

Please sign in to comment.