package pA;
import java.util.Scanner;
import java.util.Arrays;
import java.util.Random;
import java.math.*;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class Main {
public static Recommend[] arr;
public static Pair[] arrp;
public static Scanner input;
public static Recommend A;
public static int n;
public static int num;
public static int information;
//JDBC的协议、主机名(此处是本地机)、端口号、需要连接的数据库等信息
public static String url = "jdbc:mysql://localhost:3306/recommend";
public static String user = "root"; //登录用户名
public static String password = "123456"; //登录密码
public static Connection con;
public static Statement stmt;
public static void main(String[] args) {
setup();
loop();
}
public static void setup() {
try {
Class.forName("com.mysql.jdbc.Driver"); //指定MySQL驱动。
con = DriverManager.getConnection(url, user, password); //连接MySQL数据库
System.out.println("连接数据库服务器成功。");
stmt = con.createStatement();
} catch (ClassNotFoundException e) { //捕获没有找到驱动的异常,是forName()方法抛出的。
System.out.println("没有找到MySQL驱动。");
} catch (SQLException e) { //捕获连接失败的异常,是Connection对象抛出的。
System.out.println("连接数据库服务器失败。");
}
String chars = "abcdefghijklmnopqrstuvwxyz"; //!!!here
//String sql1 = "INSERT INTO user VALUES(0,1,2,3,4,5,6,7,'Zhangsan')";
String sql1 = "INSERT INTO user VALUES("+
Math.random()+","+
Math.random()+","+
Math.random()+","+
Math.random()+","+
Math.random()+","+
Math.random()+","+
Math.random()+","+
Math.random()+","+
"'"+
"Zhangsan"+ //单字母用户
"')";
try {
int cnt1 = stmt.executeUpdate(sql1);//执行UPDATE语句,返回更新的记录数。
if (cnt1 != 0) {
System.out.println("INSERT语句执行成功。");
}
} catch (SQLException e) { //捕获连接失败的异常,是Connection对象抛出的。
System.out.println("连接数据库服务器失败。");
}
//again
Random random = new Random();
//System.out.println(chars.charAt((int)(Math.random() * 26)));
for (int i = 0; i < 10; i++) {
String tmp = "INSERT INTO user VALUES("+
Math.random()+","+
Math.random()+","+
Math.random()+","+
Math.random()+","+
Math.random()+","+
Math.random()+","+
Math.random()+","+
Math.random()+","+
"'"+
chars.charAt((int)(Math.random() * 26))+ //用户
chars.charAt((int)(Math.random() * 26))+
"')";
try {
int cnt1 = stmt.executeUpdate(tmp);//执行UPDATE语句,返回更新的记录数。
if (cnt1 != 0) {
System.out.println("INSERT语句执行成功。");
}
} catch (SQLException e) { //捕获连接失败的异常,是Connection对象抛出的。
System.out.println("连接数据库服务器失败。");
}
}
//again1
for (int i = 0; i < 10; i++) {
String tmp = "INSERT INTO feature VALUES("+
Math.random()+","+
Math.random()+","+
Math.random()+","+
Math.random()+","+
Math.random()+","+
Math.random()+","+
Math.random()+","+
Math.random()+","+
"'"+
chars.charAt((int)(Math.random() * 26))+ //商品名
chars.charAt((int)(Math.random() * 26))+
"')";
try {
int cnt1 = stmt.executeUpdate(tmp);//执行UPDATE语句,返回更新的记录数。
if (cnt1 != 0) {
System.out.println("INSERT语句执行成功。");
}
} catch (SQLException e) { //捕获连接失败的异常,是Connection对象抛出的。
System.out.println("连接数据库服务器失败。");
}
}
System.out.println("Please input the number of features of one vector.");
input = new Scanner(System.in);
n = input.nextInt();
System.out.println("Please input the considering vector.");
A = new Recommend(n);
System.out.println("Please input the number of comparing vectors.");
num = input.nextInt();
System.out.println("Please input these vectors.");
arr = new Recommend[num];
for (int i = 0; i < num; i++) {
arr[i] = new Recommend(n);
}
//Recommend B = new Recommend(n);
//double s = A.getSimiliar(B);
//System.out.print("The similiar factor is ");
//System.out.println(s);
arrp = new Pair[num];
}
public static void loop() {
while (true) {
catchInfo();
//dealWithInfo();
}
}
public static void catchInfo() {
System.out.println("Please input the kind of information.");
System.out.println("1 for update; 2 for output to screen.");
information = input.nextInt();
switch (information) {
case 1:
update();
break;
case 2:
outputToScreen();
}
}
//public static void dealWithInfo() {
//}
public static void outputToScreen() {
System.out.println("Input recommend mode:");
System.out.println("0 for all; 1 for part; 2 for this user's preference.");
int mode = input.nextInt();
if (mode == 0) {
for (int i = 0; i < num; i++) {
arrp[i] = new Pair(i, A.getSimiliar(arr[i]));
}
} else if (mode == 1) {
System.out.println("Input the part of recommend.");
int index = input.nextInt();
for (int i = 0; i < num; i++) {
arrp[i] = new Pair(i, A.getSimiliarPartOf(arr[i], index));
}
} else if (mode == 2) {
for (int i = 0; i < num; i++) {
arrp[i] = new Pair(i, A.getThisUsersPrefer(arr[i]));
}
}
Arrays.sort(arrp, new PairCmp());
//System.out.println("The pre " + num + " number of goods and the recommend rates are:");
System.out.println("The pre " + num + " number of goods are:");
for (int i = 0; i < num; i++) { //输出前num个物品,推荐度依次降低
//System.out.println((int)arrp[i].first + " " + arrp[i].second);
System.out.println((int)arrp[i].first);
}
}
public static void update() {
System.out.println("Please input the way which the customer view or buy.");
System.out.println("1 for view; 2 for buy.");
int w = input.nextInt();
if (w == 1) {
A.rate[0] += 1;
A.maintainRate();
for (int i = 0; i < n; i++) {
A.feature[i] = A.rate[i];
}
} else if (w == 2) {
A.rate[0] += 3;
A.maintainRate();
for (int i = 0; i < n; i++) {
A.feature[i] = A.rate[i];
}
} else {
System.out.println("Input wrong.");
}
}
}