---
# System prepended metadata

title: Lab7-2

---

2. Create a class called **Employee** that includes three pieces of information as data members — a **first name** (type string), a **last name** (type string) and a **monthly salary**(type int). 

- Your class should have a **constructor** that initializes the three data members. 
- Provide a **set and a get function** for each data member. 
  - You can write functions. Property is optional.
- If the **monthly salary** is negative, throw a **NegativeNumberException** with Message **“Negative salary is not permitted”** and prompts user to enter a positive salary.
  - c# don't have this exception, so you need to create a new exception class.
      - ```csharp
        class Employee
        {
        
        }
        public class NegativeNumberException : Exception
        {
            public NegativeNumberException(string messageValue) : base(messageValue)
            {
            }
        }
        ```

  - **Note that both the constructor and the set function can throw such exception.**

## DEMO
- Write a test program that demonstrates class Employee’s capabilities. 
  - Create **two** Employee objects, display each object’s **yearly salary**(monthly salary * 12) and cause **NegativeNumberException** to occur, respectively.

- **Test Code Example**
    Test code 1:
    ```csharp
    // test constructor
    try
    {
        Employee a = new Employee("Alice", "Chen", 5000);
        Employee b = new Employee("Bob", "Lin", -1000);
        // will throw error here
        Console.WriteLine(12 * a.getSalary());
        Console.WriteLine(12 * b.getSalary());
    }
    catch (... ...)
    {
        ... ...
    }
    ```
    Test code 2:
    ```csharp
    // test set function
    try
    {
        Employee a = new Employee("Alice", "Chen", 5000);
        Employee b = new Employee("Bob", "Lin", 2000);
        Console.WriteLine(12 * a.getSalary());
        Console.WriteLine(12 * b.getSalary());
     
        b.setSalary(-1000);
        // will throw error here
    }
    catch (... ...)
    {
        ... ...
    }
    ```
    