-
Better Economy of Code
Hello, I am in an intro class. We have a task to write a code to compute the employee salary and type. This is what I have so far. It works but is there a better way to save on the amount of code? Instead of putting the system.out in every if/if else statement? That is what I think would be better. Any idea?
import java.util.Scanner;
import java.io.*;
public class SalaryCalculation {
public static void main (String [] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter your name: ");
BufferedReader br = new BufferedReader(new InputStreamReader ((System.in));
String name = null;
try {
name = br.readLine();
}
catch (IOException ioe) {
System.out.println("IO error trying to read your name!");
System.exit(1);
}
System.out.print ("what week is this: ");
int weeks = input.nextInt();
while (weeks > 52 && weeks < 1) {
System.out.print("Out of range. Please re-enter: ");
weeks = input.nextInt();
}
System.out.print("Enter your employee type: ");
int empType = input.nextInt();
if (empType == 1){
System.out.print("Enter your hourly rate: ");
double hourlyRate = input.nextDouble();
System.out.print("Enter your hours worked: ");
int hoursWorked = input.nextInt();
whiile (hoursWorked > 70) {
System.out.print("Out of range. Please re-enter: ");
hoursWorked = input.nextInt();
}
double pay = hourlyRate * hoursWorked;
System.out.println("============================== ==");
System.out.println("Week #: " + weeks);
System.out.println("Employee Name: " + name);
System.out.println("Employee Type: " + empType);
System.out.println ("Hourly Salary: $" + hourlyRate);
System.out.println ("Number of Hours Worked: " + hoursWorked );
System.out.println ("Salary Due: $" + pay );
System.out.println("============================== ==");
}
else if (empType == 2)
{
System.out.print("Enter your wage per piece: ");
double wagePiece = input.nextDouble();
System.out.print("Enter your number of pieces: ");
int numberPieces = input.nextInt();
double piecePay = wagePiece * numberPieces;
}
else if (empType == 3)
{
System.out.print("Enter your pieces sold: ");
int soldPieces = input.nextInt();
System.out.print("Enter your weekly rate: ");
double weeklyRate = input.nextDouble();
System.out.print("Enter your commission: ");
int commission = input.nextInt();
int salaryPay = soldPieces * (commission / 100) + weeklyRate;
}
else {
System.out.println("ERROR: Incorrect Start Over!");
System.exit(0);
}
}
}
-
If you can find the common things between the different employee types, then you can create a method that prints out that common information instead of printing it out with each employee type.
An observation:
Code:
System.out.println("============================== ==");
System.out.println("Week #: " + weeks);
System.out.println("Employee Name: " + name);
System.out.println("Employee Type: " + empType);
System.out.println ("Hourly Salary: $" + hourlyRate);
System.out.println ("Number of Hours Worked: " + hoursWorked );
System.out.println ("Salary Due: $" + pay );
System.out.println("============================== ==");
Shouldn't the above be printed AFTER the employee type is determened and processed? Right now it just prints employee type 1 information.
Luck,
CJSL