Nama : Edgar Andrew Hutauruk
NRP : 05111740000166
Kelas : PBO-B
Auction
Lot
Bid
Person
NRP : 05111740000166
Kelas : PBO-B
Auction
import java.util.ArrayList;
/**
* Write a description of class Auction here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class Auction
{
private ArrayList<Lot> lots;
private int nextLotNumber;
//Constructor
public Auction()
{
lots = new ArrayList<Lot>();
nextLotNumber = 1;
}
//Mendaftarkan lot baru
public void enterLot(String description)
{
lots.add(new Lot(nextLotNumber, description));
nextLotNumber++;
}
//Print Semua Lot
public void showLots()
{
for(Lot lot : lots)
{
System.out.println(lot.toString());
}
}
//Melakukan Bid pada suatu Lot
public void makeABid(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("Bid untuk Lot pada urutan " + lotNumber + " sukses didaftarkan.");
}
else
{
Bid highestBid = selectedLot.getHighestBid();
System.out.println("Lot urutan ke: " + lotNumber + " memiliki Bid sebesar: " + highestBid.getValue());
}
}
}
//Mencari Lot yang terdaftar
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 urutan ke " +selectedLot.getNumber() + " dimunculkan, sebagai ganti dari " +lotNumber);
selectedLot = null;
}
return selectedLot;
}
else
{
System.out.println("Lot urutan ke: " + lotNumber +" tidak ditemukan.");
return null;
}
}
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());
}
}
}
}
Lot
/**
* Write a description of class Lot here.
*
* @author (Edgar Andrew Hutauruk)
* @version (a version number or a date)
*/
public class Lot
{
//ID Lot
private final int number;
//Deskripsi dari sebuah Lot
private String description;
//Bid tertinggi dari Lot
private Bid highestBid;
//Constructor
public Lot(int number, String description)
{
this.number = number;
this.description = description;
this.highestBid = null;
}
public boolean bidFor(Bid bid)
{
if(highestBid == null)
{
//Bid belum ada
highestBid = bid;
return true;
}
else if(bid.getValue() > highestBid.getValue())
{
//Bid lebih besar dari nilai Bid sebelumnya
highestBid = bid;
return true;
}
else
{
//Bid lebih kecil dari nilai Bid sebelumnya
return false;
}
}
//Deskripsi rincian Lot
public String toString()
{
String details = number + ": " + description;
if(highestBid != null)
{
details += " Bid: " +
highestBid.getValue();
}
else
{
details += " (No bid)";
}
return details;
}
//Return ID Lot
public int getNumber()
{
return number;
}
//Return Deskripsi Lot
public String getDescription()
{
return description;
}
//Return Bid tertinggi
public Bid getHighestBid()
{
return highestBid;
}
}
Bid
/**
* Write a description of class Bid here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class Bid
{
//variable yang hanya bisa diinisialisasi 1 kali
private final Person biddername;
private final long value;
public Bid(Person name, long v)
{
this.biddername = name;
this.value = v;
}
//value nama bidder
public Person getBidder()
{
return biddername;
}
//value berupa nilai bid
public long getValue()
{
return value;
}
}
Person
/**
* Write a description of class Person here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class Person
{
//variable final untuk nama orang (1 kali inisialisasi)
private final String personname;
//constructor
public Person(String name)
{
this.personname = name;
}
//value berupa nama orang
public String getName()
{
return personname;
}
}
Komentar
Posting Komentar