import java.util.ArrayList;
public class Auction
{
private ArrayList<Lot> lotsArr;
private int LotIndex;
//Inisiasi auction/lelang
public Auction()
{
lotsArr = new ArrayList<Lot>();
LotIndex = 1;
}
//Memasukkan barang yg akan di lelang
public void enterLot(String lotName)
{
lotsArr.add(new Lot(LotIndex, lotName));
LotIndex++;
}
//Menunjukkan seluruh list barang yang sedang dilelang
public void showlotsArr()
{
for(Lot lot : lotsArr) {
System.out.println(lot.detail());
}
}
//Mencari lot berdasarkan ID atau index
public Lot getLot(int CurrentLotIndex)
{
if((CurrentLotIndex > 0) && (CurrentLotIndex < LotIndex)) {
Lot selectedLot = lotsArr.get(CurrentLotIndex - 1);
if(selectedLot.getId() != CurrentLotIndex) {
System.out.println("Internal error : Lot number " +
selectedLot.getId() +
" has been returned, Not a Lot number " +
CurrentLotIndex);
selectedLot = null;
}
return selectedLot;
}
else {
System.out.println("Lot number : " + CurrentLotIndex +
" doesn\'t exist.");
return null;
}
}
//Melakukan bid
public void MakeBid(int CurrentLotIndex, Person bidder, long value)
{
Lot selectedLot = getLot(CurrentLotIndex);
if(selectedLot != null) {
boolean check = selectedLot.bidFor(new Bid(bidder, value));
if(check) {
System.out.println("Bid for Lot number " +
CurrentLotIndex + " Success, Bidden by " +
bidder.getName()+ " with price " +value);
}
else {
Bid highestBid = selectedLot.getHighestBid();
System.out.println("Lot number :\\t" + CurrentLotIndex +
"\\nwith the highest Bid:\\t" +
highestBid.getBid());
}
}
}
//Menutup pelelangan dan menampilkan siapa yang menang lelang
public void close()
{
System.out.println("Auction has ended.");
for(Lot lot : lotsArr)
{
System.out.println(lot.getId() + ": " +lot.getLotName());
Bid bid = lot.getHighestBid();
if (bid==null)
{
System.out.println("There is no bid for this Lot");
}
else
{
System.out.println("This Lot has been sold to " +
bid.getBidder().getName() + " with price :\\t"
+ bid.getBid());
}
}
}
}