-
Notifications
You must be signed in to change notification settings - Fork 0
/
Related.java
119 lines (110 loc) · 3.66 KB
/
Related.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
import java.util.*;
interface RelatedProducts
{
void registerPurchase(long customerID, long productId);
Vector<Long> getRelatedProducts(long customerID, long productId, int numProducts);
long getRelatedCustomer(long customerID, long productId);
}
/*
Related class implements RelatedProducts interface
*/
class Related implements RelatedProducts
{
/*
Checks if the given customerID and productId are valid
*/
public boolean checkValidity(long customerID, long productId)
{
if(Main.customers.containsKey(customerID) && Main.products.containsKey(productId))
{
return true;
}
System.out.println("\nNot a valid customer id: "+ customerID + " or productid: " + productId);
return false;
}
/*
If valid then add "customerID" to "purchasedByCustomers" vector of product class and
add "productId" to "productsPurchased" vector of Customer class
*/
public void registerPurchase(long customerID, long productId)
{
Customer c;
Product p;
if(checkValidity(customerID, productId))
{
c = Main.customers.get(customerID);
if(!c.productsPurchased.contains(productId))
c.productsPurchased.add(productId);
p = Main.products.get(productId);
if(!p.purchasedByCustomers.contains(customerID))
p.purchasedByCustomers.add(customerID);
}
}
/*
If the customerID has purchased productId then return other products purchased by the
customer as related products
If customerID has not purchased productId, no related products
*/
public Vector<Long> getRelatedProducts(long customerID, long productId, int numProducts)
{
Vector<Long> vec = new Vector<Long>();
Customer c;
Product p;
if(checkValidity(customerID, productId))
{
c = Main.customers.get(customerID);
p = Main.products.get(productId);
if(c.productsPurchased.contains(productId))
{
int count = 0;
int i = 0;
while(count < numProducts && i < c.productsPurchased.size())
{
//Dont add the same product in RelatedProducts
if(c.productsPurchased.get(i) != productId)
{
vec.add(c.productsPurchased.get(i));
count++;
}
i++;
}
}
else
{
System.out.println("\tNo Releted products found");
}
}
return vec;
}
/*
If productId is purchased by customerId, then return a customerId who as
also purchased productId as relatedCustomer
If not then there are no relatedCustomer for this productId
*/
public long getRelatedCustomer(long customerID, long productId)
{
long relatedCustomer = -1;
Customer c;
Product p;
if(checkValidity(customerID, productId))
{
c = Main.customers.get(customerID);
p = Main.products.get(productId);
if(p.purchasedByCustomers.contains(customerID))
{
for(int i=0; i<p.purchasedByCustomers.size(); i++)
{
//Dont return same customerID
if(p.purchasedByCustomers.get(i) != customerID)
{
relatedCustomer = p.purchasedByCustomers.get(i);
return relatedCustomer;
}
}
}
if(relatedCustomer == -1)
System.out.println("\tNo Releted customers found");
}
return relatedCustomer;
}
}