AUCTION
Nama : Timothyus Tanner
NRP : 05111740000103
Kelas : PBO - A
Source Code
1. Auction
/**
* Nama : Timothyus Tanner
* NRP : 05111740000103
* Kelas : PBO - A
*
*/
import java.util.ArrayList;
public class Auction
{
//The list of Lots in this auction.
private ArrayList<Lot> lots;
//The number that will be given to the nect lot entered into this auction
private int nextLotNumber;
//Create new auction
public Auction(){
lots = new ArrayList<Lot>();
nextLotNumber = 1;
}
//Enter a new lot into the auction
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.detail());
}
}
//Make a bid for a lot
//A message is printed indicating whether the bid is successful or not
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 succesdful.");
System.out.println("The bid is belong to " + bidder.getName());
}
else{
//Report which bid is higher
Bid highestBid = selectedLot.getHighestBid();
System.out.println("Lot number " + lotNumber + " already has a bid of " + highestBid.getBid());
}
}
}
//Return the lot with the given number.
//Return null if a lot with this number does not exist.
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.getId() != lotNumber){
System.out.println("Internal error: Lot number " + selectedLot.getId() + " was returned instead of " + lotNumber);
selectedLot = null;
}
return selectedLot;
}
else{
System.out.println("Lot number: " + lotNumber + " does not exist.");
return null;
}
}
//Close the auction
public void close(){
System.out.println("Auction is closed.");
for(Lot lot : lots){
System.out.println(lot.getId() + ": " + lot.getDescription());
Bid bid = lot.getHighestBid();
if(bid == null){
System.out.println("There are no more Bids for this lot.");
}
else{
System.out.println("This Item has been sold to " + bid.getBidder().getName() + " Price : " + bid.getBid());
}
}
}
}
2. Lots
/**
* Nama : Timothyus Tanner
* NRP : 05111740000103
* Kelas : PBO - A
*
*/
public class Lot
{
// The current highest bid for this lot.
private final int id;
private String description;
private Bid highestBid;
//Make a new Lot with id and description
public Lot(int numb, String description){
this.id = numb;
this.description = description;
}
/**
* 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.getBid() > highestBid.getBid()){
//The bid is better than the previous one.
highestBid = bid;
return true;
}
else{
//The bid is not better.
return false;
}
}
//To return the lot's id and description
public String detail(){
String details = id + ":" + description;
if(highestBid != null){
details += " Bid : " + highestBid.getBid();
}
else{
details += " (No bid)";
}
return details;
}
//To return Lot's id
public int getId(){
return id;
}
//To return Lot's description
public String getDescription(){
return description;
}
//To return the highest bid value
public Bid getHighestBid(){
return highestBid;
}
}
3. Bid
/**
* Nama : Timothyus Tanner
* NRP : 05111740000103
* Kelas : PBO - A
*/
public class Bid
{
private final Person bidder;
private final long value;
//Create new bidder with names and bid value
public Bid(Person bidder, long value){
this.bidder = bidder;
this.value = value;
}
//To return bidder's name
public Person getBidder(){
return bidder;
}
//To return bidder's bid value
public long getBid(){
return value;
}
}
4. Person
/**
* Nama : Timothyus Tanner
* NRP : 05111740000103
* Kelas : PBO - A
*/
public class Person
{
//Create new string for person's name
private final String name;
public Person(String bidName){
this.name = bidName;
}
//To return person's name
public String getName(){
return name;
}
}
Screenshoot
Tidak ada komentar:
Posting Komentar