Java : No output or result show - java

My program is supposed to show the calculation of the worker's total salary but somehow after all the input in include the program is end without showing any result.
It also to check if the total hours enter is over 40 and if does, it will calculate the overtime salary.
public class GajiTest {
public static void main(String[] args)
{
Scanner input= new Scanner(System.in);
System.out.print("Enter number of Employees: ");
int numberOfEmp= input.nextInt();
int[] arrayList= new int[numberOfEmp];
for (int i = 0; i < arrayList.length; i++){
System.out.print("Enter Employee Name: ");
String empName= input.next();
System.out.print("Enter hourly rate: ");
int rate= input.nextInt();
System.out.print("Enter hours worked: ");
int hours=input.nextInt();
class Salary {
public double CalculateGaji(int hours,int rate){
if (hours >=40)
{
double regPay= hours * rate;
double otPay = (hours-40) *(rate*1.5);
double totalPay= regPay + otPay;
System.out.print("\nEmployee name: " + empName+"\n Regular pay: " + regPay +"\n Overtime pay: " + otPay+ "\n Total pay: " + totalPay+ "\n"+ "\n");
}
else
{
double regPay= hours * rate;
double otPay =0;
double totalPay= regPay + otPay;
System.out.print("\nEmployee name: " + empName+ "\n Regular pay: " + regPay +"\n Overtime pay: " + otPay+ "\n Total pay: " + totalPay+ "\n"+ "\n");
}
}
}
}}}

I suppose the problem add two lines try this :
public static void main(String[] args)
{
Scanner input= new Scanner(System.in);
System.out.print("Enter number of Employees: ");
int numberOfEmp= input.nextInt();
int[] arrayList= new int[numberOfEmp];
for (int i = 0; i < arrayList.length; i++){
System.out.print("Enter Employee Name: ");
String empName= input.next();
System.out.print("Enter hourly rate: ");
int rate= input.nextInt();
System.out.print("Enter hours worked: ");
int hours=input.nextInt();
Salary sal=new Salary();
sal.CalculateGaji(hours,rate,empName);
}
}
}
class Salary {
public void CalculateGaji(int hours,int rate,String empName){
if (hours >=40)
{
double regPay= hours * rate;
double otPay = (hours-40) *(rate*1.5);
double totalPay= regPay + otPay;
System.out.print("\nEmployee name: " + empName+"\n Regular pay: " + regPay +"\n Overtime pay: " + otPay+ "\n Total pay: " + totalPay+ "\n"+ "\n");
}
else
{
double regPay= hours * rate;
double otPay =0;
double totalPay= regPay + otPay;
System.out.print("\nEmployee name: " + empName+ "\n Regular pay: " + regPay +"\n Overtime pay: " + otPay+ "\n Total pay: " + totalPay+ "\n"+ "\n");
}
}
}

