creating an employee class

Terabojin

Active member
Joined
Mar 4, 2012
Messages
33
Programming Experience
Beginner
I am attempting to make an employee class and falling short somewhere. This shouls include three things... first name as sting, last name as string, and monthly salary as integer. its to have a constructor that initializes the three variables. the property for the monthly salary hould ensure that its value remains positive, if an attempt is made to assign a negative value, throw an excption. this should also show the capabilities of the class employee. im to create tw employee objects and display each objects yearly salary. then give them both a 10% raise and display eah emloyees yearly salary again.

this is what i have. it is taking the inputs, but its not calculating anything nor is it outputting anything. someone please help?


Sub Main()


Dim firstName As String


Dim lastName As String


Dim monthlySalary As Integer



Console.WriteLine("Enter Employee 1's First Name:") 'prompts user to enter employee 1's first name

firstName =
Console.ReadLine() 'gets employee 1's first name from user


Console.WriteLine("Enter Employee 1's Last Name:") 'prompts user to enter employee 1's last name

lastName =
Console.ReadLine() 'gets employee 1's last name from user


Console.WriteLine("Enter Employee 1's Monthly Salary:") 'prompts user to enter employee 1's monthly salary

monthlySalary =
Console.ReadLine() 'gets employee 1's monthly salary from user



Console.WriteLine("Enter Employee 2's First Name:") 'prompts user to enter employee 2's first name

firstName =
Console.ReadLine() 'gets employee 2's first name from user


Console.WriteLine("Enter Employee 2's Last Name:") 'prompts user to enter employee 2's last name

lastName =
Console.ReadLine()


Console.WriteLine("Enter Employee 2's Monthly Salary:")

monthlySalary =
Console.ReadLine()


 

 

 

 


End Sub



Public Class Employee


Private firstName As String ' employee first name


Private lastName As String ' employee last name


Private employee1MonthlySalary As Integer = 5000 ' employee 1 monthly salary


Private employee2MonthlySalary As Integer = 4000 ' employee 2 monthly salary



Public Sub New(ByVal first As String, ByVal last As String)

firstName = first

lastName = last


End Sub



Public ReadOnly Property First() As String


Get


Return firstName


End Get


End Property



Public ReadOnly Property Last() As String


Get


Return lastName


End Get


End Property



Public Function Employee()



Dim salary1 As Integer ' Employee 1 Yearly Salary calculation


Dim salary2 As Integer ' Employee 2 Yearly Salary calculation


Dim raise1Salary As Integer ' Employee 1 Yearly Salary with 10% raise


Dim raise2Salary As Integer ' Employee 2 Yearly Salary with 10% raise


salary1 = employee1MonthlySalary * 12

salary2 = employee2MonthlySalary * 12

raise1Salary =
CInt(employee1MonthlySalary * 1.1)

raise2Salary =
CInt(employee2MonthlySalary * 1.1)



If employee1MonthlySalary < 0 Then


Throw New ArgumentOutOfRangeException


Console.WriteLine("Employee 1's salary amount must be greater than zero.", "Out of Range")


End If



If employee2MonthlySalary < 0 Then


Throw New ArgumentOutOfRangeException


Console.WriteLine("Employee 2's salary amount must be greater than zero.", "Out of Range")


End If



Dim employee1 As New Employee("John", "Doe")


Console.WriteLine("Employee 1: " & employee1.First & " " & employee1.Last & " - " & "Yearly salary: " & salary1 & vbCrLf)



Dim employee2 As New Employee("Jane", "Doe")


Console.WriteLine("Employee 2: " & employee2.First & " " & employee2.Last & " - " & "Yearly salary: " & salary2 & vbCrLf)



Console.WriteLine(vbCrLf & "Yearly salary adjusted for 10% increase" & vbCrLf)



Console.WriteLine("Employee 1: " & employee1.First & " " & employee1.Last & " - " & "Yearly salary: " & raise1Salary & vbCrLf)



Console.WriteLine("Employee 2: " & employee2.First & " " & employee2.Last & " - " & "Yearly salary: " & raise2Salary & vbCrLf)



End Function



