-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNamedUser.java
91 lines (91 loc) · 3.78 KB
/
NamedUser.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
public class NamedUser extends User
{
private String name;
/**
* Constructor for the class. Sets name instance variable to a given String.
* @param name, the String to set name to
*/
public NamedUser(String name)
{
super();
this.name=name;
}
/**
* Accessor method for the instance variable name.
* @return the instance variable name, a String
*/
public String getName()
{
return name;
}
/**
* This class' implementation of readBook(). Adds given Book to booksRead variable and whether they
* liked it to corresponding index in booksLiked variable. In addition, if this Book is not already in
* the given BookRecommender's books ArrayList variable, it will be added to both that and to that
* BookRecommender's WeightedGraph variable. The weights of the edges between this and the other items'
* in that graph will then be updated based on the user's previous preferences and their preference for this
* Book. If the user liked both this and another Book, or disliked both this and another Book, the weight of
* the edge between those is incremented, and if the user liked one but not the other, that edge is decremented.
* @param newBook, the Book to be read by this user
* @param liked, a boolean representing whether the user liked it or not
* @param recommender, the BookRecommender being used (not needed in GuestUser implementation)
*/
public void readBook(Book newBook, boolean liked, BookRecommender recommender)
{
getBooksRead().add(newBook);
getBooksLiked().add(liked);
if(!recommender.getBooks().contains(newBook))
{
recommender.getGraph().addVertex();
recommender.getBooks().add(newBook);
}
for(Book book:recommender.getBooks())
{
if(getBooksRead().contains(book) && !book.equals(newBook))
{
if((liked && getBooksLiked().get(getBooksRead().indexOf(book))) || (!liked && !getBooksLiked().get(getBooksRead().indexOf(book))))
{
recommender.getGraph().incrementEdge(recommender.getBooks().indexOf(book), recommender.getBooks().indexOf(newBook));
}
else
{
recommender.getGraph().decrementEdge(recommender.getBooks().indexOf(book), recommender.getBooks().indexOf(newBook));
}
}
}
}
/**
* Gets the menu text that gives the user their options as a GuestUser.
* @return a String containing the menu text for this User
*/
public String getMenuText()
{
return "\n (1) Rate books\n (2) Add books\n (3) Get recommendation\n (4) Close and Save: ";
}
/**
* Takes a String representing input from the user where they choose a menu option and
* converts it to an int that is common to all subclasses of User (adjusted to reflect the
* number of options one type of User might have compared to another).
* @param indStr, a String containing the user's input (should be "1", "2", "3", or "4")
* @return an int after their input has been converted to a number that is common to all Users
*/
public int convertMenuInd(String ind)
{
try
{
return Integer.parseInt(ind);
}
catch(Exception e)
{
return 0;
}
}
/**
* toString method that overrides the one inherited from the User class. Returns result of
* call to super() version of the method along with the value of the name instance variable.
*/
public String toString()
{
return name+": \n"+super.toString();
}
}