Auction

Tugas Pemrograman Berbasis Objek (PBO)
Membuat Sistem Lelang

Kali ini saya akan membuat suatu sistem lelang menggunakan BlueJ. Berikut source code dan tampilan akhirnya. Saya membutuhkan lima class dalam hal ini, yaitu Auction, Bid, Lot, Person, dan Auction Test.

1. Source Code Auction

 import java.util.ArrayList;  
 /**  
  * A simple model of an auction  
  *The auction mantains a list of lots of arbitatry length  
  * @author Yuki Yanuar Ratna  
  * @version 2018.10.04  
  */  
 public class Auction  
 {  
   //The list of Lots in this auction  
   private ArrayList<Lot> lots;  
   //The number that will be given to the next lot entered  
   private int nextLotNumber;  
   /**  
    * Create a new auction  
    */  
   public Auction()  
   {  
     lots = new ArrayList<Lot>();  
     nextLotNumber = 1;  
   }  
   /**  
    * Enter a new lot into the auction  
    * @param description A description of the lot  
    */    
   public void enterLot(String description)  
   {  
     lots.add(new Lot(nextLotNumber, description));  
     nextLotNumber++;  
   }  
   /**  
    * Show the full list of lots in this auction  
    */  
   public void showLots()  
   {  
     for(Lot lot : lots)  
     {  
       System.out.println(lot.toString());  
     }  
   }  
   /**  
    * Make a bid for a lot  
    * A message is printed indicating whether the bid is  
    * succesfulor not  
    * @param lotNumber The lot being bid for.  
    * @param bidder The person bidding for the lot.  
    * @param value The value of the bid.  
    */  
   public void makeABid(int lotNumber, Person bidder, long value)  
   {  
     Lot selectedLot = getLot(lotNumber);  
     if (selectedLot != null)  
     {  
       boolean succesful = selectedLot.bidFor(new Bid(bidder, value));  
       if (succesful)  
       {  
         System.out.println("The bid for lot number " + lotNumber + " was succesful.");  
       }  
       else  
       {  
         //Report which bid is higher  
         Bid highestBid = selectedLot.getHighestBid();  
         System.out.println("Lot number: " + lotNumber + " already has a bid of: " + highestBid.getValue());  
       }  
     }  
   }  
   /**  
    * Return the lot with the given number. Return null   
    * if a lot with this number does not exist.  
    * @param lotNumber The number of the lot to return.  
    */  
   public Lot getLot(int lotNumber)  
   {  
     if((lotNumber >= 1) && (lotNumber < nextLotNumber))  
     {  
       //The number seems to be reasonable  
       Lot selectedLot = lots.get(lotNumber-1);  
       //Include a confidence check to be sure we have the right lot  
       if (selectedLot.getNumber() != lotNumber)  
       {  
         System.out.println("Internal error: Lot number " + selectedLot.getNumber() + " was returned instead of " + lotNumber);  
         selectedLot = null;  
       }  
       return selectedLot;  
     }  
     else  
     {  
       System.out.println("Lot number: " + lotNumber + " does not exist.");  
       return null;  
     }  
   }  
   /**  
    * Close an auction. Print the final status of each lot  
    */  
   public void close()  
   {  
     System.out.println("Closing auction.");  
     for (Lot lot : lots)  
     {  
       System.out.println(lot.getNumber() + ": " + lot.getDescription());  
       if (lot.getHighestBid() == null)  
       {  
         System.out.println (" (No bids) ");  
       }  
       else  
       {  
         Bid highestBid = lot.getHighestBid();  
         System.out.println(" sold to " + highestBid.getBidder().getName() + " for " + highestBid.getValue());  
       }  
     }  
   }  
 }  

2. Source Code Bit

 /**  
  * A class that models an auction bid  
  *  
  * @author Yuki Yanuar Ratna  
  * @version 2018.10.04  
  */  
 public class Bid  
 {  
   //The person making the bid  
   private final Person bidder;  
   //The value of the bid. This could be a large number so  
   //the long type has been used  
   private final long value;  
   /**  
    * Create a bid  
    * @param bidder Who is bidding for the lot  
    * @param value The value of the bid  
    */  
   public Bid(Person bidder, long value)  
   {  
     this.bidder = bidder;  
     this.value = value;  
   }  
   /**  
    * @return The bidder.  
    */  
   public Person getBidder()  
   {  
     return bidder;  
   }  
   /**  
    * @return The value of the bid.  
    */  
   public long getValue()  
   {  
     return value;  
   }  
 }  

3. Source Code Lot

 /**  
  * A class to model an item (or set of items) in an   
  *auction: a lot.  
  *  
  * @author Yuki Yanuar Ratna  
  * @version 2018.10.04  
  */  
 public class Lot  
 {  
   //A unique identifying number.  
   private final int number;  
   //A description of the lot  
   private String description;  
   //The current highest bid for this lot.  
   private Bid highestBid;  
   /**  
    * Construct a Lot, setting its number and description.  
    * @param number The lot number.  
    * @param description A description of this lot.  
    */  
   public Lot (int number, String description)  
   {  
     this.number = number;  
     this.description = description;  
     this.highestBid = null;  
   }  
   /**  
    * Attempt to bid for this lot.   
    * A succesful bid must have a value higher than any existing bid.  
    * @param bid A new bid  
    * @return true if succesful. false otherwise  
    */  
   public boolean bidFor(Bid bid)  
   {  
     if (highestBid==null)  
     {  
       //There is no previous bid  
       highestBid=bid;  
       return true;  
     }  
     else if (bid.getValue() > highestBid.getValue())  
     {  
       //The bid is better than the previous one.  
       highestBid = bid;  
       return true;  
     }  
     else  
     {  
       //The biid is not better  
       return false;  
     }  
   }  
   /**  
    * @return A string representation of this lot's details.  
    */  
   public String toString()  
   {  
     String details = number + ": " + description;  
     if (highestBid != null)  
     {  
       details += " Bid: " + highestBid.getValue();  
     }  
     else  
     {  
       details += " (No bid))";  
     }  
     return details;  
   }  
   /**  
    * @return The lot's number.  
    */  
   public int getNumber()  
   {  
     return number;  
   }  
   /**  
    * @return The lot's description.  
    */  
   public String getDescription()  
   {  
     return description;  
   }  
   /**  
    * @return The highest bid for this lot.  
    *     This could be null if there is no current bid.  
    */  
   public Bid getHighestBid()  
   {  
     return highestBid;  
   }  
 }  

4. Source Code Person

 /**  
  * Maintain details of someone who participates in an auction  
  *  
  * @author Yuki Yanuar Ratna  
  * @version 2018.10.04  
  */  
 public class Person  
 {  
   //The name of this person.  
   private final String name;  
   /**  
    * Create a new person with the given name.  
    * @param name The person's name.  
    */  
   public Person (String name)  
   {  
     this.name = name;  
   }  
   /**  
    * @return The person's name.  
    */  
   public String getName()  
   {  
     return name;  
   }  
 }  

5. Source Code AuctionTest

 import static org.junit.Assert.*;  
 import org.junit.After;  
 import org.junit.Before;  
 import org.junit.Test;  
 /**  
  * The test class AuctionTest.  
  *  
  * @author Yuki Yanuar Ratna  
  * @version 2018.10.04  
  */  
 public class AuctionTest  
 {  
   private Auction auction1;  
   /**  
    * Default constructor for test class AuctionTest  
    */  
   public AuctionTest()  
   {  
   }  
   /**  
    * Sets up test fixture.  
    *   
    * Called before every test case method.  
    */  
   @Before  
   public void setUp()  
   {  
     auction1 = new Auction();  
   }  
   /**  
    * Tears down the test fixture  
    *   
    * Called after every test case method.   
    */  
   @After  
   public void tearDown()  
   {  
   }  
   @Test  
   public void testClose()  
   {  
     //Make some lots  
     auction1.enterLot("The book of Michael Purba");  
     auction1.enterLot("Painting of a monalisa");  
     auction1.enterLot("Gold cup");  
     //Show the entered lots  
     auction1.showLots();  
     //Make a bidder  
     Person person1 = new Person("Source Hunter");  
     //Make a bid  
     auction1.makeABid(2, person1, 1000);  
     //Show lots to check bid  
     auction1.showLots();  
     //Close auctions  
     auction1.close();  
   }  
 }  

6. Tampilan Akhir

Sekian, semoga bermanfaat

Komentar

Postingan populer dari blog ini

Tugas Membuat Jam Digital

Codeigniter "Toko Buah"

Personal Web