Sunday, October 7, 2018

Tugas PBO-A: Membuat Auction System (Sistem Lelang)

Nama; Muhammad Naufal Refadi
NRP: 05111740000097
Kelas: PBO-A


Ini adalah tugas rumah saya membuat Auction System atau bisa disebut sistem lelang dengan menggunakan Java.

Berikut Formatted Source code saya:
a) Person
 public class Person   
  {   
   // The name of this person.   
   private final String name;   
   /**   
   * Create a new person with the given name.   
   */   
   public Person(String name)   
   {   
    this.name = name;   
   }   
   /**   
   * @return The Person's name*   
   */   
   public String getName()   
   {   
    return name;   
   }   
  }   

b) Bid
 public class Bid   
  {   
   private final Person bidder;   
   private final long value;   
   /**   
   * Create a 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;   
   }   
  }   

c) Lot
 public class Lot   
  {   
   private final int number;    
   private String description;    
   private Bid highestBid;   
   /**   
    * Construct a Lot, setting its number and description.    
   */   
   public Lot(int number, String description)   
   {   
    this.number = number;   
    this.description = description;   
   }   
   /**   
   * Attempt to bid for this lot. A successful bid   
   * must have a value higher than any existing bid.    
   * @return true if successful, false otherwise.   
   */   
   public boolean bidFor(Bid bid)   
   {   
    if((highestBid == null)||(bid.getValue() > highestBid.getValue()))   
    {   
     highestBid = bid;   
     return true;   
    }   
    else{   
     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;   
   }    
   public String getDescription()   
   {   
    return description;   
   }   
   public Bid getHighestBid()   
   {   
    return highestBid;   
   }   
  }   

d)Auction
 /**  
  * Auction Systems  
  * @author Muhammad Naufal Refadi  
  * @version 7 October 2018  
  */  
 import java.util.ArrayList;   
  public class Auction   
  {    
   private ArrayList<Lot> lots;    
   private int nextLotNumber;    
   public Auction()   
   {   
    lots = new ArrayList<Lot>();   
    nextLotNumber = 1;   
   }   
   public void enterLot(String description)   
   {   
    lots.add(new Lot(nextLotNumber, description));   
    nextLotNumber++;   
   }    
   public void showLots()   
   {   
    for(Lot lot : lots)   
    {   
     System.out.println(lot.toString());   
    }   
   }   
   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   
     {    
      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))   
    {   
     Lot selectedLot = lots.get(lotNumber-1);   
     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;   
    }   
   }   
   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());   
     }   
    }   
   }   
  }   

Screenshot jalannya dan output program

Pertama klik class person dan klik newperson() untuk membuat nama siapa aja yang akan mengikuti acara lelang. Anggap saja nama orangnya orang1,orang2, dan orang 3


Lalu klik class auction dan klik enterslot() untuk memasukkan nama orang untuk mengikut acara lelang.

lalu klik void makeABid()(untuk memberikan tawaran seharga yang diinput



Jika kita ingin melihat hasil tawaran sementara bisa menggunakan void showLots() 

klik void close() untuk menutup lelangan dan mengeluarkan hasil akhirnya

No comments:

Post a Comment