init
This commit is contained in:
commit
9c7f39c937
BIN
lib/gson-2.8.5.jar
Normal file
BIN
lib/gson-2.8.5.jar
Normal file
Binary file not shown.
BIN
lib/javax.annotation.jar
Normal file
BIN
lib/javax.annotation.jar
Normal file
Binary file not shown.
BIN
lib/javax.ejb.jar
Normal file
BIN
lib/javax.ejb.jar
Normal file
Binary file not shown.
BIN
lib/javax.jms.jar
Normal file
BIN
lib/javax.jms.jar
Normal file
Binary file not shown.
BIN
lib/javax.persistence.jar
Normal file
BIN
lib/javax.persistence.jar
Normal file
Binary file not shown.
BIN
lib/javax.resource.jar
Normal file
BIN
lib/javax.resource.jar
Normal file
Binary file not shown.
BIN
lib/javax.servlet.jar
Normal file
BIN
lib/javax.servlet.jar
Normal file
Binary file not shown.
BIN
lib/javax.servlet.jsp.jar
Normal file
BIN
lib/javax.servlet.jsp.jar
Normal file
Binary file not shown.
BIN
lib/javax.transaction.jar
Normal file
BIN
lib/javax.transaction.jar
Normal file
Binary file not shown.
BIN
lib/jstl-1.2.jar
Normal file
BIN
lib/jstl-1.2.jar
Normal file
Binary file not shown.
93
src/com/arnaudpc/Anagram.java
Normal file
93
src/com/arnaudpc/Anagram.java
Normal file
@ -0,0 +1,93 @@
|
|||||||
|
package com.arnaudpc;
|
||||||
|
|
||||||
|
import com.google.gson.Gson;
|
||||||
|
|
||||||
|
import javax.servlet.ServletException;
|
||||||
|
import javax.servlet.annotation.WebServlet;
|
||||||
|
import javax.servlet.http.HttpServlet;
|
||||||
|
import javax.servlet.http.HttpServletRequest;
|
||||||
|
import javax.servlet.http.HttpServletResponse;
|
||||||
|
import java.io.*;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Scanner;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* User: ministicraft
|
||||||
|
* Date: 04/03/2019 08:49
|
||||||
|
*/
|
||||||
|
@WebServlet("/anagram")
|
||||||
|
public class Anagram extends HttpServlet {
|
||||||
|
private static final long serialVersionUID = 1L;
|
||||||
|
private List words = loadWords();
|
||||||
|
|
||||||
|
public Anagram() {
|
||||||
|
super();
|
||||||
|
}
|
||||||
|
|
||||||
|
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
|
||||||
|
PrintWriter out = response.getWriter();
|
||||||
|
String letters = request.getParameter("letters");
|
||||||
|
List anagrams = getAnagrams(words, letters);
|
||||||
|
Gson gson = new Gson();
|
||||||
|
Return returnObj = new Return(anagrams,letters);
|
||||||
|
|
||||||
|
out.println(gson.toJson(returnObj));
|
||||||
|
}
|
||||||
|
|
||||||
|
private List<String> loadWords() {
|
||||||
|
Scanner s = null;
|
||||||
|
List<String> list = new ArrayList<String>();
|
||||||
|
|
||||||
|
s = new Scanner(this.getClass().getClassLoader().getResourceAsStream("/ods5.txt"));
|
||||||
|
while (s.hasNext()){
|
||||||
|
list.add(s.next());
|
||||||
|
}
|
||||||
|
s.close();
|
||||||
|
|
||||||
|
return list;
|
||||||
|
}
|
||||||
|
|
||||||
|
private List<String> getAnagrams(List<String> words, String letters) {
|
||||||
|
List<String> anagrams = new ArrayList<>();
|
||||||
|
for (String word : words) {
|
||||||
|
if(areAnagram(letters.toCharArray(), word.toCharArray())) anagrams.add(word);
|
||||||
|
}
|
||||||
|
return anagrams;
|
||||||
|
}
|
||||||
|
|
||||||
|
static boolean areAnagram(char[] str1, char[] str2)
|
||||||
|
{
|
||||||
|
// Get lenghts of both strings
|
||||||
|
int n1 = str1.length;
|
||||||
|
int n2 = str2.length;
|
||||||
|
|
||||||
|
// If length of both strings is not same,
|
||||||
|
// then they cannot be anagram
|
||||||
|
if (n1 != n2)
|
||||||
|
return false;
|
||||||
|
|
||||||
|
// Sort both strings
|
||||||
|
Arrays.sort(str1);
|
||||||
|
Arrays.sort(str2);
|
||||||
|
|
||||||
|
// Compare sorted strings
|
||||||
|
for (int i = 0; i < n1; i++)
|
||||||
|
if (str1[i] != str2[i])
|
||||||
|
return false;
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
private class Return {
|
||||||
|
private List<String> anagrams;
|
||||||
|
private String letters;
|
||||||
|
|
||||||
|
public Return(List<String> anagrams, String letters) {
|
||||||
|
this.anagrams = anagrams;
|
||||||
|
this.letters = letters;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
185
src/com/arnaudpc/Requests.java
Normal file
185
src/com/arnaudpc/Requests.java
Normal file
@ -0,0 +1,185 @@
|
|||||||
|
package com.arnaudpc;
|
||||||
|
import java.sql.*;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This class details all methods useful to get data from the data base
|
||||||
|
*/
|
||||||
|
|
||||||
|
public class Requests {
|
||||||
|
|
||||||
|
private static final long serialVersionUID = 1L;
|
||||||
|
|
||||||
|
private String url = "jdbc:mysql://192.168.2.5/FromagesVins";
|
||||||
|
private String user = "user";
|
||||||
|
private String passwd = "85N8C34MfFNVbcvS";
|
||||||
|
private String driver = "com.mysql.jdbc.Driver";
|
||||||
|
|
||||||
|
private Utils U;
|
||||||
|
private String wineList[];
|
||||||
|
private String cheeseList[];
|
||||||
|
|
||||||
|
|
||||||
|
public Requests() {
|
||||||
|
Utils U = new Utils();
|
||||||
|
wineList = new String[500];
|
||||||
|
cheeseList = new String[500];
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This method returns the number of cheeses available in the data base
|
||||||
|
* <p>
|
||||||
|
* @return an int
|
||||||
|
*/
|
||||||
|
|
||||||
|
public int numberOfCheeses() {
|
||||||
|
Connection con = null;
|
||||||
|
Statement st = null;
|
||||||
|
ResultSet rs = null;
|
||||||
|
cheeseList[0] = "Data base is empty";
|
||||||
|
int i = 0;
|
||||||
|
try {
|
||||||
|
Class.forName(driver).newInstance();
|
||||||
|
con = DriverManager.getConnection(url, user, passwd);
|
||||||
|
st = con.createStatement();
|
||||||
|
rs = st.executeQuery("SELECT nom FROM fromages");
|
||||||
|
while(rs.next()) {
|
||||||
|
cheeseList[i] = Utils.stripSlashes(rs.getString(1));
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (Exception e) {
|
||||||
|
System.err.println("listCheeses() Exception: " + e.getMessage());
|
||||||
|
}
|
||||||
|
finally {
|
||||||
|
try {
|
||||||
|
if(rs != null) rs.close();
|
||||||
|
if(st != null) st.close();
|
||||||
|
if(con != null) con.close();
|
||||||
|
}
|
||||||
|
catch (SQLException e) {
|
||||||
|
System.err.println("listCheeses() SQLException: " + e.getMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
System.gc();
|
||||||
|
return(i);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This method returns the number of wines according to a chosen cheese
|
||||||
|
* and a given wine color (red or white)
|
||||||
|
* <p>
|
||||||
|
* @param cheese A chosen cheese
|
||||||
|
* @param wineColor A given color for wine
|
||||||
|
* @return an int
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
|
public int numberOfSuggestedWines(String cheese, String wineColor) {
|
||||||
|
Connection con = null;
|
||||||
|
Statement st = null;
|
||||||
|
ResultSet rs = null;
|
||||||
|
Statement st2 = null;
|
||||||
|
ResultSet rs2 = null;
|
||||||
|
String relation = null;
|
||||||
|
String table = null;
|
||||||
|
int i = 0;
|
||||||
|
int id = 0;
|
||||||
|
switch (wineColor) {
|
||||||
|
case "white":
|
||||||
|
relation = "vb";
|
||||||
|
table = "vinblancs";
|
||||||
|
break;
|
||||||
|
case "red":
|
||||||
|
relation = "vr";
|
||||||
|
table = "vinrouges";
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
i = 0;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
Class.forName(driver).newInstance();
|
||||||
|
con = DriverManager.getConnection(url, user, passwd);
|
||||||
|
st = con.createStatement();
|
||||||
|
System.out.println("SELECT r.id"+relation+" FROM "+relation+" r, fromages f WHERE f.nom ='"+ Utils.addSlashes(cheese)+"' AND f.id=r.idf");
|
||||||
|
rs = st.executeQuery("SELECT r.id"+relation+" FROM "+relation+" r, fromages f WHERE f.nom ='"+ Utils.addSlashes(cheese)+"' AND f.id=r.idf");
|
||||||
|
while(rs.next()) {
|
||||||
|
id = rs.getInt(1);
|
||||||
|
st2 = con.createStatement();
|
||||||
|
System.out.println("SELECT nom FROM "+table+" WHERE id ='"+id+"'");
|
||||||
|
rs2 = st2.executeQuery("SELECT nom FROM "+table+" WHERE id ='"+id+"'");
|
||||||
|
while(rs2.next()) {
|
||||||
|
wineList[i] = rs2.getString(1);
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (Exception e) {
|
||||||
|
System.err.println("numberOfSuggestedWines() Exception: " + e.getMessage());
|
||||||
|
|
||||||
|
}
|
||||||
|
finally {
|
||||||
|
try {
|
||||||
|
if(rs2 != null) rs.close();
|
||||||
|
if(st2 != null) st.close();
|
||||||
|
if(rs != null) rs.close();
|
||||||
|
if(st != null) st.close();
|
||||||
|
if(con != null) con.close();
|
||||||
|
}
|
||||||
|
catch (SQLException e) {
|
||||||
|
System.err.println("numberOfSuggestedWines() SQLException: " + e.getMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
System.gc();
|
||||||
|
return(i);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This method returns all cheeses available in the data base
|
||||||
|
* <p>
|
||||||
|
* @return an array of Strings
|
||||||
|
*/
|
||||||
|
|
||||||
|
public String[] listCheeses() {
|
||||||
|
String myList[];
|
||||||
|
int numberOfChesses=0;
|
||||||
|
try {
|
||||||
|
numberOfChesses = this.numberOfCheeses();
|
||||||
|
}
|
||||||
|
catch (Exception ex) {
|
||||||
|
System.out.println("listCheeses(), DataBase request, Exeception "+ ex.getMessage());
|
||||||
|
}
|
||||||
|
myList = new String[numberOfChesses];
|
||||||
|
|
||||||
|
System.arraycopy(cheeseList, 0, myList, 0, numberOfChesses);
|
||||||
|
return(myList);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This method returns some wines according to a chosen cheese and a wine color (red or white)
|
||||||
|
* <p>
|
||||||
|
* @param cheese A chosen cheese
|
||||||
|
* @param wineColor A given color for wine
|
||||||
|
* @return an array of Strings
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
|
public String[] suggestWines(String cheese, String wineColor) {
|
||||||
|
String myList[];
|
||||||
|
int numberOfWines=0;
|
||||||
|
try {
|
||||||
|
numberOfWines = this.numberOfSuggestedWines(cheese, wineColor);
|
||||||
|
}
|
||||||
|
catch (Exception ex) {
|
||||||
|
System.out.println("suggestWines(), DataBase request, Exeception "+ ex.getMessage());
|
||||||
|
}
|
||||||
|
System.out.println("numberOfWines "+ numberOfWines);
|
||||||
|
myList = new String[numberOfWines];
|
||||||
|
|
||||||
|
for (int i = 0; i<numberOfWines; i++) {
|
||||||
|
myList[i] = wineList[i];
|
||||||
|
}
|
||||||
|
return(myList);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
36
src/com/arnaudpc/Utils.java
Normal file
36
src/com/arnaudpc/Utils.java
Normal file
@ -0,0 +1,36 @@
|
|||||||
|
package com.arnaudpc;
|
||||||
|
public class Utils {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Cette méthode retourne la chaîne de caractères, après avoir échappé tous les caractères qui doivent l'être,<br>
|
||||||
|
* pour être utilisée dans une requête de base de données.
|
||||||
|
* @param s La chaîne de caractères à traiter
|
||||||
|
* @return la chaîne traitée
|
||||||
|
*/
|
||||||
|
public static String addSlashes(String s) {
|
||||||
|
return s.replaceAll("(['\"\\\\])", "\\\\$1");
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Cette méthode supprime les antislashs d'une chaîne.
|
||||||
|
* @param s La chaîne de caractères à traiter
|
||||||
|
* @return la chaîne traitée
|
||||||
|
*/
|
||||||
|
public static String stripSlashes(String s) {
|
||||||
|
return s.replaceAll("\\\\(['\"\\\\])", "$1");
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Cette méthode transforme une chaîne de caractères en entier si cette chaîne représente un entier.<br>
|
||||||
|
* Elle propagez une exception sinon.
|
||||||
|
* @param s La chaîne de caractères à traiter
|
||||||
|
* @return l'entier correspondant
|
||||||
|
* @throws NumberFormatException dont le message est "String is null"
|
||||||
|
*/
|
||||||
|
public static int checkNumeric(String s) throws NumberFormatException {
|
||||||
|
if (s != null) {
|
||||||
|
return Integer.parseInt(s);
|
||||||
|
}
|
||||||
|
else throw new NumberFormatException("String is null");
|
||||||
|
}
|
||||||
|
}
|
378989
src/com/arnaudpc/ods5.txt
Normal file
378989
src/com/arnaudpc/ods5.txt
Normal file
File diff suppressed because it is too large
Load Diff
378989
src/ods5.txt
Normal file
378989
src/ods5.txt
Normal file
File diff suppressed because it is too large
Load Diff
6
web/WEB-INF/web.xml
Normal file
6
web/WEB-INF/web.xml
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
|
||||||
|
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||||
|
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
|
||||||
|
version="4.0">
|
||||||
|
</web-app>
|
70
web/index.jsp
Normal file
70
web/index.jsp
Normal file
@ -0,0 +1,70 @@
|
|||||||
|
<%@ taglib prefix = "c" uri = "http://java.sun.com/jsp/jstl/core" %>
|
||||||
|
<%--
|
||||||
|
Created by IntelliJ IDEA.
|
||||||
|
User: ministicraft
|
||||||
|
Date: 21/01/2019
|
||||||
|
Time: 14:36
|
||||||
|
To change this template use File | Settings | File Templates.
|
||||||
|
--%>
|
||||||
|
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
|
||||||
|
<html>
|
||||||
|
<head>
|
||||||
|
<title>Anagramme</title>
|
||||||
|
<link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet">
|
||||||
|
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/materialize/1.0.0/css/materialize.min.css">
|
||||||
|
<script src="https://cdnjs.cloudflare.com/ajax/libs/materialize/1.0.0/js/materialize.min.js"></script>
|
||||||
|
<meta name="viewport" content="width=device-width, initial-scale=1.0"/>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<nav>
|
||||||
|
<div class="nav-wrapper">
|
||||||
|
<a href="index.jsp" class="brand-logo">Anagramme</a>
|
||||||
|
<ul id="nav-mobile" class="right hide-on-med-and-down">
|
||||||
|
<li><a href="index.jsp">Home</a></li>
|
||||||
|
</ul>
|
||||||
|
</div>
|
||||||
|
</nav>
|
||||||
|
<main class="container">
|
||||||
|
|
||||||
|
<h2>Enter letter who you want the anagram to be found:</h2>
|
||||||
|
<div class="input-field col s5">
|
||||||
|
<input id="letters" type="text">
|
||||||
|
<label for="letters">Letters</label>
|
||||||
|
</div>
|
||||||
|
<button class="btn waves-effect waves-light col s2" id="search">Search</button>
|
||||||
|
<div>
|
||||||
|
<h3>Result:</h3>
|
||||||
|
<ul class="collection col s8" id="resultList">
|
||||||
|
</ul>
|
||||||
|
</div>
|
||||||
|
</main>
|
||||||
|
<script>
|
||||||
|
document.getElementById('search').onclick = function(){
|
||||||
|
var letters = document.getElementById('letters').value;
|
||||||
|
var Http = new XMLHttpRequest();
|
||||||
|
var url='/anagram?letters='+letters;
|
||||||
|
Http.open("GET", url);
|
||||||
|
Http.send();
|
||||||
|
Http.onreadystatechange=(e)=>{
|
||||||
|
console.log(Http.responseText);
|
||||||
|
var result = JSON.parse(Http.responseText);
|
||||||
|
var resultList = document.getElementById('resultList');
|
||||||
|
while (resultList.firstChild) {
|
||||||
|
resultList.removeChild(resultList.firstChild);
|
||||||
|
}
|
||||||
|
result.anagrams.forEach(function(anagram) {
|
||||||
|
var node = document.createElement("li");
|
||||||
|
node.className = "collection-item";
|
||||||
|
var textnode = document.createTextNode(anagram);
|
||||||
|
node.appendChild(textnode);
|
||||||
|
resultList.appendChild(node)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
};
|
||||||
|
</script>
|
||||||
|
<footer class="page-footer">
|
||||||
|
<div class="container">
|
||||||
|
</div>
|
||||||
|
</footer>
|
||||||
|
</body>
|
||||||
|
</html>
|
Loading…
x
Reference in New Issue
Block a user