Konix

Payroll (Tester class)

Nov 14th, 2013
629
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.97 KB | None | 0 0
  1. /**
  2.    A class to calculate employee payroll using the
  3.    Company interface and the Salaried and Hourly classes.
  4.    
  5.    @auth Dave Lastname
  6.    @date 11/14/13
  7. */
  8.  
  9. import java.util.*;
  10.  
  11. public class Payroll
  12. {
  13.    public static void main(String args[])
  14.    {
  15.       Scanner scan = new Scanner(System.in);
  16.       int numEmps = 0;
  17.       ArrayList<Company> employees = new ArrayList<Company>();
  18.       boolean go = true;
  19.      
  20.       while(go == true)
  21.       {
  22.          System.out.println("What do you want to do?");
  23.          System.out.println("   1. Create a new employee");
  24.          System.out.println("   2. Set pay");
  25.          System.out.println("   3. Show weekly pay");
  26.          System.out.println("   4. Show annual pay");
  27.          System.out.println("   5. Show summary");
  28.          System.out.println("   6. Quit");
  29.          
  30.          int num = scan.nextInt();
  31.          
  32.          if(num > 6 || num < 0)
  33.          {
  34.             System.out.println("ERROR: Bad option, please try again.");
  35.          }
  36.          
  37.          if(num == 1)
  38.          {
  39.             System.out.println("What type of employee would you like to create (Salaried or Hourly)?");
  40.             String typeInput = scan.next();
  41.             while(!(typeInput.equalsIgnoreCase("salaried") || typeInput.equalsIgnoreCase("hourly")))
  42.             {
  43.                System.out.printf("Please enter either 'salaried' or 'hourly'");
  44.                typeInput = scan.next();
  45.             }
  46.             if(typeInput.equalsIgnoreCase("hourly"))
  47.             {
  48.                System.out.printf("Please enter the name: \n");
  49.                String name = scan.next();
  50.                System.out.printf("Please enter the social security number: \n");
  51.                String SSN = scan.next();
  52.                System.out.printf("Please enter the rate of hourly pay: \n");
  53.                double pay = scan.nextDouble();
  54.                while(pay <= 0)
  55.                {
  56.                   System.out.println("Please enter a number > 0");
  57.                   pay = scan.nextDouble();
  58.                }
  59.                System.out.printf("Please enter the average number of hours worked per week: \n");
  60.                double avgHours = scan.nextDouble();
  61.                while(avgHours <= 0)
  62.                {
  63.                   System.out.println("Please enter a number > 0");
  64.                   avgHours = scan.nextDouble();
  65.                }
  66.                Hourly hourlyEmp = new Hourly(name, SSN, avgHours);
  67.                hourlyEmp.setPay(pay);
  68.                employees.add(hourlyEmp);
  69.                numEmps++;
  70.                System.out.printf("Employee " + numEmps + " has been created. \n");
  71.             }
  72.             else if(typeInput.equalsIgnoreCase("salaried"))
  73.             {
  74.                System.out.printf("Please enter the name: \n");
  75.                String name = scan.next();
  76.                System.out.printf("Please enter the social security number: \n");
  77.                String SSN = scan.next();
  78.                System.out.printf("Please enter the annual pay: \n");
  79.                double pay = scan.nextDouble();
  80.                while(pay <= 0)
  81.                {
  82.                   System.out.println("Please enter a number > 0");
  83.                   pay = scan.nextDouble();
  84.                }
  85.                Salaried salariedEmp = new Salaried(name, SSN);
  86.                salariedEmp.setPay(pay);
  87.                employees.add(salariedEmp);
  88.                numEmps++;
  89.                System.out.printf("Employee " + numEmps + " has been created. \n");
  90.             }
  91.          }//end of case 1
  92.          else if(num == 2)
  93.          {
  94.             Company emp;
  95.             if(numEmps >= 2)
  96.             {
  97.                System.out.println("Which employee do you wish to use (enter 1 to " + numEmps + ")?");
  98.                int empNum = scan.nextInt();
  99.                while(!(empNum > 0 && empNum < numEmps))
  100.                {
  101.                   System.out.println("ERROR: Please enter number between 1 and " + numEmps + ".");
  102.                   empNum = scan.nextInt();
  103.                }
  104.                emp = employees.get(empNum - 1);
  105.             }
  106.             else
  107.             {
  108.                emp = employees.get(0);
  109.             }    
  110.             System.out.printf("How much would you like to set the annual pay to? \n");
  111.             int pay = scan.nextInt();
  112.             while(pay <= 0)
  113.             {
  114.                System.out.println("Please enter an amount > 0");
  115.                pay = scan.nextInt();
  116.             }
  117.             emp.setPay(pay);
  118.          }//end of case 2
  119.          else if(num == 3)
  120.          {
  121.             Company emp1;
  122.             if(numEmps >= 2)
  123.             {
  124.                System.out.println("Which employee do you wish to use (enter 1 to " + numEmps + ")?");
  125.                int empNum1 = scan.nextInt();
  126.                while(!(empNum1 > 0 && empNum1 < numEmps))
  127.                {
  128.                   System.out.println("ERROR: Please enter number between 1 and " + numEmps + ".");
  129.                   empNum1 = scan.nextInt();
  130.                }
  131.                emp1 = employees.get(empNum1 - 1);
  132.             }
  133.             else
  134.             {
  135.                emp1 = employees.get(0);
  136.             }  
  137.             System.out.printf("The weekly pay is $ %.2f", emp1.calcWeeklyPay());
  138.             System.out.println(" ");
  139.          }//end of case 3
  140.          else if(num == 4)
  141.          {
  142.             Company emp2;
  143.             if(numEmps >= 2)
  144.             {
  145.                System.out.println("Which employee do you wish to use (enter 1 to " + numEmps + ")?");
  146.                int empNum2 = scan.nextInt();
  147.                while(!(empNum2 > 0 && empNum2 < numEmps))
  148.                {
  149.                   System.out.println("ERROR: Please enter number between 1 and " + numEmps + ".");
  150.                   empNum2 = scan.nextInt();
  151.                }
  152.                emp2 = employees.get(empNum2 - 1);
  153.             }
  154.             else
  155.             {
  156.                emp2 = employees.get(0);
  157.             }
  158.             System.out.printf("The weekly pay is $ %.2f", emp2.calcAnnualPay());
  159.             System.out.println(" ");
  160.          }//end of case 4
  161.          else if(num == 5)
  162.          {
  163.             Company emp3;
  164.             if(numEmps >= 2)
  165.             {
  166.                System.out.println("Which employee do you wish to use (enter 1 to " + numEmps + ")?");
  167.                int empNum3 = scan.nextInt();
  168.                while(!(empNum3 > 0 && empNum3 < numEmps))
  169.                {
  170.                   System.out.println("ERROR: Please enter number between 1 and " + numEmps + ".");
  171.                   empNum3 = scan.nextInt();
  172.                }
  173.                emp3 = employees.get(empNum3 - 1);
  174.             }
  175.             else
  176.             {
  177.                emp3 = employees.get(0);
  178.             }
  179.             System.out.print(emp3);
  180.             System.out.println(" ");
  181.          }//end of case 5
  182.          else if(num == 6)
  183.          {
  184.             go = false;
  185.          }//end of case 6
  186.       }//end of while loop GO
  187.    }//end of main
  188. }//end class
Advertisement
Add Comment
Please, Sign In to add comment