Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

How do I add another object for a user to enter?  RSS feed

 
Dustin Boor
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My program is calculating an employees salary with overtime, I want my program to display data for two employees after data is entered for two employees. What do I need to add to input data for a second employee and so that the program doesn't fully execute until the data is properly entered? Try not to get too technical, i'm just a beginner!

 
Tushar Goel
Ranch Hand
Posts: 934
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you have used infinite loop without anything.. nothing else will be executed then..

You have not put any logic to take request again from the employee and also you put everything in main.

I think you should first use a paper and pen to write down idea in plain language and then implement it.
 
Ganesh Patekar
Bartender
Posts: 696
23
Eclipse IDE Hibernate Java jQuery MySQL Database Netbeans IDE Oracle Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think you better declare employeeName, hoursWorked, overtimePay, totalPay as instance variables in class salaryCalculator Or you can create a separate class Employee ( I would prefer this ) which will have these instance variables with getter and setter methods. Let payRate be  static final in class salaryCalculator If It is common to all employees.
Example:


In salaryCalculator you can ask user to enter how many number of employee's information they want to enter then you can create array of Employee of that size and in a for loop you can create object of Employee and set their information and also claculate salary. Finaly display elements of array of Employee.

  • class name always starts with capital letter, It suppose to be SalaryCalculator. Please refer this --->Naming Conventions
  • You have infinite while loop when hoursWorked entered is less than 0.
  • You are calculating overtimePay redundantly when hoursWorked is more than 40 hours.
  • You are calculating totalPay redundantly when hoursWorked is not more than 40 hours.



  •  
    Dustin Boor
    Greenhorn
    Posts: 18
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I'm very new to Java, just wondering why my calculations are not being displayed when I run the program. Both of the employees salary are being displayed as 0.0. Both of my classes are displayed. Any clarification would be great.



     
    Tushar Goel
    Ranch Hand
    Posts: 934
    4
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    1) You dint call calculatePay
    2) In displayLine you are trying to print overTime which i think should be grossPay
    3) You have not taken overPay from the user so it is by default 0
     
    Dustin Boor
    Greenhorn
    Posts: 18
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    What do you mean by "call" calculate pay? I do apologize, but I'm learning. Here is my updated calculate pay method:

     
    Ganesh Patekar
    Bartender
    Posts: 696
    23
    Eclipse IDE Hibernate Java jQuery MySQL Database Netbeans IDE Oracle Spring Tomcat Server
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    You made good changes

  • Did you notice when hours is not greater than 40 you are calculating  hours * payRate  redundantly here in else clause
  • which is already calculated and assigned to totalPay on line number 36?

  • The value of overTime changes ONLY when hours is greater than 40 so you need only one If statement no need of else part, need to move code up and down only just ponder on It.
  • You can override toString method of Object into Employee class to display employee information so you just need to print reference variable employee1 and employee2 using print statement in main method of SalaryCalculator like this System.out.println(employee1); then you don't need method displayLine.

  •  
    Ganesh Patekar
    Bartender
    Posts: 696
    23
    Eclipse IDE Hibernate Java jQuery MySQL Database Netbeans IDE Oracle Spring Tomcat Server
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Dustin Boor wrote:What do you mean by "call" calculate pay?
    You need to invoke Or call calculatePay() to execute code enclosed in that method the way you called displayLine method in main method of SalaryCalculator here employee1.displayLine();

    Same way you need to call calculatePay() for both employees. I don't see anywhere you called calculatePay() method.
     
    Dustin Boor
    Greenhorn
    Posts: 18
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Even with taking out displayLine() my output for the employee's is still showing as 0.0 for Employee #1 and Employee #2. My calculate pay method is still the same as listed above :-/
     
    Ganesh Patekar
    Bartender
    Posts: 696
    23
    Eclipse IDE Hibernate Java jQuery MySQL Database Netbeans IDE Oracle Spring Tomcat Server
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I assume you called calculatePay() on both reference variable like this employee1.calculatePay(); employee2.calculatePay(); in main method.

    Have look at this meth0d Java is case sensitive.
     
    Dustin Boor
    Greenhorn
    Posts: 18
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I have added employee1.calculatePay and employee2.calcluatePay underneath the display lines of employee1 and employee2, yes. Still getting the same input.
     
    Ganesh Patekar
    Bartender
    Posts: 696
    23
    Eclipse IDE Hibernate Java jQuery MySQL Database Netbeans IDE Oracle Spring Tomcat Server
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    No you have to call calculatePay method before displayLine method. Did you notice my recent post? parameter of setHours method? It is Hours which is suppose to be hours. Because Hours and hours is different as Java is case sensitive.
     
    Ganesh Patekar
    Bartender
    Posts: 696
    23
    Eclipse IDE Hibernate Java jQuery MySQL Database Netbeans IDE Oracle Spring Tomcat Server
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Or If you still won't get correct output, you can repost your latest updated code no problem.
     
    Dustin Boor
    Greenhorn
    Posts: 18
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Thank you so much for your help! I got the correct output I needed :-)
     
    Ganesh Patekar
    Bartender
    Posts: 696
    23
    Eclipse IDE Hibernate Java jQuery MySQL Database Netbeans IDE Oracle Spring Tomcat Server
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Glad to know you got the correct output  , will you please repost the latest code so others who perhaps have same problem can benefit from that.
     
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!