# Payroll Calculator

• (2 Pages)
• 1
• 2

## 21 Replies - 5754 Views - Last Post: 12 September 2009 - 10:49 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=124965&amp;s=e5e948eff13bddb737ba164062a49166&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #16 TheNobleSword

Reputation: 2
• Posts: 57
• Joined: 05-September 09

## Re: Payroll Calculator

Posted 11 September 2009 - 05:38 PM

haha what a newb!

It works perfect!

I do have one question, how do I limit the "double" to 2 decimal places?

Also, for this part

"After you are successful at getting the program to work with one set of data at a time - add a while loop that will make the program stop when the user enters a 'Q' or 'q' as the next employee type."

```while ('a' != Q)
```

Thanks for the help Anton!

### #17 TheNobleSword

Reputation: 2
• Posts: 57
• Joined: 05-September 09

## Re: Payroll Calculator

Posted 11 September 2009 - 10:03 PM

I am almost done!

In order to round the currency output to two decimal places I used this code.

```DecimalFormat money = new DecimalFormat("0.00");
"\nEmployee Name:"+ " " +name+
"\nEmployee Salary:"+ " \$" +money.format(salary)+
"\nGross Earnings:"+ " \$" +money.format(grossS)+
"\nTotal tax deducated:"+ " \$" +money.format(taxS)+
"\nWeekly Pay:"+ " \$" +money.format(netS));

```

I do not know if this is the best way to do this, but it seems to work.

The only thing I need to do now is add the while loop that will disable the program is "q" is entered.

I tried.

```while ('a' != 'q')
```

Thinking, as long as char 'a' does not equal 'q' then the program will run. but it did not work for me.

### #18 AntonWebsters

• D.I.C Regular

Reputation: 88
• Posts: 428
• Joined: 15-August 09

## Re: Payroll Calculator

Posted 11 September 2009 - 11:34 PM

Try this out.
```
import javax.swing.JOptionPane;
import java.util.Scanner;
public class CylinderVolume {

public static void main(String[] args) {
char a = ' ';
while(a != 'q'){
String type = JOptionPane.showInputDialog("Enter Employee Type: ");
a = type.toLowerCase().charAt(0);

String name = JOptionPane.showInputDialog("Enter Employees Name: ");

if (a=='s'){
String salaryStr = JOptionPane.showInputDialog("Enter Employee Salary: ");
int salary = Integer.parseInt(salaryStr);

double grossS= salary*1/52;
double taxS= grossS*0.18;
double netS= grossS-taxS;

JOptionPane.showMessageDialog(null,"Employee type:" +'a'+
"/nEmployee Name:" +name+
"/nWeekly pay:" + netS+
"/nTotal tax deducated:"+taxS);
}
else if (a=='h'){

String rateStr = JOptionPane.showInputDialog("Enter Employee Rate: ");
int rate = Integer.parseInt(rateStr);

String hoursStr = JOptionPane.showInputDialog("Enter Employee Hours: ");

double grossH= rate*hours;
double taxH= rate*hours*0.18;
double netH= grossH-taxH;

JOptionPane.showMessageDialog(null,"Employee type:" +'a'+
"/nEmployee Name:" +name+
"/nWeekly pay:" + netH+
"/nTotal tax deducted:"+taxH);
}
}

}
}

```

### #19 TheNobleSword

Reputation: 2
• Posts: 57
• Joined: 05-September 09

## Re: Payroll Calculator

Posted 12 September 2009 - 01:42 AM

I think I got it! Take a look Anton, you did most of it. Thanks so much, your really took your time to help me, I really appreciate it!

I am sure that there are somethings that still need to be adjust, feel free to point them out!

```
package payrollcalculator;
import javax.swing.JOptionPane;
import java.text.*;

public class PayrollCalculator {

public static void main(String[] args) {

char a=' ';
while (a !='q'){

DecimalFormat money = new DecimalFormat("0.00");

//Employee Type
String type = JOptionPane.showInputDialog("Enter Employee Type: ");
a = type.toLowerCase().charAt(0);

//Employee Name
String name = JOptionPane.showInputDialog("Enter Employees Name: ");

//Salary Employee
if (a=='s'){
String salaryStr = JOptionPane.showInputDialog("Enter Employee Salary: ");
int salary = Integer.parseInt(salaryStr);

//Salary divided by 52, 18% tax deducted
double grossS= salary*1/52;
double taxS= grossS*0.18;
double netS= grossS-taxS;

JOptionPane.showMessageDialog(null,
"Employee type:"+" " +type+
"\nEmployee Name:"+ " " +name+
"\nEmployee Salary:"+ " \$" +money.format(salary)+
"\nGross Earnings:"+ " \$" +money.format(grossS)+
"\nTotal tax deducated:"+ " \$" +money.format(taxS)+
"\nWeekly Pay:"+ " \$" +money.format(netS));

}

//Temp Employee
else if (a=='t'){

//Hourly Rate
String TrateStr = JOptionPane.showInputDialog("Enter Employee Rate: ");
int rateT = Integer.parseInt(TrateStr);
//Amount of hours worked
String ThoursStr = JOptionPane.showInputDialog("Enter Employee Hours: ");

//Rate*Time, No tax deducted
double grossT= rateT*hoursT;
double netT= rateT*hoursT;

JOptionPane.showMessageDialog(null,
"Employee type:"+" "+type+
"\nEmployee Name:"+ " " +name+
"\nHourly Rate:"+ " \$" +money.format(rateT)+
"\nHours Worked:"+ " "+hoursT+
"\nNO TAX DEDUCTED"+
"\nWeekly Pay:"+ " \$" + money.format(netT));
}

//Hourly Employee
else if (a=='h'){

//Hourly Rate
String HrateStr = JOptionPane.showInputDialog("Enter Employee Rate: ");
int rateH = Integer.parseInt(HrateStr);

//Amount of hours
String HhoursStr = JOptionPane.showInputDialog("Enter Employee Hours: ");

//Rate*Time, 18% tax deducted
double  grossH= rateH*hoursH;
double taxH= rateH*hoursH*0.18;
double netH= grossH-taxH;

JOptionPane.showMessageDialog(null,
"Employee type:"+" " +type+
"\nEmployee Name:"+ " " +name+
"\nHourly Rate:"+ " \$" +money.format(rateH)+
"\nHours Worked:"+ " "+hoursH+
"\nGross Earnings:"+ " \$"+money.format(grossH)+
"\nTotal Tax Deducted:"+ " \$"+money.format(taxH)+
"\nWeekly Pay:"+ " \$" + money.format(netH));
}
}
}
}

```

### #20 AntonWebsters

• D.I.C Regular

Reputation: 88
• Posts: 428
• Joined: 15-August 09

## Re: Payroll Calculator

Posted 12 September 2009 - 02:09 AM

You are welcome, sir.
Well, the code doesn't seem to have any errors to me, so I guess it's alright.
By the way, I'm really sorry for that quote I made, which you're using as signature right now lol. I meant no offense.

### #21 TheNobleSword

Reputation: 2
• Posts: 57
• Joined: 05-September 09

## Re: Payroll Calculator

Posted 12 September 2009 - 10:17 AM

No offense at all, I thought it was funny. I am not afraid to admit that I am a beginner.

### #22 TheNobleSword

Reputation: 2
• Posts: 57
• Joined: 05-September 09

## Re: Payroll Calculator

Posted 12 September 2009 - 10:49 AM

double post, oops.

This post has been edited by TheNobleSword: 12 September 2009 - 10:50 AM