diff --git a/src/model/Model.java b/src/model/Model.java index ac4df9d..4e0cc80 100644 --- a/src/model/Model.java +++ b/src/model/Model.java @@ -24,6 +24,71 @@ public Model() { } //################################################################################################################################################## + private static String addQuery(String TableName, String columns[], String data[]) { + String queryA = "INSERT INTO `" + TableName + "` ("; + int i, j; + for (i = 0; i < columns.length - 1; i++) { + queryA += columns[i] + ", "; + } + queryA += columns[i] + ") VALUES ("; + for (j = 0; j < data.length - 1; j++) { + queryA += "'"+ data[j] + "', "; + } + queryA += "'"+ data[i] + "'"; + queryA += ")"; + return queryA; + } + //################################################################################################################################################## + private static String editQuery(String TableName,String TableKey, String columns[], String data[], String index) { + String query = "UPDATE `" + TableName + "` SET "; + int i; + for (i = 0; i < data.length - 1; i++) { + query += columns[i] + " = '" + data[i] + "', "; + } + query += columns[i] + " = '" + data[i] + "'" + " WHERE " + TableName +"." + TableKey + " = " + index; + return query; + } + //################################################################################################################################################## + private String[] userToList(Users user) { + String[] data = {"", "", "", "", "", "", "", "", "", ""}; //{"last_name", "first_name", "user_name", "password", "email", "city", "address", "phone", "gender", "privileges"}; + Object[] functions = {user.getLastName(), user.getFirstName(), user.getUserName(), user.getPassword(), user.getMail(), user.getCity(), user.getAddress(), user.getPhone(), user.getGender(), user.getPrivileges()}; + for (int i = 0; i < data.length; i++) { + data[i] = (String) functions[i]; + } + return data; + } + //################################################################################################################################################## + private String[] itemToList(Items item) { + String[] data = {"", "", "", "", "", "", ""}; //{"name", "owner", "category", "item_value", "item_condition", "description", "picture"}; + Object[] functions = {item.getName(), item.getOwnerID(), item.getCategory(), item.getItemValue(), item.getCondition(), item.getDecription(), item.getPicture()}; + for (int i = 0; i < data.length; i++) { + data[i] = (String) functions[i]; + } + return data; + } + //################################################################################################################################################## + private String[] requestToList(Requests request) { + String[] data = {"", "", "", "", "", ""}; //{"itemID", "owner", "borrower", "period", "date", "response"}; + Object[] functions = {request.getItemID(), request.getOwnerID(), request.getBorrowerID(), request.getPeriod(), request.getDate(), request.getResponse()}; + for (int i = 0; i < data.length; i++) { + data[i] = (String) functions[i]; + } + return data; + } + //################################################################################################################################################## + private double scoreByString(String search_query, String str) { + //System.out.println("***********item name is: " + name + "******************"); + double score = 1; + String[] words = str.split(" "); + for (int i = 0; i < words.length; i++) { + System.out.println("word " + i + " distance is: " + dist(search_query, words[i])); + if (dist(search_query, words[i]) < score) { + score = dist(search_query, words[i]); + } + } + return score; + } + //################################################################################################################################################## public List itemSearch(String search_query) { // main search Items item = new Items(); String query = "SELECT * FROM items"; // get all data from database @@ -92,18 +157,6 @@ public List smartSearch(String search_query) { // main search return null; } } - private double scoreByString(String search_query, String str) { - //System.out.println("***********item name is: " + name + "******************"); - double score = 1; - String[] words = str.split(" "); - for (int i = 0; i < words.length; i++) { - System.out.println("word " + i + " distance is: " + dist(search_query, words[i])); - if (dist(search_query, words[i]) < score) { - score = dist(search_query, words[i]); - } - } - return score; - } //################################################################################################################################################## public Users login(String user_name, String password) { // login function with check if user name already exists Users user = new Users(); // create new empty user and assign data @@ -117,7 +170,7 @@ public Users login(String user_name, String password) { // login function with c user.setLastName(myRs.getString("last_name")); user.setFirstName(myRs.getString("first_name")); user.setUserName(myRs.getString("user_name")); - user.setPassword(null); + user.setPassword(myRs.getString("password")); user.setMail(myRs.getString("email")); user.setAddress(myRs.getString("address")); user.setPhone(myRs.getString("phone")); @@ -572,58 +625,6 @@ public List getSentRequestsByUserID(String userID) { } } //################################################################################################################################################## - private static String addQuery(String TableName, String columns[], String data[]) { - String queryA = "INSERT INTO `" + TableName + "` ("; - int i, j; - for (i = 0; i < columns.length - 1; i++) { - queryA += columns[i] + ", "; - } - queryA += columns[i] + ") VALUES ("; - for (j = 0; j < data.length - 1; j++) { - queryA += "'"+ data[j] + "', "; - } - queryA += "'"+ data[i] + "'"; - queryA += ")"; - return queryA; - } - //################################################################################################################################################## - private static String editQuery(String TableName,String TableKey, String columns[], String data[], String index) { - String query = "UPDATE `" + TableName + "` SET "; - int i; - for (i = 0; i < data.length - 1; i++) { - query += columns[i] + " = '" + data[i] + "', "; - } - query += columns[i] + " = '" + data[i] + "'" + " WHERE " + TableName +"." + TableKey + " = " + index; - return query; - } - //################################################################################################################################################## - private String[] userToList(Users user) { - String[] data = {"", "", "", "", "", "", "", "", "", ""}; //{"last_name", "first_name", "user_name", "password", "email", "city", "address", "phone", "gender", "privileges"}; - Object[] functions = {user.getLastName(), user.getFirstName(), user.getUserName(), user.getPassword(), user.getMail(), user.getCity(), user.getAddress(), user.getPhone(), user.getGender(), user.getPrivileges()}; - for (int i = 0; i < data.length; i++) { - data[i] = (String) functions[i]; - } - return data; - } - //################################################################################################################################################## - private String[] itemToList(Items item) { - String[] data = {"", "", "", "", "", "", ""}; //{"name", "owner", "category", "item_value", "item_condition", "description", "picture"}; - Object[] functions = {item.getName(), item.getOwnerID(), item.getCategory(), item.getItemValue(), item.getCondition(), item.getDecription(), item.getPicture()}; - for (int i = 0; i < data.length; i++) { - data[i] = (String) functions[i]; - } - return data; - } - //################################################################################################################################################## - private String[] requestToList(Requests request) { - String[] data = {"", "", "", "", "", ""}; //{"itemID", "owner", "borrower", "period", "date", "response"}; - Object[] functions = {request.getItemID(), request.getOwnerID(), request.getBorrowerID(), request.getPeriod(), request.getDate(), request.getResponse()}; - for (int i = 0; i < data.length; i++) { - data[i] = (String) functions[i]; - } - return data; - } - //################################################################################################################################################## public double dist(String stringA, String stringB) { char[] s1 = stringToChars(stringA); @@ -658,6 +659,7 @@ public double dist(String stringA, String stringB) { double normal = ((double)prev[s2.length] / (double) Math.max(stringA.length(), stringB.length())); return normal; } + //################################################################################################################################################## public char[] stringToChars(String str) { // Creating array of string length @@ -668,7 +670,9 @@ public char[] stringToChars(String str) { } return ch; } + //################################################################################################################################################## public void setGoodscore(double score) { this.goodScore = score; } + //################################################################################################################################################## } \ No newline at end of file