Advertisement
Guest User

Untitled

a guest
Mar 22nd, 2018
769
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.30 KB | None | 0 0
  1. /**
  2.  *
  3.  * @author Blake
  4.  * @version 03/22/18
  5.  */
  6.  
  7. public class ElectionTesterV1
  8. {
  9.     public static void main(String[] args)
  10.     {
  11.         Candidate[] candidateList = new Candidate[5];
  12.  
  13.         // create inventory
  14.         candidateList[0] = new Candidate("Lucy Robertson", 6000);
  15.         candidateList[1] = new Candidate("Marie Grace", 2400);
  16.         candidateList[2] = new Candidate("Tyler Murphy", 1800);
  17.         candidateList[3] = new Candidate("Mia Garcia", 3200);
  18.         candidateList[4] = new Candidate("John Smith", 2100);
  19.         System.out.println("      Raw Election Data       ");
  20.         System.out.println("______________________________");
  21.        
  22.         printCandidates(candidateList);
  23.         int votesTotal = findTotal(candidateList);
  24.        
  25.         System.out.println();
  26.         System.out.println();
  27.         printTable(candidateList, votesTotal);
  28.     }
  29.  
  30.     public static void printCandidates(Candidate[] itemList)
  31.     {
  32.         for(int i = 0; i < itemList.length; i++)
  33.            System.out.println(itemList[i]);
  34.     }
  35.    
  36.     public static void printTable(Candidate[] itemList, int votesTotal)
  37.     {
  38.         System.out.println("Election Results: ");
  39.         System.out.println();
  40.         System.out.println("                          Votes           % of Total");
  41.         System.out.println("Candidate                Received            Votes");
  42.         System.out.println("-----------------------------------------------------");
  43.         //need to printf this before submitting
  44.         for(int i = 0; i < itemList.length; i++){
  45.            System.out.println(itemList[i].getName() + "        " + itemList[i].getVotes() + "             " + ((double)((double)itemList[i].getVotes() / (double)votesTotal) * 10));
  46.         }
  47.         System.out.println();
  48.         System.out.println("Total number of votes cast in election: " + votesTotal);
  49.     }
  50.    
  51.    
  52.     public static int findTotal(Candidate[] itemList)
  53.     {
  54.         int votesTotal = 0;
  55.         int index;
  56.  
  57.         if (itemList.length != 0)
  58.         {
  59.             votesTotal = itemList[0].getVotes();
  60.             index = 0;
  61.  
  62.             for (int i = 1; i < itemList.length; i++)
  63.             {
  64.                 votesTotal += itemList[i].getVotes();
  65.             }
  66.         }
  67.         return votesTotal;
  68.     }
  69.    
  70. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement