Skip to content

Instantly share code, notes, and snippets.

Created February 14, 2016 11:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/02a702182bcf21980b37 to your computer and use it in GitHub Desktop.
Save anonymous/02a702182bcf21980b37 to your computer and use it in GitHub Desktop.
Implementation Class
import javax.swing.JOptionPane;
public class Donations {
public static void main(String[] args){
double donationTotal=0.0;
Campaign campaign1 = new Campaign("IT Student Scholarship Fund", "Non Profit", 15000.0, 6855.0, 6855.0);
String campName = promptUserForCampName();
String campType = promptUserForCampType();
double campGoal = promptUserForCampGoal();
double donationAmount = promptUserForCampDonation(campGoal);
Campaign campaign2 = new Campaign(campName, campType, campGoal, donationAmount, donationTotal);
calculateTotal(donationTotal,donationAmount);
printReport(campaign1, campaign2);
}
//Prompts user for campaign name
public static String promptUserForCampName(){
String campName;
campName = JOptionPane.showInputDialog("Enter the name of your campaign: ");
return campName;
}
//Prompts user for campaign type
public static String promptUserForCampType(){
String campType;
do{
campType = JOptionPane.showInputDialog("Enter the type of campaign you have (Non Profit or For Profit)");
if((!campType.equalsIgnoreCase("Non Profit") && (!campType.equalsIgnoreCase("For Profit")))){
JOptionPane.showMessageDialog(null, "Error: Please enter either Non Profit or For Profit for your campaign type.");
}
}while(!campType.equalsIgnoreCase("Non Profit") && (!campType.equalsIgnoreCase("For Profit")));
return campType;
}
/*Prompts user for campaign goal; if invalid,
error is displayed and user is reprompted.
*/
public static double promptUserForCampGoal(){
double campGoal=0.0;
String defaultOrNo;
boolean trigger= false;
defaultOrNo = JOptionPane.showInputDialog("Would you like to use the default goal amount of 10000.0?");
if(defaultOrNo.equalsIgnoreCase("Yes")){
campGoal=10000.0;
}else if(defaultOrNo.equalsIgnoreCase("No")){
do{
try{
campGoal=Double.parseDouble(JOptionPane.showInputDialog("Please enter your goal amount: "));
}catch(NumberFormatException e){
campGoal=-1;
JOptionPane.showMessageDialog(null, "Error: please enter a valid goal amount: ");
trigger = true;
}
if(campGoal<0.0 || campGoal>25000.0){
JOptionPane.showMessageDialog(null, "Error: please enter a valid goal amount (1-25000)");
trigger = true;
}
}while(trigger = true && (campGoal<0.0 || campGoal>25000.0));
}
return campGoal;
}
//Prompts for campaign donations
public static double promptUserForCampDonation(double campGoal){
double donationAmount=0.0;
while(donationAmount<campGoal){
do{
try{
donationAmount = Double.parseDouble(JOptionPane.showInputDialog("Please enter your donations, up to "+ campGoal));
}catch(NumberFormatException e){
donationAmount=-1;
}
if(donationAmount<0){
JOptionPane.showMessageDialog(null, "Error: please enter a valid donation amount: ");
}
}while(donationAmount<campGoal);
}
return donationAmount;
}
public static double calculateTotal(double donationTotal, double donationAmount){
donationTotal+=donationAmount;
return donationTotal;
}
//Comparisons between goal amounts and donation totals
public static void printReport(Campaign campaign1, Campaign campaign2){
String report= "Donation Tracking System Report";
report+= "\n\nCampaign Name: "+campaign1.getCampName();
report+= "\nTotal amount of donations collected: "+campaign1.getDonationTotal();
//All of campaign1's comparisons
if(campaign1.getDonationTotal()<campaign1.getCampGoal()){
report+= "\n"+campaign1.getCampName() + " was below the goal amount by "+ (campaign1.getCampGoal()-campaign1.getDonationTotal());
}else if(campaign1.getDonationTotal()>campaign1.getCampGoal()){
report+="\n"+campaign1.getCampName() + " was above the goal amount by "+ (campaign1.getDonationTotal()-campaign1.getCampGoal());
}else if(campaign1.getDonationTotal()==campaign1.getCampGoal()){
report+="\n"+campaign1.getCampName() + " met the exact goal amount of "+campaign1.getCampGoal();
}
report+= "\n\nCampaign Name: "+campaign2.getCampName();
report+= "\nTotal amount of donations collected: "+campaign2.getDonationTotal();
//All of campaign2's comparisons
if(campaign2.getDonationTotal()<campaign1.getCampGoal()){
report+= "\n"+campaign2.getCampName() + " was below the goal amount by "+ (campaign2.getCampGoal()-campaign2.getDonationTotal());
}else if(campaign2.getDonationTotal()>campaign1.getCampGoal()){
report+="\n"+campaign2.getCampName() + " was above the goal amount by "+ (campaign2.getDonationTotal()-campaign2.getCampGoal());
}else if(campaign1.getDonationTotal()==campaign1.getCampGoal()){
report+="\n"+campaign2.getCampName() + " met the exact goal amount of "+campaign2.getCampGoal();
}
if(campaign1.getDonationTotal()>campaign2.getDonationTotal()){
report+="\n"+campaign1.getCampName()+" collected the highest amount of donations.";
}else if(campaign2.getDonationTotal()<campaign1.getDonationTotal()){
report+="\n"+campaign2.getCampName()+" collected the highest amount of donations.";
}else if(campaign1.getDonationTotal()==campaign2.getDonationTotal()){
report+="\nBoth campaigns collected the same amount of donations.";
}
//Prints to the user with report
JOptionPane.showMessageDialog(null, report);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment