Auction System menggunakan BlueJ? Sederhana Saja!

Minggu ini saya diberikan sebuah tugas untuk melengkapi sebuah program untuk Auction System atau pelelangan. Cara kerjanya adalah kita memasukkan barang untuk dilelang, kemudian menambahkan harga penawaran barang. Setelah itu, kita menawar harga barang tersebut. Program ini masih sederhana, dan bersumber dari e-book yang digunakan untuk kelas PBO.

Tampilan

Class Diagram

Input Bidder

 

 
Input Lot 


 

Input Bid

 
Penutupan Lelang


Source Code

Lot

 /**  
  * @author Indira Nursyamsina Hazimi  
  */  
 public class Lot  
 {  
   // The current highest bid for this lot.  
   private final int id;  
   private String description;  
   private Bid highestBid;  
   //Make a new Lot with id and description  
   public Lot(int numb, String description){  
     this.id = numb;  
     this.description = description;  
   }  
   /**  
   * Attempt to bid for this lot. A successful bid  
   * must have a value higher than any existing bid.  
   * @param bid A new bid.  
   * @return true if successful, false otherwise*/  
   public boolean bidFor(Bid bid)  
   {  
     if(highestBid == null){  
       //There is no previous bid.  
       highestBid = bid;  
       return true;  
     }  
     else if(bid.getBid() > highestBid.getBid()){  
       //The bid is better than the previous one.  
       highestBid = bid;  
       return true;  
     }  
     else{  
       //The bid is not better.  
       return false;  
     }  
   }  
   //To return the lot's id and description  
   public String detail(){  
     String details = id + ":" + description;  
     if(highestBid != null){  
       details += "  Bid : " + highestBid.getBid();  
     }  
     else{  
       details += "  (No bid)";  
     }  
     return details;  
   }  
   //To return Lot's id  
   public int getId(){  
     return id;  
   }  
   //To return Lot's description  
   public String getDescription(){  
     return description;  
   }  
   //To return the highest bid value  
   public Bid getHighestBid(){  
     return highestBid;  
   }  
 }  

Person

 /**  
  * @author Indira Nursyamsina Hazimi  
  */  
 public class Person  
 {  
   //Create new string for person's name  
   private final String name;  
   public Person(String bidName){  
     this.name = bidName;  
   }  
   //To return person's name  
   public String getName(){  
     return name;  
   }  
 }  

Bid

 /**  
  * @author Indira Nursyamsina Hazimi  
  */  
 public class Bid  
 {  
   private final Person bidder;  
   private final long value;  
   //Create new bidder with names and bid value  
   public Bid(Person bidder, long value){  
     this.bidder = bidder;  
     this.value = value;  
   }  
   //To return bidder's name  
   public Person getBidder(){  
     return bidder;  
   }  
   //To return bidder's bid value  
   public long getBid(){  
     return value;  
   }  
 }  

Auction

 /**  
  * @author Indira Nursyamsina Hazimi  
  * A simple model of an auction  
  * The auction maintains a list of lots of arbitary length.  
 */  
 import java.util.ArrayList;  
 public class Auction  
 {  
   //The list of Lots in this auction.  
   private ArrayList<Lot> lots;  
   //The number that will be given to the nect lot entered into this auction  
   private int nextLotNumber;  
   //Create new auction  
   public Auction(){  
     lots = new ArrayList<Lot>();  
     nextLotNumber = 1;  
   }  
   //Enter a new lot into the auction  
   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.detail());  
     }  
   }  
   //Make a bid for a lot  
   //A message is printed indicating whether the bid is successful or not  
   public void makeABid(int lotNumber, Person bidder, long value){  
     Lot selectedLot = getLot(lotNumber);  
     if(selectedLot != null){  
       Bid bid = new Bid(bidder, value);  
       boolean successful = selectedLot.bidFor(bid);  
       if(successful){  
         System.out.println("The bid for lot number " + lotNumber + " was succesdful.");  
         System.out.println("The bid is belong to " + bidder.getName());  
       }  
       else{  
         //Report which bid is higher  
         Bid highestBid = selectedLot.getHighestBid();  
         System.out.println("Lot number " + lotNumber + " already has a bid of " + highestBid.getBid());  
       }  
     }  
   }  
   //Return the lot with the given number.  
   //Return null if a lot with this number does not exist.  
   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.getId() != lotNumber){  
         System.out.println("Internal error: Lot number " + selectedLot.getId() + " was returned instead of " + lotNumber);  
         selectedLot = null;  
       }  
       return selectedLot;  
     }  
     else{  
       System.out.println("Lot number: " + lotNumber + " does not exist.");  
       return null;  
     }  
   }  
   //Close the auction  
   public void close(){  
     System.out.println("Auction is closed.");  
     for(Lot lot : lots){  
       System.out.println(lot.getId() + ": " + lot.getDescription());  
       Bid bid = lot.getHighestBid();  
       if(bid == null){  
         System.out.println("There are no more Bids for this lot.");  
       }  
       else{  
         System.out.println("This Item has been sold to " + bid.getBidder().getName() + "  Price : " + bid.getBid());  
       }  
     }  
   }  
 }  

Komentar

Postingan populer dari blog ini

Halaman Berita? HTML & CSS!

Menggunakan CodeIgniter!!

Sistem Informasi Cinema 21 - APSI