Apa itu auction system? Yap, system tersebut adalah kalau di bahasa kita adalah lelang. Kali ini, kita mau membahas pelelangan tanah
Langsung saja, ini dia kodingannya:
Class Bid:
Dokumentasi pada saat berjalan:
Itu saja dari mimin, semoga bermanfaat....
Langsung saja, ini dia kodingannya:
Class Bid:
public class Bid
{
private final Person bidder;
private final long value;
public Bid(Person bidder, long value)
{
this.bidder = bidder;
this.value = value;
}
public Person getBidder()
{
return bidder;
}
public long getValue()
{
return value;
}
}
Class Person: public class Person
{
private final String name;
public Person(String name)
{
this.name = name;
}
public String getName()
{
return name;
}
}
Class Lot: public class Lot
{
private final int number;
private String description;
private Bid highestBid;
public Lot(int number, String description)
{
this.number = number;
this.description = description;
}
public boolean bidFor(Bid bid)
{
if((highestBid == null) ||
(bid.getValue() > highestBid.getValue())) {
highestBid = bid;
return true;
}
else {
return false;
}
}
public String toString()
{
String details = number + ": " + description;
if(highestBid != null) {
details += " Penawaran: " +
highestBid.getValue();
}
else {
details += " (Tidak ada penawaran)";
}
return details;
}
public int getNumber()
{
return number;
}
public String getDescription()
{
return description;
}
public Bid getHighestBid()
{
return highestBid;
}
}
Class Auction: import java.util.ArrayList;
public class Auction
{
private ArrayList<Lot> lots;
private int nextLotNumber;
public Auction()
{
lots = new ArrayList<Lot>();
nextLotNumber = 1;
}
public void enterLot(String description)
{
lots.add(new Lot(nextLotNumber, description));
nextLotNumber++;
}
public void showLots()
{
for(Lot lot : lots) {
System.out.println(lot.toString());
}
}
public void bidFor(int lotNumber, Person bidder, long value)
{
Lot selectedLot = getLot(lotNumber);
if(selectedLot != null) {
boolean successful = selectedLot.bidFor(new Bid(bidder, value));
if(successful) {
System.out.println("Penawaran untuk tanah nomor " +
lotNumber + " sukses.");
}
else {
Bid highestBid = selectedLot.getHighestBid();
System.out.println("Tanah nomor: " + lotNumber +
" sudah ditawar dengan harga: " +
highestBid.getValue());
}
}
}
public Lot getLot(int lotNumber)
{
if((lotNumber >= 1) && (lotNumber < nextLotNumber)) {
Lot selectedLot = lots.get(lotNumber - 1);
if(selectedLot.getNumber() != lotNumber) {
System.out.println("Kesalahan internal: Tanah nomor: " +
selectedLot.getNumber() +
" dikembalikan dari " +
lotNumber);
selectedLot = null;
}
return selectedLot;
}
else {
System.out.println("Tanah nomor: " + lotNumber +
" tidak ada.");
return null;
}
}
}
Dokumentasi pada saat berjalan:
Itu saja dari mimin, semoga bermanfaat....
Komentar
Posting Komentar