End Class


End
Module
 
As you can see for yourself, your code is very hard to read as you have posted it. Please post it as plain text, not HTML, between formatting tags so that we can read it, i.e. [xcode=vb]your code here[/xcode]
 
As you can see for yourself, your code is very hard to read as you have posted it. Please post it as plain text, not HTML, between formatting tags so that we can read it, i.e.
your code here

 Sub Main()
        Dim firstName As String
        Dim lastName As String
        Dim monthlySalary As Integer
        Console.WriteLine("Enter Employee 1's First Name:") 'prompts user to enter employee 1's first name 
        firstName = Console.ReadLine() 'gets employee 1's first name from user
        Console.WriteLine("Enter Employee 1's Last Name:") 'prompts user to enter employee 1's last name
        lastName = Console.ReadLine() 'gets employee 1's last name from user
        Console.WriteLine("Enter Employee 1's Monthly Salary:") 'prompts user to enter employee 1's monthly salary
        monthlySalary = Console.ReadLine() 'gets employee 1's monthly salary from user
        Console.WriteLine("Enter Employee 2's First Name:") 'prompts user to enter employee 2's first name
        firstName = Console.ReadLine() 'gets employee 2's first name from user
        Console.WriteLine("Enter Employee 2's Last Name:") 'prompts user to enter employee 2's last name
        lastName = Console.ReadLine()
        Console.WriteLine("Enter Employee 2's Monthly Salary:")
        monthlySalary = Console.ReadLine()
 
 
    End Sub
    Public Class Employee
        Private firstName As String ' employee first name
        Private lastName As String ' employee last name
        Private employee1MonthlySalary As Integer = 5000 ' employee 1 monthly salary
        Private employee2MonthlySalary As Integer = 4000 ' employee 2 monthly salary
        Public Sub New(ByVal first As String, ByVal last As String)
            firstName = first
            lastName = last
        End Sub
        Public ReadOnly Property First() As String
            Get
                Return firstName
            End Get
        End Property
        Public ReadOnly Property Last() As String
            Get
                Return lastName
            End Get
        End Property
        Public Function Employee()
            Dim salary1 As Integer ' Employee 1 Yearly Salary calculation
            Dim salary2 As Integer ' Employee 2 Yearly Salary calculation
            Dim raise1Salary As Integer ' Employee 1 Yearly Salary with 10% raise
            Dim raise2Salary As Integer ' Employee 2 Yearly Salary with 10% raise
            salary1 = employee1MonthlySalary * 12 'calculates employee 1's yearly salary
            salary2 = employee2MonthlySalary * 12 'calculates employee 2's yearly salary
            raise1Salary = CInt(employee1MonthlySalary * 1.1) 'calculates employee 1's salary after a 10% raise
            raise2Salary = CInt(employee2MonthlySalary * 1.1) 'calculates employee 2's salary after a 10% raise
            If employee1MonthlySalary < 0 Then 'checks to see if the salary of employee 1 is less than zero, if it is display an error message
                Throw New ArgumentOutOfRangeException
                Console.WriteLine("Employee 1's salary amount must be greater than zero.", "Out of Range")
            End If
            If employee2MonthlySalary < 0 Then 'checks to see if the salary of employee w is less than zero, if it is display an error message
                Throw New ArgumentOutOfRangeException
                Console.WriteLine("Employee 2's salary amount must be greater than zero.", "Out of Range")
            End If
            Dim employee1 As New Employee("John", "Doe")
            Console.WriteLine("Employee 1: " & employee1.First & " " & employee1.Last & " - " & "Yearly salary: " & salary1 & vbCrLf) 'writes first name, last name, yearly salary of employee 1
            Dim employee2 As New Employee("Jane", "Doe")
            Console.WriteLine("Employee 2: " & employee2.First & " " & employee2.Last & " - " & "Yearly salary: " & salary2 & vbCrLf) 'writes first name, last name, yearly salary of employee 2
            Console.WriteLine(vbCrLf & "Yearly salary adjusted for 10% increase" & vbCrLf) 'displays a message to user letting them know the following is adjusted yearly salary after a 10% raise
            Console.WriteLine("Employee 1: " & employee1.First & " " & employee1.Last & " - " & "Yearly salary: " & raise1Salary & vbCrLf) 'displays employee 1's first name, last name, and yearly salary after 10% raise
            Console.WriteLine("Employee 2: " & employee2.First & " " & employee2.Last & " - " & "Yearly salary: " & raise2Salary & vbCrLf) 'displays employee 2's first name, last name, and yearly salary after 10% raise
        End Function
    End Class
