Advertisement
Guest User

Untitled

a guest
Feb 14th, 2016
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.63 KB | None | 0 0
  1. import javax.swing.JOptionPane;
  2.  
  3. public class Donations {
  4.  
  5.    public static void main(String[] args){
  6.    
  7.       double donationTotal=0.0;
  8.       Campaign campaign1 = new Campaign("IT Student Scholarship Fund", "Non Profit", 15000.0, 6855.0, 6855.0);
  9.      
  10.      
  11.       String campName = promptUserForCampName();
  12.       String campType = promptUserForCampType();
  13.       double campGoal = promptUserForCampGoal();
  14.       double donationAmount = promptUserForCampDonation(campGoal);
  15.      
  16.       Campaign campaign2 = new Campaign(campName, campType, campGoal, donationAmount, donationTotal);
  17.       calculateTotal(donationTotal,donationAmount);
  18.      
  19.       printReport(campaign1, campaign2);
  20.    }
  21.    
  22.    //Prompts user for campaign name
  23.    public static String promptUserForCampName(){
  24.       String campName;
  25.      
  26.       campName = JOptionPane.showInputDialog("Enter the name of your campaign: ");
  27.       return campName;
  28.    }
  29.    
  30.    //Prompts user for campaign type
  31.    public static String promptUserForCampType(){
  32.  
  33.       String campType;
  34.  
  35.       do{
  36.      
  37.          campType = JOptionPane.showInputDialog("Enter the type of campaign you have (Non Profit or For Profit)");
  38.          if((!campType.equalsIgnoreCase("Non Profit") && (!campType.equalsIgnoreCase("For Profit")))){
  39.              
  40.             JOptionPane.showMessageDialog(null, "Error: Please enter either Non Profit or For Profit for your campaign type.");
  41.          }
  42.          
  43.       }while(!campType.equalsIgnoreCase("Non Profit") && (!campType.equalsIgnoreCase("For Profit")));
  44.        
  45.       return campType;
  46.     }
  47.    
  48.     /*Prompts user for campaign goal; if invalid,
  49.     error is displayed and user is reprompted.
  50.     */
  51.     public static double promptUserForCampGoal(){
  52.        
  53.       double campGoal=10000.0;
  54.       String defaultOrNo;
  55.       boolean trigger= false;
  56.        
  57.          defaultOrNo = JOptionPane.showInputDialog("Would you like to use the default goal amount of 10000.0?");
  58.             if(defaultOrNo.equalsIgnoreCase("Yes")){
  59.                campGoal=10000.0;
  60.             }else if(defaultOrNo.equalsIgnoreCase("No")){
  61.  
  62.                do{
  63.  
  64.                   try{
  65.  
  66.                       campGoal=Double.parseDouble(JOptionPane.showInputDialog("Please enter your goal amount: "));
  67.  
  68.                   }catch(NumberFormatException e){
  69.  
  70.                       campGoal=-1;
  71.  
  72.                       JOptionPane.showMessageDialog(null, "Error: please enter a valid goal amount: ");
  73.  
  74.                       trigger = true;
  75.  
  76.                   }
  77.                   if(campGoal<0.0 || campGoal>25000.0){
  78.                      JOptionPane.showMessageDialog(null, "Error: please enter a valid goal amount (1-25000)");
  79.                      trigger = true;
  80.                 }
  81.                
  82.                }while(trigger = true && (campGoal<0.0 || campGoal>25000.0));
  83.               }        
  84.           return campGoal;
  85.          }
  86.    //Prompts for campaign donations
  87.    public static double promptUserForCampDonation(double campGoal){
  88.      
  89.      
  90.       double donationAmount=0.0;
  91.      
  92.       while(donationAmount<campGoal){
  93.      
  94.             do{
  95.                try{
  96.                    donationAmount = Double.parseDouble(JOptionPane.showInputDialog("Please enter your donations, up to "+ campGoal));
  97.                }catch(NumberFormatException e){
  98.                    donationAmount=-1;
  99.                    JOptionPane.showMessageDialog(null, "Error: please enter a valid donation amount: ");
  100.                
  101.                  }
  102.                if(donationAmount<0 || donationAmount>campGoal){
  103.                   JOptionPane.showMessageDialog(null, "Error: please enter a valid donation amount: ");
  104.                  
  105.                  
  106.                }else{
  107.                  
  108.                  
  109.                }  
  110.               }while(donationAmount<campGoal);
  111.             }
  112.            
  113.        return donationAmount;
  114.            }
  115.          
  116.    public static double calculateTotal(double donationTotal, double donationAmount){  
  117.      
  118.      
  119.       donationTotal+=donationAmount;
  120.      
  121.     return donationTotal;
  122.    }
  123.    
  124.    //Comparisons between goal amounts and donation totals        
  125.    public static void printReport(Campaign campaign1, Campaign campaign2){
  126.      
  127.       String report= "Donation Tracking System Report";
  128.          report+= "\nCampaign Name: "+campaign1.getCampName();
  129.          report+= "\nTotal amount of donations collected: "+campaign1.getDonationTotal();
  130.        
  131.        
  132.       //All of campaign1's comparisons
  133.       if(campaign1.getDonationTotal()<campaign1.getCampGoal()){
  134.      
  135.          report+= "\n"+campaign1.getCampName() + " was below the goal amount by "+ (campaign1.getCampGoal()-campaign1.getDonationTotal());
  136.      
  137.       }else if(campaign1.getDonationTotal()>campaign1.getCampGoal()){
  138.      
  139.          report+="\n"+campaign1.getCampName() + " was above the goal amount by "+ (campaign1.getDonationTotal()-campaign1.getCampGoal());
  140.          
  141.       }else if(campaign1.getDonationTotal()==campaign1.getCampGoal()){
  142.      
  143.          report+="\n"+campaign1.getCampName() + " met the exact goal amount of "+campaign1.getCampGoal();
  144.       }
  145.      
  146.       report+= "\nCampaign Name: "+campaign2.getCampName();
  147.       report+= "\nTotal amount of donations collected: "+campaign2.getDonationTotal();
  148.  
  149.       //All of campaign2's comparisons
  150.       if(campaign2.getDonationTotal()<campaign1.getCampGoal()){
  151.      
  152.          report+= "\n"+campaign2.getCampName() + " was below the goal amount by "+ (campaign2.getCampGoal()-campaign2.getDonationTotal());
  153.      
  154.       }else if(campaign2.getDonationTotal()>campaign1.getCampGoal()){
  155.      
  156.          report+="\n"+campaign2.getCampName() + " was above the goal amount by "+ (campaign2.getDonationTotal()-campaign2.getCampGoal());
  157.          
  158.       }else if(campaign1.getDonationTotal()==campaign1.getCampGoal()){
  159.      
  160.          report+="\n"+campaign2.getCampName() + " met the exact goal amount of "+campaign2.getCampGoal();
  161.       }
  162.      
  163.      
  164.      
  165.       if(campaign1.getDonationTotal()>campaign2.getDonationTotal()){
  166.          
  167.          report+="\n"+campaign1.getCampName()+" collected the highest amount of donations.";
  168.          
  169.       }else if(campaign2.getDonationTotal()<campaign1.getDonationTotal()){
  170.          
  171.          report+="\n"+campaign2.getCampName()+" collected the highest amount of donations.";
  172.       }else if(campaign1.getDonationTotal()==campaign2.getDonationTotal()){
  173.          report+="\nBoth campaigns collected the same amount of donations.";
  174.       }  
  175.      
  176.       //Prints to the user with report
  177.       JOptionPane.showMessageDialog(null, report);
  178.      }
  179.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement