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
 
Hi,

Firstly do you want to get rid of all the code you posted above. It has no relevance here.

Secondly, I have tried your inputs and all is fine, so on the basis that you have not changed any of my code the only thing I can think of is:- Is your console window big enought to display the full results? Try expanding the window or scrolling down and I am hoping your second employee details will be there.

Let me know and we can go from there.

Cheers,

Ian
 
this is the code im running:

Module employeeClass
    Sub Main()
        Dim Employee1 As New EmployeeName
        Dim Employee2 As New EmployeeName
        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 name from user
        Console.WriteLine("Enter Employee 2's Last Name:") 'prompts user to enter employee 2's last name
        Employee2.LastName = Console.ReadLine() 'gets employee 2's last name from user
        Console.WriteLine("Enter Employee 2's Monthly Salary:") 'prompts user to enter employee 2's monthly salary
        Employee2.Salary = CSng(Console.ReadLine()) 'gets employee 2's monthly salary from user
        'shows results of employee 1 and employee 2
        Employee1.ShowEmployeeResults()
        Console.WriteLine()
        Employee2.ShowEmployeeResults()
        Console.WriteLine()
    End Sub
End Module
Public Class EmployeeName
    'three properties defined for employee information
    Public Property FirstName As String
    Public Property LastName As String
    Public Property Salary As Single
    Public Sub ShowEmployeeResults()
        Dim SalaryRaised As Single 'Employee yearly salary with 10% raise
        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) 'calculate employee 1's salary after 10% raise
        End If
        'output the employee information
        Console.WriteLine("Employee : " & FirstName & " " & LastName & " _ " & "Yearly Salary: " & Salary) 'writes first name, last name, and salary of employee
        Console.WriteLine(vbCrLf & "Yearly Salary Adjusted for 10% Increase" & vbCrLf) 'diplays a message to user letting them know the following is the 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
        'prevents window from closing
        Console.ReadLine()
    End Sub
End Class

 
Hi,

You have been silly here. You have changed the code to add "Console.ReadLine()" in the class. This will stop the code form continuing until you enter another value for the Console.ReadLine().

Press Return and you will see Employee2 details as well.

Then again, you could have left it as I had it and you would see both details at the same time?

Cheers,

Ian
 
Ian,

sorry about the jumbled code before, it wasnt supposed to have done that. I expanded the window and i did not show anything past employee 1's adjusted income....did i miss something?

Ian, what line(s) did i change accidentally?
 
Did you get my last post?

Cheers,

Ian

Hi,

Have a look at the last line of code in the EmployeeName class being Console.ReadLine. Remove that and put it back to the last line in the Sub Main routine and it will all work fine.

Cheers,

Ian
 
Ian,
The reason i added that console.readline is because after i hit enter it immediately runs and closes the program making it unable for me to read the output. where could i put that to keep the window open? after the end sub and before the end class?
 
Hi,

It does not close the application if you have it as I created it. It should be:-

VB.NET:
Employee1.ShowEmployeeResults()
Console.WriteLine()
Employee2.ShowEmployeeResults()
Console.ReadLine()
Is it working now?

Cheers,

Ian
 
oh my gosh!! thank you!!! it was staring me right in the face the whole time. you are such an awesome peron!!!! thanks a million!
 

Latest posts

Back
Top