CS project Learning Objectives Implement a class with instance data. (10 points)
ID: 3698065 • Letter: C
Question
CS project
Learning Objectives
Implement a class with instance data. (10 points)
Implement a constructor properly. (10 points)
Implement accessor and mutator methods. (20 points)
Use ArrayList in two programmer defined classes. (20 points)
Construct an object from a user defined class (10 points)
Fill in method stubs in two classes (Campaign, Fall2016). (20 points)
10 points will be awarded for the documentation of your program. That means using good names for variables, proper and consistent indentation of code, and meaningful use of whitespace.
When your program is completed and running, have the teaching assistants check it to get credit for the lab. If you do not submit your assignment during the laboratory, it will be due on Wednesday, May 4 by 11:59 p.m.
Description
With presidential elections coming around, we will write software to keep track of a political campaign. Campaigns run on money. Money comes from donors, so keeping track of donors is the main purpose of campaign software.
Each donor has a name and a history of making donations. The Donor class is written below in UML. The – means private and + means public. We’ll talk about them in more detail in class soon.
Donor
-name: String
-donations: ArrayList<Double>
+Donor(name: String)
+Donor(name: String, donation: double)
+getName(): String
+getTotalDonations(): double
+toString(): String
+addDonation(donation: double)
Each Campaign has a candidate and donors. The candidate will have a name. The donors will be stored in an ArrayList<Donor>. Using an ArrayList<Donor> is almost exactly like using an ArrayList<String> . The only difference is that the objects being stored are Donor objects, and will behave like them. The Campaign class is summarized in UML below.
Campaign
-candidateName: String
-donors: ArrayList<Donor>
+Campaign(name: String)
+getCandidateName(): String
+getDonors(): String
+getAllDonations(): double
+addDonor(name: String): void
+getDonation(donor: String) :double
+getDonationList(donor: String) :String
+addDonation(donorName: String, donation: double) : void
Notice which methods are accessors and mutators. Also, some accessors apply only to one of the many Donors (getDonation() is an example, you can tell it applies to only one donor because of the parameter). Accessors like getAllDonations apply to all donors. In order to implement this class, you will have to implement Donor first since this class uses Donor.
I’ve made many of the methods in this class stubs, just to make implementing it less time consuming. This means that I have put a fake return type in the method where necessary so that the program will compile.
The main method that runs this program is menu driven. I’ve written all but two methods for you. This class (Fall2016) is designed like our previous programs have been—with no instance data. The design is below in UML. The underlined methods and data are static (class methods and class data).
Fall2016
-ADD_DONOR: int
-DONATION: int
-SUM_DONATIONS: int
-SINGLE_DONOR_DETAILS: int
-QUIT: int
+main(args: String[]) : void
-menu(keyboard: Scanner): int
-addDonor(keyboard: Scanner, candidate: Campaign): void
-addDonations(keyboard: Scanner, candidate: Campaign): void
-sumDonations(candidate: Campaign): void
-singleDonorDetails(keyboard: Scanner, candidate: Campaign):void
I left a neat little surprise for you in the menu method. If you plan to continue in programming, check it out!
Hints
The purpose of separating programs into classes is to make things simpler. It may not seem like this goal is achieved at first. The best piece of programming advice I can give you as we make this transition is that you need to know what type of object you have at all times. Make sure you only use methods that are appropriate for the type of object.
For example:
ArrayList<Donor> list; // assume this is constructed and initialized with some data
If we do list.get() we will get a Donor object because that was what list stores.
Donor person = list.get(0);
If, on the other hand we have this:
ArrayList<Double> donations; // assume this is constructed and initialized with some data
When we do:
double money = donations.get(0) we have a Double (changed to a double, thanks to autoboxing).
The methods in these classes are just a few lines of code (usually 2-10) and that’s the way it’s supposed to be. If you find you’re writing long methods, please ask for help as soon as possible.
import java.util.ArrayList;
public class Campaign {
private String candidateName;
private ArrayList<Donor> 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<donors.size(); ++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<donors.size(); ++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
}
}
import java.util.ArrayList;
public class Donor
{
private String name;
private ArrayList<Double> donations;
public Donor(String name)
{
this.name = name;
this.donations = new ArrayList<Double>();
}
public Donor (String name, double donation)
{
this(name);
donations.add(donation);
}
public String getName()
{
return name;
}
public double getTotalDonations()
{
double sum = 0.0;
for (int i=0; i<donations.size(); ++i)
{
sum += donations.get(i);
}
return sum;
}
public String toString()
{
String result = name + " ";
for(int i=0; i<donations.size(); ++i)
{
result += donations.get(i) + " "; // won't be pretty
}
return result;
}
public void addDonation(double donation)
{
donations.add(donation);
}
}
import java.util.Scanner;
/** This class runs a campaign for Donald Duck.
*
* @author Deborah A. Trytten
*
*/
public class Fall2016 {
// These constants are used for a menu system
private static final int ADD_DONOR = 1;
private static final int DONATION = 2;
private static final int SUM_DONATIONS = 3;
private static final int SINGLE_DONOR_DETAILS = 4;
private static final int QUIT = 5;
public static void main(String[] args)
{
Campaign candidate = new Campaign("Donald Duck");
Scanner keyboard = new Scanner(System.in);
int menuChoice = 0;
while (menuChoice != QUIT)
{
menuChoice = menu(keyboard);
if (menuChoice == ADD_DONOR)
addDonor(keyboard, candidate);
else if (menuChoice == DONATION)
addDonation(keyboard, candidate);
else if (menuChoice == SUM_DONATIONS)
sumDonations(candidate);
else if (menuChoice == SINGLE_DONOR_DETAILS)
singleDonorDetails(keyboard, candidate);
else if (menuChoice == QUIT)
System.out.println("Goodbye");
else
System.out.println("Unanticipated case");
}
}
private static final int menu(Scanner keyboard)
{
System.out.println("Enter your choice below");
System.out.println(ADD_DONOR + ": add new donor");
System.out.println(DONATION + ": make donation");
System.out.println(SUM_DONATIONS + ": find total donations");
System.out.println(SINGLE_DONOR_DETAILS + ": single donor details");
System.out.println(QUIT + ": quit");
int value = keyboard.nextInt();
keyboard.nextLine();
if (value < ADD_DONOR || value > QUIT)
{
System.out.println(value + " is not in the legal range. Please re-enter");
return menu(keyboard); // this is a cool trick called recursion
}
else // it was legal
{
return value;
}
}
private static void addDonor(Scanner keyboard, Campaign candidate)
{
System.out.println("Enter the name of the donor");
String name = keyboard.nextLine();
candidate.addDonor(name);
}
private static void addDonation(Scanner keyboard, Campaign candidate)
{
//TODO Fill in this method
}
private static void sumDonations(Campaign candidate)
{
System.out.println(candidate.getCandidateName() + " has $" + candidate.getAllDonations()
+ " of donations");
}
private static void singleDonorDetails(Scanner keyboard, Campaign candidate)
{
//TODO Fill in this method
}
}
Donor
-name: String
-donations: ArrayList<Double>
+Donor(name: String)
+Donor(name: String, donation: double)
+getName(): String
+getTotalDonations(): double
+toString(): String
+addDonation(donation: double)
Explanation / Answer
############## Donor.java #######################
import java.util.ArrayList;
public class Donor
{
private String name;
private ArrayList<Double> donations;
public Donor(String name)
{
this.name = name;
this.donations = new ArrayList<Double>();
}
public Donor (String name, double donation)
{
this(name);
donations.add(donation);
}
public String getName()
{
return name;
}
public double getTotalDonations()
{
double sum = 0.0;
for (int i=0; i<donations.size(); ++i)
{
sum += donations.get(i);
}
return sum;
}
public String toString()
{
String result = name + " ";
for(int i=0; i<donations.size(); ++i)
{
result += donations.get(i) + " "; // won't be pretty
}
return result;
}
public void addDonation(double donation)
{
donations.add(donation);
}
}
######################## Campaign.java ###############################
import java.util.ArrayList;
public class Campaign {
private String candidateName;
private ArrayList<Donor> donors;
public Campaign(String name)
{
//TODO Initialize all of the instance data
candidateName = name;
donors = new ArrayList<Donor>();
}
public String getCandidateName()
{
//TODO Complete the accessor
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 = 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
for(int i=0; i<donors.size(); i++){
if(name.equalsIgnoreCase(donors.get(i).getName()))
return; // do not add
}
// TODO If we get here this donor does not exist--add them in
donors.add(new Donor(name));
}
public double getDonation(String donor)
{
//TODO Complete this method
for(Donor donorObj : donors){
if(donor.equals(donorObj.getName()))
return donorObj.getTotalDonations();
}
return 0.0; // stub
}
public String getDonationList(String donor)
{
for (int i=0; i<donors.size(); ++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
for(Donor donorObj : donors){
if(donorName.equals(donorObj.getName()))
donorObj.addDonation(donation);
}
}
}
######################### Fall2016.java #######################
import java.util.Scanner;
/** This class runs a campaign for Donald Duck.
*
* @author Deborah A. Trytten
*
*/
public class Fall2016 {
// These constants are used for a menu system
private static final int ADD_DONOR = 1;
private static final int DONATION = 2;
private static final int SUM_DONATIONS = 3;
private static final int SINGLE_DONOR_DETAILS = 4;
private static final int QUIT = 5;
public static void main(String[] args)
{
Campaign candidate = new Campaign("Donald Duck");
Scanner keyboard = new Scanner(System.in);
int menuChoice = 0;
while (menuChoice != QUIT)
{
menuChoice = menu(keyboard);
if (menuChoice == ADD_DONOR)
addDonor(keyboard, candidate);
else if (menuChoice == DONATION)
addDonation(keyboard, candidate);
else if (menuChoice == SUM_DONATIONS)
sumDonations(candidate);
else if (menuChoice == SINGLE_DONOR_DETAILS)
singleDonorDetails(keyboard, candidate);
else if (menuChoice == QUIT)
System.out.println("Goodbye");
else
System.out.println("Unanticipated case");
}
}
private static final int menu(Scanner keyboard)
{
System.out.println("Enter your choice below");
System.out.println(ADD_DONOR + ": add new donor");
System.out.println(DONATION + ": make donation");
System.out.println(SUM_DONATIONS + ": find total donations");
System.out.println(SINGLE_DONOR_DETAILS + ": single donor details");
System.out.println(QUIT + ": quit");
int value = keyboard.nextInt();
keyboard.nextLine();
if (value < ADD_DONOR || value > QUIT)
{
System.out.println(value + " is not in the legal range. Please re-enter");
return menu(keyboard); // this is a cool trick called recursion
}
else // it was legal
{
return value;
}
}
private static void addDonor(Scanner keyboard, Campaign candidate)
{
System.out.println("Enter the name of the donor");
String name = keyboard.nextLine();
candidate.addDonor(name);
}
private static void addDonation(Scanner keyboard, Campaign candidate)
{
//TODO Fill in this method
System.out.println("Enter the name of the donor for which you want to add donation: ");
String name = keyboard.nextLine();
System.out.println("Enter donation : ");
double donation = keyboard.nextDouble();
candidate.addDonation(name, donation);
}
private static void sumDonations(Campaign candidate)
{
System.out.println(candidate.getCandidateName() + " has $" + candidate.getAllDonations()
+ " of donations");
}
private static void singleDonorDetails(Scanner keyboard, Campaign candidate)
{
//TODO Fill in this method
System.out.println("Enter the name of the donor for which you want to get details: ");
String name = keyboard.nextLine();
String details = candidate.getDonationList(name);
}
}