[jboss-cvs] jboss-seam/examples/dvdstore/src/com/jboss/dvd/seam ...
Norman Richards
norman.richards at jboss.com
Sun Dec 17 02:20:05 EST 2006
User: nrichards
Date: 06/12/17 02:20:05
Modified: examples/dvdstore/src/com/jboss/dvd/seam
AfterShippingAction.java CheckoutAction.java
Order.java OrderApprovalDecision.java Product.java
ShoppingCart.java ShoppingCartBean.java
StoreManager.java StoreManagerBean.java
Log:
use bigdecimal for dollar amounts, also clear up some messed up line endings
Revision Changes Path
1.2 +2 -1 jboss-seam/examples/dvdstore/src/com/jboss/dvd/seam/AfterShippingAction.java
(In the diff below, changes in quantity of whitespace are not shown.)
Index: AfterShippingAction.java
===================================================================
RCS file: /cvsroot/jboss/jboss-seam/examples/dvdstore/src/com/jboss/dvd/seam/AfterShippingAction.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -b -r1.1 -r1.2
--- AfterShippingAction.java 3 Jan 2006 18:04:10 -0000 1.1
+++ AfterShippingAction.java 17 Dec 2006 07:20:05 -0000 1.2
@@ -1,5 +1,6 @@
package com.jboss.dvd.seam;
+import java.math.BigDecimal;
import org.jboss.seam.ScopeType;
import org.jboss.seam.annotations.In;
import org.jboss.seam.annotations.Name;
@@ -13,7 +14,7 @@
@Name("afterShipping")
public class AfterShippingAction {
@In Long orderId;
- @In float amount;
+ @In BigDecimal amount;
@In(scope=ScopeType.BUSINESS_PROCESS)
String customer;
1.7 +3 -4 jboss-seam/examples/dvdstore/src/com/jboss/dvd/seam/CheckoutAction.java
(In the diff below, changes in quantity of whitespace are not shown.)
Index: CheckoutAction.java
===================================================================
RCS file: /cvsroot/jboss/jboss-seam/examples/dvdstore/src/com/jboss/dvd/seam/CheckoutAction.java,v
retrieving revision 1.6
retrieving revision 1.7
diff -u -b -r1.6 -r1.7
--- CheckoutAction.java 30 Jun 2006 21:08:27 -0000 1.6
+++ CheckoutAction.java 17 Dec 2006 07:20:05 -0000 1.7
@@ -7,6 +7,8 @@
package com.jboss.dvd.seam;
+import java.math.BigDecimal;
+
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
@@ -53,13 +55,10 @@
@Out(scope=ScopeType.BUSINESS_PROCESS, required=false)
long orderId;
@Out(scope=ScopeType.BUSINESS_PROCESS, required=false)
- float amount;
+ BigDecimal amount = BigDecimal.ZERO;
@Out(value="customer",scope=ScopeType.BUSINESS_PROCESS, required=false)
String customerName;
-// public void startCheckout() {
-// }
-
@Begin(nested=true, pageflow="checkout")
public void createOrder() {
currentOrder = new Order();
1.16 +24 -19 jboss-seam/examples/dvdstore/src/com/jboss/dvd/seam/Order.java
(In the diff below, changes in quantity of whitespace are not shown.)
Index: Order.java
===================================================================
RCS file: /cvsroot/jboss/jboss-seam/examples/dvdstore/src/com/jboss/dvd/seam/Order.java,v
retrieving revision 1.15
retrieving revision 1.16
diff -u -b -r1.15 -r1.16
--- Order.java 21 Nov 2006 03:20:31 -0000 1.15
+++ Order.java 17 Dec 2006 07:20:05 -0000 1.16
@@ -7,6 +7,7 @@
package com.jboss.dvd.seam;
import java.io.Serializable;
+import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
@@ -29,12 +30,14 @@
{
public enum Status {OPEN,CANCELLED,PROCESSING,SHIPPED}
+ public static BigDecimal TAX_RATE = new BigDecimal(".0825");
+
long orderId;
Date orderDate;
Customer customer;
- float netAmount;
- float tax;
- float totalAmount;
+ BigDecimal netAmount = BigDecimal.ZERO;
+ BigDecimal tax = BigDecimal.ZERO;
+ BigDecimal totalAmount = BigDecimal.ZERO;
List<OrderLine> orderLines = new ArrayList<OrderLine>();
Status status = Status.OPEN;
String trackingNumber;
@@ -105,26 +108,26 @@
}
@Column(name="NETAMOUNT",nullable=false,precision=12,scale=2)
- public float getNetAmount() {
+ public BigDecimal getNetAmount() {
return netAmount;
}
- public void setNetAmount(float amount) {
+ public void setNetAmount(BigDecimal amount) {
this.netAmount = amount;
}
@Column(name="TAX",nullable=false,precision=12,scale=2)
- public float getTax() {
+ public BigDecimal getTax() {
return tax;
}
- public void setTax(float amount) {
+ public void setTax(BigDecimal amount) {
this.tax = amount;
}
@Column(name="TOTALAMOUNT",nullable=false,precision=12,scale=2)
- public float getTotalAmount() {
+ public BigDecimal getTotalAmount() {
return totalAmount;
}
- public void setTotalAmount(float amount) {
+ public void setTotalAmount(BigDecimal amount) {
this.totalAmount = amount;
}
@@ -150,17 +153,19 @@
}
public void calculateTotals() {
- float total = 0;
+ BigDecimal total = BigDecimal.ZERO;
int index = 1;
for (OrderLine line: orderLines) {
line.setPosition(index++);
- total += round(line.getProduct().getPrice() * line.getQuantity());
+ total = total.add(line.getProduct().getPrice().multiply(new BigDecimal(line.getQuantity())));
}
- setNetAmount(round(total));
- setTax(round(getNetAmount() * .0825));
- setTotalAmount(round(getNetAmount() + getTax()));
+ setNetAmount(total);
+
+
+ setTax(round(getNetAmount().multiply(TAX_RATE)));
+ setTotalAmount(getNetAmount().add(getTax()));
}
public void cancel() {
@@ -176,12 +181,12 @@
setTrackingNumber(tracking);
}
- // just make sure it only has 2 digits
- private float round(double val) {
- int tmp = (int) (val * 100.0 + .5);
- float res = (float) (tmp / 100.0);
- return res;
+ /**
+ * round a positive big decimal to 2 decimal points
+ */
+ private BigDecimal round(BigDecimal amount) {
+ return new BigDecimal(amount.movePointRight(2).add(new BigDecimal(".5")).toBigInteger()).movePointLeft(2);
}
@Transient
1.3 +9 -5 jboss-seam/examples/dvdstore/src/com/jboss/dvd/seam/OrderApprovalDecision.java
(In the diff below, changes in quantity of whitespace are not shown.)
Index: OrderApprovalDecision.java
===================================================================
RCS file: /cvsroot/jboss/jboss-seam/examples/dvdstore/src/com/jboss/dvd/seam/OrderApprovalDecision.java,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -b -r1.2 -r1.3
--- OrderApprovalDecision.java 11 Jan 2006 03:42:20 -0000 1.2
+++ OrderApprovalDecision.java 17 Dec 2006 07:20:05 -0000 1.3
@@ -1,13 +1,17 @@
package com.jboss.dvd.seam;
+import java.math.BigDecimal;
import org.jboss.seam.annotations.In;
import org.jboss.seam.annotations.Name;
@Name("orderApproval")
public class OrderApprovalDecision {
- @In float amount;
+ private static BigDecimal CUTOFF = new BigDecimal(100);
+
+ @In BigDecimal amount;
+
public String getHowLargeIsOrder()
{
- return amount > 100 ? "large order" : "small order";
+ return (amount.compareTo(CUTOFF) >= 0) ? "large order" : "small order";
}
}
1.11 +4 -3 jboss-seam/examples/dvdstore/src/com/jboss/dvd/seam/Product.java
(In the diff below, changes in quantity of whitespace are not shown.)
Index: Product.java
===================================================================
RCS file: /cvsroot/jboss/jboss-seam/examples/dvdstore/src/com/jboss/dvd/seam/Product.java,v
retrieving revision 1.10
retrieving revision 1.11
diff -u -b -r1.10 -r1.11
--- Product.java 14 May 2006 18:48:54 -0000 1.10
+++ Product.java 17 Dec 2006 07:20:05 -0000 1.11
@@ -7,6 +7,7 @@
package com.jboss.dvd.seam;
import java.io.Serializable;
+import java.math.BigDecimal;
import java.util.List;
import java.util.Set;
@@ -32,7 +33,7 @@
String title;
String description;
String imageURL;
- float price;
+ BigDecimal price = BigDecimal.ZERO;
List<Actor> actors;
Set<Category> categories;
@@ -114,10 +115,10 @@
}
@Column(name="PRICE",nullable=false,precision=12,scale=2)
- public float getPrice() {
+ public BigDecimal getPrice() {
return price;
}
- public void setPrice(float price) {
+ public void setPrice(BigDecimal price) {
this.price=price;
}
}
1.6 +4 -3 jboss-seam/examples/dvdstore/src/com/jboss/dvd/seam/ShoppingCart.java
(In the diff below, changes in quantity of whitespace are not shown.)
Index: ShoppingCart.java
===================================================================
RCS file: /cvsroot/jboss/jboss-seam/examples/dvdstore/src/com/jboss/dvd/seam/ShoppingCart.java,v
retrieving revision 1.5
retrieving revision 1.6
diff -u -b -r1.5 -r1.6
--- ShoppingCart.java 7 May 2006 02:10:51 -0000 1.5
+++ ShoppingCart.java 17 Dec 2006 07:20:05 -0000 1.6
@@ -6,6 +6,7 @@
*/
package com.jboss.dvd.seam;
+import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
@@ -17,9 +18,9 @@
public List<OrderLine> getCart();
public Map getCartSelection();
- public float getSubtotal();
- public float getTax();
- public float getTotal();
+ public BigDecimal getSubtotal();
+ public BigDecimal getTax();
+ public BigDecimal getTotal();
public void updateCart();
public void resetCart();
1.26 +5 -3 jboss-seam/examples/dvdstore/src/com/jboss/dvd/seam/ShoppingCartBean.java
(In the diff below, changes in quantity of whitespace are not shown.)
Index: ShoppingCartBean.java
===================================================================
RCS file: /cvsroot/jboss/jboss-seam/examples/dvdstore/src/com/jboss/dvd/seam/ShoppingCartBean.java,v
retrieving revision 1.25
retrieving revision 1.26
diff -u -b -r1.25 -r1.26
--- ShoppingCartBean.java 10 May 2006 01:45:40 -0000 1.25
+++ ShoppingCartBean.java 17 Dec 2006 07:20:05 -0000 1.26
@@ -8,6 +8,8 @@
import static org.jboss.seam.ScopeType.SESSION;
+import java.math.BigDecimal;
+
import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
@@ -55,15 +57,15 @@
return cartSelection;
}
- public float getSubtotal() {
+ public BigDecimal getSubtotal() {
return cartOrder.getNetAmount();
}
- public float getTax() {
+ public BigDecimal getTax() {
return cartOrder.getTax();
}
- public float getTotal() {
+ public BigDecimal getTotal() {
return cartOrder.getTotalAmount();
}
1.3 +5 -4 jboss-seam/examples/dvdstore/src/com/jboss/dvd/seam/StoreManager.java
(In the diff below, changes in quantity of whitespace are not shown.)
Index: StoreManager.java
===================================================================
RCS file: /cvsroot/jboss/jboss-seam/examples/dvdstore/src/com/jboss/dvd/seam/StoreManager.java,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -b -r1.2 -r1.3
--- StoreManager.java 15 May 2006 21:33:28 -0000 1.2
+++ StoreManager.java 17 Dec 2006 07:20:05 -0000 1.3
@@ -6,6 +6,7 @@
*/
package com.jboss.dvd.seam;
+import java.math.BigDecimal;
import javax.ejb.Local;
@Local
@@ -14,5 +15,5 @@
public long getNumberOrders();
public long getUnitsSold();
public long getTotalInventory();
- public double getTotalSales();
+ public BigDecimal getTotalSales();
}
1.10 +7 -6 jboss-seam/examples/dvdstore/src/com/jboss/dvd/seam/StoreManagerBean.java
(In the diff below, changes in quantity of whitespace are not shown.)
Index: StoreManagerBean.java
===================================================================
RCS file: /cvsroot/jboss/jboss-seam/examples/dvdstore/src/com/jboss/dvd/seam/StoreManagerBean.java,v
retrieving revision 1.9
retrieving revision 1.10
diff -u -b -r1.9 -r1.10
--- StoreManagerBean.java 1 Dec 2006 13:27:52 -0000 1.9
+++ StoreManagerBean.java 17 Dec 2006 07:20:05 -0000 1.10
@@ -6,6 +6,7 @@
*/
package com.jboss.dvd.seam;
+import java.math.BigDecimal;
import java.io.Serializable;
import javax.ejb.Stateless;
@@ -30,14 +31,14 @@
.getSingleResult();
}
- public double getTotalSales() {
+ public BigDecimal getTotalSales() {
try {
- Double totalSales = (Double) em.createQuery("select sum(o.totalAmount) from Order o where o.status != :status")
+ BigDecimal totalSales = (BigDecimal) em.createQuery("select sum(o.totalAmount) from Order o where o.status != :status")
.setParameter("status", Order.Status.CANCELLED)
.getSingleResult();
- return totalSales==null ? 0.0 : totalSales;
+ return totalSales==null ? BigDecimal.ZERO : totalSales;
} catch (NoResultException e) {
- return 0.0;
+ return BigDecimal.ZERO;
}
}
More information about the jboss-cvs-commits
mailing list