forked from gshipley/book-insultapp
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
32 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
||
} |