forked from Apress/functional-interfaces-in-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSalesPromotion.java
More file actions
191 lines (166 loc) · 5.24 KB
/
SalesPromotion.java
File metadata and controls
191 lines (166 loc) · 5.24 KB
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
package chapter4;
import java.util.function.*;
import java.util.ArrayList;
class Customer
{
String name;
String phoneNum;
String sport;
String team;
double gpa;
String subject;
int numFriends;
String friends;
public Customer(String n, String pn, String sp, String t,
double g, String s, int nf, String f)
{
name = n;
phoneNum = pn;
sport = sp;
team = t;
gpa = g;
subject = s;
numFriends = nf;
friends = f;
}
}
abstract class Record
{
String name;
String phoneNum;
int index;
public Record(String n, String pn, int i)
{
name = n;
phoneNum = pn;
index = i;
}
@Override
public String toString() {
return "name: " + name + " phone number: " + phoneNum;
}
}
class SportRecord extends Record
{
String team;
public SportRecord(String n, String pn, int i, String t)
{
super(n, pn, i);
team = t;
}
@Override
public String toString() {
return super.toString() + " favorite team is the " + team;
}
}
class GpaRecord extends Record
{
String subject;
public GpaRecord(String n, String pn, int i, String s)
{
super(n, pn, i);
subject = s;
}
@Override
public String toString() {
return super.toString() + " favorite subject is " + subject;
}
}
class FriendsRecord extends Record
{
String friends;
public FriendsRecord(String n, String pn, int i, String f)
{
super(n, pn, i);
friends = f;
}
@Override
public String toString() {
return super.toString() + " friends are " + friends;
}
}
public class SalesPromotion
{
final static Customer[] customers = {
new Customer("John Smith" , "9084321212", "football",
"Giants", 3.61, null , 0, null),
new Customer("Indira Patel" , "7325551234", "tennis" ,
null , 3.92, "Java" , 0, null),
new Customer("Sarah Johnson", "2123231245", "football",
"Eagles", 3.71, null , 1,
"Jane Hernadez,2017765765"),
new Customer("Javier Jones" , "8568768765", "golf" ,
null , 3.85, "Physics", 1,
"Maria Regina,9086547654")
};
private static void matchCustomers(Customer c,
ArrayList< BiFunction<Customer,Integer, ? extends Record> > f)
{
for (int j=0; j < f.size(); ++j)
{
Record record;
int index = 0;
do
{
record = f.get(j).apply(c,index);
if (record != null)
{
System.out.println(record);
index = record.index + 1;
}
} while (record != null);
}
System.out.println();
}
public static void main(String[] args)
{
BiFunction<Customer,Integer,SportRecord> fsport = (x,z) -> {
SportRecord sport = null;
for (int i=z;
i < customers.length && sport == null;
++ i)
if (customers[i].sport.equals(x.sport))
sport = new SportRecord(customers[i].name,
customers[i].phoneNum, i, customers[i].team);
return sport;
};
BiFunction<Customer,Integer,GpaRecord> fgpa = (x,z) -> {
GpaRecord gpa = null;
for (int i=z; i < customers.length && gpa == null; ++ i)
if (customers[i].gpa >= x.gpa)
gpa = new GpaRecord(customers[i].name,
customers[i].phoneNum, i,
customers[i].subject);
return gpa;
};
BiFunction<Customer,Integer,FriendsRecord> ffriends = (x,z) -> {
FriendsRecord friends = null;
for (int i=z;
i < customers.length && friends == null;
++ i)
if (customers[i].numFriends >= x.numFriends)
friends = new FriendsRecord(customers[i].name,
customers[i].phoneNum,
i,
customers[i].friends);
return friends;
};
ArrayList< BiFunction<Customer,Integer,? extends Record> >
list = new ArrayList<>();
list.add(fsport);
list.add(fgpa);
list.add(ffriends);
System.out.println(
"SUNDAY FOOTBALL PROMOTION - Call the following customers:");
matchCustomers(new Customer(null, null, "football", null,
Double.MAX_VALUE, null, Integer.MAX_VALUE, null), list);
System.out.println(
"TUESDAY HIGH-TECH PROMOTION - Call the following customers:");
matchCustomers(new Customer(null, null, null, null, 3.75,
null, Integer.MAX_VALUE, null), list);
System.out.println(
"FRIDAY BRING A FRIEND PROMOTION - Call the following customers:");
matchCustomers(new Customer(null, null, null, null,
Double.MAX_VALUE, null, 1, null), list);
}
}