Payroll Calculator

Take a look!

  • (2 Pages)
  • +
  • 1
  • 2

21 Replies - 6462 Views - Last Post: 12 September 2009 - 10:49 AM Rate Topic: -----

#1 TheNobleSword  Icon User is offline

  • D.I.C Head

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

Payroll Calculator

Posted 09 September 2009 - 11:31 PM

This is my second project for my java class. I think I am comfortable with the variables and arithmetic, but i need to do a while loop. and I am a little confused by the directions. I would appreciate help!


This is the part I do not get:

You will use the String data type to contain the employee names and a char data type to contain the type of employee. The employee type will be input as a String and you will be able to convert it to a char by using the charAt() method that is part of the String class. For example, assume the following code:

String word;
char oneChar;
word = JOptionPane.showInputDialog("Enter a word");
oneChar = word.charAt(4);

What is he asking?

Is This A Good Question/Topic? 0
  • +

Replies To: Payroll Calculator

#2 AntonWebsters  Icon User is offline

  • D.I.C Regular
  • member icon

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

Re: Payroll Calculator

Posted 09 September 2009 - 11:51 PM

The example that is given is clear enough actually.
At first, you should make the code prompt users to enter employee type, in string.
Then, the 5th index of the string will be extracted out and converted into char type, using chatAt() method.
Let's say, you run the code provided, and you entered this word: "Engineer", then the 5th alphabet of the word, which is 'n' will be converted into char, and will be assigned to oneChar.
Was This Post Helpful? 1
  • +
  • -

#3 TheNobleSword  Icon User is offline

  • D.I.C Head

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

Re: Payroll Calculator

Posted 10 September 2009 - 12:04 AM

Okay, Thanks.

I understand that charat(x) is going to assign the character at x+1 as the Char value.....I think?

What is the advantage of this, why not just use the whole string. If the employee type was Hourly, why would I want it to be converted to..say "u"?

Maybe I am just really confused. I will post the whole assignment maybe that will make more sense

"The focus of this programming assignment is on using the decision structure to solve a problem. You are to write a payroll calculation problem for “Bernard’s Travel Bureau” that will calculate the weekly paycheck for both salaried and hourly employees. Salaried employees will be paid 1/52 of their annual pay minus 18% that is withheld for taxes. There are two types of weekly employees; permanent employees who also have 18% withheld for taxes, and student employees who are tax exempt. Bernard pays time and a half for hours worked over 35 per week (this applies to students as well as permanent hourly employees).
You will use the String data type to contain the employee names and a char data type to contain the type of employee. The employee type will be input as a String and you will be able to convert it to a char by using the charAt() method that is part of the String class. For example, assume the following code:

String word;
char oneChar;
word = JOptionPane.showInputDialog("Enter a word");
oneChar = word.charAt(4);

Assume that the user enters the word "foundation" when prompted. After the execution of the last statement, a 'd' would be in the char variable oneChar because the character at the 5th (4 + 1) location is a 'd'. The counting starts at position 0, so word.charAt(0) would be the 'f', word.charAt(1) would be the 'o', etc.

The program will read the employees and type. An ‘S’ in the type field is for salaried, an ‘H’ is for permanent hourly, and a ‘T’ is for temporary student. If the type is an ‘S’, you will read the annual salary figure. If the type is an ‘H’ or ‘T’, you will read the hours worked and pay rate. Your program should be able to work even if the user enters a lower case 's', 'h', or 't'. Output will consist of the employee’s name, their weekly gross pay, and their weekly net pay. If it is a student, the string “NO TAXES DEDUCTED” will appear in the weekly net pay column. 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. Three sample sets of data are shown below: "
Was This Post Helpful? 0
  • +
  • -

#4 AntonWebsters  Icon User is offline

  • D.I.C Regular
  • member icon

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

Re: Payroll Calculator

Posted 10 September 2009 - 12:25 AM

I'm not sure why they want you to do that either..maybe for learning purposes? Lol..
Was This Post Helpful? 0
  • +
  • -

#5 Fuzzyness  Icon User is offline

  • Comp Sci Student
  • member icon

Reputation: 669
  • View blog
  • Posts: 2,438
  • Joined: 06-March 09

Re: Payroll Calculator

Posted 10 September 2009 - 01:46 AM

It would be pointless, what if the Name is Lei? Not even 4 chars wrong. I would say that they are trying to get you familiar with the method as it is. So you will be able to say "oh hey, the word the entered was Salary, but idc because I only want the first letter" BAM! you know how to do it, compare the first letter to an if else statement block that will decided whether or not you use salary or hourly.

Yes what if they mispell salary as Salerie? It wouldnt read it correctly because you would be using the .equals method if it were a String. Using a char, just about everyone can figure out whether spelling it or sounding it out the first letter. :)

Just a thought that they might have taken into consideration.

String type = JOptionPane.showInputDialog("Enter Pay Type: ");
char let = type.toLowerCase().charAt(0); // to the to lowercase that way whether it is already lower it wont matter and if it is upper you can now just use a sigle operator instead of doing  let == 's' || let == 'S'
if(let == 's')
{ ...}
else { ... }


Hope this helps!
Was This Post Helpful? 1
  • +
  • -

#6 TheNobleSword  Icon User is offline

  • D.I.C Head

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

Re: Payroll Calculator

Posted 10 September 2009 - 10:12 AM

Okay that could make sense. So it would not matter is the user is inputting salary or salaried or salad :/

Why is the s in quotes?

if(let == 's')

Was This Post Helpful? 0
  • +
  • -

#7 AntonWebsters  Icon User is offline

  • D.I.C Regular
  • member icon

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

Re: Payroll Calculator

Posted 10 September 2009 - 10:21 AM

It's in single-quotes because it's of char type.
You see, you include string in double-quotes, right?
It's different for char types. You should include them inside single-quotes.
String s = "Salad";
char ch = 's';


Was This Post Helpful? 0
  • +
  • -

#8 TheNobleSword  Icon User is offline

  • D.I.C Head

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

Re: Payroll Calculator

Posted 10 September 2009 - 10:59 AM

Okay I see.

So once the string is converted to a char, the char is now what represents the input.

This is the code I have so far. I have lots of errors on the if (a==h) part. and I am not sure why.


package payrollcalculator;
import javax.swing.JOptionPane;
import java.util.Scanner;
public class PayrollCalculator {

	public static void main(String[] args) {
		Scanner keyboard=new Scanner(System.in);

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

		JOptionPane.showInputDialog("Enter Employee Name: ");
		String name;
		name=keyboard.next();

	  

		

	   if (a=='s')
		   
		JOptionPane.showInputDialog("Enter Employee Salary: ");
		int salary;
		salary=keyboard.nextInt();

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

		JOptionPane.showMessageDialog(null,"Employee type:" +'a'+ 
				"/nEmployee Name:" +name+
				"Weekly pay:" + weeklypayS);
		
	   else if (a=='h')
		   
		JOptionPane.showInputDialog("Enter Employee Rate: ");
		int rate
		rate=keyboard.nextInt();
		
		JOptionPane.showInputDialog("Enter Employee Hours: ");
		int hours
		hours=keyboard.nextInt();
		
		double grossH= rate*hours;
		double taxH= grossH*0.18;
		double netH= grossH-taxH;

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





Man I Have no idea what I am doing!
Was This Post Helpful? 0
  • +
  • -

#9 AntonWebsters  Icon User is offline

  • D.I.C Regular
  • member icon

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

Re: Payroll Calculator

Posted 10 September 2009 - 11:15 AM

import javax.swing.JOptionPane;
import java.util.Scanner;
public class BankAccount {

	public static void main(String[] args) {
		Scanner keyboard=new Scanner(System.in);

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

		JOptionPane.showInputDialog("Enter Employee Name: ");
		String name;
		name=keyboard.next();

	  

		

	   if (a=='s'){
		   
		JOptionPane.showInputDialog("Enter Employee Salary: ");
		int salary;
		salary=keyboard.nextInt();

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

		JOptionPane.showMessageDialog(null,"Employee type:" +'a'+ 
				"/nEmployee Name:" +name+
				"Weekly pay:" + weeklypayS);
	   }
		
	   else if (a=='h'){
		   
		JOptionPane.showInputDialog("Enter Employee Rate: ");
		int rate;
		rate=keyboard.nextInt();
		
		JOptionPane.showInputDialog("Enter Employee Hours: ");
		int hours;
		hours=keyboard.nextInt();
		
		
		double grossH= rate*hours;
		double taxH= grossH*0.18;
		double netH= grossH-taxH;

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



You gotta use curly-brackets to include the codes under if statement and else if statement. And you haven't declared the variable "weeklypayS" yet. Hope this helps.

EDIT: Oh my god..I just noticed another bunch of errors in your code...is it that you don't understand any of the basics? Which part don't you understand?

This post has been edited by AntonWebsters: 10 September 2009 - 11:18 AM

Was This Post Helpful? 0
  • +
  • -

#10 TheNobleSword  Icon User is offline

  • D.I.C Head

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

Re: Payroll Calculator

Posted 10 September 2009 - 11:59 AM

Well I know the basics, I think. This is only my second project. The only thing I did not know was the curly brackets on the if statement. Last time I used an if statement I did not use them.

The var were messed up because I changed them last second before I posted, mostly just typos.

how about now?

package payrollcalculator;
import javax.swing.JOptionPane;
import java.util.Scanner;
public class PayrollCalculator {

	public static void main(String[] args) {
		Scanner keyboard=new Scanner(System.in);

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

		JOptionPane.showInputDialog("Enter Employee Name: ");
		String name;
		name=keyboard.next();

	  

		

	   if (a=='s'){
		   
		JOptionPane.showInputDialog("Enter Employee Salary: ");
		int salary;
		salary=keyboard.nextInt();
		
		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'){
		   
		JOptionPane.showInputDialog("Enter Employee Rate: ");
		int rate;
		rate=keyboard.nextInt();
		
		JOptionPane.showInputDialog("Enter Employee Hours: ");
		int hours;
		hours=keyboard.nextInt();
		
		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);
	   }
	
	}
}





The program dies after entering the name. It is not getting to the if statement?

This post has been edited by TheNobleSword: 10 September 2009 - 12:01 PM

Was This Post Helpful? 0
  • +
  • -

#11 AntonWebsters  Icon User is offline

  • D.I.C Regular
  • member icon

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

Re: Payroll Calculator

Posted 10 September 2009 - 12:20 PM

First of all, if there's only one line of statement under the if condition or else if condition, then you won't be needing the curly-brackets. But if there're a lot of lines of them, then the curly-brackets would be needed.
Example:
if(a == 'a')
   System.out.println("I love Java!");

else if(b == 'b'){
   int i = 5;
   i++;
}



And where exactly did you enter the data? At the console? Or at the JOptionPane? If you're planning to make the program prompt the user to enter data at the console, then JOptionPane should be removed. Otherwise, just use JOptionPane to get the data from the user.
God..I seriously suck at explaining things..here, I'll correct the code, see if you understand or not.

This code uses JOptionPane to prompt user to enter the data
package payrollcalculator;
import javax.swing.JOptionPane;
import java.util.Scanner;
public class PayrollCalculator {

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


	   if (a=='s'){
		//The data entered is a string   
		String salaryStr = JOptionPane.showInputDialog("Enter Employee Salary: ");
		//So you have to convert it to int
		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: ");
		//Same thing here, convert rateStr to rate
		int rate = Integer.parseInt(rateStr);
		
		String hoursStr = JOptionPane.showInputDialog("Enter Employee Hours: ");
		int hours = Integer.parseInt(hoursStr);
		
		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);
	   }
	
	}
}



This prompts user to enter the data via the console window.

package payrollcalculator;
import javax.swing.JOptionPane;
import java.util.Scanner;
public class PayrollCalculator {

	public static void main(String[] args) {
		Scanner keyboard=new Scanner(System.in);

		String type = scanner.nextLine();
		char a = type.toLowerCase().charAt(0);

		String name;
		name=keyboard.nextLine();

  
	   if (a=='s'){
		   
		int salary;
		salary=keyboard.nextInt();
		
		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'){
		   
		int rate;
		rate=keyboard.nextInt();
		
		int hours;
		hours=keyboard.nextInt();
		
		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);
	   }
	
	}
}



There you go. Hope this helps.
Was This Post Helpful? 1
  • +
  • -

#12 TheNobleSword  Icon User is offline

