first commit
This commit is contained in:
@@ -0,0 +1,17 @@
|
||||
#BlueJ class context
|
||||
comment0.params=
|
||||
comment0.target=Auction()
|
||||
comment0.text=\n\ Create\ a\ new\ auction.\n
|
||||
comment1.params=description
|
||||
comment1.target=void\ enterLot(java.lang.String)
|
||||
comment1.text=\n\ Enter\ a\ new\ lot\ into\ the\ auction.\n\ @param\ description\ A\ description\ of\ the\ lot.\n
|
||||
comment2.params=
|
||||
comment2.target=void\ showLots()
|
||||
comment2.text=\n\ Show\ the\ full\ list\ of\ lots\ in\ this\ auction.\n
|
||||
comment3.params=lotNumber\ bidder\ value
|
||||
comment3.target=void\ makeABid(int,\ Person,\ long)
|
||||
comment3.text=\n\ Make\ a\ bid\ for\ a\ lot.\n\ A\ message\ is\ printed\ indicating\ whether\ the\ bid\ is\n\ successful\ or\ not.\n\ \n\ @param\ lotNumber\ The\ lot\ being\ bid\ for.\n\ @param\ bidder\ The\ person\ bidding\ for\ the\ lot.\n\ @param\ value\ \ The\ value\ of\ the\ bid.\n
|
||||
comment4.params=lotNumber
|
||||
comment4.target=Lot\ getLot(int)
|
||||
comment4.text=\n\ Return\ the\ lot\ with\ the\ given\ number.\ Return\ null\n\ if\ a\ lot\ with\ this\ number\ does\ not\ exist.\n\ @param\ lotNumber\ The\ number\ of\ the\ lot\ to\ return.\n
|
||||
numComments=5
|
104
Semester 1/Programming 1/Java/examples/projects/chapter04/auction/Auction.java
Executable file
104
Semester 1/Programming 1/Java/examples/projects/chapter04/auction/Auction.java
Executable file
@@ -0,0 +1,104 @@
|
||||
import java.util.ArrayList;
|
||||
|
||||
/**
|
||||
* A simple model of an auction.
|
||||
* The auction maintains a list of lots of arbitrary length.
|
||||
*
|
||||
* @author David J. Barnes and Michael Kölling.
|
||||
* @version 2016.02.29
|
||||
*/
|
||||
public class Auction
|
||||
{
|
||||
// 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;
|
||||
|
||||
/**
|
||||
* Create a new auction.
|
||||
*/
|
||||
public Auction()
|
||||
{
|
||||
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 whether 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 does not 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;
|
||||
}
|
||||
}
|
||||
}
|
11
Semester 1/Programming 1/Java/examples/projects/chapter04/auction/Bid.ctxt
Executable file
11
Semester 1/Programming 1/Java/examples/projects/chapter04/auction/Bid.ctxt
Executable file
@@ -0,0 +1,11 @@
|
||||
#BlueJ class context
|
||||
comment0.params=bidder\ value
|
||||
comment0.target=Bid(Person,\ long)
|
||||
comment0.text=\n\ Create\ a\ bid.\n\ @param\ bidder\ Who\ is\ bidding\ for\ the\ lot.\n\ @param\ value\ The\ value\ of\ the\ bid.\n
|
||||
comment1.params=
|
||||
comment1.target=Person\ getBidder()
|
||||
comment1.text=\n\ @return\ The\ bidder.\n
|
||||
comment2.params=
|
||||
comment2.target=long\ getValue()
|
||||
comment2.text=\n\ @return\ The\ value\ of\ the\ bid.\n
|
||||
numComments=3
|
42
Semester 1/Programming 1/Java/examples/projects/chapter04/auction/Bid.java
Executable file
42
Semester 1/Programming 1/Java/examples/projects/chapter04/auction/Bid.java
Executable file
@@ -0,0 +1,42 @@
|
||||
/**
|
||||
* A class that models an auction bid.
|
||||
* It contains a reference to the Person bidding and the amount bid.
|
||||
*
|
||||
* @author David J. Barnes and Michael Kölling.
|
||||
* @version 2016.02.29
|
||||
*/
|
||||
public class Bid
|
||||
{
|
||||
// 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;
|
||||
|
||||
/**
|
||||
* Create a bid.
|
||||
* @param bidder Who is bidding for the lot.
|
||||
* @param value The value of the bid.
|
||||
*/
|
||||
public Bid(Person bidder, long value)
|
||||
{
|
||||
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;
|
||||
}
|
||||
}
|
20
Semester 1/Programming 1/Java/examples/projects/chapter04/auction/Lot.ctxt
Executable file
20
Semester 1/Programming 1/Java/examples/projects/chapter04/auction/Lot.ctxt
Executable file
@@ -0,0 +1,20 @@
|
||||
#BlueJ class context
|
||||
comment0.params=number\ description
|
||||
comment0.target=Lot(int,\ java.lang.String)
|
||||
comment0.text=\n\ Construct\ a\ Lot,\ setting\ its\ number\ and\ description.\n\ @param\ number\ The\ lot\ number.\n\ @param\ description\ A\ description\ of\ this\ lot.\n
|
||||
comment1.params=bid
|
||||
comment1.target=boolean\ bidFor(Bid)
|
||||
comment1.text=\n\ Attempt\ to\ bid\ for\ this\ lot.\ A\ successful\ bid\n\ must\ have\ a\ value\ higher\ than\ any\ existing\ bid.\n\ @param\ bid\ A\ new\ bid.\n\ @return\ true\ if\ successful,\ false\ otherwise\n
|
||||
comment2.params=
|
||||
comment2.target=java.lang.String\ toString()
|
||||
comment2.text=\n\ @return\ A\ string\ representation\ of\ this\ lot's\ details.\n
|
||||
comment3.params=
|
||||
comment3.target=int\ getNumber()
|
||||
comment3.text=\n\ @return\ The\ lot's\ number.\n
|
||||
comment4.params=
|
||||
comment4.target=java.lang.String\ getDescription()
|
||||
comment4.text=\n\ @return\ The\ lot's\ description.\n
|
||||
comment5.params=
|
||||
comment5.target=Bid\ getHighestBid()
|
||||
comment5.text=\n\ @return\ The\ highest\ bid\ for\ this\ lot.\n\ \ \ \ \ \ \ \ \ This\ could\ be\ null\ if\ there\ is\n\ \ \ \ \ \ \ \ \ no\ current\ bid.\n
|
||||
numComments=6
|
94
Semester 1/Programming 1/Java/examples/projects/chapter04/auction/Lot.java
Executable file
94
Semester 1/Programming 1/Java/examples/projects/chapter04/auction/Lot.java
Executable file
@@ -0,0 +1,94 @@
|
||||
/**
|
||||
* A class to model an item (or set of items) in an
|
||||
* auction: a lot.
|
||||
*
|
||||
* @author David J. Barnes and Michael Kölling.
|
||||
* @version 2016.02.29
|
||||
*/
|
||||
public class Lot
|
||||
{
|
||||
// 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;
|
||||
|
||||
/**
|
||||
* 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 description)
|
||||
{
|
||||
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 current bid.
|
||||
*/
|
||||
public Bid getHighestBid()
|
||||
{
|
||||
return highestBid;
|
||||
}
|
||||
}
|
@@ -0,0 +1,8 @@
|
||||
#BlueJ class context
|
||||
comment0.params=name
|
||||
comment0.target=Person(java.lang.String)
|
||||
comment0.text=\n\ Create\ a\ new\ person\ with\ the\ given\ name.\n\ @param\ name\ The\ person's\ name.\n
|
||||
comment1.params=
|
||||
comment1.target=java.lang.String\ getName()
|
||||
comment1.text=\n\ @return\ The\ person's\ name.\n
|
||||
numComments=2
|
@@ -0,0 +1,27 @@
|
||||
/**
|
||||
* Maintain details of someone who participates in an auction.
|
||||
* @author David J. Barnes and Michael Kölling.
|
||||
* @version 2016.02.29
|
||||
*/
|
||||
public class Person
|
||||
{
|
||||
// The name of this person.
|
||||
private final String name;
|
||||
|
||||
/**
|
||||
* Create a new person with the given name.
|
||||
* @param name The person's name.
|
||||
*/
|
||||
public Person(String name)
|
||||
{
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return The person's name.
|
||||
*/
|
||||
public String getName()
|
||||
{
|
||||
return name;
|
||||
}
|
||||
}
|
26
Semester 1/Programming 1/Java/examples/projects/chapter04/auction/README.TXT
Executable file
26
Semester 1/Programming 1/Java/examples/projects/chapter04/auction/README.TXT
Executable file
@@ -0,0 +1,26 @@
|
||||
Project: auction
|
||||
Authors: David J. Barnes and Michael Kölling
|
||||
|
||||
This project is part of the supplementary material for
|
||||
chapter 4 of the book
|
||||
|
||||
Objects First with Java - A Practical Introduction using BlueJ
|
||||
Sixth edition
|
||||
David J. Barnes and Michael Kölling
|
||||
Pearson Education, 2016
|
||||
|
||||
Purpose of project: To demonstrate collections of objects
|
||||
How to start this project:
|
||||
+ Create an Auction object.
|
||||
|
||||
+ Enter a few lots via its enterLot method. Only String
|
||||
descriptions of the lots are required.
|
||||
|
||||
+ Create one or more Person objects to represent bidders.
|
||||
|
||||
+ Show the lots and decide on one to bid for. Make a note
|
||||
of the lot number.
|
||||
|
||||
+ Enter a bid for the lot by calling the makeABid method on
|
||||
the Auction object. Pass the number of the lot, the Person
|
||||
who is bidding, and the amount of the bid.
|
@@ -0,0 +1,83 @@
|
||||
#BlueJ package file
|
||||
dependency1.from=Auction
|
||||
dependency1.to=Lot
|
||||
dependency1.type=UsesDependency
|
||||
dependency2.from=Auction
|
||||
dependency2.to=Person
|
||||
dependency2.type=UsesDependency
|
||||
dependency3.from=Auction
|
||||
dependency3.to=Bid
|
||||
dependency3.type=UsesDependency
|
||||
dependency4.from=Lot
|
||||
dependency4.to=Bid
|
||||
dependency4.type=UsesDependency
|
||||
dependency5.from=Bid
|
||||
dependency5.to=Person
|
||||
dependency5.type=UsesDependency
|
||||
objectbench.height=76
|
||||
objectbench.width=774
|
||||
package.editor.height=429
|
||||
package.editor.width=666
|
||||
package.editor.x=70
|
||||
package.editor.y=80
|
||||
package.numDependencies=5
|
||||
package.numTargets=4
|
||||
package.showExtends=true
|
||||
package.showUses=true
|
||||
project.charset=UTF-8
|
||||
readme.editor.height=722
|
||||
readme.editor.width=842
|
||||
readme.editor.x=64
|
||||
readme.editor.y=23
|
||||
target1.editor.height=705
|
||||
target1.editor.width=876
|
||||
target1.editor.x=109
|
||||
target1.editor.y=42
|
||||
target1.height=60
|
||||
target1.name=Lot
|
||||
target1.naviview.expanded=true
|
||||
target1.showInterface=false
|
||||
target1.type=ClassTarget
|
||||
target1.typeParameters=
|
||||
target1.width=110
|
||||
target1.x=220
|
||||
target1.y=210
|
||||
target2.editor.height=720
|
||||
target2.editor.width=886
|
||||
target2.editor.x=55
|
||||
target2.editor.y=93
|
||||
target2.height=60
|
||||
target2.name=Auction
|
||||
target2.naviview.expanded=true
|
||||
target2.showInterface=false
|
||||
target2.type=ClassTarget
|
||||
target2.typeParameters=
|
||||
target2.width=110
|
||||
target2.x=100
|
||||
target2.y=80
|
||||
target3.editor.height=706
|
||||
target3.editor.width=926
|
||||
target3.editor.x=114
|
||||
target3.editor.y=60
|
||||
target3.height=60
|
||||
target3.name=Bid
|
||||
target3.naviview.expanded=true
|
||||
target3.showInterface=false
|
||||
target3.type=ClassTarget
|
||||
target3.typeParameters=
|
||||
target3.width=110
|
||||
target3.x=460
|
||||
target3.y=140
|
||||
target4.editor.height=711
|
||||
target4.editor.width=847
|
||||
target4.editor.x=114
|
||||
target4.editor.y=60
|
||||
target4.height=60
|
||||
target4.name=Person
|
||||
target4.naviview.expanded=true
|
||||
target4.showInterface=false
|
||||
target4.type=ClassTarget
|
||||
target4.typeParameters=
|
||||
target4.width=110
|
||||
target4.x=340
|
||||
target4.y=320
|
Reference in New Issue
Block a user