Skip to content
Loading
  • Jaish Mathews
    0
    What I understood is that you need to take employees name and salary through input and then print them. Below is a refined code for it. I used VS 2019 with C#  7.x and it's a console application. Main areas corrected are -
     
    1. Naming convention for Types, Objects and variables
    2. Using of automatic properties and their default assignments
    3. Commenting of many unwanted methods
    This is again a basic coding practice with minimum standards
    1. using System;  
    2. using System.Collections.Generic;  
    3.   
    4. namespace ConsoleApp1  
    5. {  
    6.     class Program  
    7.     {  
    8.         static void Main(string[] args)  
    9.         {  
    10.             try  
    11.             {  
    12.                 List employeeList = new List();  
    13.                 Console.WriteLine("Howmany employees to consider?");  
    14.                 int employeeCount = int.Parse(Console.ReadLine());  
    15.   
    16.                 for (int i = 0; i < employeeCount; i++)  
    17.                 {  
    18.                     Console.WriteLine("Name:");  
    19.                     string name = Console.ReadLine();  
    20.                     Console.WriteLine("Salary:");  
    21.                     double salary = double.Parse(Console.ReadLine());  
    22.                     Employee employee = new Employee(name, salary);  
    23.                     employeeList.Add(employee);  
    24.                     Console.Clear();  
    25.                 }  
    26.   
    27.                 foreach (Employee employee in employeeList)  
    28.                 {  
    29.                     Console.WriteLine(employee.Name + "," + employee.Salary);  
    30.                 }  
    31.             }  
    32.             catch(Exception ex)  
    33.             {  
    34.                 Console.WriteLine(ex.Message);  
    35.             }  
    36.             Console.ReadLine();  
    37.         }  
    38.     }  
    39.     class Employee  
    40.     {  
    41.         public string Name { get; set; }  
    42.         public double Salary { get; set; } = 0;  
    43.         //public double salary  
    44.         //{  
    45.         //    get { return Salary; }  
    46.   
    47.         //    set  
    48.         //    {  
    49.         //        Salary = value;  
    50.         //        if (Salary < 0) Salary = 0;  
    51.         //    }  
    52.   
    53.         //}  
    54.         // constructor de clasa  
    55.         public Employee(string name , double salary = 0)  
    56.         {  
    57.   
    58.             Salary = salary;  
    59.             Name = name;  
    60.         }  
    61.         //constructor de copiere  
    62.         //public produs(salary s)  
    63.         //{  
    64.         //    salary = s.salary;  
    65.         //    nsme = s.name;  
    66.         //}  
    67.   
    68.         //public void Citire()  
    69.         //{  
    70.         //    Console.WriteLine("NAme:");  
    71.         //    Name = Console.ReadLine();  
    72.         //    Console.WriteLine("Salary:");  
    73.         //    Salary = double.Parse(Console.ReadLine());  
    74.         //}  
    75.         //public void Scriere()  
    76.         //{  
    77.         //    Console.WriteLine(Name + "," + Salary);  
    78.         //}  
    79.     };  
    80.       
    81.   
    82. }  
    83.   
    84.   
    85.   
    86.      
    0