  • D.I.C Head

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

Re: Payroll Calculator

Posted 11 September 2009 - 01:13 PM

The program is still dying after entering the name. It just disappears. I did it using dialog boxes.

What am I doing wrong?


package payrollcalculator;
import javax.swing.JOptionPane;
import java.util.Scanner;
public class PayrollCalculator {

	public static void main(String[] args) {
		Scanner keyboard=new Scanner(System.in);

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

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


		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: ");
		int hours = Integer.parseInt(hoursStr);

		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);
	   }

	}
}




This post has been edited by TheNobleSword: 11 September 2009 - 01:25 PM

Was This Post Helpful? 0
  • +
  • -

#13 AntonWebsters  Icon User is offline

  • D.I.C Regular
  • member icon

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

Re: Payroll Calculator

Posted 11 September 2009 - 01:45 PM

Erm, get rid of the Scanner keyboard=new Scanner(System.in); and name =keyboard.next();, you don't need them if you're planning to use JOptionPane Dialog boxes. See if the problem is resolved. It works for me.
import javax.swing.JOptionPane;
import java.util.Scanner;
public class CylinderVolume {

	public static void main(String[] args) {
		

		String type = JOptionPane.showInputDialog("Enter Employee Type: ");
		char 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: ");
		int hours = Integer.parseInt(hoursStr);

		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);
	   }

	}
}




Hope this helps.
Was This Post Helpful? 1
  • +
  • -

#14 TheNobleSword  Icon User is offline

  • D.I.C Head

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

Re: Payroll Calculator

Posted 11 September 2009 - 01:52 PM

Okay that worked! However the output is a little funky. does /n not work in a dialog box? I wanted it to be out put on four separate lines.

Employee type:
Employee name:
Employee salary:
total tax deducted:
weekly pay:

How do I achieve this.
Was This Post Helpful? 0
  • +
  • -

#15 AntonWebsters  Icon User is offline

  • D.I.C Regular
  • member icon

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

Re: Payroll Calculator

Posted 11 September 2009 - 02:03 PM

Sir, the correct escape line sequence is '\n' , not '/n'.
It's a backslash, not a slash.
Hope that helps. =)
Was This Post Helpful? 1
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2