1. Class Window
2. Instances
3. Source Code
import java.util.ArrayList;
/**
* Auction Class.
*
* @author (Arino Jenynof)
* @version (20181008)
*/
public class Auction
{
private ArrayList<Lot> lots;
private int nextLotNumber;
public Auction()
{
lots = new ArrayList<Lot>();
nextLotNumber = 1;
}
public void enterLot(String desc)
{
lots.add(new Lot(nextLotNumber, desc));
nextLotNumber++;
}
public void showLot()
{
for (Lot lot : lots)
{
System.out.println(lot.toString());
}
}
public void makeABid(int lotNum, Person bidder, long value)
{
Lot selectedLot = getLot(lotNum);
if (selectedLot != null)
{
Bid bid = new Bid(bidder, value);
boolean success = selectedLot.bidFor(bid);
if (success)
{
System.out.println(bidder.getName() + "'s bid for lot number " + lotNum + " was successful");
}
else
{
Bid highestBid = selectedLot.getHighestBid();
System.out.println("Lot number: " + lotNum + " already has a bid of " + highestBid.getValue() + ", " + bidder.getName() + "'s bid unsuccessful");
}
}
}
public Lot getLot(int lotNum)
{
if (lotNum >= 1 && lotNum < nextLotNumber)
{
Lot selectedLot = lots.get(lotNum - 1);
if (selectedLot.getNumber() != lotNum)
{
System.out.println("Internal Error: Lot number " + selectedLot.getNumber() + " was returned instead of " + lotNum);
selectedLot = null;
}
return selectedLot;
}
else
{
System.out.println("Lot number: " + lotNum + " doesn't exist");
return null;
}
}
public void stop()
{
System.out.println("Auction is finished!");
for (Lot lot : lots)
{
if (lot.getHighestBid() != null)
{
System.out.println("Lot number: " + lot.getNumber() + " sold with price of " + lot.getHighestBid().getValue() + " to " + lot.getHighestBid().getBidder().getName());
}
else
{
System.out.println("Lot number: " + lot.getNumber() + " didn't sell");
}
}
}
}
/**
* Lot Class;
*
* @author (Arino Jenynof)
* @version (20181008)
*/
public class Lot
{
private int lotNumber;
private Bid highestBid;
private String desc;
public Lot(int lotNumber, String desc)
{
highestBid = null;
this.lotNumber = lotNumber;
this.desc = desc;
}
public boolean bidFor(Bid bid)
{
if (highestBid == null)
{
highestBid = bid;
return true;
}
else if (highestBid.getValue() < bid.getValue())
{
highestBid = bid;
return true;
}
else
{
return false;
}
}
public Bid getHighestBid()
{
return highestBid;
}
public int getNumber()
{
return lotNumber;
}
public String toString()
{
return "Lot number: " + lotNumber + " is " + desc;
}
}
/**
* Bid Class.
*
* @author (Arino Jenynof)
* @version (20181008)
*/
public class Bid
{
private long value;
private Person bidder;
public Bid(Person bidder, long value)
{
this.bidder = bidder;
this.value = value;
}
public long getValue()
{
return value;
}
public Person getBidder()
{
return bidder;
}
}
/**
* Person Class.
*
* @author (Arino Jenynof)
* @version (20101008)
*/
public class Person
{
private String name;
public Person(String name)
{
this.name = name;
}
public String getName()
{
return name;
}
}
4. Output
Setelah memasukkan lot melalui method enterLot(), menjalankan method showLot() akan menunjukkan semua lot yang sudah dimasukkan
Menggunakan method makeABid() untuk melakukan pelelangan
stop() method untuk menghentikan pelelangan
Comments
Post a Comment