Wednesday, October 2, 2024

Pertemuan 5 - Auction System

Nama: Muhammad Risyad Himawan Putra

NRP: 5025231205

Kelas: Pemrograman Berbasis Obyek (G)


 Auction System in BlueJ


Source Code Github


Class Auction
public class Auction
{
private int nextLotNumber;
public Auction()
{
lots = new ArrayList();
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) {
if(selectedLot.bidFor(new Bid(bidder, value))) {
System.out.println("The bid for lot number " +
lotNumber + " was successful.");
}
else {
System.out.println("Lot number: " + lotNumber +
" already has a bid of: " +
selectedLot.getHighestBid().getValue());
}
}
}
public Lot getLot(int lotNumber)
{
if((lotNumber >= 1) && (lotNumber < nextLotNumber)) {
Lot selectedLot = (Lot) lots.get(lotNumber - 1);
if(selectedLot.getNumber() != lotNumber) {
System.out.println("Internal error: " +
"Wrong lot returned. " +
"Number: " + lotNumber);
}
return selectedLot;
}
else {
System.out.println("Lot number: " + lotNumber +
" does not exist.");
return null;
}
}
}

Class ini mengelola seluruh proses auction. Berfungsi untuk menyimpan lot, menerima penawaran,
dan menampilkan status dari lot yang dilelang.

Class Lot
public class Lot
{
public Lot(int number, String description)
{
this.number = number;
this.description = description;
}
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;
}
}
public String toString()
{
String details = number + ": " + description;
if(highestBid != null) {
details += " Bid: " +
highestBid.getValue();
}
else {
details += " (No bid)";
}
return details;
}
public int getNumber()
{
return number;
}
public String getDescription()
{
return description;
}
public Bid getHighestBid()
{
return highestBid;
}
}

Class ini sebagai wadah untuk seluruh barang-barang yang akan dilelang. Berfungsi untuk
memasukkan nama barang yang dilelamg dan menyimpan bid tertinggi dari person.

Class Bid

public class Bid
{
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;
}
}

Class ini mewakili penawaran dalam lelang, yang mencakup informasi tentang penawar dan
jumlah uang yang ditawarkan.

Class Person

public class Person
{
private final String name;
public Person(String name)
{
this.name = name;
}
public String getName()
{
return name;
}
}

Class ini menyimpan identitas seseorang yang berperan sebagai penawar di sebuah lelang.



Cara Kerja

1. Buat sebuah auction.


2. Buat lot. Lot yang telah dibuat dapat ditampilkan dengan fungsi ShowLots()





3. Buat profile untuk para pelelang.



4. Bid menggunakan fungsi BidFor(). ShowLots() akan menampilkan bid tertinggi sekarang.































No comments:

Post a Comment

PPB EAS - Aplikasi Member Timezone

Manajemen Keanggotaan & Loyalty Program Timezone dengan Room Database dan Jetpack Compose Aryaka Leorgi Epridaka - 5025231117 Muhammad ...