Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package salarycalculator;
- import java.text.DecimalFormat; // Decimal Format - for showing 2 digits after dot
- import java.util.InputMismatchException; // InputMismatch Exception util
- import java.util.Scanner; // Scanner util
- public class SalaryCalculator {
- /* Needed variables */
- public static String workerName;
- public static double basicSalary;
- public static boolean workerOfTheMonth;
- public static int experienceInYears, numberOfChildren;
- /* Tax variables */
- public static float taxForSalaryBet0To4000;
- public static float taxForSalaryBet4000To6000;
- public static float taxForSalaryMore6000;
- /* Convert data from args to the tax variables */
- public static void convertDataFromArgs(String[] args) {
- try
- {
- taxForSalaryBet0To4000 = Float.parseFloat(args[0]);
- taxForSalaryBet4000To6000 = Float.parseFloat(args[1]);
- taxForSalaryMore6000 = Float.parseFloat(args[2]);
- }
- /* Safe Fail Checks */
- catch (NumberFormatException ex)
- {
- System.err.println("Fatal Error: One of the arguments is invaild (not a float)");
- System.err.println("Error Code: " + ex);
- System.err.println("Program has terminate itself.");
- System.exit(1);
- }
- catch (ArrayIndexOutOfBoundsException ex2)
- {
- System.err.println("Fatal Error: No anought arguments (need 3)!");
- System.err.println("Error Code: " + ex2);
- System.err.println("Program has terminate itself.");
- System.exit(2);
- }
- }
- /* Print the menu */
- public static void printMenu() {
- System.out.println("******************************************");
- System.out.println("* I: Information about worker *");
- System.out.println("* N: Print net salary *");
- System.out.println("* V: Print gross salary *");
- System.out.println("* T: Print Tax *");
- System.out.println("* P: P-Print pay slip *");
- System.out.println("* E: End program *");
- System.out.println("******************************************");
- }
- /* Get worker's data */
- public static void getData() {
- try
- {
- Scanner s = new Scanner(System.in);
- System.out.println("Please enter worker name:");
- workerName = s.nextLine();
- System.out.println("Please enter worker's basic salary:");
- basicSalary = s.nextDouble();
- System.out.println("Is the worker \"Worker of The Month\"?");
- workerOfTheMonth = s.nextBoolean();
- System.out.println("Please enter worker's experience in years:");
- experienceInYears = s.nextInt();
- /* Salary bounus per child only counts when the worker is "Worker of The Month" */
- if (workerOfTheMonth)
- {
- System.out.println("How much children does the worker have?");
- numberOfChildren = s.nextInt();
- }
- }
- catch(InputMismatchException ex)
- {
- System.err.println("Fatal Error: Input Mismatch!");
- System.err.println("Error Code: " + ex);
- System.err.println("Retrying . . . ");
- getData();
- }
- }
- /* Get net salary */
- public static double getNetSalary() {
- double Tax = (calcTaxOfBaseSalary() + (calcTaxOfBonus()));
- return (getGrossSalary() - Tax);
- }
- /* Get gross salary
- * Gross salary = basic salary + bonus
- *
- *
- * Bonues:
- * 1. Monthly bonus: If the worker is the "Worker of The Month", 5% bonus
- * per child for the first 3 childs, 2.5% bonus per child for 3rd to 6th
- * child. If the worker isn't "Worker of The Month" - no bonus.
- *
- * 2. Experiences bonus: The worker gets bonus depends on worker's experience
- * in years.
- * Experiences bonus = Square root(basic salary * experience in years)
- */
- public static double getGrossSalary() {
- double GrossSalary = basicSalary;
- double salaryBonus = Math.sqrt(basicSalary*experienceInYears);
- /* Checks if the worker is "Worker of The Month" */
- if (workerOfTheMonth)
- {
- switch (numberOfChildren)
- {
- case 0: GrossSalary += salaryBonus;
- case 1:
- case 2:
- case 3:
- {
- for(int i = 0; i < numberOfChildren; i++)
- GrossSalary = (GrossSalary + ((GrossSalary * 5) / 100));
- GrossSalary += salaryBonus;
- }
- default:
- {
- for(int i = 0; i < 3; i++)
- GrossSalary = (GrossSalary + ((GrossSalary * 5) / 100));
- for (int i = 0; i < numberOfChildren-3; i++)
- {
- if (i < 7)
- GrossSalary = (GrossSalary + ((GrossSalary * 2.5) / 100));
- }
- GrossSalary += salaryBonus;
- }
- }
- }
- else
- GrossSalary += salaryBonus;
- return GrossSalary;
- }
- /* Calculates tax for basic salary */
- public static float calcTaxOfBaseSalary() {
- float tax;
- if ((basicSalary >= 0) && (basicSalary <= 4000))
- tax = (float) (basicSalary * taxForSalaryBet0To4000);
- else if ((basicSalary > 4000) && (basicSalary < 6000))
- tax = (float) (basicSalary * taxForSalaryBet4000To6000);
- else
- tax = (float) (taxForSalaryBet0To4000 + (2000 * taxForSalaryBet4000To6000) + ((basicSalary - 6000) * taxForSalaryMore6000));
- return tax;
- }
- /* Calculates tax for bounus */
- public static float calcTaxOfBonus() {
- return (float) (((getGrossSalary() - basicSalary) * 12) / 100);
- }
- /* Prints the tax */
- public static void printTax() {
- double taxbasesalary = calcTaxOfBaseSalary(); // Get tax of base salary
- double taxbonus = calcTaxOfBonus(); // Get tax of bonus
- DecimalFormat df = new DecimalFormat("#.00");
- System.out.println("Tax paid on bonus:" + df.format(taxbonus) + " nis");
- System.out.println("Tax paid on base salary: " + df.format(taxbasesalary) + " nis");
- System.out.println("Total tax: " + (df.format((taxbonus + taxbasesalary))) + " nis");
- }
- /* Prints entire information of the worker */
- public static void printPaySlip() {
- double taxbase = calcTaxOfBaseSalary(); // Get tax of base salary
- double taxbonus = calcTaxOfBonus(); // Get tax of bonus
- double GrossSalary = getGrossSalary(); // Get gross salary
- DecimalFormat df = new DecimalFormat("#.00");
- System.out.println("PAY SLIP For worker: " + workerName);
- System.out.println("Basic salary is: " + basicSalary + " nis");
- System.out.println("Tax for basic salary: " + df.format(taxbase) + " nis");
- System.out.println("Bonus salary is: " + (df.format(GrossSalary - basicSalary)) + " nis");
- System.out.println("Tax for bonus salary: " + df.format(taxbonus) + " nis");
- System.out.println("Total gross salary is: " + df.format(GrossSalary) + " nis");
- System.out.println("Total tax is: " + (df.format(taxbase + taxbonus)) + " nis");
- System.out.println("Total amount in bank: " + df.format(getNetSalary()) + " nis");
- }
- public static void main(String[] args) {
- convertDataFromArgs(args); // Get the args
- printMenu(); // Print the menu
- Scanner in = new Scanner(System.in); // Creates a scanner (for input)
- String input; // Input temp variable
- char tav; // The char variable
- /* While loop - so the program will never end until we send the exit command */
- while(true)
- {
- input = in.nextLine(); // Get the input line
- tav = input.charAt(0); // Get the first char from line
- switch(tav)
- {
- /* Information about worker command */
- case 'I':
- {
- getData();
- continue;
- }
- /* Print net salary command */
- case 'N':
- {
- DecimalFormat df = new DecimalFormat("#.00");
- System.out.println("Net salary is: " + df.format(getNetSalary()) + " nis");
- continue;
- }
- /* Print gross salary command */
- case 'V':
- {
- DecimalFormat df = new DecimalFormat("#.00");
- System.out.println("Gross salary is: " + df.format(getGrossSalary()) + " nis");
- continue;
- }
- /* Print Tax command */
- case 'T':
- {
- printTax();
- continue;
- }
- /* Print pay slip command */
- case 'P':
- {
- printPaySlip();
- continue;
- }
- /* Exit command */
- case 'E':
- {
- System.out.println("Good Bye!");
- System.exit(0);
- }
- /* When the command is invaild */
- default: System.out.println("Error: Invaild argument");
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement