IN JAVA complete where it says \" TODO\" : AND THE DONOR CLASS IS IN THIS LINK (
ID: 3827311 • Letter: I
Question
IN JAVA complete where it says " TODO" :
AND THE DONOR CLASS IS IN THIS LINK ( https://s2.postimg.org/nmuej2ze1/donor_class.png)
import java.util.ArrayList;
public class Campaign {
private String candidateName;
private ArrayList donors;
public Campaign(String name)
{
//TODO Initialize all of the instance data
}
public String getCandidateName()
{
//TODO Complete the accessor
return null; // stub
}
public String getDonors()
{
String result = candidateName + " ";
result += donors.toString();
return result;
}
public double getAllDonations()
{
double sum = 0.0;
for (int i=0; i
{
Donor d = donors.get(i);
sum += d.getTotalDonations();
}
return sum;
}
public void addDonor(String name)
{
// TODO Check that there is not a donor by this name already
// TODO If we get here this donor does not exist--add them in
}
public double getDonation(String donor)
{
//TODO Complete this method
return 0.0; // stub
}
public String getDonationList(String donor)
{
for (int i=0; i
{
Donor d = donors.get(i);
if (d.getName().equals(donor))
{
return d.toString();
}
}
return "No donor with that name was found";
}
public void addDonation(String donorName, double donation)
{
// TODO Complete this method
}
}
Explanation / Answer
import java.util.ArrayList;
public class Campaign {
private String candidateName;
private ArrayList donors;
public Campaign(String name)
{
candidateName = name;
}
public String getCandidateName()
{
return candidateName;
}
public String getDonors()
{
String result = candidateName + " ";
result += donors.toString();
return result;
}
public double getAllDonations()
{
double sum = 0.0;
for (int i=0; i<donors.size(); i++)
{
Donor d = (Donor) donors.get(i);
sum += d.getTotalDonations();
}
return sum;
}
public void addDonor(String name,double donation)
{
for (int i=0; i<donors.size(); i++)
{
Donor d = (Donor) donors.get(i);
if (d.getName().equals(name))
{
System.out.println("Donor already exist");
return ;
}
}
Donor d = new Donor(name, donation);
donors.add(d);
}
public double getDonation(String donor)
{
for (int i=0; i<donors.size(); i++)
{
Donor d = (Donor) donors.get(i);
if (d.getName().equals(donor))
{
return d.getTotalDonations();
}
}
System.out.println("No donor with that name was found");
return -0.0; // stub
}
public String getDonationList(String donor)
{
for (int i=0; i<donors.size(); i++)
{
Donor d = (Donor) donors.get(i);
if (d.getName().equals(donor))
{
return d.toString();
}
}
return "No donor with that name was found";
}
public void addDonation(String donorName, double donation)
{
for (int i=0; i<donors.size(); i++)
{
Donor d = (Donor) donors.get(i);
if (d.getName().equals(donorName))
{
d.setDonation(donation);
}
}
System.out.println("No donor with that name was found");
}
}
class Donor{
private String name;
private double donation;
public Donor(String donorName, double donation){
}
public String getName(){
return name;
}
public double getTotalDonations(){
return donation;
}
public void setDonation(double d){
donation = d;
}
public String toString(){
return name;
}
}