Tugas Minggu Keempat Kelas PBO-C
TUGAS PBO-C Pertemuan-4 : Auction System ( Sistem lelang)
Pada pertemuan keempat kelas PBO-C, saya diberi tugas untuk membuat Auction System dengan menggunakan Blue-J. Auction System/Sistem Lelang itu sendiri mempunyai beberapa Class diantaranya : Auction, Person, Bid, dan Lot. Berikut hasil Source Codenya.
Auction Class
Pada Auction Class ini befungsi sebagai tempat perlelangan itu berlangsung atau juga berfungsi sebagai memelihara daftar barang.
Source Code Auction Class :
/**
* Auction Class
*
* Andymas Narendra Bagaskara
* 1 November 2020
*/
import java.util.ArrayList;
public class Auction
{
// instance variables
// The list of Lots in this auction
private ArrayList<Lot> lots;
// The number that will be given to the next lot entered
// into this auction
private int nextLotNumber;
/**
* Constructor for objects of class Auction
* Create a new auction
*/
public Auction()
{
// initialise instance variables
lots = new ArrayList<>();
nextLotNumber = 1;
}
/**
* Enter a new lot into the auction
*
* @param description A 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 wheter the bid is
* successful or not.
*
* @param lotNumber The lot being bid for.
* @param bidder The person bidding for the lot.
* @param value The value of the bid.
*/
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("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 the given number. Return null
* if a lot with this number doesn't exist.
*
* @param lotNumber The number of the lot to return
*/
public Lot getLot(int lotNumber)
{
if((lotNumber >= 1) && (lotNumber < nextLotNumber))
{
// The number seems to be reasonable.
Lot selectedLot = lots.get(lotNumber - 1);
// Include a confidence check to be sure we have the right lot.
if(selectedLot.getNumber() != lotNumber)
{
System.out.println("Internal error: Lot number " +
selectedLot.getNumber() +
" was returned instead of " +
lotNumber);
// Don't return an invalid lot.
selectedLot = null;
}
return selectedLot;
}
else
{
System.out.println("Lot number: " + lotNumber +
" does not exist.");
return null;
}
}
}
Lot Class
Pada Lot Class ini berfungsi sebagai menyimpan detail-detail dan harga-harga barang, bisa juga digunakan untuk melihat adanya tawaran tertinggi. jika ditemukan adanya tawaran tertinggi maka nilai dalam bid tersebut akan berubah.
Source Code Lot Class :
/**
* Lot Class
*
* Andymas Narendra Bagaskara
* 1 November 2020
*/
public class Lot
{
// instance variables
// A unique identifying number.
private final int number;
// A description of the lot.
private String description;
// The current highest bid for this lot.
private Bid highestBid;
/**
* Constructor for objects of class Lot
* Construct a Lot, setting its number and description.
* @param number The lot number.
* @param description A description of this lot.
*/
public Lot(int number, String descrition)
{
// initialise instance variables
this.number = number;
this.description = description;
this.highestBid = null;
}
/**
* 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)
{
// There is no previous bid.
highestBid = bid;
return true;
}
else if (bid.getValue() > highestBid.getValue())
{
// The bid is better than the previous one.
highestBid = bid;
return true;
}
else
{
// The bid is not better.
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 currrent bid.
*/
public Bid getHighestBid()
{
return highestBid;
}
}
Bid Class
Pada Bid Class ini berfungsi sebagai tempat Person untuk melakukan sebuah penawaran yang nantinya akan merekam nama Person dan juga jumlah uang yang di tawarkan.
Source Code Bid Class :
/**
* Bid Class
*
* Andymas Narendra Bagaskara
* 1 November 2020
*/
public class Bid
{
// instance variables
// 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;
/**
* Constructor for objects of class Bid
* Create a bid.
* @param bidder Who is bidding for the lot.
* @param value The value of the bid.
*/
public Bid(Person bidder, long value)
{
// initialise instance variables
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;
}
}
Person Class
Pada Person Class ini berfungsi sebagai objek yang melakukan penawaran pada perlelangan ini. yang dimana didalamnya terdapat nama-nama person yang melakukan penawaran tersebut.
Source Code Person Class :
/**
* Person Class
*
* Andymas Narendra Bagaskara
* 1 November 2020
*/
public class Person
{
// The name of this person.
private final String name;
/**
* Constructor for objects of class Person
* Create a new person with the given name.
* @param name The person's name.
*/
public Person(String name)
{
// initialise instance variables
this.name = name;
}
/**
* A method to return the person's name
*
* @return The person's name.
*/
public String getName()
{
return name;
}
}
Komentar
Posting Komentar