Tugas kali ini merupakan program mengenai pelelangan.
Class Auction:
Class Lot:
Class Person:
Class Bid:
Class Auction:
import java.util.ArrayList;
public class Auction
{
private ArrayList<Lot> lots; //list barang yang akan dilelang
private int nextLotNumber;
public Auction(){
lots = new ArrayList<Lot>();
nextLotNumber = 1;
}
public void enterLot(String description){//masukan barang baru
lots.add(new Lot(nextLotNumber, description));
nextLotNumber++;
}
public void showLots(){//menampilkan semua barang
for(Lot lot : lots){
System.out.println(lot.toString());
}
}
public void makeBid(int lotNumber, Person bidder, long value){
Lot selectedLot = getLot(lotNumber);
if(selectedLot != null){
Bid bid = new Bid(bidder,value);
boolean successful = selectedLot.bidFor(bid);
if(successful){
System.out.println(bidder.getName() + " berhasil melakukan penawaran.");
}
else{
//penawar tertinggi
Bid highestBid = selectedLot.getHighestBid();
System.out.println("Lot Number: " + lotNumber + "Penawaran: " + highestBid.getValue());
}
}
}
public Lot getLot(int lotNumber){
if((lotNumber >= 1) && (lotNumber < nextLotNumber)){
//nomor yang mungkin
Lot selectedLot = lots.get(lotNumber - 1);
if(selectedLot.getNumber() != lotNumber){
System.out.println("Error: Lot number " + selectedLot.getNumber() + "was retuened instead of " + lotNumber);
selectedLot = null;
}
return selectedLot;
}
else{
System.out.println("Lot number: " + lotNumber + "does't exist.");
return null;
}
}
public void close(){
System.out.println("Lelang Selesai.");
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());
}
}
}
}
Class Lot:
public class Lot
{
private Bid highestBid; //Bid terbesar untuk Barang ini
private final int number;
private String description;
public Lot(int number, String description){
this.number = number;
this.description = description;
}
public boolean bidFor(Bid bid){
if(highestBid == null){
highestBid = bid; //Bid awal
return true;
}
else if(bid.getValue() > highestBid.getValue()){
highestBid = bid; //Bid lebih tinggi
return true;
}
else{
//Bid lebih rendah
return false;
}
}
public String toString(){
String details = number + ": " + description;
if(highestBid != null){
details += " Penawaran: " + highestBid.getValue();
}
else{
details += " Tidak ada penawaran";
}
return details;
}
public int getNumber(){
return number;
}
public String getDescription(){
return description;
}
public Bid getHighestBid(){
return highestBid;
}
}
Class Person:
public class Person
{
private final String name;
public Person(String name){
this.name = name;
}
public String getName(){
return name;
}
}
Class Bid:
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 getValue(){
return value;
}
}
Komentar
Posting Komentar