Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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=10000.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;
- JOptionPane.showMessageDialog(null, "Error: please enter a valid donation amount: ");
- }
- if(donationAmount<0 || donationAmount>campGoal){
- JOptionPane.showMessageDialog(null, "Error: please enter a valid donation amount: ");
- }else{
- }
- }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+= "\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+= "\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);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement