Derek MC
Derek MC

Reputation: 376

create a salary/bonus calculator

I have to create a salary/bonus calculator.

Depending on what salary is enter the bonus will be higher or lower/ eg. under 20000 will be 7% and over that will be 5.5%.

At the end of it I've to display the total amount of bonuses and the total amount of salaries with the bonus. I'm having trouble with the totals when the program is done.

Here is my code.

class Salaries
{

    public static void main(String agrs[])
    {

        int salary; // this takes in each salary
        double salaryTotal = 0; // this adds the bonus to to salary
        double bonus = 0; // this hold the bonus amount.
        double sumSalary=0;
        double sumBonus=0;
        String exit = "y";// This is the string to be entered to exit the loop.

        // below I'm using a do/while loop to keep it going till a key that isn't "y is entered"
        do
        {

        // the try/catch makes sure that only a number is entered.
        try{
            // below prompts the user to enter the salary amount
        System.out.println("Enter an Employes wages.");
        // below takes in the salary
        salary = EasyIn.getInt();

        // if else ensure that the salary is not below 0  
        // and to determine if the amount of bonus to be added.
        if (salary <=-1)
        {
        System.out.println("The salary can not be less than 0");
        }

        else if  (salary >0 && salary <=20000)
            {
            bonus = salary*7/100; // This takes the entered salary and calculates the bonus.
            salaryTotal = salary + bonus;
            System.out.println("The bonus paid is " + bonus);
            System.out.println("********************");
            System.out.println("The total salary + bonus is " + salaryTotal);
            System.out.println("********************");
            System.out.println("Enter y to enter another Employes wages. ");

            System.out.println("Press any other letter to exit.");
            System.out.println("********************");
            System.out.println();
            exit = EasyIn.getString();

            }
        else if (salary >20000 && salary <=30000)
            {
            bonus = salary*5.5/100; // This takes the entered salary and calculates the bonus.
            salaryTotal = salary + bonus;
            System.out.println("The bonus paid is " + bonus);
            System.out.println("********************");
            System.out.println("The total salary + bonus is " + salaryTotal);
            System.out.println("********************");
            System.out.println("Enter y to enter another Employes wages. ");

            System.out.println("Press any other letter to exit.");
            System.out.println("********************");
            System.out.println();
            exit = EasyIn.getString();


            }

        else if (salary >30000 && salary <=40000)
            {

            bonus = salary*4/100;
            salaryTotal = salary + bonus; // This takes the entered salary and calculates the bonus.
            System.out.println("The bonus paid is " + bonus);
            System.out.println("********************");
            System.out.println("The total salary + bonus is " + salaryTotal);
            System.out.println("********************");
            System.out.println("Enter y to enter another Employes wages. ");

            System.out.println("Press any other letter to exit.");
            System.out.println("********************");
            System.out.println();
            exit = EasyIn.getString();


            }

        else if (salary >40000)
            {
            bonus = salary*3.5/100;
            salaryTotal = salary + bonus; // This takes the entered salary and calculates the bonus.
            System.out.println("The bonus paid is " + bonus);
            System.out.println("********************");
            System.out.println("The total salary + bonus is " + salaryTotal);
            System.out.println("********************");
            System.out.println("Enter y to enter another Employes wages. ");

            System.out.println("Press any other letter to exit.");
            System.out.println("********************");
            System.out.println();
            exit = EasyIn.getString();



            }




        }
        catch(Exception e)
        {
            System.out.println("ERROR!!!! Please enter a number.");
        }


        }


        while(exit.equals("y"));


        System.out.println("The total amout of bonus is " + sumBonus );
        System.out.println("The total of all the salaries is " + sumSalary);



    }




}

Upvotes: 0

Views: 13389

Answers (1)

Kody Manharth
Kody Manharth

Reputation: 23

Make this a double. Division between int and double can lead to getting an integer back.

int salary; // this takes in each salary

For more information: http://www.cs.umd.edu/~clin/MoreJava/Intro/expr-int-div.html

Also it's a bit more efficient to do your bonus calculation as such:

bonus = salary * 0.055;

I'm also not seeing where you move the value from bonus to sumBonus for this line:

System.out.println("The total amout of bonus is " + sumBonus );

Perhaps change sumBonus to just bonus, don't forget to delete the declaration.

Upvotes: 2

Related Questions