PageRenderTime 25ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/src/sin/app/hbook/DataManager.java

https://bitbucket.org/cos_clav/hellobooks
Java | 513 lines | 324 code | 127 blank | 62 comment | 96 complexity | 7eaf4b8656d93da09e392f8ed3c83a1a MD5 | raw file
  1. package sin.app.hbook;
  2. import sin.app.hbook.db.SimpleDB;
  3. import sin.app.hbook.obj.Book;
  4. import sin.app.hbook.obj.Evals;
  5. import sin.app.hbook.obj.Genre;
  6. import sin.app.hbook.obj.Points;
  7. import sin.app.hbook.obj.User;
  8. import sin.app.hbook.util.FileRW;
  9. public class DataManager {
  10. final static int BHISTORY_MAX = 80;
  11. final static int EHISTORY_MAX = 50;
  12. private SimpleDB db;
  13. private CollaborativeFilter filter;
  14. // public BookHistory bhis;
  15. // public EvalHistory ehis;
  16. public User[] users;
  17. public int userNum = 0;
  18. public Book[] books;
  19. public int bookNum = 0;
  20. public DataManager(SimpleDB d) {
  21. db = d;
  22. // bhis = new BookHistory(HISTORY_MAX);
  23. // ehis = new EvalHistory(HISTORY_MAX);
  24. users = new User[10+1];
  25. for (int i = 0; i < users.length; i++)
  26. users[i] = new User(BHISTORY_MAX, EHISTORY_MAX);
  27. books = new Book[50];
  28. for (int i = 0; i < books.length; i++)
  29. books[i] = new Book();
  30. initDB();
  31. }
  32. private void initDB() {
  33. String tmp;
  34. String[] ary;
  35. int i;
  36. // users
  37. FileRW usersrw = new FileRW("users.txt");
  38. i = 0;
  39. // System.out.println("\n userID, username");
  40. while ((tmp = usersrw.readline()) != null) {
  41. if (!(tmp.charAt(0) == '/' && tmp.charAt(1) == '/')) {
  42. ary = tmp.split(" ");
  43. if (ary.length >= 2) {
  44. // "id name"
  45. users[i].userID = Integer.parseInt(ary[0]);
  46. users[i].name = ary[1];
  47. // System.out.println(users[i].userID + ", " +
  48. // users[i].name);
  49. if (i >= users.length)
  50. break;
  51. }
  52. i++;
  53. }
  54. }
  55. // books
  56. FileRW booksrw = new FileRW("books.txt");
  57. i = 0;
  58. // System.out.println("\n bookID, title, author, publisher, genreNumber");
  59. while ((tmp = booksrw.readline()) != null) {
  60. if (tmp.length() == 0) {
  61. continue;
  62. } else if (!(tmp.charAt(0) == '/' && tmp.charAt(1) == '/')) {
  63. ary = tmp.split(" ");
  64. if (ary.length >= 5) {
  65. // "id title author publisher genre"
  66. // int str str str int
  67. books[i].bookID = Integer.parseInt(ary[0]);
  68. books[i].title = ary[1];
  69. books[i].author = ary[2];
  70. books[i].publisher = ary[3];
  71. books[i].genre = int2genre(Integer.parseInt(ary[4]));
  72. // System.out.println("" + books[i].bookID + ", " +
  73. // books[i].title + ", " + books[i].author + ", " +
  74. // books[i].publisher + ", " + books[i].genre);
  75. if (i >= books.length)
  76. break;
  77. }
  78. i++;
  79. }
  80. }
  81. // books
  82. FileRW ehisrw = new FileRW("ehis.txt");
  83. // System.out.println("\n bookID, title, author, publisher, genreNumber");
  84. while ((tmp = ehisrw.readline()) != null) {
  85. if (tmp.length() == 0) {
  86. continue;
  87. } else if (!(tmp.charAt(0) == '/' && tmp.charAt(1) == '/')) {
  88. ary = tmp.split(" ");
  89. if (ary.length >= 3) {
  90. User u;
  91. long uid;
  92. long bid;
  93. Evals e;
  94. // "uid bid star"
  95. // long long int
  96. uid = Long.parseLong(ary[0]);
  97. bid = Long.parseLong(ary[1]);
  98. e = int2evals(Integer.parseInt(ary[2]));
  99. u = getUser(uid);
  100. u.ehis.pushEval(bid, e);
  101. // System.out.println("" + uid + ", " + bid + ", " +
  102. // e.name());
  103. }
  104. }
  105. }
  106. // books
  107. FileRW bhisrw = new FileRW("bhis.txt");
  108. // System.out.println("\n bookID, title, author, publisher, genreNumber");
  109. while ((tmp = bhisrw.readline()) != null) {
  110. if (tmp.length() == 0) {
  111. continue;
  112. } else if (!(tmp.charAt(0) == '/' && tmp.charAt(1) == '/')) {
  113. ary = tmp.split(" ");
  114. if (ary.length >= 2) {
  115. User u;
  116. long uid;
  117. long bid;
  118. // "uid bid star"
  119. // long long int
  120. uid = Long.parseLong(ary[0]);
  121. bid = Long.parseLong(ary[1]);
  122. u = getUser(uid);
  123. u.bhis.pushBook(bid);
  124. // System.out.println("" + uid + ", " + bid);
  125. }
  126. }
  127. }
  128. for (i=0; i<users.length; i++) {
  129. if (users[i].userID == -1)
  130. continue;
  131. // System.out.println("userid=" + i);
  132. calcPoints(users[i].userID);
  133. }
  134. }
  135. // DB???????
  136. public void readDB() {
  137. }
  138. // public getReccom
  139. public String[] getUserEval(long uid, int max) {
  140. return getUserEval(getUserName(uid), max);
  141. }
  142. public String[] getUserEval(String uid, int max) {
  143. //String[] result = new String[max * 3];
  144. String[] result = new String[max];
  145. int ptr = 0;
  146. User user = null;
  147. user = getUser(uid);
  148. if (user == null) {
  149. result[ptr] = "such no user";
  150. } else {
  151. // System.out.println("[debug]data.getUserEval(): max=" + max +
  152. // "user=" + user.name);
  153. for (int i=0; i<max; i++) {
  154. if (i >=user.ehis.getNum())
  155. break;
  156. Book b = getBook(user.ehis.getBook(i));
  157. if (b.title == null) {
  158. result[ptr] = "no evaluations";
  159. break;
  160. } else {
  161. result[ptr++] = "[" + b.bookID + "]?" + b.title + "? - " + user.ehis.getEvals(i).name();
  162. //result[ptr++] = " " + b.author + ", " + b.publisher;
  163. //result[ptr++] = " " + "eval: [" + user.ehis.getEvals(i).name() + "] by " + user.name + "[" + user.userID + "]";
  164. }
  165. }
  166. }
  167. return result;
  168. }
  169. public String[] getBookHistory(String username, int max) {
  170. String[] result = new String[max];
  171. int ptr = 0;
  172. User user = null;
  173. user = getUser(username);
  174. if (user == null) {
  175. result[ptr] = "hbshell: no such user";
  176. } else {
  177. // System.out.println("[debug]data.getUserEval(): max=" + max +
  178. // "user=" + user.name);
  179. for (int i=0; i<max; i++) {
  180. // System.out.println("[debug]data.getUserEval(): bhis.max=" + user.bhis.getMax());
  181. if (i >= user.bhis.getNum())
  182. break;
  183. Book b = getBook(user.bhis.getBook(i));
  184. if (b.title == null) {
  185. result[ptr] = "no histry";
  186. break;
  187. } else {
  188. result[ptr++] = "[" + b.bookID + "]?" + b.title + "?";
  189. }
  190. }
  191. }
  192. return result;
  193. }
  194. public String[] getBookHistory(long uid, int max) {
  195. return getBookHistory(getUserName(uid), max);
  196. }
  197. public Book getBook(long bid) {
  198. Book b = null;
  199. for (int i = 0; i < books.length; i++) {
  200. if (books[i].bookID == bid) {
  201. b = books[i];
  202. break;
  203. }
  204. }
  205. return b;
  206. }
  207. public User getUser(String name) {
  208. User u = null;
  209. for (int i = 0; i < users.length; i++) {
  210. if (users[i].name.equals(name)) {
  211. u = users[i];
  212. break;
  213. }
  214. }
  215. return u;
  216. }
  217. public User getUser(long uid) {
  218. User u = null;
  219. for (int i = 0; i < users.length; i++) {
  220. if (users[i].userID == uid) {
  221. u = users[i];
  222. break;
  223. }
  224. }
  225. return u;
  226. }
  227. public String getUserName(long uid) {
  228. User u = getUser(uid);
  229. return (u==null ? "" : u.name);
  230. }
  231. public long addUser(String name) {
  232. long uid = -1;
  233. for (int i=0; i<users.length; i++) {
  234. if (users[i].userID == -1) {
  235. users[i].userID = i;
  236. users[i].name = name;
  237. uid = users[i].userID;
  238. calcPoints(uid);
  239. break;
  240. }
  241. }
  242. return uid;
  243. }
  244. public boolean bookPush(long uid, long bid) {
  245. int n;
  246. boolean success = false;
  247. for (int i=0; i<users.length; i++) {
  248. if (users[i].userID == uid) {
  249. users[i].bhis.pushBook(bid);
  250. success = true;
  251. break;
  252. }
  253. }
  254. return success;
  255. }
  256. public boolean evalPush(long uid, long bid, int eval) {
  257. int n;
  258. User u = getUser(uid);
  259. boolean success = false;
  260. boolean alreadyEvaled = false;
  261. //????????
  262. for (int i=0; i<u.ehis.getNum(); i++) {
  263. if (u.ehis.getBook(i) == bid) {
  264. alreadyEvaled = true;
  265. break;
  266. }
  267. }
  268. // System.out.println(bid + ", " + eval + ", " + int2evals(eval));
  269. if (!alreadyEvaled && (int2evals(eval) != Evals.NULL)) {
  270. u.ehis.pushEval(bid, int2evals(eval));
  271. success = true;
  272. }
  273. return success;
  274. }
  275. //?????????????????????
  276. //???((s[1:5] - 3) * 2)????
  277. public void calcPoints(long uid) {
  278. User u = getUser(uid);
  279. Points p = u.pt;
  280. Book b = null;
  281. //i: ????????
  282. for (int i=0; i<p.getNum(); i++) {
  283. int num = u.bhis.getNum();
  284. int cnt = 0;
  285. float point;
  286. // System.out.println("ubhis.num=" + num);
  287. //?????????
  288. for (int j=0; j<num; j++) {
  289. b = getBook(u.bhis.getBook(j));
  290. // System.out.println("(i, j)=(" + i + ", " + j + "), isbook=" + (b == null));
  291. // if (b == null)
  292. // continue;
  293. if (int2genre(i) == b.genre)
  294. cnt++;
  295. }
  296. point = cnt / (float)num * 100;
  297. //?????????
  298. num = 0;
  299. for (int j=0; j<u.ehis.getNum(); j++) {
  300. Evals e = u.ehis.getEvals(j);
  301. b = getBook(u.ehis.getBook(j));
  302. if (u.ehis.getBook(j) == -1 || e == Evals.NULL)
  303. continue;
  304. if (b.genre == int2genre(i))
  305. num += (e.getStars() - 3) * 15;
  306. }
  307. // for (int j=0; j<u.ehis.getNum(); j++) {
  308. // int star = u.ehis.getEvals(i).getStars();
  309. // b = getBook(u.ehis.getBook(j));
  310. //
  311. // if (b.bookID == -1)
  312. // continue;
  313. //
  314. // if (b.genre == int2genre(i))
  315. // System.out.println("star=" + u.ehis.getEvals(i).name());
  316. // num += (star - 3) * 10;
  317. // }
  318. p.points[i] = (int)point + num;
  319. }
  320. }
  321. public String[] recom(long uid, int num) {
  322. String[] list = null;
  323. Genre g;
  324. g = CollaborativeFilter.detemineGenre(getUser(uid), this);
  325. list = new String[num + 1];
  326. list[0] = "genre: " + g.name();
  327. for (int i=1; i<=num; i++) {
  328. if (books[i].genre == g) {
  329. list[i] = "[" + books[i].bookID + "]?" + books[i].title + "?";
  330. }
  331. }
  332. return list;
  333. }
  334. public Evals int2evals(int e) {
  335. Evals evl = Evals.NULL;
  336. switch (e) {
  337. case 1:
  338. evl = Evals.STAR1;
  339. break;
  340. case 2:
  341. evl = Evals.STAR2;
  342. break;
  343. case 3:
  344. evl = Evals.STAR3;
  345. break;
  346. case 4:
  347. evl = Evals.STAR4;
  348. break;
  349. case 5:
  350. evl = Evals.STAR5;
  351. break;
  352. default:
  353. evl = Evals.NULL;
  354. }
  355. return evl;
  356. }
  357. public Genre int2genre(int g) {
  358. Genre gnr = Genre.NULL;
  359. switch (g) {
  360. case 0:
  361. gnr = Genre.Popular;
  362. break;
  363. case 1:
  364. gnr = Genre.Literature;
  365. break;
  366. case 2:
  367. gnr = Genre.Mistery;
  368. break;
  369. case 3:
  370. gnr = Genre.SciFi;
  371. break;
  372. case 4:
  373. gnr = Genre.Child;
  374. break;
  375. case 5:
  376. gnr = Genre.NonFiction;
  377. break;
  378. case 6:
  379. gnr = Genre.Essay;
  380. break;
  381. // default:
  382. // gnr = Genre.NULL;
  383. }
  384. return gnr;
  385. }
  386. }