Implementasi Simple Auction di BlueJ IDE

Hai pada kesempatan kali ini saya ingin berbagi sedikit ilmu yang telah saya dapat pada pertemuan keenam Pemrograman Berbasis Objek bersama Dosen Kami Bapak Fajar Baskoro. Jadi pada Implementasi kali ini kita ingin menampilkan program seperti gambar berikut.

nah selanjutnya merupakan Class-Class yang berhubungan dengan program kali ini, masri kita simak

1. Person

1:  /**  
2:   * Person Class  
3:   *  
4:   * @author annas  
5:   */  
6:  public class Person  
7:  {  
8:    private final String name;  
9:    public Person(String name)  
10:    {  
11:      this.name = name;  
12:    }  
13:    public String getName()  
14:    {  
15:      return name;  
16:    }  
17:  }  

2. Bid

1:  /**  
2:   * Class Bid  
3:   *  
4:   * @author annas  
5:   */  
6:  public class Bid  
7:  {  
8:    private final long value;  
9:    private final Person bidder;  
10:    public Bid(Person bidder, long value)  
11:    {  
12:      this.bidder = bidder;  
13:      this.value = value;  
14:    }  
15:    public Person getBidder()  
16:    {  
17:      return bidder;  
18:    }  
19:    public long getValue()  
20:    {  
21:      return value;  
22:    }  
23:  }  

3. Lot

1:  /**  
2:   * Class Lot  
3:   *  
4:   * @author annas  
5:   */  
6:  public class Lot  
7:  {  
8:    private final int number;  
9:    private String description;  
10:    private Bid highestBid;  
11:    public Lot(int number, String description)  
12:    {  
13:      this.number = number;  
14:      this.description = description;  
15:    }  
16:    public boolean bidFor(Bid bid)  
17:    {  
18:      if((highestBid == null) ||   
19:          (bid.getValue() > highestBid.getValue())){  
20:            highestBid = bid;  
21:            return true;  
22:      }  
23:      else{  
24:        return false;  
25:      }  
26:    }  
27:    public String toString()  
28:    {  
29:      String details = number + ": " + description;  
30:      if(highestBid != null){  
31:        details += "  Bid: " + highestBid.getValue();  
32:      }  
33:      else{  
34:        details +="  (No Bid)";  
35:      }  
36:      return details;  
37:    }  
38:    public int getNumber()  
39:    {  
40:      return number;  
41:    }  
42:    public String getDescription()  
43:    {  
44:      return description;  
45:    }  
46:    public Bid getHighestBid()  
47:    {  
48:      return highestBid;  
49:    }  
50:  }  

4. Auction

1:  import java.util.ArrayList;  
2:  /**  
3:   * Class Auction  
4:   *  
5:   * @author annas  
6:   */  
7:  public class Auction  
8:  {  
9:    private ArrayList<Lot> lots;  
10:    private int nextLotNumber;  
11:    public Auction()  
12:    {  
13:      lots = new ArrayList<Lot>();  
14:      nextLotNumber = 1;  
15:    }  
16:    public void enterLot(String description)  
17:    {  
18:      lots.add(new Lot(nextLotNumber, description));  
19:      nextLotNumber++;  
20:    }  
21:    public void showLots()  
22:    {  
23:      for(Lot lot : lots) {  
24:        System.out.println(lot.toString());  
25:      }  
26:    }  
27:    public void bidFor(int lotNumber, Person bidder, long value)  
28:    {  
29:      Lot selectedLot = getLot(lotNumber);  
30:      if(selectedLot != null) {  
31:        boolean successful = selectedLot.bidFor(new Bid(bidder, value));  
32:        if(successful) {  
33:          System.out.println("The bid for lot number " +  
34:                    lotNumber + " was successful.");  
35:        }  
36:        else {  
37:          // Report which bid is higher.  
38:          Bid highestBid = selectedLot.getHighestBid();  
39:          System.out.println("Lot number: " + lotNumber +  
40:                    " already has a bid of: " +  
41:                    highestBid.getValue());  
42:        }  
43:      }  
44:    }  
45:    public Lot getLot(int lotNumber)  
46:    {  
47:      if((lotNumber >= 1) && (lotNumber < nextLotNumber)) {  
48:        Lot selectedLot = lots.get(lotNumber - 1);  
49:        if(selectedLot.getNumber() != lotNumber) {  
50:          System.out.println("Internal error: Lot number " +  
51:                    selectedLot.getNumber() +  
52:                    " was returned instead of " +  
53:                    lotNumber);  
54:          selectedLot = null;  
55:        }  
56:        return selectedLot;  
57:      }  
58:      else {  
59:        System.out.println("Lot number: " + lotNumber +  
60:                  " does not exist.");  
61:        return null;  
62:      }  
63:    }  
64:  }  
Hanya itu yang bisa saya bagi pada kesempatan kali ini.

Komentar

Postingan populer dari blog ini

Pong Java Game

EAS PBO-B

Image Viewer versi 3