Membuat program lelang - PBO A
1. Enter Lot
2. Lot
3. Person
4. Bid
2. Enter person
3. Start Bid
4. Close Bid
Source Code :
1. Auction :
import java.util.ArrayList;
import java.util.Iterator;
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("The bid for lot number " + lotNumber +
" was successful.");
}
else {
Bid highestBid = selectedLot.getHighestBid();
System.out.println("Lot number: " + lotNumber +
" already has a bid of: " + highestBid.getValue());
}
}
}
/**
* Return a list of the unsold lots.
*/
public void close()
{
System.out.println("The auction is closed.");
for(Lot lot : lots) {
System.out.println(lot.getNumber() + ": " +lot.getDescription());
Bid bid = lot.getHighestBid();
if (bid==null){
System.out.println("(No Bids for this lot.)");
}
else {
System.out.println( "sold to " +
bid.getBidder().getName() + " for "
+ bid.getValue());
}
}
}
public Lot getLot(int lotNumber)
{
if((lotNumber >= 1) && (lotNumber < nextLotNumber)) {
Lot selectedLot = lots.get(lotNumber - 1);
if(selectedLot.getNumber() != lotNumber) {
System.out.println("Internal error: lot number " +
selectedLot.getNumber() + " was returned instead of " +
lotNumber);
selectedLot = null;
}
return selectedLot;
}
else {
System.out.println("lot number: " + lotNumber + " does not exist.");
return null;
}
}
}
2. 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;
}
/**
* Attempt to bid for this lot. A successful bid
* must have a value higher than any existing bid.
* @param bid A new bid.
* @return true if successful, false otherwise.
*/
public boolean bidFor(Bid bid)
{
if((highestBid == null)||(bid.getValue() > highestBid.getValue()))
{
// This bid is the best so far
highestBid = bid;
return true;
}
else{
return false;
}
}
/**
* @return A string representation of this lot's details.
*/
public String toString()
{
String details = number + ": " + description;
if(highestBid!=null) {
details+= " Bid: " +highestBid.getValue();
}
else {
details += " (No bid)";
}
return details;
}
/**
* @return The lot's number.
*/
public int getNumber()
{
return number;
}
/**
* @return The lot's description.
*/
public String getDescription()
{
return description;
}
/**
* @return The highest bid for this lot.
* This could be null if there is no current bid.
*/
public Bid getHighestBid()
{
return highestBid;
}
}
3. Person
public class Person
{
// The name of this person.
private final String name;
/**
* Create a new person with the given name.
* @param name The person's name.
*/
public Person(String name)
{
this.name = name;
}
/**
* @return The Person's name*
*/
public String getName()
{
return name;
}
}
4. Bid
public class Bid
{
// The person making the bid
private final Person bidder;
// The value of the bid. This could be a large number so the long type has been used
private final long value;
public Bid(Person bidder, long value)
{
this.bidder = bidder;
this.value = value;
}
public Person getBidder()
{
return bidder;
}
long getValue()
{
return value;
}
}
Komentar
Posting Komentar