/src/sin/app/hbook/DataManager.java
Java | 513 lines | 324 code | 127 blank | 62 comment | 96 complexity | 7eaf4b8656d93da09e392f8ed3c83a1a MD5 | raw file
- package sin.app.hbook;
-
- import sin.app.hbook.db.SimpleDB;
- import sin.app.hbook.obj.Book;
- import sin.app.hbook.obj.Evals;
- import sin.app.hbook.obj.Genre;
- import sin.app.hbook.obj.Points;
- import sin.app.hbook.obj.User;
- import sin.app.hbook.util.FileRW;
-
- public class DataManager {
- final static int BHISTORY_MAX = 80;
- final static int EHISTORY_MAX = 50;
-
- private SimpleDB db;
- private CollaborativeFilter filter;
-
- // public BookHistory bhis;
- // public EvalHistory ehis;
- public User[] users;
- public int userNum = 0;
- public Book[] books;
- public int bookNum = 0;
-
-
-
- public DataManager(SimpleDB d) {
- db = d;
-
- // bhis = new BookHistory(HISTORY_MAX);
- // ehis = new EvalHistory(HISTORY_MAX);
-
- users = new User[10+1];
- for (int i = 0; i < users.length; i++)
- users[i] = new User(BHISTORY_MAX, EHISTORY_MAX);
- books = new Book[50];
- for (int i = 0; i < books.length; i++)
- books[i] = new Book();
-
- initDB();
- }
-
- private void initDB() {
- String tmp;
- String[] ary;
- int i;
-
- // users
- FileRW usersrw = new FileRW("users.txt");
- i = 0;
- // System.out.println("\n userID, username");
- while ((tmp = usersrw.readline()) != null) {
-
- if (!(tmp.charAt(0) == '/' && tmp.charAt(1) == '/')) {
-
- ary = tmp.split(" ");
-
- if (ary.length >= 2) {
-
- // "id name"
- users[i].userID = Integer.parseInt(ary[0]);
- users[i].name = ary[1];
- // System.out.println(users[i].userID + ", " +
- // users[i].name);
-
- if (i >= users.length)
- break;
- }
- i++;
-
- }
- }
-
- // books
- FileRW booksrw = new FileRW("books.txt");
- i = 0;
- // System.out.println("\n bookID, title, author, publisher, genreNumber");
- while ((tmp = booksrw.readline()) != null) {
- if (tmp.length() == 0) {
- continue;
- } else if (!(tmp.charAt(0) == '/' && tmp.charAt(1) == '/')) {
-
- ary = tmp.split(" ");
-
- if (ary.length >= 5) {
-
- // "id title author publisher genre"
- // int str str str int
- books[i].bookID = Integer.parseInt(ary[0]);
- books[i].title = ary[1];
- books[i].author = ary[2];
- books[i].publisher = ary[3];
- books[i].genre = int2genre(Integer.parseInt(ary[4]));
- // System.out.println("" + books[i].bookID + ", " +
- // books[i].title + ", " + books[i].author + ", " +
- // books[i].publisher + ", " + books[i].genre);
-
- if (i >= books.length)
- break;
- }
- i++;
- }
- }
-
- // books
- FileRW ehisrw = new FileRW("ehis.txt");
- // System.out.println("\n bookID, title, author, publisher, genreNumber");
- while ((tmp = ehisrw.readline()) != null) {
- if (tmp.length() == 0) {
- continue;
- } else if (!(tmp.charAt(0) == '/' && tmp.charAt(1) == '/')) {
-
- ary = tmp.split(" ");
-
- if (ary.length >= 3) {
- User u;
- long uid;
- long bid;
- Evals e;
-
- // "uid bid star"
- // long long int
- uid = Long.parseLong(ary[0]);
- bid = Long.parseLong(ary[1]);
- e = int2evals(Integer.parseInt(ary[2]));
- u = getUser(uid);
-
- u.ehis.pushEval(bid, e);
- // System.out.println("" + uid + ", " + bid + ", " +
- // e.name());
-
- }
- }
- }
-
-
- // books
- FileRW bhisrw = new FileRW("bhis.txt");
- // System.out.println("\n bookID, title, author, publisher, genreNumber");
- while ((tmp = bhisrw.readline()) != null) {
- if (tmp.length() == 0) {
- continue;
- } else if (!(tmp.charAt(0) == '/' && tmp.charAt(1) == '/')) {
-
- ary = tmp.split(" ");
-
- if (ary.length >= 2) {
- User u;
- long uid;
- long bid;
-
- // "uid bid star"
- // long long int
- uid = Long.parseLong(ary[0]);
- bid = Long.parseLong(ary[1]);
- u = getUser(uid);
-
- u.bhis.pushBook(bid);
- // System.out.println("" + uid + ", " + bid);
-
- }
- }
- }
-
- for (i=0; i<users.length; i++) {
- if (users[i].userID == -1)
- continue;
-
- // System.out.println("userid=" + i);
- calcPoints(users[i].userID);
- }
-
- }
-
- // DB???????
- public void readDB() {
-
- }
-
- // public getReccom
- public String[] getUserEval(long uid, int max) {
- return getUserEval(getUserName(uid), max);
- }
-
-
- public String[] getUserEval(String uid, int max) {
- //String[] result = new String[max * 3];
- String[] result = new String[max];
- int ptr = 0;
- User user = null;
-
- user = getUser(uid);
-
- if (user == null) {
- result[ptr] = "such no user";
-
- } else {
- // System.out.println("[debug]data.getUserEval(): max=" + max +
- // "user=" + user.name);
-
- for (int i=0; i<max; i++) {
- if (i >=user.ehis.getNum())
- break;
-
- Book b = getBook(user.ehis.getBook(i));
- if (b.title == null) {
- result[ptr] = "no evaluations";
- break;
- } else {
- result[ptr++] = "[" + b.bookID + "]?" + b.title + "? - " + user.ehis.getEvals(i).name();
- //result[ptr++] = " " + b.author + ", " + b.publisher;
- //result[ptr++] = " " + "eval: [" + user.ehis.getEvals(i).name() + "] by " + user.name + "[" + user.userID + "]";
- }
- }
- }
-
- return result;
- }
-
-
- public String[] getBookHistory(String username, int max) {
- String[] result = new String[max];
- int ptr = 0;
- User user = null;
-
- user = getUser(username);
-
- if (user == null) {
- result[ptr] = "hbshell: no such user";
-
- } else {
- // System.out.println("[debug]data.getUserEval(): max=" + max +
- // "user=" + user.name);
-
- for (int i=0; i<max; i++) {
- // System.out.println("[debug]data.getUserEval(): bhis.max=" + user.bhis.getMax());
- if (i >= user.bhis.getNum())
- break;
-
- Book b = getBook(user.bhis.getBook(i));
- if (b.title == null) {
- result[ptr] = "no histry";
- break;
- } else {
- result[ptr++] = "[" + b.bookID + "]?" + b.title + "?";
- }
- }
-
- }
-
- return result;
- }
-
- public String[] getBookHistory(long uid, int max) {
- return getBookHistory(getUserName(uid), max);
- }
-
-
- public Book getBook(long bid) {
- Book b = null;
-
- for (int i = 0; i < books.length; i++) {
- if (books[i].bookID == bid) {
- b = books[i];
- break;
- }
- }
-
- return b;
- }
-
- public User getUser(String name) {
- User u = null;
-
- for (int i = 0; i < users.length; i++) {
- if (users[i].name.equals(name)) {
- u = users[i];
- break;
- }
- }
-
- return u;
- }
-
- public User getUser(long uid) {
- User u = null;
-
- for (int i = 0; i < users.length; i++) {
- if (users[i].userID == uid) {
- u = users[i];
- break;
- }
- }
-
- return u;
- }
-
- public String getUserName(long uid) {
- User u = getUser(uid);
-
- return (u==null ? "" : u.name);
- }
-
-
- public long addUser(String name) {
- long uid = -1;
-
- for (int i=0; i<users.length; i++) {
- if (users[i].userID == -1) {
- users[i].userID = i;
- users[i].name = name;
- uid = users[i].userID;
- calcPoints(uid);
- break;
- }
- }
-
- return uid;
- }
-
-
-
-
-
-
- public boolean bookPush(long uid, long bid) {
- int n;
- boolean success = false;
-
- for (int i=0; i<users.length; i++) {
- if (users[i].userID == uid) {
- users[i].bhis.pushBook(bid);
- success = true;
- break;
- }
- }
-
- return success;
- }
-
-
- public boolean evalPush(long uid, long bid, int eval) {
- int n;
- User u = getUser(uid);
- boolean success = false;
- boolean alreadyEvaled = false;
-
-
- //????????
- for (int i=0; i<u.ehis.getNum(); i++) {
- if (u.ehis.getBook(i) == bid) {
- alreadyEvaled = true;
- break;
- }
- }
-
- // System.out.println(bid + ", " + eval + ", " + int2evals(eval));
- if (!alreadyEvaled && (int2evals(eval) != Evals.NULL)) {
- u.ehis.pushEval(bid, int2evals(eval));
- success = true;
- }
-
- return success;
- }
-
-
- //?????????????????????
- //???((s[1:5] - 3) * 2)????
- public void calcPoints(long uid) {
- User u = getUser(uid);
- Points p = u.pt;
- Book b = null;
-
-
- //i: ????????
- for (int i=0; i<p.getNum(); i++) {
- int num = u.bhis.getNum();
- int cnt = 0;
- float point;
-
- // System.out.println("ubhis.num=" + num);
- //?????????
- for (int j=0; j<num; j++) {
- b = getBook(u.bhis.getBook(j));
-
- // System.out.println("(i, j)=(" + i + ", " + j + "), isbook=" + (b == null));
- // if (b == null)
- // continue;
-
- if (int2genre(i) == b.genre)
- cnt++;
- }
- point = cnt / (float)num * 100;
-
- //?????????
- num = 0;
- for (int j=0; j<u.ehis.getNum(); j++) {
- Evals e = u.ehis.getEvals(j);
- b = getBook(u.ehis.getBook(j));
-
- if (u.ehis.getBook(j) == -1 || e == Evals.NULL)
- continue;
-
- if (b.genre == int2genre(i))
- num += (e.getStars() - 3) * 15;
- }
- // for (int j=0; j<u.ehis.getNum(); j++) {
- // int star = u.ehis.getEvals(i).getStars();
- // b = getBook(u.ehis.getBook(j));
- //
- // if (b.bookID == -1)
- // continue;
- //
- // if (b.genre == int2genre(i))
- // System.out.println("star=" + u.ehis.getEvals(i).name());
- // num += (star - 3) * 10;
- // }
-
- p.points[i] = (int)point + num;
- }
- }
-
- public String[] recom(long uid, int num) {
- String[] list = null;
- Genre g;
-
- g = CollaborativeFilter.detemineGenre(getUser(uid), this);
-
- list = new String[num + 1];
- list[0] = "genre: " + g.name();
- for (int i=1; i<=num; i++) {
- if (books[i].genre == g) {
- list[i] = "[" + books[i].bookID + "]?" + books[i].title + "?";
- }
- }
-
- return list;
- }
-
-
-
-
- public Evals int2evals(int e) {
- Evals evl = Evals.NULL;
-
- switch (e) {
- case 1:
- evl = Evals.STAR1;
- break;
-
- case 2:
- evl = Evals.STAR2;
- break;
-
- case 3:
- evl = Evals.STAR3;
- break;
-
- case 4:
- evl = Evals.STAR4;
- break;
-
- case 5:
- evl = Evals.STAR5;
- break;
-
- default:
- evl = Evals.NULL;
- }
-
- return evl;
- }
-
- public Genre int2genre(int g) {
- Genre gnr = Genre.NULL;
-
- switch (g) {
- case 0:
- gnr = Genre.Popular;
- break;
-
- case 1:
- gnr = Genre.Literature;
- break;
-
- case 2:
- gnr = Genre.Mistery;
- break;
-
- case 3:
- gnr = Genre.SciFi;
- break;
-
- case 4:
- gnr = Genre.Child;
- break;
-
- case 5:
- gnr = Genre.NonFiction;
- break;
-
- case 6:
- gnr = Genre.Essay;
- break;
-
- // default:
- // gnr = Genre.NULL;
- }
-
- return gnr;
- }
-
- }