Tugas PBO A - Auction System

Kali ini tugas PBO A yaitu membuat Auction System.

Nama : Ifta Jihan N (05111740000034)
Kelas : PBO A

Terdapat 4 class yang saya pakai, yaitu:
1. Auction
2. Person
3. Bid
4. Lot

Source code:

1. Auction 
 import java.util.ArrayList;  
 /**  
  * Auction.  
  * @author Ifta Jihan N (05111740000034)  
  * PBO A 07/10/2018  
  */  
 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;    
   public Auction() //create a new 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++; //description. A description of the lot  
   }   
   //Show the full list of lots in this auction    
   public void showLots()   
   {   
    for(Lot lot : lots)   
    {   
     System.out.println(lot.lotDetails());   
    }   
   }   
   //Make a bid for a lot   
   //A message is printed indicating whether the bid is succesful or not    
   public void makeABid(int lotNumber, Person bidder, long value)   
   {   
    //lotNumber. The lot being bid for  
    // bidder. The person bidding for the lot  
    // value The value of the bid  
    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 doesn't exist    
   public Lot getLot(int lotNumber)   
   { //lotNumber. The number of the lot to return  
    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 for this Lot");   
     }   
     else   
     {   
      Bid highestBid = lot.getHighestBid();   
      System.out.println(" sold to " + highestBid.getBidder().getName() + " for " + highestBid.getValue());   
     }   
    }   
   }   
 }  


2. Person
 /**  
  * Person.  
  * @author Ifta Jihan N (05111740000034)   
  * PBO A 07/10/2018  
  */  
 public class 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;   
   }   
 }  


3. Bid
 /**  
  * Bid.  
  * @author Ifta Jihan N (05111740000034)  
  * PBO A 07/10/2018  
  */  
 public class Bid  
 {  
   //The person making the bid   
   private final Person bidder;   
   //The value of the bid.   
   private final long value;   
   //Create a bid   
   public Bid(Person bidder, long value)   
   {   
    this.bidder = bidder; //bidder. who is bidding for the lot  
    this.value = value;  //value. The value of the bid  
   }   
   //return the bidder  
   public Person getBidder()   
   {   
    return bidder;   
   }   
   //return the value of the bid  
   public long getValue()   
   {   
    return value;   
   }   
 }  


4. Lot
 /**  
  * Lot.  
  * @author Ifta Jihan N(05111740000034)  
  * PBO A 07/10/2018  
  */  
 public class Lot //class to model an item (or set of items) in an auction  
 {  
   //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.    
   public Lot (int number, String description)   
   {   
    this.number = number; //The lot number  
    this.description = description; //A description of this lot  
    this.highestBid = null;   
   }   
   //Attempt to bid for this lot.   
   //A succesful bid must have a value higher than any existing bid.    
   public boolean bidFor(Bid bid)   
   {   
    if (highestBid==null)   
    {   
     //There is no previous bid   
     highestBid=bid; //A new bid  
     return true; //return true if succesful  
    }   
    else if (bid.getValue() > highestBid.getValue())   
    {   
     //The bid is better than the previous one.   
     highestBid = bid;   
     return true;   
    }   
    else   
    {   
     //The bid is not better   
     return false;   
    }   
   }   
   //return A string representation of this lot's details.    
   public String lotDetails()   
   {   
    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  
   public Bid getHighestBid()   
   {   
    return highestBid;   
   }   
 }  


Hasil Output:
Class dari project saya



 Memasukkan nama orang yang ingin menjadi bidder


 Memasukkan barang yang akan dilelang

 Memasukkan harga yang ditawarkan oleh person1




Ada yang ingin menawarkan harga lagi, yaitu person2


Lelang ditutup 

Comments

Popular Posts