Novice in C# -Incrementing Employee Class
Hello,
I have written a code that needs to increment my employee object by 1 for each instance of the class Employee.I am using a variable private static int numEmployee = 0;
My program runs fine, but it keeps giving me total employees = "2" and "3" for employee1 and employee2, and I am not sure what I am doing wrong. Please look at this code and offer any help !!!
This is my first post, so not sure if this format is correct or not. I used Visual Studio 2010 for C# to compile.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Help
{
class Employee
{
private string firstName;
private string lastName;
private char gender;
private int dependents;
private double annualSalary;
private double pay;
public static int numEmployees = 0; // created static variable
public const string DEFAULT_FIRST = " not given";
public const string DEFAULT_LAST = " not given";
public const char DEFAULT_GENDER = 'U';
public const int DEFAULT_DEPENDENTS = 0;
private const double DEFAULT_ANNUALSALARY = 20000;
public Employee()
{
FirstName = DEFAULT_FIRST;
LastName = DEFAULT_LAST;
Gender = DEFAULT_GENDER;
Dependents = DEFAULT_DEPENDENTS;
AnnualSalary = DEFAULT_ANNUALSALARY;
numEmployees++; // incremented numEmployees
}
public Employee(string first, string last, char gen, int dep, double salary)
{
DisplayEmployeeInformation(first, last, gen, dep, salary);
numEmployees++; //incremented numEmployees
}
~Employee()
{
Console.WriteLine("Cancel the employee!!");
}
public string FirstName
{
get
{ return firstName; }
set
{firstName = value;}
}
public string LastName
{
get
{ return lastName; }
set
{lastName = value;}
}
public char Gender
{
get
{ return gender; }
set
{gender = value;}
}
public int Dependents
{
get
{ return dependents; }
set
{ dependents = value;}
}
public double AnnualSalary
{
get { return annualSalary; }
set
{
if(value > 19999)
annualSalary = value;
else
annualSalary = DEFAULT_ANNUALSALARY;
}
}
public static int GetNumEmployees() //
{
return numEmployees;
}
public double CalculatePay()
{
return (annualSalary / 52);
}
public override string ToString()
{
string str;
str = "Employee AnnualSalary: $" + AnnualSalary.ToString("N2") + "\n ";
return str;
}
public void DisplayEmployeeInformation(string first, string last, char gen, int dep, double salary)
{
Console.WriteLine("First Name: " + first);
Console.WriteLine("Last Name: " + last);
Console.WriteLine("Gender: " + gen);
Console.WriteLine("Dependents: " + dep);
Console.WriteLine("Annual Salary:$ " + salary.ToString("N2"));
annualSalary = salary;
pay = CalculatePay();
Console.WriteLine("Employee Weekly Pay:$ " + pay.ToString("N2"));
Console.WriteLine("Total Employees:" + numEmployees);
}
}
}
***** start of Program.cs *****
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Help
{
public class Program
{
static void Main(string[] args)
{
DisplayApplicationInformation();
DisplayDivider("Start Program");
//Employee.DisplayEmployeeInformation();
DisplayDivider("Employee Information");
DisplayDivider("Creating and testing employee1");
Employee employee1 = new Employee();
Employee.GetNumEmployees(); // is this where we access the
employee1.FirstName = GetInput("First Name");
employee1.LastName = GetInput("Last Name");
employee1.Gender = char.Parse(GetInput("Gender"));
employee1.Dependents = int.Parse(GetInput("Dependents"));
employee1.AnnualSalary = double.Parse(GetInput("Salary"));
DisplayDivider("Creating and testing employee2");
Employee employee2 = new Employee();
Employee.GetNumEmployees();
employee2.FirstName = GetInput("First Name");
employee2.LastName = GetInput("Last Name");
employee2.Gender = char.Parse(GetInput("Gender"));
employee2.Dependents = int.Parse(GetInput("Dependents"));
employee2.AnnualSalary = double.Parse(GetInput("Salary"));
Console.Clear();
Employee employee1write = new Employee(employee1.FirstName, employee1.LastName, employee1.Gender, employee1.Dependents, employee1.AnnualSalary);
Console.WriteLine();
Employee employee2write = new Employee(employee2.FirstName, employee2.LastName, employee2.Gender, employee2.Dependents, employee2.AnnualSalary);
TerminateApplication();
}
private static void DisplayApplicationInformation()
{
Console.WriteLine("Welcome to Employee Class Program");
Console.WriteLine("CIS247, Week 3 Lab");
Console.WriteLine("Name: Chuck");
Console.WriteLine("This program will increment the employee class to count the employees");
Console.WriteLine();
}
private static void DisplayDivider(string outputTitle)
{
Console.WriteLine("***************" + outputTitle + " " + "***************");
}
private static string GetInput(string inputType)
{
string strInput = String.Empty;
Console.Write(" Please enter " + inputType + " : ");
strInput = Console.ReadLine();
return strInput;
}
private static void TerminateApplication()
{
DisplayDivider("Program Termination");
Console.Write("Thank You! Please press any key to terminate the program");
Console.ReadKey();
}
}
}
|