Tugas PBO-C Pertemuan ke-Empat "Auction System"

November 03, 2020 0

     Pada tugas kali ini, saya akan membuat sebuah program sederhana berupa Auction System. Program Auction System ini mengimplementasikan program pelelangan barang kepada banyak orang dimana barang akan dilelang kepada orang yang menawar barang tersebut dengan harga tertinggi. Pada program ini terdapat beberapa class, yaitu class Auction, Lot, Bid, dan Person. 

        Class Auction berfungsi sebagai tempat berlangsungnya pelelangan barang. Class Lot berfungsi untuk menyimpan penjelasan dari berbagai barang, seperti deskripsi barang dan tawaran tertinggi. Class Bid berfungsi sebagai tempat untuk melakukan penawaran. Class Person berfungsi untuk menyimpan nama-nama orang yang melakukan penawaran.

Source Code Class "Main"     :

import java.util.ArrayList;
 
public class Main {
    // The list of lots in this auction.
    private ArrayList<Lot> lots;
   
    // The number that will be given to the next lot entered
    // into this auction.
    private int nextLotNumber;
   
    /**
     * Create a new auction.
     */
    public Main () {
        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.getNumber() + "." + lot.toString());
        }
    }
   
    /**
     * Make a bid for a lot.
     * A message is printed indicating wether the bid is
     * successful or not.
     * @param lotNumber The lot being bid for.
     * @param bidder The person biddng 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 lor 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;
        }
    }
   
    /**
      * Remove the lot with the given lot number.
     * @param number The number of the lot to be removed.
     * @return The Lot with the given number, or null if
     * there is no such lot.
     */
    public Lot removeLot(int number) {
        for (Lot lot : lots) {
            if (lot.getNumber() > number) {
                lot.changeLotNumber();
            }
        }
       
        return lots.remove(number - 1);
    }
   
    // Closing method for the auction.
    public void close () {
        if (lots.size() >= 1) {
            for (Lot lot : lots) {
                if (lot.getHighestBid() != null) {
                    System.out.print("Lot number " + lot.getNumber() +
                                     " has been sold with value of: "
                                     + lot.getHighestBid().getValue());
                    System.out.println(". The winner of the auction for " +
                                       "this lot is: " +
                                       lot.getHighestBid().infoBidder().getName()
                                       + ".");
                } else {
                    System.out.println("Lot number " + lot.getNumber() +
                                       " has no bidder,");
                }
            }
        } else {
            System.out.println("This auction has no lot");
        }
    }
}

Source Code Class "Lot"        :

public class Lot {
    private Bid highestBid;     // the current highest bid for this lot.
    private int lotNumber;      // this lot number.
    private String description; // description of this lot.
   
    // This is constuctor
    public Lot (int number, String desc) {
        lotNumber = number;
        description = desc;
    }
   
    /**
     * 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 bid successful, false otherwise.
     */
    public boolean bidFor (Bid bid) {
        if (highestBid == null) {
            // There is no prevous 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 bid is not better
            return false;
        }
    }
   
    // Return the highest bid for this lot.
    public Bid getHighestBid () {
        return highestBid;
    }
   
    // Return number of this lot.
    public int getNumber () {
        return lotNumber;
    }
   
    public String toString () {
        return description;
    }
   
    public void changeLotNumber () {
        lotNumber--;
    }
}

Source Code Class "Bid"        :

public class Bid {
    private long value;
    private Person bidder;
   
    // This is constructor.
    public Bid (Person thisBidder, long val) {
        bidder = thisBidder;
        value = val;
    }
   
    // Return the value on this bid.
    public long getValue () {
        return value;
    }
   
    // To get information of bidder, use this method
    public Person infoBidder () {
        return bidder;
    }
}

Source Code Class "Person"   :

public class Person {
    private String name;
   
    public Person (String namePerson) {
        name = namePerson;
    }
   
    public String getName () {
        return name;
    }
}


Cara Menggunakan "Auction System" ini :





















Demikian jika ada kesalahan mohon dikoreksi. Terima kasih telah berkunjung 😁

0 Comments for "Tugas PBO-C Pertemuan ke-Empat "Auction System""