Advertisement
Hyuna

Salary Calculator - HomeWork

Apr 15th, 2015
243
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 10.28 KB | None | 0 0
  1. package salarycalculator;
  2.  
  3. import java.text.DecimalFormat; // Decimal Format - for showing 2 digits after dot
  4. import java.util.InputMismatchException; // InputMismatch Exception util
  5. import java.util.Scanner; // Scanner util
  6.  
  7. public class SalaryCalculator {
  8.    
  9.     /* Needed variables */
  10.     public static String workerName;
  11.     public static double basicSalary;
  12.     public static boolean workerOfTheMonth;
  13.     public static int experienceInYears, numberOfChildren;  
  14.    
  15.     /* Tax variables */
  16.     public static float taxForSalaryBet0To4000;
  17.     public static float taxForSalaryBet4000To6000;
  18.     public static float taxForSalaryMore6000;
  19.  
  20.     /* Convert data from args to the tax variables  */
  21.     public static void convertDataFromArgs(String[] args) {
  22.         try
  23.         {
  24.             taxForSalaryBet0To4000 = Float.parseFloat(args[0]);
  25.             taxForSalaryBet4000To6000 = Float.parseFloat(args[1]);
  26.             taxForSalaryMore6000 = Float.parseFloat(args[2]);
  27.         }
  28.        
  29.         /* Safe Fail Checks */
  30.         catch (NumberFormatException ex)
  31.         {
  32.             System.err.println("Fatal Error: One of the arguments is invaild (not a float)");
  33.             System.err.println("Error Code: " + ex);
  34.             System.err.println("Program has terminate itself.");
  35.             System.exit(1);
  36.         }
  37.        
  38.         catch (ArrayIndexOutOfBoundsException ex2)
  39.         {
  40.             System.err.println("Fatal Error: No anought arguments (need 3)!");
  41.             System.err.println("Error Code: " + ex2);
  42.             System.err.println("Program has terminate itself.");
  43.             System.exit(2);
  44.         }
  45.     }
  46.    
  47.     /* Print the menu */
  48.     public static void printMenu() {
  49.         System.out.println("******************************************");    
  50.         System.out.println("*       I: Information about worker      *");
  51.         System.out.println("*       N: Print net salary              *");
  52.         System.out.println("*       V: Print gross salary            *");
  53.         System.out.println("*       T: Print Tax                     *");
  54.         System.out.println("*       P: P-Print pay slip              *");
  55.         System.out.println("*       E: End program                   *");
  56.         System.out.println("******************************************");  
  57.     }
  58.    
  59.     /* Get worker's data */
  60.     public static void getData() {
  61.         try
  62.         {
  63.             Scanner s = new Scanner(System.in);
  64.            
  65.             System.out.println("Please enter worker name:");
  66.             workerName = s.nextLine();
  67.  
  68.             System.out.println("Please enter worker's basic salary:");
  69.             basicSalary = s.nextDouble();
  70.  
  71.             System.out.println("Is the worker \"Worker of The Month\"?");
  72.             workerOfTheMonth = s.nextBoolean();
  73.  
  74.             System.out.println("Please enter worker's experience in years:");
  75.             experienceInYears = s.nextInt();
  76.  
  77.             /* Salary bounus per child only counts when the worker is "Worker of The Month" */
  78.             if (workerOfTheMonth)
  79.             {
  80.                 System.out.println("How much children does the worker have?");
  81.                 numberOfChildren = s.nextInt();
  82.             }
  83.         }
  84.        
  85.         catch(InputMismatchException ex)
  86.         {
  87.             System.err.println("Fatal Error: Input Mismatch!");
  88.             System.err.println("Error Code: " + ex);
  89.             System.err.println("Retrying . . . ");
  90.             getData();
  91.         }
  92.     }
  93.    
  94.     /* Get net salary */
  95.     public static double getNetSalary() {
  96.         double Tax = (calcTaxOfBaseSalary() + (calcTaxOfBonus()));
  97.         return (getGrossSalary() - Tax);
  98.     }
  99.    
  100.     /* Get gross salary
  101.     * Gross salary = basic salary + bonus
  102.     *
  103.     *
  104.     * Bonues:
  105.     * 1. Monthly bonus: If the worker is the "Worker of The Month", 5% bonus
  106.     *    per child for the first 3 childs, 2.5% bonus per child for 3rd to 6th
  107.     *    child. If the worker isn't "Worker of The Month" - no bonus.
  108.     *
  109.     * 2. Experiences bonus: The worker gets bonus depends on worker's experience
  110.     *    in years.
  111.     *    Experiences bonus = Square root(basic salary * experience in years)
  112.     */
  113.     public static double getGrossSalary() {
  114.         double GrossSalary = basicSalary;
  115.         double salaryBonus = Math.sqrt(basicSalary*experienceInYears);
  116.        
  117.         /* Checks if the worker is "Worker of The Month" */
  118.         if (workerOfTheMonth)
  119.         {
  120.                switch (numberOfChildren)
  121.                {
  122.                    case 0: GrossSalary += salaryBonus;
  123.                    
  124.                    case 1:
  125.                    case 2:
  126.                    case 3:
  127.                    {
  128.                         for(int i = 0; i < numberOfChildren; i++)
  129.                             GrossSalary = (GrossSalary + ((GrossSalary * 5) / 100));
  130.                        
  131.                         GrossSalary += salaryBonus;
  132.                    }
  133.                    
  134.                    default:
  135.                    {
  136.                        for(int i = 0; i < 3; i++)
  137.                             GrossSalary = (GrossSalary + ((GrossSalary * 5) / 100));
  138.                        
  139.                        for (int i = 0; i < numberOfChildren-3; i++)
  140.                        {
  141.                            if (i < 7)
  142.                                GrossSalary = (GrossSalary + ((GrossSalary * 2.5) / 100));
  143.                        }
  144.                        
  145.                        GrossSalary += salaryBonus;
  146.                    }
  147.                }
  148.         }
  149.        
  150.         else
  151.             GrossSalary += salaryBonus;
  152.        
  153.         return GrossSalary;
  154.     }
  155.    
  156.     /* Calculates tax for basic salary */
  157.     public static float calcTaxOfBaseSalary() {
  158.         float tax;
  159.        
  160.         if ((basicSalary >= 0) && (basicSalary <= 4000))
  161.             tax = (float) (basicSalary * taxForSalaryBet0To4000);
  162.        
  163.         else if ((basicSalary > 4000) && (basicSalary < 6000))
  164.             tax = (float) (basicSalary * taxForSalaryBet4000To6000);
  165.        
  166.         else
  167.             tax = (float) (taxForSalaryBet0To4000 + (2000 * taxForSalaryBet4000To6000) + ((basicSalary - 6000) * taxForSalaryMore6000));
  168.                    
  169.         return tax;
  170.     }
  171.    
  172.    
  173.     /* Calculates tax for bounus */
  174.     public static float calcTaxOfBonus() {
  175.         return (float) (((getGrossSalary() - basicSalary) * 12) / 100);
  176.     }
  177.    
  178.     /* Prints the tax */
  179.     public static void printTax() {
  180.         double taxbasesalary = calcTaxOfBaseSalary(); // Get tax of base salary
  181.         double taxbonus = calcTaxOfBonus();           // Get tax of bonus
  182.        
  183.         DecimalFormat df = new DecimalFormat("#.00");
  184.        
  185.         System.out.println("Tax paid on bonus:" + df.format(taxbonus) + " nis");
  186.         System.out.println("Tax paid on base salary: " + df.format(taxbasesalary) + " nis");
  187.         System.out.println("Total tax: " + (df.format((taxbonus + taxbasesalary))) + " nis");
  188.     }
  189.    
  190.     /* Prints entire information of the worker */
  191.     public static void printPaySlip() {
  192.         double taxbase = calcTaxOfBaseSalary(); // Get tax of base salary
  193.         double taxbonus = calcTaxOfBonus();     // Get tax of bonus
  194.         double GrossSalary = getGrossSalary();  // Get gross salary
  195.        
  196.         DecimalFormat df = new DecimalFormat("#.00");
  197.        
  198.         System.out.println("PAY SLIP For worker: " + workerName);
  199.         System.out.println("Basic salary is: " + basicSalary + " nis");                          
  200.         System.out.println("Tax for basic salary: " + df.format(taxbase) + " nis");
  201.         System.out.println("Bonus salary is: " + (df.format(GrossSalary - basicSalary)) + " nis");
  202.         System.out.println("Tax for bonus salary: " + df.format(taxbonus) + " nis");
  203.         System.out.println("Total gross salary is: " + df.format(GrossSalary) + " nis");
  204.         System.out.println("Total tax is: " + (df.format(taxbase + taxbonus)) + " nis");
  205.         System.out.println("Total amount in bank: " + df.format(getNetSalary()) + " nis");
  206.     }
  207.    
  208.     public static void main(String[] args) {
  209.         convertDataFromArgs(args); // Get the args
  210.         printMenu(); // Print the menu
  211.        
  212.         Scanner in = new Scanner(System.in); // Creates a scanner (for input)
  213.        
  214.         String input; // Input temp variable
  215.         char tav; // The char variable
  216.        
  217.         /* While loop - so the program will never end until we send the exit command */
  218.         while(true)
  219.         {
  220.             input = in.nextLine(); // Get the input line
  221.             tav = input.charAt(0); // Get the first char from line
  222.            
  223.             switch(tav)
  224.             {      
  225.                 /* Information about worker command */
  226.                 case 'I':
  227.                 {
  228.                     getData();
  229.                     continue;
  230.                 }
  231.                
  232.                 /* Print net salary command */
  233.                 case 'N':
  234.                 {
  235.                     DecimalFormat df = new DecimalFormat("#.00");
  236.                     System.out.println("Net salary is: " + df.format(getNetSalary()) + " nis");
  237.                     continue;
  238.                 }
  239.                
  240.                 /* Print gross salary command */
  241.                 case 'V':
  242.                 {
  243.                     DecimalFormat df = new DecimalFormat("#.00");
  244.                     System.out.println("Gross salary is: " + df.format(getGrossSalary()) + " nis");
  245.                     continue;
  246.                 }
  247.                
  248.                 /* Print Tax command */
  249.                 case 'T':
  250.                 {
  251.                     printTax();
  252.                     continue;
  253.                 }
  254.                
  255.                 /* Print pay slip command */
  256.                 case 'P':
  257.                 {
  258.                     printPaySlip();
  259.                     continue;
  260.                 }
  261.                
  262.                 /* Exit command */
  263.                 case 'E':
  264.                 {
  265.                     System.out.println("Good Bye!");
  266.                     System.exit(0);
  267.                 }
  268.                
  269.                 /* When the command is invaild */
  270.                 default: System.out.println("Error: Invaild argument");
  271.             }
  272.         }
  273.     }
  274.    
  275. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement