Auction
import java.util.ArrayList;
/**
* class Auction
*
* @author (Ismail)
* @version (09102018)
*/
public class Auction
{
private ArrayList<Lot> lots;
private int LotNumber;
//Membuat Auction baru
public Auction()
{
lots = new ArrayList<Lot>();
LotNumber = 1;
}
//Menambahkan barang baru
public void enterLot(String description)
{
lots.add(new Lot(LotNumber, description));
LotNumber++;
}
//Menampilkan barang yang dilelang
public void showLots()
{
for(Lot lot : lots) {
System.out.println(lot.detail());
}
}
//Melakukan tawaran
public void MakeBid(int CurrentlotNumber, Person bidder, long value)
{
Lot selectedLot = getLot(CurrentlotNumber);
if(selectedLot != null)
{
boolean succes = selectedLot.bidFor(new Bid(bidder, value));
if(succes)
{
System.out.println("The bid for lot number " +CurrentlotNumber+ " was successful.");
}
else
{
Bid highestBid = selectedLot.getHighestBid();
System.out.println("Lot number: " +CurrentlotNumber+ " already has a bid of: " +highestBid.getBid());
}
}
}
public Lot getLot(int CurrentlotNumber)
{
if((CurrentlotNumber >= 1) && (CurrentlotNumber < LotNumber))
{
Lot selectedLot = lots.get(CurrentlotNumber - 1);
if(selectedLot.getId() != CurrentlotNumber)
{
System.out.println("Internal error: Lot number " +selectedLot.getId()+ " was returned instead of " +CurrentlotNumber);
selectedLot = null;
}
return selectedLot;
}
else
{
System.out.println("Lot number : " + CurrentlotNumber + " does not exist.");
return null;
}
}
}
Person
/**
* class Person
*
* @author (Ismail)
* @version (09102018)
*/
public class Person
{
private final String name;
public Person(String newName)
{
this.name = newName;
}
public String getName()
{
return name;
}
}
Lot
/**
* Class Lot
*
* @author (Ismail)
* @version (09102018)
*/
public class Lot
{
private final int Id;
private String description;
private Bid highestBid;
public Lot(int number, String description)
{
this.Id = number;
this.description = description;
}
public boolean bidFor(Bid bid)
{
if((highestBid ==null) || (bid.getBid() > highestBid.getBid()))
{
highestBid = bid;
return true;
}
else
{
return false;
}
}
public String detail()
{
String details = Id+ ": " +description;
if(highestBid != null)
{
details += " Bid: " +highestBid.getBid();
}
else
{
details += " (No Bid)";
}
return details;
}
public int getId()
{
return Id;
}
public String getDescription()
{
return description;
}
public Bid getHighestBid()
{
return highestBid;
}
}
Bid
/**
* class Bid
*
* @author (Ismail)
* @version (09102018)
*/
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 getBid()
{
return value;
}
}
Tidak ada komentar:
Posting Komentar