Question: public class Fundraiser { private String name; private AuctionItem [ ] items; public Fundraiser ( ) { this.name = Default Fundraiser; this.items = new AuctionItem

public class Fundraiser {
private String name;
private AuctionItem[] items;
public Fundraiser(){
this.name = "Default Fundraiser";
this.items = new AuctionItem[0];
}
public Fundraiser(String name, AuctionItem[] items){
this.name = name;
this.items = items.clone();
}
public String getName(){
return name;
}
public AuctionItem[] getItems(){
return items;
}
public String toString(){
if (items == null || items.length ==0){
return "Fundraiser without any items";
}
String s = name +"
auction items:
";
for (int i =0; i < items.length; i++){
s += items[i].toString();
}
return s;
}
/*
* Purpose: get the money raised, which will be the
* sum of all auction items that are sold
* Parameters: none
* Returns: int - the total money this fundraiser raises
*/
public int moneyRaised(){
int totalMoney =0;
if (items != null){
for (AuctionItem item : items){
if (item != null){
totalMoney += item.getPrice();
}
}
}
return totalMoney;
}
}
/*
* Purpose: get the total money spent at this fundraiser
* by those with the given name
* Parameters: String name - the name to search for
* Returns: int - the total money spent by name
*/
public int moneySpent(String name){
int totalMoneySpent =0;
if (items != null && name != null){
for (AuctionItem item : items){
if (item != null && name.equalsIgnoreCase(item.getBidderName())){
totalMoneySpent += item.getHighestBid();
}
}
}
return totalMoneySpent;
}
/*
* Purpose: get the most expensive auction item at this fundraiser
* Parameters: none
* Returns: AuctionItem - the most expensive item
* Preconditions: items is not null, and items.length >0
*/
public AuctionItem mostExpensive(){
if (items == null || items.length ==0){
return null;
}
AuctionItem mostExpensive = null;
for (AuctionItem item : items){
if (item != null){
if (mostExpensive == null || item.getHighestBid()> mostExpensive.getHighestBid()){
mostExpensive = item;
}
}
return mostExpensive;
}
/*
* Purpose: adds the new item to the items sold at this fundraiser
* Parameters: AuctionItem newItem - the item to add to the fundraiser
* Returns: void - nothing
*/
public void addToFundraiser(AuctionItem newItem){
// TODO: implement this
if (items == null){
items = new AuctionItem[1];
items[0]= newItem;
} else {
AuctionItem[] newArray = new AuctionItem[items.length +1];
for (int i =0; i < items.length; i++){
newArray[i]= items[i];
}
newArray[items.length]= newItem;
items = newArray;
}
}import java.util.Arrays;
public class A2Tester {
private static int testPassCount =0;
private static int testCount =0;
private static double THRESHOLD =0.1; // allowable margin of error for floating point results
public static void main(String[] args){
/* Tests for methods inside Fundraiser.java */
testConstructor();
// testMoneyRaised();
// testMoneySpent();
// testMostExpensive();
// testAddToFundraiser();
/* Tests for methods inside A2Exercises.java */
// testTotalMoneyRaised();
// testTotalSpent();
// testMostExpensiveItems();
// testAverageOfMostExpensive();
System.out.println();
System.out.println("PASSED "+testPassCount+"/"+testCount+" TESTS");
}
public static void testConstructor(){
System.out.println("
Testing Fundraiser Constructor");
String nameResult;
AuctionItem[] itemsResult;
// Testing default constructor
//(the constructor with 0 arguments)
Fundraiser f0= new Fundraiser();
nameResult = f0.getName();
displayResults(nameResult==null, "empty constructor name");
itemsResult = f0.getItems();
displayResults(itemsResult==null, "empty constructor items");
// Setting up AuctionItems for Fundraiser objects
AuctionItem a1= new AuctionItem("colorful seashell", 4, "Sam");
AuctionItem a2= new AuctionItem("autographed photo", 35, "Ali");
AuctionItem a3= new AuctionItem("event ticket", 220, "Ali");
AuctionItem[] arr1={a1};
AuctionItem[] arr2={a1, a2, a3};
// Testing second constructor with f1 and f2
Fundraiser f1= new Fundraiser("Family Fundraiser", arr1);
Fundraiser f2= new Fundraiser("Community Fundraiser", arr2);
String expectedName;
AuctionItem[] expectedItems;
nameResult = f1.getName();
itemsResult = f1.getItems();
expectedName = new String("Family Fundraiser");
expectedItems = arr1;
displayResults(expectedName.equals(nameResult),"f1 constructor name");
displayResults(Arrays.equals(itemsResult,expectedItems),"f1 constructor items");
nameResult = f2.getName();
itemsResult = f2.getItems();

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!