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
|
package examples.shop.impl.session;
import java.util.Iterator;
import java.util.List;
import javax.annotation.Resource;
import javax.ejb.EJB;
import javax.ejb.Remote;
import javax.ejb.Stateless;
import javax.jws.WebService;
import examples.shop.impl.entity.Customer;
import examples.shop.impl.entity.LineItem;
import examples.shop.logic.Pricer;
import examples.shop.logic.UserManager;
/**
* Stateless Session Bean which computes prices based
* upon a set of pricing rules. The pricing rules are
* deployed with the bean as environment properties.
*/
@Stateless
@Remote(Pricer.class)
@WebService(serviceName="PricerService", portName="PricerPort")
public class PricerBean implements Pricer {
@Resource(name="taxRate")
public int taxRate = 0;
@Resource(name="bulkDiscountRate")
public int bulkDiscountRate = 0;
@EJB
UserManager userManager;
public PricerBean() {
}
/**
* bulk discounts apply to quantities of BULK or more items
*/
private static final int BULK = 5;
/**
* This method computes the applicable discount in absolute
* figure, based on bulk and personal discounts that may apply.
*
* @param quantity the number of items that a user intends to buy
* @param basePrice the overall, non-discounted volume of the
* purchase (individual price times quantity)
* @param the user name
* @return the subTotal for the line item after applying any
* applicable discounts, excluding taxes
*/
public double getDiscount(int quantity, double basePrice, String user) {
double discountRate = getPersonalDiscountRate(user);
if (quantity >= BULK) {
discountRate += getBulkDiscountRate();
System.out.println("Using getBulkDiscountRate " + getBulkDiscountRate());
}
/*
* Calculate the discount in absolute figures
*/
return basePrice * (discountRate / 100);
}
/**
* A bulk discount applies to quantities of more than 5 pieces.
* @return the bulk discount rate int percent
*/
public double getBulkDiscountRate() {
return this.bulkDiscountRate;
}
/**
* Customers with certain names get discounts. The discount rules
* are stored in the environment properties that the bean is
* deployed with.
*/
public double getPersonalDiscountRate(String userName) {
/*
* Get the name of this customer.
*/
Customer user = userManager.getUser(userName);
if( user != null)
return user.getDiscount();
else
return 0;
}
/**
* Computes the subtotal price for a set of products the customer
* is interested in. The subtotal takes into account the price of
* each product the customer wants, the quantity of each product,
* and any personal discounts the customer gets. However, the
* subtotal ignores taxes.
*
* @param quote All the data needed to compute the
* subtotal is in this parameter.
*/
public double priceSubtotal(String user, List<LineItem> items) {
System.out.println("PricerBean.priceSubtotal() called");
/*
* Compute the subtotal
*/
double subTotal = 0;
for(Iterator<LineItem> iter = items.iterator(); iter.hasNext(); ) {
LineItem item = iter.next();
item.setDiscount(
getDiscount(item.getQuantity(), item.basePrice(),user));
/*
* Add the price to the subtotal.
*/
subTotal += (item.basePrice() - item.getDiscount());
}
return subTotal;
}
/**
* Computes the taxes on a quote.
*/
public double priceTaxes(double subtotal) {
System.out.println("PricerBean.priceTaxes() called, taxes: " + getTaxRate());
return (getTaxRate() / 100) * subtotal;
}
/**
* @return the applicable tax rate
*/
public double getTaxRate() {
return taxRate;
}
}
|