Results 1 to 2 of 2
  1. #1
    Zombie is offline Member
    Join Date
    Mar 2009
    Posts
    1
    Rep Power
    0

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



    }
    }

  2. #2
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    8

    Default

    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:
    Java 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
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

Similar Threads

  1. Why doesn't this code accept my code?
    By PeterFeng in forum New To Java
    Replies: 5
    Last Post: 02-03-2009, 02:39 PM
  2. I need help fixing my code.. or non code?
    By MrHuggykins in forum New To Java
    Replies: 1
    Last Post: 03-19-2008, 11:12 PM
  3. Replies: 1
    Last Post: 04-26-2007, 04:52 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •