コード例 #1
0
 @Override
 public boolean registerBid(double value, int uniqueUserId, int auctionId) throws RemoteException {
   RMIClientIntf clientIntf = registeredUsers.get(uniqueUserId);
   Bid bid = new Bid(value, uniqueUserId);
   if (!liveAuctionItems.containsKey(auctionId)) {
     clientIntf.callBack("Auction item id does not exist\n");
     return false;
   }
   if (uniqueUserId == liveAuctionItems.get(auctionId).getCreator_id()) {
     clientIntf.callBack("Not able to register bid - bidder is the creator of this auction\n");
     return false;
   } else {
     if (liveAuctionItems.get(auctionId).addBidder(bid)) {
       clientIntf.callBack(
           "Bid registered to auction with id "
               + auctionId
               + " and amount "
               + bid.getBid()
               + "\n");
       return true;
     } else {
       clientIntf.callBack(
           "Bid rejected, user has already placed a bid for this auction, can only place higher bids\n");
       return false;
     }
   }
 }
コード例 #2
0
  @Override
  public void notifyResult(AuctionItem item) {
    if (registeredUsers.containsKey(item.getCreator_id())) {
      RMIClientIntf creator = registeredUsers.get(item.getCreator_id());
      try {
        creator.callBack("Your Auction has produced the following results :\n ");
        creator.callBack(item.getResult());
      } catch (RemoteException e) {
        System.out.println("Creator no longer online\n");
      }
    } else {
      System.out.println("Creator id does not exist\n");
    } // Normally an auction with no creator registered should not exist but since this is a
    // simplified implementation
    // We allow bids on auctions with no creator (usually the initialised ones) for testing
    // purposes.
    if (item.numberOfBids() != 0) {
      System.out.println("Notifying bidders");
      Iterator<Bid> it = item.getBidders();
      while (it.hasNext()) {
        Bid bidder = it.next();
        if (registeredUsers.containsKey(bidder.getUserId())) {
          RMIClientIntf client = registeredUsers.get(bidder.getUserId());
          try {
            client.callBack(item.getResult());
          } catch (RemoteException e) {
            System.out.println("Bidder with id " + bidder.getUserId() + " no longer online\n");
          }

        } else {
          System.out.println("User id does not exist\n");
        }
      }
    }
  }