In: Computer Science
package construction;
public class Bid{ private String contractor; private float price; public Bid(String contractor, float price) { this.contractor = contractor; this.price = price; } public String getContractor() { return contractor; } public float getPrice() { return price; } }
package construction; public class ContractorBids{
// assume proper variables and other methods are here public void winningBid(Bid[] bids, int numBids){ /**************************** * your code would go here * ****************************/ } }
You are doing renovations on your building, and multiple contractors have given bids. Given the above Bid and ContractorBids class, write code that should go in the area marked * your code would go here * to complete the “winningBid(Bid[] bids, int numBids)” method. The output should be printed to the console (screen). There are numBids Bids in the array Bid[] bids. These items are in locations 0 to numBids - 1 of the array.
Each Bid item should have its contractor and price output on a single line. There will be marks for properly right justifying the price and outputting two decimals. As you are printing the Bid objects, you should keep track of the Bid object with the lowest price. At the end print the contractor name followed by the price, a finder's fee of 10% and the total price. For example, if the Bid array had 3 Bids, a Carl's Construction with $40 000, a Pat's Paving for $50 000, and Debbie's Demolition for $30 000, we would get the following output.
Top Bids:
Carl's Construction 40000.00
Pat's Paving 50000.00
Debbie's Demolition 30000.00
The winning bid goes to Debbie's Demolition.
Price: $30000.00 Finder's Fee: $3000.00 Total: $33000.00
Solution:
Here is the code for the winningBid method:
public void winningBid(Bid[] bids, int numBids){
float min_bid = 0f; // Variable to hold minimum Bid price
float finder_fee = 0f; // Variable to hold the finder fee (10%)
String min_contract = ""; // Variable to hold the Contractor name for the minimum Bid price
min_bid = bids[0].getPrice(); // Assume that the first Bid has the minimum Bid price
// For every subsequent Bid check if the Bid price is less than the minimum Bid price.
// If yes, then make that Bid price as the minimum Bid price. Also, capture the Contractor's name for the minimum Bid price.
for (int i = 1; i<numBids; i++){
if (bids[i].getPrice() < min_bid){
min_bid = bids[i].getPrice();
min_contract = bids[i].getContractor();
}
}
finder_fee = 0.1f * min_bid; // Calculating the 10% finder fee on the minimum Bid price.
float total = finder_fee + min_bid; //Calculating the total
System.out.println("Top Bids:");
// Print the Contractor's name and the bid price for every Bid in the array.
for(Bid num : bids){
System.out.println(num.getContractor() + " " + String.format ("%.2f",num.getPrice()));
}
// Print the winning bid with the minimum Bid price.
System.out.println("The winning bid goes to " + min_contract + ".");
// Print the Price, Finder's Fee and the Total.
System.out.println("Price: $" + String.format ("%.2f",min_bid) + " Finder's Fee: $" + String.format ("%.2f",finder_fee) + " Total: $" + String.format ("%.2f",total));
}
Also, for testing purpose, here is the complete code with the example provided in the question:
package construction;
public class Bid {
private String contractor;
private float price;
public Bid(String contractor, float price) {
this.contractor = contractor;
this.price = price;
}
public String getContractor() { return contractor; }
public float getPrice() { return price; }
}
package construction;
public class ContractorBids {
public void winningBid(Bid[] bids, int numBids){
float min_bid = 0f; // Variable to hold minimum Bid price
float finder_fee = 0f; // Variable to hold the finder fee (10%)
String min_contract = ""; // Variable to hold the Contractor name for the minimum Bid price
min_bid = bids[0].getPrice(); // Assume that the first Bid has the minimum Bid price
// For every subsequent Bid check if the Bid price is less than the minimum Bid price.
// If yes, then make that Bid price as the minimum Bid price. Also, capture the Contractor's name for the minimum Bid price.
for (int i = 1; i<numBids; i++){
if (bids[i].getPrice() < min_bid){
min_bid = bids[i].getPrice();
min_contract = bids[i].getContractor();
}
}
finder_fee = 0.1f * min_bid; // Calculating the 10% finder fee on the minimum Bid price.
float total = finder_fee + min_bid; //Calculating the total
System.out.println("Top Bids:");
// Print the Contractor's name and the bid price for every Bid in the array.
for(Bid num : bids){
System.out.println(num.getContractor() + " " + String.format ("%.2f",num.getPrice()));
}
// Print the winning bid with the minimum Bid price.
System.out.println("The winning bid goes to " + min_contract + ".");
// Print the Price, Finder's Fee and the Total.
System.out.println("Price: $" + String.format ("%.2f",min_bid) + " Finder's Fee: $" + String.format ("%.2f",finder_fee) + " Total: $" + String.format ("%.2f",total));
}
}
package construction;
public class ContractBidInput {
public static void main(String[] args) {
Bid c1 = new Bid("Carl's Construction", 40000);
Bid c2 = new Bid("Pat's Paving", 50000);
Bid c3 = new Bid("Debbie's Demolition", 30000);
ContractorBids cbid = new ContractorBids();
Bid arr[] = new Bid[]{c1, c2, c3};
cbid.winningBid(arr,3);
}
}
Below is the output of the program:
Top Bids:
Carl's Construction 40000.00
Pat's Paving 50000.00
Debbie's Demolition 30000.00
The winning bid goes to Debbie's Demolition.
Price: $30000.00 Finder's Fee: $3000.00 Total: $33000.00