Why it is not printing anything is because you are not calling your method CalculateGaji() of class Salary.
You need to call it by creating the object of the class Salary. (After the local class in the main method.)
Salary salary = new Salary();
salary.CalculateGaji(hours,rate);
Apart from it, there are a few things I would like to say,
You are creating an array of integers int[] arrayList= new int[numberOfEmp]; ArrayList is something else. What is an ArrayList? There should be no need to write the name of the variable to be arrayList. It might confuse you at some stage.
As per your code, you have created the class class Salary in your main method. This way it becomes the [Local Class][2]. If it is what you want then, it is find. Otherwise you could have written your class Salary out side main method and outside class class GajiTest.
But I don't see any need of creating any local class to achieve it. You can directly do it the following way,
public static void main(String[] args)
{
Scanner input= new Scanner(System.in);
System.out.print("Enter number of Employees: ");
int numberOfEmp= input.nextInt();
int[] arrayList= new int[numberOfEmp];
for (int i = 0; i < arrayList.length; i++){
System.out.print("Enter Employee Name: ");
String empName= input.next();
System.out.print("Enter hourly rate: ");
int rate= input.nextInt();
System.out.print("Enter hours worked: ");
int hours=input.nextInt();
if (hours >=40)
{
double regPay= hours * rate;
double otPay = (hours-40) *(rate*1.5);
double totalPay= regPay + otPay;
System.out.print("\nEmployee name: " + empName+"\n Regular pay: " + regPay +"\n Overtime pay: " + otPay+ "\n Total pay: " + totalPay+ "\n"+ "\n");
}
else
{
double regPay= hours * rate;
double otPay =0;
double totalPay= regPay + otPay;
System.out.print("\nEmployee name: " + empName+ "\n Regular pay: " + regPay +"\n Overtime pay: " + otPay+ "\n Total pay: " + totalPay+ "\n"+ "\n");
}
}

From your code, I think what you are trying to do is create an array of employees and print out their names and salaries. There are multiple errors in your code that will not allow it to function if that is your intention.
Here is the code to create an array of employees and print out their names and salaries based on your CalculateGaji function:
public class calculateSalary {
static class employee{
private double rate;
private double hours;
private String empName;
public employee(){}
public void setRate(double value){
rate = value;
}
public void setHours(double value){
hours = value;
}
public void setName(String value){
empName = value;
}
public void CalculateGaji(){
if (hours >=40){
double regPay= hours * rate;
double otPay = (hours-40) *(rate*1.5);
double totalPay= regPay + otPay;
System.out.print("\nEmployee name: " + empName +"\n Regular pay: " + regPay +"\n Overtime pay: " + otPay+ "\n Total pay: " + totalPay+ "\n"+ "\n");
}
else{
double regPay= hours * rate;
double otPay =0;
double totalPay= regPay + otPay;
System.out.print("\nEmployee name: " + empName + "\n Regular pay: " + regPay +"\n Overtime pay: " + otPay+ "\n Total pay: " + totalPay+ "\n"+ "\n");
}
}
}
public static void main(String[] args){
Scanner input = new Scanner(System.in);
System.out.print("Enter number of Employees: ");
int numberOfEmp= input.nextInt();
employee[] employeeArray= new employee[numberOfEmp];
for (int i = 0; i < employeeArray.length; i++){
employeeArray[i] = new employee();
System.out.println("Enter Employee Name: ");
employeeArray[i].setName(input.next());
System.out.println("Enter hourly rate: ");
employeeArray[i].setRate(input.nextDouble());
System.out.println("Enter hours worked: ");
employeeArray[i].setHours(input.nextDouble());
}
for(int i = 0; i < employeeArray.length; i++){
employeeArray[i].CalculateGaji();
}
}
}
Hope this helps.

Related

Interest not updating

It is meant to calculate interest and then put that into a table with years, interest, and new balance. For some reason the interest is not being calculated correctly and is not updating.
import java.util.Scanner;
import java.text.*;
public class Interest
{
public static void main(String[] args)
{
printIntro();
Scanner input = new Scanner(System.in);
System.out.print("Enter initial balance: ");
int balanceAmount = input.nextInt();
System.out.print("Enter interest rate: ");
double interestRate = input.nextDouble();
System.out.print("Enter the number of years: ");
int years = input.nextInt();
printTable(years, balanceAmount, interestRate);
}
public static double calcInterest(double balanceAmount, double interestRate, double years)
{
double interest = balanceAmount * Math.pow((1 + interestRate/100),years);
return interest;
}
public static void printRow(int rowNum, double balanceAmount, double interestRate)
{
System.out.println(rowNum + "\t" + balanceAmount + "\t" + "\t" + interestRate + "\t" + "\t" + (balanceAmount + interestRate));
//balanceAmount = (balanceAmount + interestRate);
}
public static void printTable(int numRows, double balanceAmount, double interestRate)
{
System.out.println("Year" + "\t" + "Balance" + "\t" + "\t" + "Interest" + "\t" + "New Balance");
System.out.println("----" + "\t" + "-------" + "\t" + "\t" + "--------" + "\t" + "-----------");
for (int i = 1; i <= numRows; i++)
{
printRow(i, balanceAmount, interestRate);
balanceAmount = (balanceAmount + interestRate);
}
}
public static void printIntro()
{
System.out.println("This program will calculate the interest "
+ "earned on a deposit over a certain amount of years.");
}
}
You should call your business logic to calculate interest as per your business requirement. That totaly depend on your business requirement.
Though for program specific it seems that You need to call your calcInterest method in printTable method, before you call your printRow method as below:
public static void printTable( final int numRows, double balanceAmount, final double interestRate ) {
System.out.println( "Year" + "\t" + "Balance" + "\t" + "\t" + "Interest" + "\t" + "New Balance" );
System.out.println( "----" + "\t" + "-------" + "\t" + "\t" + "--------" + "\t" + "-----------" );
for ( int i = 1; i <= numRows; i++ ) {
double interest = calcInterest( balanceAmount, interestRate, 1 );
printRow( i, balanceAmount, interest );
balanceAmount = ( balanceAmount + interest );
}
}
Also your formula to calculate interest is wrong. it should be
double interest = balanceAmount * Math.pow( ( 1 + interestRate / 100 ), years )-balanceAmount;
It will give out put as below:
This program will calculate the interest earned on a deposit over a certain amount of years.
This program will calculate the interest earned on a deposit over a certain amount of years.
Enter initial balance: 100
Enter interest rate: 10
Enter the number of years: 3
Year Balance Interest New Balance
---- ------- -------- -----------
1 100.0 10.000000000000014 110.00000000000001
2 110.00000000000001 23.100000000000037 133.10000000000005
3 133.10000000000005 44.05610000000007 177.15610000000012
You are not calling calcInterest.
You need to call that under the printRow method before the
System.out.println(rowNum + "\t" + balanceAmount + "\t" + "\t" + interestRate + "\t" + "\t" + (balanceAmount + interestRate));
line

calcAverage issue

I don't know why my code is not calculating the average. It is compiling and running, but gives 0% as average. Here are the instructions for the assignment
Average and Grade (Methods Program):
Write a program that asks the user to enter 5 test scores. Your program should then display a letter
grade for each test score, the average test score and the overall letter grade
for the average score. You can NOT use a for loop or an array (we haven't even covered that) in this
program - the point is to learn how to define methods and call them several
times if necessary.
import java.util.Scanner;
import java.text.DecimalFormat;
public class Homework8{
static double average;
static char grade;
public static void main (String[] args) {
Scanner keyboard = new Scanner(System.in);
char letter1;
char letter2;
char letter3;
char letter4;
char letter5;
double score1;
double score2;
double score3;
double score4;
double score5;
double score;
System.out.println("Please enter the first score between 0 and 100: ");
score1 = keyboard.nextDouble();
System.out.println("Please enter the second score between 0 and 100: ");
score2 = keyboard.nextDouble();
System.out.println("Please enter the third score between 0 and 100: ");
score3 = keyboard.nextDouble();
System.out.println("Please enter the fourth score between 0 and 100: ");
score4 = keyboard.nextDouble();
System.out.println("Please enter the fifth score between 0 and 100: ");
score5 = keyboard.nextDouble();
//System.out.println("The average of your five tests was: " +
System.out.println(
"For TestA you scored: " + score1 + " giving you Grade: "
+ determineGrade(score1)
+ "\nFor TestB you scored: " + score2 + " giving you Grade: "
+ determineGrade(score2)
+ "\nFor TestC you scored: " + score3 + " giving you Grade: "
+ determineGrade(score3)
+ "\nFor TestD you scored: " + score4 + " giving you Grade: "
+ determineGrade(score4)
+ "\nFor TestF you scored: " + score5 + " giving you Grade: "
+ determineGrade(score5)
+ "\nBased on your average: " + average + " Your final Grade is: "
+ determineGrade((double)average));
}
public static double calcAverage(double score1, double score2, double score3, double score4, double score5) {
double average = (score1+score2+score3+score4+score5) / 5;
return average;
}
public static char determineGrade(double score)
{
if(score<=100 && score>=90)
{
return 'A';
}
else if(score>=80)
{
return 'B';
}
else if(score>=70)
{
return 'C';
}
else if(score>=60)
{
return 'D';
}
else
{
return 'F';
}
}
}
you define the method calcAverage but you didn't call it.
import java.util.Scanner;
import java.text.DecimalFormat;
public class Homework8{
static double average;
static char grade;
public static void main (String[] args) {
Scanner keyboard = new Scanner(System.in);
char letter1;
char letter2;
char letter3;
char letter4;
char letter5;
double score1;
double score2;
double score3;
double score4;
double score5;
double score;
System.out.println("Please enter the first score between 0 and 100: ");
score1 = keyboard.nextDouble();
System.out.println("Please enter the second score between 0 and 100: ");
score2 = keyboard.nextDouble();
System.out.println("Please enter the third score between 0 and 100: ");
score3 = keyboard.nextDouble();
System.out.println("Please enter the fourth score between 0 and 100: ");
score4 = keyboard.nextDouble();
System.out.println("Please enter the fifth score between 0 and 100: ");
score5 = keyboard.nextDouble();
double average = calcAverage(score1,score2,score3,score4,score5);
//System.out.println("The average of your five tests was: " +
System.out.println(
"For TestA you scored: " + score1 + " giving you Grade: "
+ determineGrade(score1)
+ "\nFor TestB you scored: " + score2 + " giving you Grade: "
+ determineGrade(score2)
+ "\nFor TestC you scored: " + score3 + " giving you Grade: "
+ determineGrade(score3)
+ "\nFor TestD you scored: " + score4 + " giving you Grade: "
+ determineGrade(score4)
+ "\nFor TestF you scored: " + score5 + " giving you Grade: "
+ determineGrade(score5)
+ "\nBased on your average: " + average + " Your final Grade is: "
+ determineGrade((double)average));
}
public static double calcAverage(double score1, double score2, double score3, double score4, double score5) {
double average = (score1+score2+score3+score4+score5) / 5.0;
return average;
}
public static char determineGrade(double score)
{
if(score<=100 && score>=90)
{
return 'A';
}
else if(score>=80)
{
return 'B';
}
else if(score>=70)
{
return 'C';
}
else if(score>=60)
{
return 'D';
}
else
{
return 'F';
}
}
}

Nesting if statements - cannot get the nested statements to execute

I'm trying to get the below nested statements to work, but am having issues getting them to execute past the first statement. I tried nesting the statements but just the first if executes. Any feedback about formatting is greatly appreciated, I understand that there may be a more efficient way of achieving this, but I have to execute the code using nested statements.
package incometax;
import java.util.Scanner;
import java.text.DecimalFormat;
public class IncomeTax {
public static void main(String[] args) {
Scanner scnr = new Scanner(System.in);
DecimalFormat df = new DecimalFormat ("#,###,000.00");
String singleStatus = "single";
String marriedStatus = "married";
String maritalStatus = "";
double annualIncome = 0;
double taxAmount = 0;
System.out.println("Please enter your martial status: ");
maritalStatus = scnr.next();
if (maritalStatus.compareTo(singleStatus) == 0){
System.out.println("Please enter your annual income: ");
annualIncome = scnr.nextDouble();
if (annualIncome <= 30000){
taxAmount = (annualIncome * .15);
System.out.println("Based on annual income of "+ "$ " +
df.format(annualIncome) + " your tax is " + "$ " +
df.format(taxAmount));
if (annualIncome > 30000){
taxAmount = (annualIncome * .25);
System.out.println("Based on annual income of "+ "$ " +
df.format(annualIncome) +
" your tax is " + "$ " + df.format(taxAmount));
}
}
else {
if (maritalStatus.compareTo(marriedStatus) == 0){
if(annualIncome <= 30000){
taxAmount = (annualIncome * .12);
System.out.println("Based on annual income of "+ "$ "
+ df.format(annualIncome) +
" your tax is " + "$ " + df.format(taxAmount));
if(annualIncome > 30000){
taxAmount = (annualIncome * .20);
System.out.println("Based on annual income of "+
"$ " + df.format(annualIncome) +
" your tax is " + "$ " + df.format(taxAmount));
}
}
}
}
}
}
}
You are going into the first if when annualIncome <= 30000. But the condition in the nested if is annualIncome > 30000. This will always be false. Try if else instead of nested if
if (annualIncome <= 30000) {
taxAmount = (annualIncome * .15);
System.out.println("Based on annual income of "+ "$ " +
df.format(annualIncome) + " your tax is " + "$ " +
df.format(taxAmount));
}
else {
taxAmount = (annualIncome * .25);
System.out.println("Based on annual income of "+ "$ " +
df.format(annualIncome) +
" your tax is " + "$ " + df.format(taxAmount));
}
It looks like you have misplaced the parenthesis of if statement.
Try this code it should work
package incometax;
import java.util.Scanner;
import java.text.DecimalFormat;
public class IncomeTax {
public static void main(String[] args) {
Scanner scnr = new Scanner(System.in);
DecimalFormat df = new DecimalFormat ("#,###,000.00");
String singleStatus = "single";
String marriedStatus = "married";
String maritalStatus = "";
double annualIncome = 0;
double taxAmount = 0;
System.out.println("Please enter your martial status: ");
maritalStatus = scnr.next();
if (maritalStatus.compareTo(singleStatus) == 0){
System.out.println("Please enter your annual income: ");
annualIncome = scnr.nextDouble();
if (annualIncome <= 30000){
taxAmount = (annualIncome * .15);
System.out.println("Based on annual income of "+ "$ " +
df.format(annualIncome) + " your tax is " + "$ " +
df.format(taxAmount));
} else {
taxAmount = (annualIncome * .25);
System.out.println("Based on annual income of "+ "$ " +
df.format(annualIncome) +
" your tax is " + "$ " + df.format(taxAmount));
}
} else if (maritalStatus.compareTo(marriedStatus) == 0){
if(annualIncome <= 30000){
taxAmount = (annualIncome * .12);
System.out.println("Based on annual income of "+ "$ "
+ df.format(annualIncome) +
" your tax is " + "$ " + df.format(taxAmount));
} else {
taxAmount = (annualIncome * .20);
System.out.println("Based on annual income of "+
"$ " + df.format(annualIncome) +
" your tax is " + "$ " + df.format(taxAmount));
}
}
}
}
A decent IDE will be able to format your code to make this sort of nesting issue more apparent.
It may not be part of the question but you also have several repeated lines of code, take a look in to methods. For example the following is cleaner (note there is no validation on the marital status or the annual income)
import java.text.DecimalFormat;
import java.util.Scanner;
public class IncomeTax
{
private static final DecimalFormat DF = new DecimalFormat("#,###,000.00");
private static final String SINGLE_STATUS = "single";
private static final String MARRIED_STATUS = "married";
private static final double SINGLE_LOW_RATE = .15;
private static final double SINGLE_HIGH_RATE = .25;
private static final double MARRIED_LOW_RATE = .12;
private static final double MARRIED_HIGH_RATE = .20;
private static final int LOWER_BRACKET = 30000;
public static void main(String[] args)
{
Scanner scnr = new Scanner(System.in);
String maritalStatus;
double annualIncome;
System.out.println(
"Please enter your martial status: ");
maritalStatus = scnr.next();
System.out.println("Please enter your annual income: ");
annualIncome = scnr.nextDouble();
if (SINGLE_STATUS.equalsIgnoreCase(maritalStatus))
{
printTaxRate(annualIncome, SINGLE_LOW_RATE, SINGLE_HIGH_RATE);
} else
{
if (MARRIED_STATUS.equalsIgnoreCase(maritalStatus))
{
printTaxRate(annualIncome, MARRIED_LOW_RATE, MARRIED_HIGH_RATE);
}
}
}
private static double calcTaxAmount(double annualIncome, double taxRate)
{
return annualIncome * taxRate;
}
private static void printTaxRate(double annualIncome, double lowRate, double highRate)
{
double taxAmount;
if (annualIncome <= LOWER_BRACKET)
{
taxAmount = calcTaxAmount(annualIncome, lowRate);
} else
{
taxAmount = calcTaxAmount(annualIncome, highRate);
}
System.out.println("Based on annual income of " + "$ "
+ DF.format(annualIncome)
+ " your tax is " + "$ " + DF.format(taxAmount));
}
}

output formatting Java

my question is simple, how can i get my output to format two values with two decimal places. for whatever reason i cant output the "current balance" & "new balance" with two decimal places. separately they work fine but when together i get a conversion error. is this a rule i'm unaware of? i would like do do this with one action if possible. this is simply a cosmetic issue and all operations perform fine when i take out the formatting.
thanks,
public static void main(String[] args)
{
double min;
int accNum;
char accType;
double bal;
double newBal;
//user inputs
System.out.println("Please enter your account number: ");
accNum = console.nextInt();
System.out.println();
System.out.println("Enter the account type: s(savings) or c(checking)");
accType = console.next().charAt(0);
System.out.println();
//savings v checking
switch (accType)
{
case 's':
case 'S':
System.out.println("Enter the current balance: ");
bal = console.nextDouble();
System.out.println("Enter the minimum balance: ");
min = console.nextDouble();
System.out.println();
if (bal < min)
{
newBal = bal - S_MIN_FEE;
System.out.println("Insufficient Funds (-$10.00)");
}
else
newBal = bal + (bal * S_INT);
System.out.printf("Account #: " + accNum + "\n"
+ "Account Type: Savings" + "\n" + "Current Balance: "
+ "$%.2f%n", bal + "New Balance: $%.2f", newBal);
case 'c':
case 'C':
System.out.println("Enter the current balance: ");
bal = console.nextDouble();
System.out.println("Enter the minimum balance: ");
min = console.nextDouble();
System.out.println();
if (bal < min)
{
newBal = bal - C_MIN_FEE;
System.out.println("Insufficent Funds (-$25.00)");
}
else if (bal < C_BAL_MAX && bal >= min)
newBal = bal + (bal * C_INT);
else
newBal = bal + (bal * C_INT_MAX);
System.out.printf("Account #: " + accNum + "\n"
+ "Account Type: Checking" + "\n" + "Current Balance: "
+ "$%.2f%n", bal + "New Balance: $%.2f%n", newBal);
That is because you put the format first, and only first.
System.out.printf("%.2f %.2f%n", bal, newBal);
This is the same problem/mistake as posted an hour ago. java.util.IllegalFormatConversionException: f != java.lang.String Error

Accepting multiple inputs loop

I've written a program in Java that does a series of calculations, basically a payroll program for four different types of employees. I'm having an issue trying to make it NOT exit after the input is completed.
For example: The user is asked how many employees are in the company. From there it should start asking what employee #1's type is (manager, hourly, etc...) then keep asking until the total employees are met, say for example 4. After each input it's okay for the employee's name and information to output.
Here's what I have thus far, which is an almost complete, working program. The only thing left is the part I described above.
Any resources to help me work out the solution are more valuable than rewriting my code.
Thanks!
import java.util.Scanner;
import java.text.DecimalFormat;
public class PayrollSwitch {
public static void main(String[] args) {
// First input group
int employeeType; // 1-4 (Manager, Hourly, Commissioned, Pieceworker)
int compSize; // Company size (4 - 10)
int hoursWrkd; // Whole Number, no partial hours.
String employeeName; // Name of the Employee
// Pay for each worker type
double rateManagerWrkr = 800.00;// Fixed weekly salary
double managerBonus = 750.00; // $750.00 bonus for manager
double rateHourWrkr; // Hourly + overtime > 40 = 1.5 time hourly rate
double hourOvertime = 1.5; // If hoursWrkd > 40
double hourOvertimeStore; // Stores value
double rateCommWrkr = 650.00; // Fixed weekly salary
double commBonus = 250.00; // $250.00 bonus for commissioned worker
double commWklySal; // 5.7% time weekly salary (650.00 * 5.7)
double ratePieceWrkr = 400.00; // Fixed weekly salary
// Deductions
double medicalDues = 20.00; // $20.00 per pay period
double fedTax = 0.30; // 30% of gross
double socialSec = 0.05; // 5% of gross
double deductDues;
double fedTaxFinal;
double socialSecFinal;
// Totals
double managerGross;
double managerNet;
double hourGross;
double hourNet;
double commGross;
double commNet;
double pieceGross;
double pieceNet;
// Convert decimals to match ####.## place ($9999.99)
DecimalFormat df = new DecimalFormat("####.##");
String employeeTitle;
Scanner input = new Scanner(System.in);
System.out.print("Enter an employee paycode (1-4): ");
employeeType = input.nextInt();
switch (employeeType)
{
case 1:
{
employeeTitle = "Manager";
System.out.println("You selected manager!");
System.out.print("What's your name? :");
employeeName = input.next();
System.out.print("Enter the amount of hours worked this week: ");
hoursWrkd = input.nextInt();
System.out.println("Name: " + employeeName);
System.out.println("Title: " + employeeTitle);
System.out.println("Type: " + employeeType);
System.out.println("Hours worked: " + hoursWrkd);
managerGross = rateManagerWrkr + managerBonus;
System.out.println("Gross pay: $" + df.format(managerGross));
System.out.println("Federal Tax: $" + df.format(managerGross * fedTax));
System.out.println("Social Security: $" + df.format(managerGross * socialSec));
System.out.println("Medical: $" + df.format(medicalDues));
fedTaxFinal = managerGross * fedTax;
socialSecFinal = managerGross * socialSec;
deductDues = (fedTaxFinal + socialSecFinal + medicalDues);
managerNet = (managerGross - deductDues);
System.out.println("Net pay: $" + df.format(managerNet));
}
break;
case 2:
{
employeeTitle = "Hourly";
System.out.println("You selected hourly!");
System.out.print("What's your name? :");
employeeName = input.next();
System.out.print("Enter the amount of hours worked this week: ");
hoursWrkd = input.nextInt();
System.out.print("Enter hourly rate: $");
rateHourWrkr = input.nextDouble();
hourGross = rateHourWrkr * hoursWrkd;
System.out.println("Name: " + employeeName);
System.out.println("Title: " + employeeTitle);
System.out.println("Type: " + employeeType);
// Begin checking hours worked
if (hoursWrkd > 40)
{
hourOvertimeStore = (hoursWrkd - 40) * rateHourWrkr * hourOvertime;
System.out.println("Hours worked: " + hoursWrkd);
System.out.println("Overtime hours: " + (hoursWrkd - 40));
System.out.println("Gross pay: $" + df.format(hourGross + hourOvertimeStore));
System.out.println("Federal Tax: $" + df.format(hourGross * fedTax));
System.out.println("Social Security: $" + df.format(hourGross * socialSec));
System.out.println("Medical: $" + df.format(medicalDues));
fedTaxFinal = hourGross * fedTax;
socialSecFinal = hourGross * socialSec;
deductDues = (fedTaxFinal + socialSecFinal + medicalDues);
hourNet = (hourGross - deductDues);
System.out.println("Net pay: $" + df.format(hourNet));
}
else
{
hourGross = hoursWrkd * rateHourWrkr;
hourOvertimeStore = 0;
System.out.println("Hours worked: " + hoursWrkd);
System.out.println("Gross pay: $" + df.format(hourGross + hourOvertimeStore));
System.out.println("Federal Tax: $" + df.format(hourGross * fedTax));
System.out.println("Social Security: $" + df.format(hourGross * socialSec));
System.out.println("Medical: $" + df.format(medicalDues));
fedTaxFinal = hourGross * fedTax;
socialSecFinal = hourGross * socialSec;
deductDues = (fedTaxFinal + socialSecFinal + medicalDues);
hourNet = (hourGross - deductDues);
System.out.println("Net pay: " + df.format(hourNet));
}
}
break;
case 3:
{
employeeTitle = "Commission";
System.out.println("You selected commission!");
System.out.print("What's your name? :");
employeeName = input.next();
System.out.print("Enter the amount of hours worked this week: ");
hoursWrkd = input.nextInt();
System.out.println("Name: " + employeeName);
System.out.println("Title: " + employeeTitle);
System.out.println("Type: " + employeeType);
System.out.println("Hours worked: " + hoursWrkd);
commGross = rateCommWrkr + commBonus;
commWklySal = (0.057 * rateCommWrkr);
System.out.println("Commission made: $" + df.format(commWklySal));
System.out.println("Gross pay: $" + df.format(commWklySal + commGross));
System.out.println("Federal Tax: $" + df.format((commWklySal + commGross) * fedTax));
System.out.println("Social Security: $" + df.format((commWklySal + commGross) * socialSec));
System.out.println("Medical: $" + df.format(medicalDues));
fedTaxFinal = (commWklySal + commGross) * fedTax;
socialSecFinal = (commWklySal + commGross) * socialSec;
deductDues = (fedTaxFinal + socialSecFinal + medicalDues);
commNet = (commWklySal + commGross) - deductDues;
System.out.println("Net pay: $" + df.format(commNet));
}
break;
case 4:
{
employeeTitle = "Pieceworker";
System.out.println("You selected pieceworker!");
System.out.print("What's your name? :");
employeeName = input.next();
System.out.print("Enter the amount of hours worked this week: ");
hoursWrkd = input.nextInt();
System.out.println("Name: " + employeeName);
System.out.println("Title: " + employeeTitle);
System.out.println("Type: " + employeeType);
System.out.println("Hours worked: " + hoursWrkd);
pieceGross = ratePieceWrkr;
System.out.println("Gross pay: $" + df.format(pieceGross));
System.out.println("Federal Tax: $" + df.format(pieceGross * fedTax));
System.out.println("Social Security: $" + df.format(pieceGross * socialSec));
System.out.println("Medical: $" + df.format(medicalDues));
pieceNet = pieceGross - fedTax - socialSec - medicalDues;
System.out.println("Net pay: $" + df.format(pieceNet));
}
break;
}
}
}
You are not asking the "Number of Employees" from the User in your code.Hope this is what you want.
package test;
import java.util.Scanner;
import java.text.DecimalFormat;
public class test {
public static void main(String[] args) {
// First input group
int employeeType; // 1-4 (Manager, Hourly, Commissioned, Pieceworker)
int compSize; // Company size (4 - 10)
int hoursWrkd; // Whole Number, no partial hours.
String employeeName; // Name of the Employee
// Pay for each worker type
double rateManagerWrkr = 800.00;// Fixed weekly salary
double managerBonus = 750.00; // $750.00 bonus for manager
double rateHourWrkr; // Hourly + overtime > 40 = 1.5 time hourly rate
double hourOvertime = 1.5; // If hoursWrkd > 40
double hourOvertimeStore; // Stores value
double rateCommWrkr = 650.00; // Fixed weekly salary
double commBonus = 250.00; // $250.00 bonus for commissioned worker
double commWklySal; // 5.7% time weekly salary (650.00 * 5.7)
double ratePieceWrkr = 400.00; // Fixed weekly salary
// Deductions
double medicalDues = 20.00; // $20.00 per pay period
double fedTax = 0.30; // 30% of gross
double socialSec = 0.05; // 5% of gross
double deductDues;
double fedTaxFinal;
double socialSecFinal;
// Totals
double managerGross;
double managerNet;
double hourGross;
double hourNet;
double commGross;
double commNet;
double pieceGross;
double pieceNet;
// Convert decimals to match ####.## place ($9999.99)
DecimalFormat df = new DecimalFormat("####.##");
String employeeTitle;
int numberOfEmployees=0;
Scanner input = new Scanner(System.in);
System.out.print("Enter the number of Employees in your company ");
numberOfEmployees = input.nextInt();
for(int i=0;i<numberOfEmployees;i++)
{
System.out.println("----------------------------------------------");
System.out.println("Enter Information for Employee Number " + Integer.toString(i+1));
System.out.print("Enter an employee paycode (1-4): ");
employeeType = input.nextInt();
switch (employeeType)
{
case 1:
{
employeeTitle = "Manager";
System.out.println("You selected manager!");
System.out.print("What's your name? :");
employeeName = input.next();
System.out.print("Enter the amount of hours worked this week: ");
hoursWrkd = input.nextInt();
System.out.println("Name: " + employeeName);
System.out.println("Title: " + employeeTitle);
System.out.println("Type: " + employeeType);
System.out.println("Hours worked: " + hoursWrkd);
managerGross = rateManagerWrkr + managerBonus;
System.out.println("Gross pay: $" + df.format(managerGross));
System.out.println("Federal Tax: $" + df.format(managerGross * fedTax));
System.out.println("Social Security: $" + df.format(managerGross * socialSec));
System.out.println("Medical: $" + df.format(medicalDues));
fedTaxFinal = managerGross * fedTax;
socialSecFinal = managerGross * socialSec;
deductDues = (fedTaxFinal + socialSecFinal + medicalDues);
managerNet = (managerGross - deductDues);
System.out.println("Net pay: $" + df.format(managerNet));
}
break;
case 2:
{
employeeTitle = "Hourly";
System.out.println("You selected hourly!");
System.out.print("What's your name? :");
employeeName = input.next();
System.out.print("Enter the amount of hours worked this week: ");
hoursWrkd = input.nextInt();
System.out.print("Enter hourly rate: $");
rateHourWrkr = input.nextDouble();
hourGross = rateHourWrkr * hoursWrkd;
System.out.println("Name: " + employeeName);
System.out.println("Title: " + employeeTitle);
System.out.println("Type: " + employeeType);
// Begin checking hours worked
if (hoursWrkd > 40)
{
hourOvertimeStore = (hoursWrkd - 40) * rateHourWrkr * hourOvertime;
System.out.println("Hours worked: " + hoursWrkd);
System.out.println("Overtime hours: " + (hoursWrkd - 40));
System.out.println("Gross pay: $" + df.format(hourGross + hourOvertimeStore));
System.out.println("Federal Tax: $" + df.format(hourGross * fedTax));
System.out.println("Social Security: $" + df.format(hourGross * socialSec));
System.out.println("Medical: $" + df.format(medicalDues));
fedTaxFinal = hourGross * fedTax;
socialSecFinal = hourGross * socialSec;
deductDues = (fedTaxFinal + socialSecFinal + medicalDues);
hourNet = (hourGross - deductDues);
System.out.println("Net pay: $" + df.format(hourNet));
}
else
{
hourGross = hoursWrkd * rateHourWrkr;
hourOvertimeStore = 0;
System.out.println("Hours worked: " + hoursWrkd);
System.out.println("Gross pay: $" + df.format(hourGross + hourOvertimeStore));
System.out.println("Federal Tax: $" + df.format(hourGross * fedTax));
System.out.println("Social Security: $" + df.format(hourGross * socialSec));
System.out.println("Medical: $" + df.format(medicalDues));
fedTaxFinal = hourGross * fedTax;
socialSecFinal = hourGross * socialSec;
deductDues = (fedTaxFinal + socialSecFinal + medicalDues);
hourNet = (hourGross - deductDues);
System.out.println("Net pay: " + df.format(hourNet));
}
}
break;
case 3:
{
employeeTitle = "Commission";
System.out.println("You selected commission!");
System.out.print("What's your name? :");
employeeName = input.next();
System.out.print("Enter the amount of hours worked this week: ");
hoursWrkd = input.nextInt();
System.out.println("Name: " + employeeName);
System.out.println("Title: " + employeeTitle);
System.out.println("Type: " + employeeType);
System.out.println("Hours worked: " + hoursWrkd);
commGross = rateCommWrkr + commBonus;
commWklySal = (0.057 * rateCommWrkr);
System.out.println("Commission made: $" + df.format(commWklySal));
System.out.println("Gross pay: $" + df.format(commWklySal + commGross));
System.out.println("Federal Tax: $" + df.format((commWklySal + commGross) * fedTax));
System.out.println("Social Security: $" + df.format((commWklySal + commGross) * socialSec));
System.out.println("Medical: $" + df.format(medicalDues));
fedTaxFinal = (commWklySal + commGross) * fedTax;
socialSecFinal = (commWklySal + commGross) * socialSec;
deductDues = (fedTaxFinal + socialSecFinal + medicalDues);
commNet = (commWklySal + commGross) - deductDues;
System.out.println("Net pay: $" + df.format(commNet));
}
break;
case 4:
{
employeeTitle = "Pieceworker";
System.out.println("You selected pieceworker!");
System.out.print("What's your name? :");
employeeName = input.next();
System.out.print("Enter the amount of hours worked this week: ");
hoursWrkd = input.nextInt();
System.out.println("Name: " + employeeName);
System.out.println("Title: " + employeeTitle);
System.out.println("Type: " + employeeType);
System.out.println("Hours worked: " + hoursWrkd);
pieceGross = ratePieceWrkr;
System.out.println("Gross pay: $" + df.format(pieceGross));
System.out.println("Federal Tax: $" + df.format(pieceGross * fedTax));
System.out.println("Social Security: $" + df.format(pieceGross * socialSec));
System.out.println("Medical: $" + df.format(medicalDues));
pieceNet = pieceGross - fedTax - socialSec - medicalDues;
System.out.println("Net pay: $" + df.format(pieceNet));
}
break;
}
}
System.out.println("Thank you for using this Application");
}
}
It would appear that you need a loop, I suggest you use a do-while loop - that is, something like,
boolean stop = false;
do {
// as before
// set stop to true to end the loop.
} while (!stop);

Categories

Resources