[jboss-cvs] jboss-seam/examples/seambay/src/org/jboss/seam/example/seambay ...
Shane Bryzak
sbryzak at redhat.com
Mon Jul 16 06:56:42 EDT 2007
User: sbryzak2
Date: 07/07/16 06:56:42
Modified: examples/seambay/src/org/jboss/seam/example/seambay
Auction.java Bid.java BidAction.java
Log:
bidding logic
Revision Changes Path
1.13 +60 -7 jboss-seam/examples/seambay/src/org/jboss/seam/example/seambay/Auction.java
(In the diff below, changes in quantity of whitespace are not shown.)
Index: Auction.java
===================================================================
RCS file: /cvsroot/jboss/jboss-seam/examples/seambay/src/org/jboss/seam/example/seambay/Auction.java,v
retrieving revision 1.12
retrieving revision 1.13
diff -u -b -r1.12 -r1.13
--- Auction.java 14 Jul 2007 01:56:16 -0000 1.12
+++ Auction.java 16 Jul 2007 10:56:42 -0000 1.13
@@ -30,7 +30,7 @@
private String description;
private Date endDate;
private AuctionImage image;
- private Account highBidder;
+ private Bid highBid;
private int bids;
private double price;
@@ -48,6 +48,7 @@
this.auctionId = auctionId;
}
+ @NotNull
@ManyToOne
@JoinColumn(name = "ACCOUNT_ID")
public Account getAccount()
@@ -60,6 +61,7 @@
this.account = account;
}
+ @NotNull
@ManyToOne
@JoinColumn(name = "CATEGORY_ID")
public Category getCategory()
@@ -72,6 +74,7 @@
this.category = category;
}
+ @NotNull
public String getTitle()
{
return title;
@@ -82,6 +85,7 @@
this.title = title;
}
+ @NotNull
public String getDescription()
{
return description;
@@ -115,14 +119,14 @@
this.image = image;
}
- public Account getHighBidder()
+ public Bid getHighBid()
{
- return highBidder;
+ return highBid;
}
- public void setHighBidder(Account highBidder)
+ public void setHighBid(Bid highBid)
{
- this.highBidder = highBidder;
+ this.highBid = highBid;
}
public int getBids()
@@ -224,8 +228,57 @@
}
@Transient
- public double getNextBidInterval()
+ public double getRequiredBid()
+ {
+ return getRequiredBid(getPrice());
+ }
+
+ /**
+ * Returns the amount required to outbid the specified bid amount.
+ *
+ * @param amount The current bid amount
+ * @return The bid amount required to outbid the current bid
+ */
+ @Transient
+ static double getRequiredBid(double amount)
+ {
+ if (amount < 100)
+ {
+ return Math.ceil(amount) + 1;
+ }
+ else if (amount < 200)
{
- return getPrice() + 1;
+ return Math.ceil(amount) + 2;
+ }
+ else if (amount < 500)
+ {
+ return Math.ceil(amount) + 5;
+ }
+ else if (amount < 1000)
+ {
+ return Math.ceil(amount) + 10;
+ }
+ else if (amount < 5000)
+ {
+ return Math.ceil(amount) + 20;
+ }
+ else if (amount < 20000)
+ {
+ return Math.ceil(amount) + 50;
+ }
+ else if (amount < 50000)
+ {
+ return Math.ceil(amount) + 100;
+ }
+ else if (amount < 100000)
+ {
+ return Math.ceil(amount) + 200;
+ }
+ else if (amount < 500000)
+ {
+ return Math.ceil(amount) + 500;
+ }
+
+ return Math.ceil(amount) + 1000;
}
}
1.3 +26 -10 jboss-seam/examples/seambay/src/org/jboss/seam/example/seambay/Bid.java
(In the diff below, changes in quantity of whitespace are not shown.)
Index: Bid.java
===================================================================
RCS file: /cvsroot/jboss/jboss-seam/examples/seambay/src/org/jboss/seam/example/seambay/Bid.java,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -b -r1.2 -r1.3
--- Bid.java 11 Jul 2007 14:32:33 -0000 1.2
+++ Bid.java 16 Jul 2007 10:56:42 -0000 1.3
@@ -7,6 +7,8 @@
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
+import org.hibernate.validator.NotNull;
+
@Entity
public class Bid implements Serializable
{
@@ -14,9 +16,10 @@
private Integer bidId;
private Auction auction;
- private User user;
+ private Account account;
private Date bidDate;
- private double amount;
+ private double maxAmount;
+ private double actualAmount;
@Id @GeneratedValue
public Integer getBidId()
@@ -29,6 +32,7 @@
this.bidId = bidId;
}
+ @NotNull
public Auction getAuction()
{
return auction;
@@ -39,16 +43,18 @@
this.auction = auction;
}
- public User getUser()
+ @NotNull
+ public Account getAccount()
{
- return user;
+ return account;
}
- public void setUser(User user)
+ public void setAccount(Account account)
{
- this.user = user;
+ this.account = account;
}
+ @NotNull
public Date getBidDate()
{
return bidDate;
@@ -59,14 +65,24 @@
this.bidDate = bidDate;
}
- public double getAmount()
+ public double getMaxAmount()
+ {
+ return maxAmount;
+ }
+
+ public void setMaxAmount(double maxAmount)
+ {
+ this.maxAmount = maxAmount;
+ }
+
+ public double getActualAmount()
{
- return amount;
+ return actualAmount;
}
- public void setAmount(double amount)
+ public void setActualAmount(double actualAmount)
{
- this.amount = amount;
+ this.actualAmount = actualAmount;
}
}
1.3 +62 -6 jboss-seam/examples/seambay/src/org/jboss/seam/example/seambay/BidAction.java
(In the diff below, changes in quantity of whitespace are not shown.)
Index: BidAction.java
===================================================================
RCS file: /cvsroot/jboss/jboss-seam/examples/seambay/src/org/jboss/seam/example/seambay/BidAction.java,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -b -r1.2 -r1.3
--- BidAction.java 16 Jul 2007 03:42:12 -0000 1.2
+++ BidAction.java 16 Jul 2007 10:56:42 -0000 1.3
@@ -3,8 +3,10 @@
import static org.jboss.seam.ScopeType.CONVERSATION;
import java.util.Date;
+import java.util.List;
import javax.persistence.EntityManager;
+import javax.persistence.LockModeType;
import org.jboss.seam.annotations.Begin;
import org.jboss.seam.annotations.In;
@@ -25,14 +27,13 @@
private Auction auction;
@In(required = false)
- private User authenticatedUser;
+ private Account authenticatedAccount;
@Begin(join = true)
public void placeBid()
{
bid = new Bid();
bid.setAuction(auction);
- bid.setUser(authenticatedUser);
updateBid();
}
@@ -41,17 +42,72 @@
{
double amount = Double.parseDouble(Contexts.getEventContext().get("bidAmount").toString());
- if (amount >= bid.getAuction().getNextBidInterval())
+ if (amount >= bid.getAuction().getRequiredBid())
{
- bid.setAmount(amount);
+ bid.setMaxAmount(amount);
}
}
+ @SuppressWarnings("unchecked")
public String confirmBid()
{
+ // We set the user here because the user may not be authenticated when placeBid() is called.
+ bid.setAccount(authenticatedAccount);
bid.setBidDate(new Date());
+ // This is where the tricky bidding logic happens
+
+ entityManager.lock(bid.getAuction(), LockModeType.WRITE);
+ entityManager.refresh(bid.getAuction());
+
+ List<Bid> bids = entityManager.createQuery(
+ "from Bid b where b.auction = :auction")
+ .setParameter("auction", bid.getAuction())
+ .getResultList();
+
+ Bid highBid = null;
+
+ for (Bid b : bids)
+ {
+ if (highBid == null)
+ {
+ highBid = b;
+ }
+ else if (b.getMaxAmount() > highBid.getMaxAmount())
+ {
+ highBid.setActualAmount(highBid.getMaxAmount());
+ b.setActualAmount(Auction.getRequiredBid(highBid.getMaxAmount()));
+ highBid = b;
+ }
+ else if (b.getMaxAmount() == highBid.getMaxAmount() &&
+ b.getBidDate().getTime() < highBid.getBidDate().getTime())
+ {
+ highBid.setActualAmount(highBid.getMaxAmount());
+ b.setActualAmount(highBid.getMaxAmount());
+ highBid = b;
+ }
+ }
+
+ if (highBid == null)
+ {
+ // There are no bids so far...
+ bid.setActualAmount(bid.getAuction().getRequiredBid());
+ bid.getAuction().setHighBid(bid);
+ }
+ else if (bid.getMaxAmount() > highBid.getMaxAmount())
+ {
+ bid.setActualAmount(Auction.getRequiredBid(highBid.getMaxAmount()));
+ bid.getAuction().setHighBid(bid);
+ }
+ else
+ {
+ bid.setActualAmount(bid.getMaxAmount());
+ }
+
+ bid.getAuction().setBids(bid.getAuction().getBids() + 1);
+
entityManager.persist(bid);
+ entityManager.flush();
Conversation.instance().end();
return "success";
@@ -64,6 +120,6 @@
public boolean isValidBid()
{
- return bid != null && bid.getAmount() >= bid.getAuction().getNextBidInterval();
+ return bid != null && bid.getMaxAmount() >= bid.getAuction().getRequiredBid();
}
}
More information about the jboss-cvs-commits
mailing list