End Module




i hope i did it right this time, sorry im a complete newbie
 
Hi Terabojin,

I am working on this for you and will come back to you soon.

Hi all forums users, I am free and am going through this now if you need to concentrate on something else.

Cheers,

Ian
 
Much easier to read now but very scary code I'm afraid. Firstly, get that class out of that module. Unless you have specific reason to do so, don't declare one type inside another.

Secondly, you need to get rid of most of the code in the class. The class should have the three properties specified and nothing else. Also, the instructions you posted say that the constructor is supposed to set three properties so if you can count then you know that that constructor is wrong. Finally, I can;t see any reason for those properties top be ReadOnly.
 
Employee class is supposed to represent a single employee.
 
Hi,

OK, lets take it one step at a time:-

1) You say "it is taking the inputs, but its not calculating anything nor is it outputting anything". The reason why nothing happens is that in a console application only the code in the Sub Main is executed when the program is ran. If you want to execute other code defined as part of the program then you need to call that code from the Sub Main routine. In this case you have NOT actually called your Employee class from your Sub Main routine and therefore the results that you have defined in your Employee class never get executed.

2) You get your inputs in Sub Main for the first employee but the first thing that you then do after that is then overwrite your first employee details by setting the same variables to your second employee details.

3) Your Employee class is way over complicated and as already stated by other forum experts each class declaration should represent a single employee. You also define your salary variables with a default value being "Private employee1MonthlySalary As Integer = 5000". There is no point doing this since you are getting your salary details from your sub main inputs. Again, as already mentioned, there is no point declaring your first and last names as read only.

4) You then have a function called Employee within your the class Employee. This is confusing and you should always declare you functions and subroutines with qualified names which describe their function.

5) There is no point in using "Throw New ArgumentOutOfRangeException" when you have not declared any error capture statements in your preceding Sub Main routine. This will just bomb the application. Just stick with "Console.WriteLine("Employee 1's salary amount must be greater than zero. Out of Range)"

6) You then declare in your function:-

VB.NET:
Dim employee1 As New Employee("John", "Doe")
Console.WriteLine("Employee 1: " & employee1.First & " " & employee1.Last & " - " & "Yearly salary: " & salary1 & vbCrLf) 'writes first name, last name, yearly salary of employee 1            
Dim employee2 As New Employee("Jane", "Doe")
Console.WriteLine("Employee 2: " & employee2.First & " " & employee2.Last & " - " & "Yearly salary: " & salary2 & vbCrLf) 'writes first name, last name, yearly salary of employee 2
This totally defeats the object of getting user inputs in the Sub Main Routine since you totally ignore them and then you just output various literals you have defined in your application.

7) When defining your variables you have to take into consideration what information is going to be held in the variable. For instance, you currently have your salary variables declared as integers but what you need to remember is that any type of currency information can have decimal values and therefore your salary declarations should have been defined as Single, Double or Decimal to be able to hold decimal values. In this example I have converted these variables to single values.

8) I think that's about it and as you can see there are many principals which you have misunderstood how to use. To try and help you understand how this should have been done see my code below. Read the comments carefully and hopefully you can see where you need to study up on:-

