Faisal Reza Maulana 05111940000009
Auction atau dalam bahasa Indonesia lelang, adalah proses membeli dan menjual barang atau jasa dengan cara menawarkan kepada penawar, menawarkan tawaran harga lebih tinggi, dan kemudian menjual barang kepada penawar harga tertinggi. Berikut adalah contoh implementasi program lelang sederhana.
- Diagram Objek
2. Class Person
/**
* Orang yang ingin menawar
*
* @author Faisal 051119-009
*/
public class Person {
//her/ his name
private static String name;
//create a new person with the given name
public Person(String name) {
Person.name = name;
}
//@return the person's name
public String getName(){
return name;
}
}
3. Class Bid
/**
* Class untuk menawar barang
*
* @author Faisal 051119-009
*/
public class Bid {
//the one making the bid
private final Person bidder;
//the value of the bid, could be a large number, so use long
private final long value;
//create a bid
public Bid(Person bidder, long value){
this.bidder = bidder;
this.value = value;
}
//@return the bidder
public Person getBidder(){
return bidder;
}
//@return the value of the bid
public long getValue() {
return value;
}
}
4. Class Lot
/**
* Barang yang ditawar
*
* @author Faisal 051119-009
*/
public class Lot {
//a unique identifier of the lot
private final int number;
//description of the lot
private final String description;
//current highest bid
private Bid highestBid;
//construct a lot, setting its number and description
public Lot(int number, String description){
this.number = number;
this.description = description;
this.highestBid = null;
}
//attempt a bid
public boolean bidFor(Bid bid){
if(highestBid == null){
highestBid = bid;
return true;
}else if(bid.getValue() > highestBid.getValue()){
highestBid = bid;
return true;
}else{
return false;
}
}
//@return the details of this lot
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, could be null if no bid
public Bid getHighestBid(){
return highestBid;
}
}
5. Class Auction
import java.util.ArrayList;
/**
* Class utama auction
*
* @author Faisal 051119-009
*/
public class Auction {
//the list of lots in this auction
private final ArrayList<Lot> lots;
//number that'll be given to the next lot
private int nextLotNumber;
//create a new auction
public Auction(){
lots = new ArrayList<>();
nextLotNumber = 1;
}
//enter a new lot, @param the description of the lot
public void enterLot(String description){
lots.add(new Lot(nextLotNumber, description));
nextLotNumber++;
}
//show the full list of lots in this auction
public void showLots(){
for(Lot lot: lots){
System.out.println(lot.toString());
}
}
//make a bid for a lot
//a message is printed indicating whether a bid is successful or not
public void makeABid(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{
//report which bid is higher
Bid highestBid = selectedLot.getHighestBid();
System.out.println("Lot number " + lotNumber + " already has a bid of: " + highestBid.getValue());
}
}
}
//return the lot with given number, return null if it doesn't exist
public Lot getLot(int lotNumber){
if((lotNumber >= 1) && (lotNumber < nextLotNumber)){
Lot selectedLot = lots.get(lotNumber - 1);
//confidence check
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;
}
}
//close the auction. print the final status of each lot
public void close(){
System.out.println("Closing auction.");
for(Lot lot: lots){
System.out.println(lot.getNumber() + ": " + lot.getDescription());
if(lot.getHighestBid() == null){
System.out.println(" (No bids) ");
}else{
Bid highestBid = lot.getHighestBid();
System.out.println(" sold to " + highestBid.getBidder().getName() + " for " + highestBid.getValue());
}
}
}
}
6. Output Terminal