Langsung ke konten utama

Tugas PBO Auction Simulator

Hey, guys!
Today I'm so excited to show you how to implement an Auction Simulator into Java.
We are going to use 4 classes, which are:
  1. Auction
  2. Bid
  3. Lot
  4. Person
You can check the source code of each classes below before we try to execute this program.
  1. Auction

  2.    
     /**  
      * A simple model of an auction  
      *  
      * @author Elkana Hans Widersen  
      * @version 02.10.2018  
      */  
     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 next lot entered  
       // into this auction.  
       private int nextLotNumber;  
         
       /**  
        * Creates a new auction.  
        */  
       public Auction() {  
         lots = new ArrayList<Lot>();  
         nextLotNumber = 1;  
       }  
         
       /**  
        * Enters 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++;  
       }  
         
       /**  
        * Shows the full list of lots in this auction.  
        */  
       public void showLots() {  
         for(Lot lot:lots) System.out.println(lot.toString());  
       }  
         
        /**   
       * Return a list of the unsold lots.   
       */   
       public void close(){   
         System.out.println("The auction is closed.");   
         for(Lot lot : lots) {   
           System.out.println(lot.getNumber() + ": " +lot.getDescription());   
           Bid bid = lot.getHighestBid();   
           if (bid == null){   
             System.out.println("(No Bids for this lot.)");   
           }   
           else {   
             System.out.println( "sold to " + bid.getBidder().getName() + " for " + bid.getValue());   
           }   
         }   
       }   
         
       /**  
        * Makes a bid for a lot.  
        * A message is printed indicating whether the bid is  
        * successful or not.  
        *   
        * @param lotNumber = The lot number of the lot being bid for.  
        * @param bidder = The person bidding for the lot.  
        * @param value = The value of te bid.  
        */  
       public void makeABid(int lotNumber, Person bidder, long value) {  
         //Searches the lot in the list and assigns it to a new variable.  
         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 successful.");  
           else {  
             Bid highestBid = selectedLot.getHighestBid();  
             System.out.println("lot number: " + lotNumber + " already has a bid of: " + highestBid.getValue());  
           }  
         }  
       }  
         
       /**  
        * Returns the lot with the given number.  
        * Returns 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;  
         }  
       }  
     }  
       
    
  3. Lot

       
     /**  
      * A class to model an item (or set of items) in an auction: a lot  
      *  
      * @author (Elkana Hans Widersen)  
      * @version (02.10.2018)  
      */  
     public class Lot {  
       // 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;  
         
       /**  
        * Constructs a Lot, setting its number and description.  
        * @param number = The lot number.  
        * @param description = A description of this lot.  
        */  
       public Lot(int number, String description) {  
         this.number = number;  
         this.description = description;  
       }  
         
       /**  
        * Attempts 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) || (bid.getValue() > highestBid.getValue())) {  
           highestBid = bid;  
           return true;  
         }  
         else return false;  
       }  
         
       /**  
        * @return detail = 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;  
       }  
         
       /**  
        * @return The lot's description.  
        */  
       public String getDescription() {  
         return description;  
       }  
         
       /**  
        * @return The highest bid for this lot.  
        *     This could be null if there is no current bid.  
        */  
       public Bid getHighestBid() {  
         return highestBid;  
       }  
     }  
       
    
  4. Bid

       
     /**  
      * A class that models an auction bid.  
      * It contains a reference to the Person bidding and the amount bid.  
      *   
      * @author (Elkana Hans Widersen)  
      * @version (02.10.2018)  
      */  
     public class Bid {  
       // The person making the bid.  
       private final Person bidder;  
         
       // The value of the bid. This could be a large number so  
       // the long type has been used.  
       private final long value;  
         
       /**  
        * Creates a bid.  
        * @param bidder = Who is bidding for the lot.  
        * @param value = The value of the 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;  
       }  
     }  
       
    
  5. Person

       
     /**  
      * A simple model of a person  
      *  
      * @author Elkana Hans Widersen  
      * @version 02.10.2018  
      */  
     public class Person {  
       // The name of this person.  
       private final String name;  
         
       /**  
        * Create a new person with the given name.  
        * @param name = The person's name.  
        */  
       public Person(String name) {  
         this.name = name;  
       }  
         
       /**  
        * @return The person's name.  
        */  
       public String getName() {  
         return name;  
       }  
     }  
       
    

HOW TO USE

  1. Right-click on the Auction Class Box then click on new Auction(). We'll use auction1 for the new auction name. Then you'll see a red box appear below with the name you just typed.
  2. Right-click on the red box and then click on method void enterLot(String description). We shall input the name of the stuff we want to include into auction. For this example, I would like to name "ASUS Laptop" for the first lot.
  3. Repeat Step 2 with a different name. We'll use "Bluetooth Speaker" for the next lot.
  4. Click on method void showLots() to print the lots from previous steps. Now we have two stuffs that we can sell.
  5. Let's register our bidders! Right-click on the Person Class Box and then click on new Person(). You can use any name, but I'll use my name for now. Notice that a new red box called person1 appear next to the auction1 box. 
  6. An auction feels lonely without a competitor. Repeat Step 5 with a different name. Now we have 3 red boxes in our arsenal.
  7. We have 2 people in the room, so let's get started! Click on the method void MakeABid(int lotNumber, Person bidder, long value) on the first red box (auction1) to make the first bid. We'll bid for the laptop, so enter into the lotNumber with a value 1, bidder with a value person1, and value with a value 500. 
    If you've done this successfully, the program will notify you more or less to be something like this:
  8. Repeat Step 7, this time with a value (2, person2, 200) into each respective attributes.
  9. Click on method void showLots() to see the changes we just made.
  10. Let's add a little bit of competition in there. Let's make a bid with a value (1, person2, 700), and yet another bid with a value (2, person1, 200).
  11. Whoops! This time the last bid we just made didn't make it through because the value is not greater than the highest bid registered in the lot. Too bad. The program will notify you about this, though.
  12. Click on method void showLots() to make sure we changes the highest bid on the laptop into 700.
  13. Perhaps that will be enough. Let's end the auction by clicking void close() on the auction1 box. Let's see the result of the auction we just did.
Phew! What an auction! Too bad I wasn't able to obtain one of the stuffs in there. But that was fun! You guys can have fun trying this at home, maybe with your friends and family. Or you can just experiment with the source code to make your own version. Be creative.

Thank you and see you next time! 

Komentar

Postingan populer dari blog ini

Tugas 3 PBO A

Hey guys! This time I have been working on making my own Ticket Machine using BlueJ. I am using 2 classes, which is Main and TicketMachine . TicketMachine /* * This class contains the declaration of the Ticket Machine we are going to use.. * * @author (Elkana Hans Widersen) * @version (17/9/2018) */ import java.util.Scanner; public class TicketMachine{ Scanner scan = new Scanner(System.in); // The price of a ticket from this machine. private int price; // The amount of money entered by a customer so far. private int balance; // The amount of tickets about to be printed. private int ticket; // The total amount of money collected by this machine. private int total; /* * Create a machine that issues tickets of the given price. * Note that the price must be greater than zero, and there * are no checks to ensure this. */ public TicketMachine(int ticketCost){

Fox and Rabbit Simulator

Hi guys! I'm going to show you a Fox and Rabbit Simulator made in BlueJ. We're going to use these classes Simulator SimulatorView Location Field FieldStats Counter Randomizer Rabbit Fox Here is the source code of those classes: Simulator import java.util.Random; import java.util.List; import java.util.ArrayList; import java.util.Iterator; import java.awt.Color; /** * A simple predator-prey simulator, based on a rectangular field * containing rabbits and foxes. * * @author Elkana Hans Widersen * @version 1.0 */ public class Simulator { /** * Constants representing configuration information for the simulation. */ private static final int def_width = 50; // The default width for the grid. private static final int def_depth = 50; // The default depth of the grid. private static final double foxProbability = 0.02; // The probability that a fo