VB.NET:
Module Module1
  Sub Main()
    '1) firstly have a look at the class that I have re-written:-
    '2) we then declare two variables of type EmployeeName to hold the details of the two employees that we need to enter
    Dim Employee1 As New EmployeeName
    Dim Employee2 As New EmployeeName
    'we then get the inputs from the user for the two employees:-
    Console.WriteLine("Enter Employee 1's First Name:") 'prompts user to enter employee 1's first name         
    Employee1.FirstName = Console.ReadLine() 'gets employee 1's first name from user        
    Console.WriteLine("Enter Employee 1's Last Name:") 'prompts user to enter employee 1's last name        
    Employee1.LastName = Console.ReadLine() 'gets employee 1's last name from user        
    Console.WriteLine("Enter Employee 1's Monthly Salary:") 'prompts user to enter employee 1's monthly salary        
    Employee1.Salary = CSng(Console.ReadLine()) 'gets employee 1's monthly salary from user        
    Console.WriteLine("Enter Employee 2's First Name:") 'prompts user to enter employee 2's first name        
    Employee2.FirstName = Console.ReadLine() 'gets employee 2's first name from user        
    Console.WriteLine("Enter Employee 2's Last Name:") 'prompts user to enter employee 2's last name        
    Employee2.LastName = Console.ReadLine()
    Console.WriteLine("Enter Employee 2's Monthly Salary:")
    Employee2.Salary = CSng(Console.ReadLine())
 
    'now we use the subroutine defined within the EmployeeName to display the results of the
    'two employees that have been added by the user
    Employee1.ShowEmployeeResults()
    Console.WriteLine()
    Employee2.ShowEmployeeResults()
    Console.ReadLine()
  End Sub
End Module

  Public Class EmployeeName
    'here you can see that we have defined three properties to hold employee information
    'the important thing to remember here is that we will define an EmployeeName class for
    'each employee that we need to get inputs for and not add multiple employee information
    'to the same class
    Public Property FirstName As String
    Public Property LastName As String
    Public Property Salary As Single
 
    Public Sub ShowEmployeeResults()
      'I have changed your function to a subroutine since you are not returning any informartion to the calling routine
      'I have also called the subroutine something more readable to explain what it does
      'it only defines one additional variable since all the other employee details are define as properties above
      Dim SalaryRaised As Single ' Employee Yearly Salary with 10% raise            
      'Here we check for a valid salary and only if valid do we do the salary raised calculation
      If Salary < 0 Then 'checks to see if the salary of employee 1 is less than zero, if it is display an error message                
        Console.WriteLine("Employee's salary amount must be greater than zero. Out of Range")
      Else
        SalaryRaised = Convert.ToSingle(Salary * 1.1) 'calculates employee 1's salary after a 10% raise            
      End If
      'Here we output the employee information as needed
      Console.WriteLine("Employee : " & FirstName & " " & LastName & " - " & "Yearly salary: " & Salary) 'writes first name, last name, yearly salary of employee 
      Console.WriteLine(vbCrLf & "Yearly salary adjusted for 10% increase" & vbCrLf) 'displays a message to user letting them know the following is adjusted yearly salary after a 10% raise         
      Console.WriteLine("Employee : " & FirstName & " " & LastName & " - " & "Raised Yearly salary: " & SalaryRaised) 'displays employee's first name, last name, and yearly salary after 10% raise            
    End Sub
  End Class
Hope that helps,

Cheers,

Ian
 
Last edited:
Just try to declare all of your variables into public so that it can be retrieved anywhere you want as long as it in the same project.
 
thank you so much for the help everyone. This is so confusing to me. Is there a "for dummies" out there for this kind of stuff? I hate to say it but this does not come naturally to me to say the least. Ian: thank you for wrting it out like that. That was so much help being able to see exactly where I messed up when I compaired the two. I had an idea of how to do it but unfortunately that idea was way off lol.
 
Ian,
after looking there is one problem i cant seem to fix, how do you get it to calculate both salaries after the raise? with what you did it only calculates the salary for one emplogyee.
 
Hi,

Just tried my own code and it works fine. So the question for you is have you actually tried running the code or have you got to this conclusion just by reading the code?

You have to remember that the class in itself only calculates one salary rise but each Exployee is declared as a separate instance of the EmployeeName Class. Therefore it calculates the rise correctly for each employee.

Hope that helps.

Cheers,

Ian
 
emp 1 : joe smith
salary 1550

emp 2: mark frank
salary 1275


after i input that info, it is only giving me joe smith's name and yearly income after the raise
 
Back
Top