Sunday, October 7, 2018

Tugas PBO - Auction System

10:56 AM Posted by Unknown No comments
Class yang digunakan :
  1. Class Auction, sebagai class utama tempat proses auction (pelelangan) dilakukan
  2. Class Lot, berfungsi mendaftarkan dan menyimpan deskripsi lot
  3. Class Person, berfungsi tempat menyimpat data / informasi orang
  4. Class Bid, berfungsi tempat menyimpan nilai bid
  • Class Auction
 /**  
  * Write a description of class Auction here.  
  *  
  * @author Bima S. Ramadhan  
  * @version 1.0  
  */  
 import java.util.ArrayList;  
 public class Auction  
 {  
   private ArrayList<Lot> lots;  
   private int nextLotNumber;  
   //Constructor  
   public Auction()  
   {  
     lots = new ArrayList<Lot>();  
     nextLotNumber = 1;  
   }  
   //Mendaftarkan lot baru  
   public void enterLot(String description)  
   {  
     lots.add(new Lot(nextLotNumber, description));  
     nextLotNumber++;  
   }  
   //Print Semua Lot  
   public void showLots()  
   {  
     for(Lot lot : lots) {  
       System.out.println(lot.toString());  
     }  
   }  
   //Melakukan Bid pada suatu Lot  
   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("Bid untuk Lot pada urutan " +  
          lotNumber + " sukses didaftarkan.");  
       }  
       else {  
         Bid highestBid = selectedLot.getHighestBid();  
         System.out.println("Lot urutan ke: " + lotNumber +  
         " memiliki Bid sebesar: " +  
         highestBid.getValue());  
       }  
     }  
   }  
   //Mencari Lot yang terdaftar  
   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 urutan ke " +  
         selectedLot.getNumber() +  
         " dimunculkan, sebagai ganti dari " +  
         lotNumber);  
         selectedLot = null;  
       }  
       return selectedLot;  
     }  
     else {  
       System.out.println("Lot urutan ke: " + lotNumber +  
       " tidak ditemukan.");  
       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());    
          }    
     }    
   }     
 }  

  • Class Lot
 /**  
  * Write a description of class Lot here.  
  *  
  * @author Bima S. Ramadhan  
  * @version 1.0  
  */  
 public class Lot  
 {  
   //ID Lot  
   private final int number;  
   //Deskripsi dari sebuah Lot  
   private String description;  
   //Bid tertinggi dari Lot  
   private Bid highestBid;  
   //Constructor  
   public Lot(int number, String description)  
   {  
     this.number = number;  
     this.description = description;  
     this.highestBid = null;  
   }  
   public boolean bidFor(Bid bid)  
   {  
     if(highestBid == null) {  
       //Bid belum ada  
       highestBid = bid;  
       return true;  
     }  
     else if(bid.getValue() > highestBid.getValue()) {  
       //Bid lebih besar dari nilai Bid sebelumnya  
       highestBid = bid;  
       return true;  
     }  
     else {  
       //Bid lebih kecil dari nilai Bid sebelumnya  
       return false;  
     }  
   }  
   //Deskripsi rincian Lot  
   public String toString()  
   {  
     String details = number + ": " + description;  
     if(highestBid != null) {  
       details += "  Bid: " +  
             highestBid.getValue();  
     }  
     else {  
       details += "  (No bid)";  
     }  
     return details;  
   }  
   //Return ID Lot  
   public int getNumber()  
   {  
     return number;  
   }  
   //Return Deskripsi Lot  
   public String getDescription()  
   {  
     return description;  
   }  
   //Return Bid tertinggi  
   public Bid getHighestBid()  
   {  
     return highestBid;  
   }  
 }  

  • Class Person
 /**  
  * Write a description of class Person here.  
  *  
  * @author Bima S. Ramadhan  
  * @version 1.0  
  */  
 public class Person  
 {  
   //variable final untuk nama orang (1 kali inisialisasi)  
   private final String personname;  
   //constructor  
   public Person(String name)  
   {  
     this.personname = name;  
   }  
   //value berupa nama orang  
   public String getName()  
   {  
     return personname;  
   }  
 }  

  • Class Bid 
 /**  
  * Write a description of class Bid here.  
  *  
  * @author Bima S. Ramadhan  
  * @version 1.0  
  */  
 public class Bid  
 {  
   //variable yang hanya bisa diinisialisasi 1 kali  
   private final Person biddername;  
   private final long value;  
   public Bid(Person name, long v)  
   {  
     this.biddername = name;  
     this.value = v;  
   }  
   //value nama bidder  
   public Person getBidder()  
   {  
     return biddername;  
   }  
   //value berupa nilai bid  
   public long getValue()  
   {  
     return value;  
   }  
 }  






0 comments:

Post a Comment