Advertisement
mnaufaldillah

Auction Tugas 3

Oct 24th, 2020
293
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.25 KB | None | 0 0
  1. import java.util.ArrayList;
  2.  
  3. /**
  4.  * A simple model of an auction.
  5.  * The auction maintains a list of lots of arbitrary length.
  6.  *
  7.  * @author Muhammad Naufaldillah
  8.  * @version 22 October 2020
  9.  */
  10. public class Auction
  11. {
  12.     // The list of Lots in this auction.
  13.     private ArrayList<Lot> lots;
  14.     // The number that will be given to the next lot entered
  15.     // into this auction
  16.     private int nextLotNumber;
  17.    
  18.     /**
  19.      * Creat a new auction.
  20.      */
  21.     public Auction()
  22.     {
  23.         lots = new ArrayList<Lot>();
  24.         nextLotNumber = 1;
  25.     }
  26.    
  27.     /**
  28.      * Enter a new lot into the auction.
  29.      * @param description A description of the lot.
  30.      *
  31.      */
  32.     public void enterLot(String description)
  33.     {
  34.         lots.add(new Lot(nextLotNumber, description));
  35.         nextLotNumber++;
  36.     }
  37.    
  38.     /**
  39.      * Show the full list of lots in this auction
  40.      */
  41.     public void showLots()
  42.     {
  43.         for(Lot lot : lots)
  44.         {
  45.             System.out.println(lot.toString());
  46.         }
  47.     }
  48.    
  49.     /**
  50.      * Make a bid for a lot.
  51.      * A message is printed indicating whether the bid os
  52.      * successful or not
  53.      *
  54.      * @param lotNumber The lot being bid for.
  55.      * @param bidder The person bidding for the lot.
  56.      * @param value The value of the bid.
  57.      */
  58.     public void makeAbid(int lotNumber, Person bidder, long value)
  59.     {
  60.         Lot selectedLot = getLot(lotNumber);
  61.         if(selectedLot != null)
  62.         {
  63.             Bid bid = new Bid(bidder, value);
  64.             boolean successful = selectedLot.bidFor(bid);
  65.             if(successful)
  66.             {
  67.                 System.out.println("The bid for lot number" +
  68.                                     lotNumber + " was suecessful.");
  69.             }
  70.             else
  71.             {
  72.                 // Report which bid is higher
  73.                 Bid highestBid = selectedLot.getHighestBid();
  74.                 System.out.println("Lot number: " + lotNumber +
  75.                                    " already has a bid of: " +
  76.                                    highestBid.getValue());
  77.             }
  78.         }
  79.     }
  80.    
  81.     /**
  82.      * Return the lot with given number. Return null
  83.      * if a lot with this number does not exist.
  84.      * @param lotNUmber The number of the lot to return
  85.      */
  86.     public Lot getLot(int lotNumber)
  87.     {
  88.         if((lotNumber >= 1) && (lotNumber < nextLotNumber))
  89.         {
  90.             // The number seems to be reasonable.
  91.             Lot selectedLot = lots.get(lotNumber - 1);
  92.             // Include aconfidence check to be sure we have the
  93.             // right lot.
  94.             if (selectedLot.getNumber() != lotNumber)
  95.             {
  96.                 System.out.println("Internal error: Lot number " +
  97.                                    selectedLot.getNumber() +
  98.                                    " was returned instead of " +
  99.                                    lotNumber);
  100.                 // Don't return an invalid lot.
  101.                 selectedLot = null;
  102.             }
  103.             return selectedLot;
  104.         }
  105.         else
  106.         {
  107.             System.out.println("Lot number: " + lotNumber +
  108.                                " does not exist.");
  109.             return null;
  110.         }
  111.     }
  112. }
  113.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement